diff --git a/src/Compilers/CSharp/Portable/Lowering/StateMachineRewriter/MethodToStateMachineRewriter.cs b/src/Compilers/CSharp/Portable/Lowering/StateMachineRewriter/MethodToStateMachineRewriter.cs index c0cd01051e1e0..1fe789a5a9a69 100644 --- a/src/Compilers/CSharp/Portable/Lowering/StateMachineRewriter/MethodToStateMachineRewriter.cs +++ b/src/Compilers/CSharp/Portable/Lowering/StateMachineRewriter/MethodToStateMachineRewriter.cs @@ -422,31 +422,16 @@ internal static bool TryUnwrapBoundStateMachineScope(ref BoundStatement statemen return false; } - private void AddVariableCleanup(ArrayBuilder cleanup, FieldSymbol field) - { - if (MightContainReferences(field.Type)) - { - cleanup.Add(F.AssignmentExpression(F.Field(F.This(), field), F.NullOrDefault(field.Type))); - } - } - /// - /// Might the given type be, or contain, managed references? This is used to determine which - /// fields allocated to temporaries should be cleared when the underlying variable goes out of scope, so + /// Clear fields allocated to temporaries when the underlying variable goes out of scope, so /// that they do not cause unnecessary object retention. /// - private bool MightContainReferences(TypeSymbol type) + private void AddVariableCleanup(ArrayBuilder cleanup, FieldSymbol field) { - if (type.IsReferenceType || type.TypeKind == TypeKind.TypeParameter) return true; // type parameter or reference type - if (type.TypeKind != TypeKind.Struct) return false; // enums, etc - if (type.SpecialType == SpecialType.System_TypedReference) return true; - if (type.SpecialType.CanOptimizeBehavior()) return false; // int, etc - if (!type.IsFromCompilation(this.CompilationState.ModuleBuilderOpt.Compilation)) return true; // perhaps from ref assembly - foreach (var f in _emptyStructTypeCache.GetStructInstanceFields(type)) + if (field.Type.IsManagedTypeNoUseSiteDiagnostics) { - if (MightContainReferences(f.Type)) return true; + cleanup.Add(F.AssignmentExpression(F.Field(F.This(), field), F.NullOrDefault(field.Type))); } - return false; } private StateMachineFieldSymbol GetOrAllocateReusableHoistedField(TypeSymbol type, out bool reused, LocalSymbol local = null) diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncIteratorTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncIteratorTests.cs index ee739b2a89bfd..457f7366de931 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncIteratorTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncIteratorTests.cs @@ -9968,12 +9968,12 @@ public static async System.Collections.Generic.IAsyncEnumerable Produce(boo } """; // Note: hoisted nested local gets cleared when exiting nested scope normally - var verifier = CompileAndVerify(src, expectedOutput: ExpectedOutput("420"), references: [libComp.EmitToImageReference()], + var verifier = CompileAndVerify(src, expectedOutput: ExpectedOutput("4242"), references: [libComp.EmitToImageReference()], verify: Verification.Skipped, targetFramework: TargetFramework.Net80).VerifyDiagnostics(); verifier.VerifyIL("C.d__0.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext()", """ { - // Code size 449 (0x1c1) + // Code size 437 (0x1b5) .maxstack 3 .locals init (int V_0, S V_1, @@ -9989,23 +9989,23 @@ .locals init (int V_0, IL_0008: ldc.i4.s -6 IL_000a: sub IL_000b: switch ( - IL_0150, - IL_00c9, + IL_0144, + IL_00bd, IL_0072, IL_002c, IL_002c, IL_002c, - IL_011a) + IL_010e) IL_002c: ldarg.0 IL_002d: ldfld "bool C.d__0.<>w__disposeMode" IL_0032: brfalse.s IL_0039 - IL_0034: leave IL_018d + IL_0034: leave IL_0181 IL_0039: ldarg.0 IL_003a: ldc.i4.m1 IL_003b: dup IL_003c: stloc.0 IL_003d: stfld "int C.d__0.<>1__state" - IL_0042: br.s IL_00ab + IL_0042: br.s IL_009f IL_0044: ldarg.0 IL_0045: ldloca.s V_1 IL_0047: initobj "S" @@ -10022,7 +10022,7 @@ .locals init (int V_0, IL_0066: dup IL_0067: stloc.0 IL_0068: stfld "int C.d__0.<>1__state" - IL_006d: leave IL_01b4 + IL_006d: leave IL_01a8 IL_0072: ldarg.0 IL_0073: ldc.i4.m1 IL_0074: dup @@ -10031,7 +10031,7 @@ .locals init (int V_0, IL_007b: ldarg.0 IL_007c: ldfld "bool C.d__0.<>w__disposeMode" IL_0081: brfalse.s IL_0088 - IL_0083: leave IL_018d + IL_0083: leave IL_0181 IL_0088: ldarg.0 IL_0089: ldfld "S C.d__0.5__2" IL_008e: box "S" @@ -10040,121 +10040,118 @@ .locals init (int V_0, IL_0099: ldc.i4.0 IL_009a: stfld "bool C.d__0.b" IL_009f: ldarg.0 - IL_00a0: ldflda "S C.d__0.5__2" - IL_00a5: initobj "S" - IL_00ab: ldarg.0 - IL_00ac: ldfld "bool C.d__0.b" - IL_00b1: brtrue.s IL_0044 - IL_00b3: ldarg.0 - IL_00b4: ldc.i4.2 - IL_00b5: stfld "int C.d__0.<>2__current" - IL_00ba: ldarg.0 - IL_00bb: ldc.i4.s -5 - IL_00bd: dup - IL_00be: stloc.0 - IL_00bf: stfld "int C.d__0.<>1__state" - IL_00c4: leave IL_01b4 - IL_00c9: ldarg.0 - IL_00ca: ldc.i4.m1 - IL_00cb: dup - IL_00cc: stloc.0 - IL_00cd: stfld "int C.d__0.<>1__state" - IL_00d2: ldarg.0 - IL_00d3: ldfld "bool C.d__0.<>w__disposeMode" - IL_00d8: brfalse.s IL_00df - IL_00da: leave IL_018d - IL_00df: ldarg.0 - IL_00e0: ldfld "System.Threading.Tasks.Task C.d__0.task" - IL_00e5: callvirt "System.Runtime.CompilerServices.TaskAwaiter System.Threading.Tasks.Task.GetAwaiter()" - IL_00ea: stloc.2 - IL_00eb: ldloca.s V_2 - IL_00ed: call "bool System.Runtime.CompilerServices.TaskAwaiter.IsCompleted.get" - IL_00f2: brtrue.s IL_0136 - IL_00f4: ldarg.0 - IL_00f5: ldc.i4.0 - IL_00f6: dup - IL_00f7: stloc.0 - IL_00f8: stfld "int C.d__0.<>1__state" - IL_00fd: ldarg.0 - IL_00fe: ldloc.2 - IL_00ff: stfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" - IL_0104: ldarg.0 - IL_0105: stloc.3 - IL_0106: ldarg.0 - IL_0107: ldflda "System.Runtime.CompilerServices.AsyncIteratorMethodBuilder C.d__0.<>t__builder" - IL_010c: ldloca.s V_2 - IL_010e: ldloca.s V_3 - IL_0110: call "void System.Runtime.CompilerServices.AsyncIteratorMethodBuilder.AwaitUnsafeOnCompletedd__0>(ref System.Runtime.CompilerServices.TaskAwaiter, ref C.d__0)" - IL_0115: leave IL_01c0 - IL_011a: ldarg.0 - IL_011b: ldfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" - IL_0120: stloc.2 + IL_00a0: ldfld "bool C.d__0.b" + IL_00a5: brtrue.s IL_0044 + IL_00a7: ldarg.0 + IL_00a8: ldc.i4.2 + IL_00a9: stfld "int C.d__0.<>2__current" + IL_00ae: ldarg.0 + IL_00af: ldc.i4.s -5 + IL_00b1: dup + IL_00b2: stloc.0 + IL_00b3: stfld "int C.d__0.<>1__state" + IL_00b8: leave IL_01a8 + IL_00bd: ldarg.0 + IL_00be: ldc.i4.m1 + IL_00bf: dup + IL_00c0: stloc.0 + IL_00c1: stfld "int C.d__0.<>1__state" + IL_00c6: ldarg.0 + IL_00c7: ldfld "bool C.d__0.<>w__disposeMode" + IL_00cc: brfalse.s IL_00d3 + IL_00ce: leave IL_0181 + IL_00d3: ldarg.0 + IL_00d4: ldfld "System.Threading.Tasks.Task C.d__0.task" + IL_00d9: callvirt "System.Runtime.CompilerServices.TaskAwaiter System.Threading.Tasks.Task.GetAwaiter()" + IL_00de: stloc.2 + IL_00df: ldloca.s V_2 + IL_00e1: call "bool System.Runtime.CompilerServices.TaskAwaiter.IsCompleted.get" + IL_00e6: brtrue.s IL_012a + IL_00e8: ldarg.0 + IL_00e9: ldc.i4.0 + IL_00ea: dup + IL_00eb: stloc.0 + IL_00ec: stfld "int C.d__0.<>1__state" + IL_00f1: ldarg.0 + IL_00f2: ldloc.2 + IL_00f3: stfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" + IL_00f8: ldarg.0 + IL_00f9: stloc.3 + IL_00fa: ldarg.0 + IL_00fb: ldflda "System.Runtime.CompilerServices.AsyncIteratorMethodBuilder C.d__0.<>t__builder" + IL_0100: ldloca.s V_2 + IL_0102: ldloca.s V_3 + IL_0104: call "void System.Runtime.CompilerServices.AsyncIteratorMethodBuilder.AwaitUnsafeOnCompletedd__0>(ref System.Runtime.CompilerServices.TaskAwaiter, ref C.d__0)" + IL_0109: leave IL_01b4 + IL_010e: ldarg.0 + IL_010f: ldfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" + IL_0114: stloc.2 + IL_0115: ldarg.0 + IL_0116: ldflda "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" + IL_011b: initobj "System.Runtime.CompilerServices.TaskAwaiter" IL_0121: ldarg.0 - IL_0122: ldflda "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" - IL_0127: initobj "System.Runtime.CompilerServices.TaskAwaiter" - IL_012d: ldarg.0 - IL_012e: ldc.i4.m1 - IL_012f: dup - IL_0130: stloc.0 - IL_0131: stfld "int C.d__0.<>1__state" - IL_0136: ldloca.s V_2 - IL_0138: call "void System.Runtime.CompilerServices.TaskAwaiter.GetResult()" - IL_013d: ldarg.0 - IL_013e: ldc.i4.3 - IL_013f: stfld "int C.d__0.<>2__current" + IL_0122: ldc.i4.m1 + IL_0123: dup + IL_0124: stloc.0 + IL_0125: stfld "int C.d__0.<>1__state" + IL_012a: ldloca.s V_2 + IL_012c: call "void System.Runtime.CompilerServices.TaskAwaiter.GetResult()" + IL_0131: ldarg.0 + IL_0132: ldc.i4.3 + IL_0133: stfld "int C.d__0.<>2__current" + IL_0138: ldarg.0 + IL_0139: ldc.i4.s -6 + IL_013b: dup + IL_013c: stloc.0 + IL_013d: stfld "int C.d__0.<>1__state" + IL_0142: leave.s IL_01a8 IL_0144: ldarg.0 - IL_0145: ldc.i4.s -6 - IL_0147: dup - IL_0148: stloc.0 - IL_0149: stfld "int C.d__0.<>1__state" - IL_014e: leave.s IL_01b4 - IL_0150: ldarg.0 - IL_0151: ldc.i4.m1 - IL_0152: dup - IL_0153: stloc.0 - IL_0154: stfld "int C.d__0.<>1__state" - IL_0159: ldarg.0 - IL_015a: ldfld "bool C.d__0.<>w__disposeMode" - IL_015f: pop - IL_0160: leave.s IL_018d + IL_0145: ldc.i4.m1 + IL_0146: dup + IL_0147: stloc.0 + IL_0148: stfld "int C.d__0.<>1__state" + IL_014d: ldarg.0 + IL_014e: ldfld "bool C.d__0.<>w__disposeMode" + IL_0153: pop + IL_0154: leave.s IL_0181 } catch System.Exception { - IL_0162: stloc.s V_4 - IL_0164: ldarg.0 - IL_0165: ldc.i4.s -2 - IL_0167: stfld "int C.d__0.<>1__state" - IL_016c: ldarg.0 - IL_016d: ldc.i4.0 - IL_016e: stfld "int C.d__0.<>2__current" - IL_0173: ldarg.0 - IL_0174: ldflda "System.Runtime.CompilerServices.AsyncIteratorMethodBuilder C.d__0.<>t__builder" - IL_0179: call "void System.Runtime.CompilerServices.AsyncIteratorMethodBuilder.Complete()" - IL_017e: ldarg.0 - IL_017f: ldflda "System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore C.d__0.<>v__promiseOfValueOrEnd" - IL_0184: ldloc.s V_4 - IL_0186: call "void System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore.SetException(System.Exception)" - IL_018b: leave.s IL_01c0 + IL_0156: stloc.s V_4 + IL_0158: ldarg.0 + IL_0159: ldc.i4.s -2 + IL_015b: stfld "int C.d__0.<>1__state" + IL_0160: ldarg.0 + IL_0161: ldc.i4.0 + IL_0162: stfld "int C.d__0.<>2__current" + IL_0167: ldarg.0 + IL_0168: ldflda "System.Runtime.CompilerServices.AsyncIteratorMethodBuilder C.d__0.<>t__builder" + IL_016d: call "void System.Runtime.CompilerServices.AsyncIteratorMethodBuilder.Complete()" + IL_0172: ldarg.0 + IL_0173: ldflda "System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore C.d__0.<>v__promiseOfValueOrEnd" + IL_0178: ldloc.s V_4 + IL_017a: call "void System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore.SetException(System.Exception)" + IL_017f: leave.s IL_01b4 } - IL_018d: ldarg.0 - IL_018e: ldc.i4.s -2 - IL_0190: stfld "int C.d__0.<>1__state" - IL_0195: ldarg.0 - IL_0196: ldc.i4.0 - IL_0197: stfld "int C.d__0.<>2__current" - IL_019c: ldarg.0 - IL_019d: ldflda "System.Runtime.CompilerServices.AsyncIteratorMethodBuilder C.d__0.<>t__builder" - IL_01a2: call "void System.Runtime.CompilerServices.AsyncIteratorMethodBuilder.Complete()" - IL_01a7: ldarg.0 - IL_01a8: ldflda "System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore C.d__0.<>v__promiseOfValueOrEnd" - IL_01ad: ldc.i4.0 - IL_01ae: call "void System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore.SetResult(bool)" - IL_01b3: ret - IL_01b4: ldarg.0 - IL_01b5: ldflda "System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore C.d__0.<>v__promiseOfValueOrEnd" - IL_01ba: ldc.i4.1 - IL_01bb: call "void System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore.SetResult(bool)" - IL_01c0: ret + IL_0181: ldarg.0 + IL_0182: ldc.i4.s -2 + IL_0184: stfld "int C.d__0.<>1__state" + IL_0189: ldarg.0 + IL_018a: ldc.i4.0 + IL_018b: stfld "int C.d__0.<>2__current" + IL_0190: ldarg.0 + IL_0191: ldflda "System.Runtime.CompilerServices.AsyncIteratorMethodBuilder C.d__0.<>t__builder" + IL_0196: call "void System.Runtime.CompilerServices.AsyncIteratorMethodBuilder.Complete()" + IL_019b: ldarg.0 + IL_019c: ldflda "System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore C.d__0.<>v__promiseOfValueOrEnd" + IL_01a1: ldc.i4.0 + IL_01a2: call "void System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore.SetResult(bool)" + IL_01a7: ret + IL_01a8: ldarg.0 + IL_01a9: ldflda "System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore C.d__0.<>v__promiseOfValueOrEnd" + IL_01ae: ldc.i4.1 + IL_01af: call "void System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore.SetResult(bool)" + IL_01b4: ret } """); } diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncTests.cs index 3435c628529c4..154953276b345 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncTests.cs @@ -6259,12 +6259,12 @@ public static async System.Threading.Tasks.Task ProduceAsync(bool b, System } """; // Note: nested hoisted local gets cleared when exiting nested scope normally - var verifier = CompileAndVerify(src, expectedOutput: ExpectedOutput("420"), references: [libComp.EmitToImageReference()], + var verifier = CompileAndVerify(src, expectedOutput: ExpectedOutput("4242"), references: [libComp.EmitToImageReference()], targetFramework: TargetFramework.Net90, verify: Verification.Skipped).VerifyDiagnostics(); verifier.VerifyIL("C.d__0.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext()", """ { - // Code size 321 (0x141) + // Code size 309 (0x135) .maxstack 3 .locals init (int V_0, int V_1, @@ -6280,8 +6280,8 @@ .locals init (int V_0, IL_0008: brfalse.s IL_0065 IL_000a: ldloc.0 IL_000b: ldc.i4.1 - IL_000c: beq IL_00eb - IL_0011: br IL_00ab + IL_000c: beq IL_00df + IL_0011: br IL_009f IL_0016: ldarg.0 IL_0017: ldloca.s V_2 IL_0019: initobj "S" @@ -6309,7 +6309,7 @@ .locals init (int V_0, IL_0058: ldloca.s V_3 IL_005a: ldarg.0 IL_005b: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AwaitUnsafeOnCompletedd__0>(ref System.Runtime.CompilerServices.TaskAwaiter, ref C.d__0)" - IL_0060: leave IL_0140 + IL_0060: leave IL_0134 IL_0065: ldarg.0 IL_0066: ldfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" IL_006b: stloc.3 @@ -6331,69 +6331,66 @@ .locals init (int V_0, IL_0099: ldc.i4.0 IL_009a: stfld "bool C.d__0.b" IL_009f: ldarg.0 - IL_00a0: ldflda "S C.d__0.5__2" - IL_00a5: initobj "S" - IL_00ab: ldarg.0 - IL_00ac: ldfld "bool C.d__0.b" - IL_00b1: brtrue IL_0016 - IL_00b6: ldarg.0 - IL_00b7: ldfld "System.Threading.Tasks.Task C.d__0.task" - IL_00bc: callvirt "System.Runtime.CompilerServices.TaskAwaiter System.Threading.Tasks.Task.GetAwaiter()" - IL_00c1: stloc.3 - IL_00c2: ldloca.s V_3 - IL_00c4: call "bool System.Runtime.CompilerServices.TaskAwaiter.IsCompleted.get" - IL_00c9: brtrue.s IL_0107 - IL_00cb: ldarg.0 - IL_00cc: ldc.i4.1 - IL_00cd: dup - IL_00ce: stloc.0 - IL_00cf: stfld "int C.d__0.<>1__state" - IL_00d4: ldarg.0 - IL_00d5: ldloc.3 - IL_00d6: stfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" - IL_00db: ldarg.0 - IL_00dc: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder C.d__0.<>t__builder" - IL_00e1: ldloca.s V_3 - IL_00e3: ldarg.0 - IL_00e4: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AwaitUnsafeOnCompletedd__0>(ref System.Runtime.CompilerServices.TaskAwaiter, ref C.d__0)" - IL_00e9: leave.s IL_0140 - IL_00eb: ldarg.0 - IL_00ec: ldfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" - IL_00f1: stloc.3 + IL_00a0: ldfld "bool C.d__0.b" + IL_00a5: brtrue IL_0016 + IL_00aa: ldarg.0 + IL_00ab: ldfld "System.Threading.Tasks.Task C.d__0.task" + IL_00b0: callvirt "System.Runtime.CompilerServices.TaskAwaiter System.Threading.Tasks.Task.GetAwaiter()" + IL_00b5: stloc.3 + IL_00b6: ldloca.s V_3 + IL_00b8: call "bool System.Runtime.CompilerServices.TaskAwaiter.IsCompleted.get" + IL_00bd: brtrue.s IL_00fb + IL_00bf: ldarg.0 + IL_00c0: ldc.i4.1 + IL_00c1: dup + IL_00c2: stloc.0 + IL_00c3: stfld "int C.d__0.<>1__state" + IL_00c8: ldarg.0 + IL_00c9: ldloc.3 + IL_00ca: stfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" + IL_00cf: ldarg.0 + IL_00d0: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder C.d__0.<>t__builder" + IL_00d5: ldloca.s V_3 + IL_00d7: ldarg.0 + IL_00d8: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AwaitUnsafeOnCompletedd__0>(ref System.Runtime.CompilerServices.TaskAwaiter, ref C.d__0)" + IL_00dd: leave.s IL_0134 + IL_00df: ldarg.0 + IL_00e0: ldfld "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" + IL_00e5: stloc.3 + IL_00e6: ldarg.0 + IL_00e7: ldflda "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" + IL_00ec: initobj "System.Runtime.CompilerServices.TaskAwaiter" IL_00f2: ldarg.0 - IL_00f3: ldflda "System.Runtime.CompilerServices.TaskAwaiter C.d__0.<>u__1" - IL_00f8: initobj "System.Runtime.CompilerServices.TaskAwaiter" - IL_00fe: ldarg.0 - IL_00ff: ldc.i4.m1 - IL_0100: dup - IL_0101: stloc.0 - IL_0102: stfld "int C.d__0.<>1__state" - IL_0107: ldloca.s V_3 - IL_0109: call "void System.Runtime.CompilerServices.TaskAwaiter.GetResult()" - IL_010e: ldc.i4.s 10 - IL_0110: stloc.1 - IL_0111: leave.s IL_012c + IL_00f3: ldc.i4.m1 + IL_00f4: dup + IL_00f5: stloc.0 + IL_00f6: stfld "int C.d__0.<>1__state" + IL_00fb: ldloca.s V_3 + IL_00fd: call "void System.Runtime.CompilerServices.TaskAwaiter.GetResult()" + IL_0102: ldc.i4.s 10 + IL_0104: stloc.1 + IL_0105: leave.s IL_0120 } catch System.Exception { - IL_0113: stloc.s V_4 - IL_0115: ldarg.0 - IL_0116: ldc.i4.s -2 - IL_0118: stfld "int C.d__0.<>1__state" - IL_011d: ldarg.0 - IL_011e: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder C.d__0.<>t__builder" - IL_0123: ldloc.s V_4 - IL_0125: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetException(System.Exception)" - IL_012a: leave.s IL_0140 + IL_0107: stloc.s V_4 + IL_0109: ldarg.0 + IL_010a: ldc.i4.s -2 + IL_010c: stfld "int C.d__0.<>1__state" + IL_0111: ldarg.0 + IL_0112: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder C.d__0.<>t__builder" + IL_0117: ldloc.s V_4 + IL_0119: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetException(System.Exception)" + IL_011e: leave.s IL_0134 } - IL_012c: ldarg.0 - IL_012d: ldc.i4.s -2 - IL_012f: stfld "int C.d__0.<>1__state" - IL_0134: ldarg.0 - IL_0135: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder C.d__0.<>t__builder" - IL_013a: ldloc.1 - IL_013b: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(int)" - IL_0140: ret + IL_0120: ldarg.0 + IL_0121: ldc.i4.s -2 + IL_0123: stfld "int C.d__0.<>1__state" + IL_0128: ldarg.0 + IL_0129: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder C.d__0.<>t__builder" + IL_012e: ldloc.1 + IL_012f: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(int)" + IL_0134: ret } """); } diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenIterators.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenIterators.cs index fa76ca71c6aa7..ed433fb595c67 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenIterators.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenIterators.cs @@ -4133,10 +4133,10 @@ public static System.Collections.Generic.IEnumerable M(bool b, T t) wher } } """; - var verifier = CompileAndVerify(src, expectedOutput: "10 42 0").VerifyDiagnostics(); + var verifier = CompileAndVerify(src, expectedOutput: "10 42 42").VerifyDiagnostics(); verifier.VerifyIL("C.d__0.System.Collections.IEnumerator.MoveNext()", """ { - // Code size 106 (0x6a) + // Code size 94 (0x5e) .maxstack 2 .locals init (int V_0) IL_0000: ldarg.0 @@ -4152,7 +4152,7 @@ .locals init (int V_0) IL_0010: ldarg.0 IL_0011: ldc.i4.m1 IL_0012: stfld "int C.d__0.<>1__state" - IL_0017: br.s IL_0060 + IL_0017: br.s IL_0054 IL_0019: ldarg.0 IL_001a: ldarg.0 IL_001b: ldfld "T C.d__0.t" @@ -4176,13 +4176,10 @@ .locals init (int V_0) IL_004a: box "T" IL_004f: call "void System.Console.Write(object)" IL_0054: ldarg.0 - IL_0055: ldflda "T C.d__0.5__2" - IL_005a: initobj "T" - IL_0060: ldarg.0 - IL_0061: ldfld "bool C.d__0.b" - IL_0066: brtrue.s IL_0019 - IL_0068: ldc.i4.0 - IL_0069: ret + IL_0055: ldfld "bool C.d__0.b" + IL_005a: brtrue.s IL_0019 + IL_005c: ldc.i4.0 + IL_005d: ret } """); verifier.VerifyIL("C.d__0.System.IDisposable.Dispose()", """ @@ -4243,10 +4240,10 @@ public static System.Collections.Generic.IEnumerable M(bool b, S s) } """; - var verifier = CompileAndVerify(src, expectedOutput: "10 42 0", references: [libComp.EmitToImageReference()]).VerifyDiagnostics(); + var verifier = CompileAndVerify(src, expectedOutput: "10 42 42", references: [libComp.EmitToImageReference()]).VerifyDiagnostics(); verifier.VerifyIL("C.d__0.System.Collections.IEnumerator.MoveNext()", """ { - // Code size 112 (0x70) + // Code size 100 (0x64) .maxstack 2 .locals init (int V_0) IL_0000: ldarg.0 @@ -4262,7 +4259,7 @@ .locals init (int V_0) IL_0010: ldarg.0 IL_0011: ldc.i4.m1 IL_0012: stfld "int C.d__0.<>1__state" - IL_0017: br.s IL_0066 + IL_0017: br.s IL_005a IL_0019: ldarg.0 IL_001a: ldarg.0 IL_001b: ldfld "S C.d__0.s" @@ -4287,13 +4284,10 @@ .locals init (int V_0) IL_0050: callvirt "string object.ToString()" IL_0055: call "void System.Console.Write(string)" IL_005a: ldarg.0 - IL_005b: ldflda "S C.d__0.5__2" - IL_0060: initobj "S" - IL_0066: ldarg.0 - IL_0067: ldfld "bool C.d__0.b" - IL_006c: brtrue.s IL_0019 - IL_006e: ldc.i4.0 - IL_006f: ret + IL_005b: ldfld "bool C.d__0.b" + IL_0060: brtrue.s IL_0019 + IL_0062: ldc.i4.0 + IL_0063: ret } """); verifier.VerifyIL("C.d__0.System.IDisposable.Dispose()", """ diff --git a/src/Compilers/CSharp/Test/Emit2/Emit/EditAndContinue/EditAndContinueStateMachineTests.cs b/src/Compilers/CSharp/Test/Emit2/Emit/EditAndContinue/EditAndContinueStateMachineTests.cs index be32d75c9af80..f814e6204c9b2 100644 --- a/src/Compilers/CSharp/Test/Emit2/Emit/EditAndContinue/EditAndContinueStateMachineTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Emit/EditAndContinue/EditAndContinueStateMachineTests.cs @@ -5493,7 +5493,7 @@ static IEnumerable F() diff1.VerifyIL("C.d__0.System.Collections.IEnumerator.MoveNext", @" { - // Code size 526 (0x20e) + // Code size 514 (0x202) .maxstack 2 .locals init (bool V_0, int V_1, @@ -5514,7 +5514,7 @@ .locals init (bool V_0, IL_0022: br IL_0165 IL_0027: ldc.i4.0 IL_0028: stloc.0 - IL_0029: leave IL_020c + IL_0029: leave IL_0200 IL_002e: ldarg.0 IL_002f: ldc.i4.m1 IL_0030: stfld ""int C.d__0.<>1__state"" @@ -5557,7 +5557,7 @@ .locals init (bool V_0, IL_00a6: ldarg.0 IL_00a7: ldc.i4.s -8 IL_00a9: stfld ""int C.d__0.<>1__state"" - IL_00ae: br IL_01a6 + IL_00ae: br IL_019a IL_00b3: ldarg.0 IL_00b4: ldarg.0 IL_00b5: ldfld ""System.Collections.Generic.IEnumerator> C.d__0.<>s__6"" @@ -5608,7 +5608,7 @@ .locals init (bool V_0, IL_013c: stfld ""int C.d__0.<>1__state"" IL_0141: ldc.i4.1 IL_0142: stloc.0 - IL_0143: leave IL_020c + IL_0143: leave IL_0200 IL_0148: ldarg.0 IL_0149: ldc.i4.s -10 IL_014b: stfld ""int C.d__0.<>1__state"" @@ -5620,7 +5620,7 @@ .locals init (bool V_0, IL_0159: stfld ""int C.d__0.<>1__state"" IL_015e: ldc.i4.1 IL_015f: stloc.0 - IL_0160: leave IL_020c + IL_0160: leave IL_0200 IL_0165: ldarg.0 IL_0166: ldc.i4.s -10 IL_0168: stfld ""int C.d__0.<>1__state"" @@ -5642,57 +5642,54 @@ .locals init (bool V_0, IL_0194: ldnull IL_0195: stfld ""System.Collections.Generic.IEnumerator> C.d__0.<>s__8"" IL_019a: ldarg.0 - IL_019b: ldflda ""System.ValueTuple C.d__0.5__7"" - IL_01a0: initobj ""System.ValueTuple"" - IL_01a6: ldarg.0 - IL_01a7: ldfld ""System.Collections.Generic.IEnumerator> C.d__0.<>s__6"" - IL_01ac: callvirt ""bool System.Collections.IEnumerator.MoveNext()"" - IL_01b1: brtrue IL_00b3 - IL_01b6: ldarg.0 - IL_01b7: call ""void C.d__0.<>m__Finally6()"" - IL_01bc: nop - IL_01bd: ldarg.0 - IL_01be: ldnull - IL_01bf: stfld ""System.Collections.Generic.IEnumerator> C.d__0.<>s__6"" - IL_01c4: ldarg.0 - IL_01c5: call ""void C.d__0.<>m__Finally5()"" - IL_01ca: nop - IL_01cb: ldarg.0 - IL_01cc: call ""void C.d__0.<>m__Finally4()"" - IL_01d1: nop - IL_01d2: ldarg.0 - IL_01d3: ldnull - IL_01d4: stfld ""System.IDisposable C.d__0.5__4"" - IL_01d9: ldarg.0 - IL_01da: ldnull - IL_01db: stfld ""System.IDisposable C.d__0.5__5"" - IL_01e0: ldarg.0 - IL_01e1: call ""void C.d__0.<>m__Finally3()"" - IL_01e6: nop - IL_01e7: ldarg.0 - IL_01e8: ldnull - IL_01e9: stfld ""System.IDisposable C.d__0.<>s__3"" - IL_01ee: ldc.i4.0 - IL_01ef: stloc.0 - IL_01f0: br.s IL_01f2 - IL_01f2: ldarg.0 - IL_01f3: call ""void C.d__0.<>m__Finally2()"" - IL_01f8: nop - IL_01f9: br.s IL_01fb - IL_01fb: ldarg.0 - IL_01fc: call ""void C.d__0.<>m__Finally1()"" - IL_0201: nop - IL_0202: leave.s IL_020c + IL_019b: ldfld ""System.Collections.Generic.IEnumerator> C.d__0.<>s__6"" + IL_01a0: callvirt ""bool System.Collections.IEnumerator.MoveNext()"" + IL_01a5: brtrue IL_00b3 + IL_01aa: ldarg.0 + IL_01ab: call ""void C.d__0.<>m__Finally6()"" + IL_01b0: nop + IL_01b1: ldarg.0 + IL_01b2: ldnull + IL_01b3: stfld ""System.Collections.Generic.IEnumerator> C.d__0.<>s__6"" + IL_01b8: ldarg.0 + IL_01b9: call ""void C.d__0.<>m__Finally5()"" + IL_01be: nop + IL_01bf: ldarg.0 + IL_01c0: call ""void C.d__0.<>m__Finally4()"" + IL_01c5: nop + IL_01c6: ldarg.0 + IL_01c7: ldnull + IL_01c8: stfld ""System.IDisposable C.d__0.5__4"" + IL_01cd: ldarg.0 + IL_01ce: ldnull + IL_01cf: stfld ""System.IDisposable C.d__0.5__5"" + IL_01d4: ldarg.0 + IL_01d5: call ""void C.d__0.<>m__Finally3()"" + IL_01da: nop + IL_01db: ldarg.0 + IL_01dc: ldnull + IL_01dd: stfld ""System.IDisposable C.d__0.<>s__3"" + IL_01e2: ldc.i4.0 + IL_01e3: stloc.0 + IL_01e4: br.s IL_01e6 + IL_01e6: ldarg.0 + IL_01e7: call ""void C.d__0.<>m__Finally2()"" + IL_01ec: nop + IL_01ed: br.s IL_01ef + IL_01ef: ldarg.0 + IL_01f0: call ""void C.d__0.<>m__Finally1()"" + IL_01f5: nop + IL_01f6: leave.s IL_0200 } fault { - IL_0204: ldarg.0 - IL_0205: call ""void C.d__0.Dispose()"" - IL_020a: nop - IL_020b: endfinally + IL_01f8: ldarg.0 + IL_01f9: call ""void C.d__0.Dispose()"" + IL_01fe: nop + IL_01ff: endfinally } - IL_020c: ldloc.0 - IL_020d: ret + IL_0200: ldloc.0 + IL_0201: ret }"); }