Bug 26736

Summary: C# async lambda with nested lambda and "goto" statement causes compiler to crash.
Product: [Mono] Compilers Reporter: bugzilla
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description bugzilla 2015-02-04 11:08:44 UTC
This code causes the C# compiler to crash.  You should be able to just paste the code into the Program.cs file of a new Console application in Xamarin Studio.  It seems to be the combination of the variable declaration statement within the async lambda which constructs a nested lambda, combined with the use of a "goto" statement below it.  The code compiles without error if the variable declaration statement is removed, or if the "goto" statement is changed to a "while" statement.

I am using Mono 3.12.0 (detached/a813491) on OS/X Yosemite 10.10.2.

(P.S. "3.12.0" is not in the version list displayed in the bugzilla.xamarin.com "new bug" form)

using System;
using System.Threading.Tasks;

namespace AsyncGotoBug
{
    class MainClass
    {
        public static void Main(string [] args)
        {
            SomeMethod(async () => {
                Func<int,int> f = b => b;
                retry:
                await Task.Yield();
                goto retry;
            });
        }
        
        static void SomeMethod(Action a)
        {
        }
    }
}

Here is the full error output:

Tool /Library/Frameworks/Mono.framework/Versions/3.12.0/lib/mono/4.5/mcs.exe execution started with arguments: /noconfig /debug:full /debug+ /optimize- /out:obj/x86/Debug/AsyncGotoBug.exe Program.cs Properties/AssemblyInfo.cs /target:exe /define:DEBUG /nostdlib /platform:x86 /reference:/Library/Frameworks/Mono.framework/Versions/3.12.0/lib/mono/4.5/System.dll /reference:/Library/Frameworks/Mono.framework/Versions/3.12.0/lib/mono/4.5/System.Core.dll /reference:/Library/Frameworks/Mono.framework/Versions/3.12.0/lib/mono/4.5/mscorlib.dll /warn:4
		
		Unhandled Exception:
		Mono.CSharp.InternalErrorException: Program.cs(8,22): AsyncGotoBug.MainClass.Main(string[]) ---> System.ArgumentOutOfRangeException: Argument is out of range.
		Parameter name: index
		  at Mono.CSharp.ParametersBlock.RemapLabeledStatement (Mono.CSharp.LabeledStatement stmt, Mono.CSharp.Block src, Mono.CSharp.Block dst) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ParametersBlock.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Statement t) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Statement.Clone (Mono.CSharp.CloneContext clonectx) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.CloneContext.LookupBlock (Mono.CSharp.Block from) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AnonymousMethodExpression.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Expression t) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Expression.Clone (Mono.CSharp.CloneContext clonectx) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.BlockVariable.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Statement target) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Statement.Clone (Mono.CSharp.CloneContext clonectx) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Block.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Statement t) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ParametersBlock.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Statement t) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Statement.Clone (Mono.CSharp.CloneContext clonectx) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ParametersBlock.PerformClone () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AnonymousMethodExpression.CompatibleMethodBody (Mono.CSharp.ResolveContext ec, Mono.CSharp.TypeInferenceContext tic, Mono.CSharp.TypeSpec return_type, Mono.CSharp.TypeSpec delegate_type) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AnonymousMethodExpression.Compatible (Mono.CSharp.ResolveContext ec, Mono.CSharp.TypeSpec type) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AnonymousMethodExpression.ImplicitStandardConversionExists (Mono.CSharp.ResolveContext ec, Mono.CSharp.TypeSpec delegate_type) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Convert.ImplicitConversionExists (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression expr, Mono.CSharp.TypeSpec target_type) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.OverloadResolver.IsArgumentCompatible (Mono.CSharp.ResolveContext ec, Mono.CSharp.Argument argument, Modifier param_mod, Mono.CSharp.TypeSpec parameter) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.OverloadResolver.IsApplicable (Mono.CSharp.ResolveContext ec, Mono.CSharp.Arguments& arguments, Int32 arg_count, Mono.CSharp.MemberSpec& candidate, IParametersMember pm, System.Boolean& params_expanded_form, System.Boolean& dynamicArgument, Mono.CSharp.TypeSpec& returnType, Boolean errorMode) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.OverloadResolver.ResolveMember[MethodSpec] (Mono.CSharp.ResolveContext rc, Mono.CSharp.Arguments& args) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.MethodGroupExpr.OverloadResolve (Mono.CSharp.ResolveContext ec, Mono.CSharp.Arguments& args, IErrorHandler cerrors, Restrictions restr) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Invocation.DoResolveOverload (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Invocation.DoResolveInvocation (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Invocation.DoResolve (Mono.CSharp.ResolveContext rc) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0 
		  --- End of inner exception stack trace ---
		  at Mono.CSharp.Method.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeDefinition.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ClassOrStruct.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Class.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeDefinition.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ModuleContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AssemblyDefinitionStatic.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0 
		[ERROR] FATAL UNHANDLED EXCEPTION: Mono.CSharp.InternalErrorException: Program.cs(8,22): AsyncGotoBug.MainClass.Main(string[]) ---> System.ArgumentOutOfRangeException: Argument is out of range.
		Parameter name: index
		  at Mono.CSharp.ParametersBlock.RemapLabeledStatement (Mono.CSharp.LabeledStatement stmt, Mono.CSharp.Block src, Mono.CSharp.Block dst) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ParametersBlock.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Statement t) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Statement.Clone (Mono.CSharp.CloneContext clonectx) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.CloneContext.LookupBlock (Mono.CSharp.Block from) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AnonymousMethodExpression.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Expression t) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Expression.Clone (Mono.CSharp.CloneContext clonectx) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.BlockVariable.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Statement target) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Statement.Clone (Mono.CSharp.CloneContext clonectx) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Block.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Statement t) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ParametersBlock.CloneTo (Mono.CSharp.CloneContext clonectx, Mono.CSharp.Statement t) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Statement.Clone (Mono.CSharp.CloneContext clonectx) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ParametersBlock.PerformClone () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AnonymousMethodExpression.CompatibleMethodBody (Mono.CSharp.ResolveContext ec, Mono.CSharp.TypeInferenceContext tic, Mono.CSharp.TypeSpec return_type, Mono.CSharp.TypeSpec delegate_type) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AnonymousMethodExpression.Compatible (Mono.CSharp.ResolveContext ec, Mono.CSharp.TypeSpec type) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AnonymousMethodExpression.ImplicitStandardConversionExists (Mono.CSharp.ResolveContext ec, Mono.CSharp.TypeSpec delegate_type) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Convert.ImplicitConversionExists (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression expr, Mono.CSharp.TypeSpec target_type) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.OverloadResolver.IsArgumentCompatible (Mono.CSharp.ResolveContext ec, Mono.CSharp.Argument argument, Modifier param_mod, Mono.CSharp.TypeSpec parameter) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.OverloadResolver.IsApplicable (Mono.CSharp.ResolveContext ec, Mono.CSharp.Arguments& arguments, Int32 arg_count, Mono.CSharp.MemberSpec& candidate, IParametersMember pm, System.Boolean& params_expanded_form, System.Boolean& dynamicArgument, Mono.CSharp.TypeSpec& returnType, Boolean errorMode) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.OverloadResolver.ResolveMember[MethodSpec] (Mono.CSharp.ResolveContext rc, Mono.CSharp.Arguments& args) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.MethodGroupExpr.OverloadResolve (Mono.CSharp.ResolveContext ec, Mono.CSharp.Arguments& args, IErrorHandler cerrors, Restrictions restr) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Invocation.DoResolveOverload (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Invocation.DoResolveInvocation (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Invocation.DoResolve (Mono.CSharp.ResolveContext rc) [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0 
		  --- End of inner exception stack trace ---
		  at Mono.CSharp.Method.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeDefinition.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ClassOrStruct.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Class.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeDefinition.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.ModuleContainer.EmitContainer () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.AssemblyDefinitionStatic.Emit () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0 
		  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0 
/Library/Frameworks/Mono.framework/Versions/3.12.0/lib/mono/4.5/Microsoft.CSharp.targets: error : Compiler crashed with code: 1.
	Task "Csc" execution -- FAILED
Comment 1 Marek Safar 2015-02-06 09:14:48 UTC
Fixed in master