Bug 14126 - invalid IL generated for async code
Summary: invalid IL generated for async code
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2013-08-20 21:02 UTC by Zoltan Varga
Modified: 2013-08-23 07:30 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Zoltan Varga 2013-08-20 21:02:56 UTC
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

class TodoItem {

		internal class MobileServiceTable2<T> {
			public async Task<List<T>> ToListAsync() {
				var r = new List<T> ();
				return r;//new Task<List<T>> (delegate { return r; });

public class Tests {

	int foo (Action t) {
		return 0;

	private async void OnTap (TodoItem task) {

        private async Task RefreshAsync()
			var ta = new MobileServiceTable2<TodoItem> ();
			var r = await ta.ToListAsync ();

			r.Select<TodoItem, int> (t => foo (() => OnTap (t)));

public static void Main (String[] args) {


mcs master generates the following il:

  .method private hidebysig instance int32 
          '<RefreshAsync>m__0'(class TodoItem t) cil managed
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) 
    // Code size       39 (0x27)
    .maxstack  3
    .locals init (class Tests/'<RefreshAsync>c__AnonStorey3' V_0)
    IL_0000:  newobj     instance void Tests/'<RefreshAsync>c__AnonStorey3'::.ctor()
    IL_0005:  stloc.0
    IL_0006:  ldloc.0
    IL_0007:  ldloc.0
    IL_0008:  stfld      valuetype Tests/'<RefreshAsync>c__async2' Tests/'<RefreshAsync>c__AnonStorey3'::'<>f__ref$2'

The generated code tries to assign loc 0 to loc_0.f__ref$2.
Comment 1 Marek Safar 2013-08-23 07:30:10 UTC

Nice find, caused me some head scratching.