From 3c7b85629ad12a1ad478e588235dbd832e52d29c Mon Sep 17 00:00:00 2001 From: tmat Date: Wed, 11 Jan 2023 14:12:29 -0800 Subject: [PATCH 1/4] Add test showing incorrect sequence point placement --- .../CSharp/Test/Emit/PDB/PDBLambdaTests.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs b/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs index b5025702e0e8d..6175b934ca36a 100644 --- a/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs +++ b/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs @@ -2100,5 +2100,77 @@ static int F(int x, out int p) ", format: CodeAnalysis.Emit.DebugInformationFormat.Pdb); } + + [Fact] + [WorkItem(32352, "https://github.com/dotnet/roslyn/issues/32352")] + public void ClosureAllocationSequencePoints() + { + var source = WithWindowsLineBreaks(@" +using System; + +class C +{ + void F(bool outer) + { + if (outer) + { + var inner = false; + var f = new Func(() => inner & outer); + } + } +}"); + var c = CompileAndVerify(source, targetFramework: TargetFramework.NetStandard20, options: TestOptions.DebugDll); + + // TODO: https://github.com/dotnet/roslyn/issues/32352 + // The inner closure allocation on IL_0018 should be withing the sequence point associated with opening brace of the if statement. + // This sequence point is currently on IL_0025, which causes NRE when the instruction pointer is moved to the opening brace. + + c.VerifyIL("C.F", @" +{ + // Code size 60 (0x3c) + .maxstack 2 + .locals init (C.<>c__DisplayClass0_0 V_0, //CS$<>8__locals0 + bool V_1, + C.<>c__DisplayClass0_1 V_2, //CS$<>8__locals1 + System.Func V_3) //f + // sequence point: + IL_0000: newobj ""C.<>c__DisplayClass0_0..ctor()"" + IL_0005: stloc.0 + IL_0006: ldloc.0 + IL_0007: ldarg.1 + IL_0008: stfld ""bool C.<>c__DisplayClass0_0.outer"" + // sequence point: { + IL_000d: nop + // sequence point: if (outer) + IL_000e: ldloc.0 + IL_000f: ldfld ""bool C.<>c__DisplayClass0_0.outer"" + IL_0014: stloc.1 + // sequence point: + IL_0015: ldloc.1 + IL_0016: brfalse.s IL_003b + // sequence point: + IL_0018: newobj ""C.<>c__DisplayClass0_1..ctor()"" + IL_001d: stloc.2 + IL_001e: ldloc.2 + IL_001f: ldloc.0 + IL_0020: stfld ""C.<>c__DisplayClass0_0 C.<>c__DisplayClass0_1.CS$<>8__locals1"" + // sequence point: { + IL_0025: nop + // sequence point: var inner = false; + IL_0026: ldloc.2 + IL_0027: ldc.i4.0 + IL_0028: stfld ""bool C.<>c__DisplayClass0_1.inner"" + // sequence point: var f = new Func(() => inner & outer); + IL_002d: ldloc.2 + IL_002e: ldftn ""bool C.<>c__DisplayClass0_1.b__0()"" + IL_0034: newobj ""System.Func..ctor(object, System.IntPtr)"" + IL_0039: stloc.3 + // sequence point: } + IL_003a: nop + // sequence point: } + IL_003b: ret +} +", sequencePoints: "C.F", source: source); + } } } From 5fb9f1e4bd4074741415edbb91e47da1d7a1246b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Matou=C5=A1ek?= Date: Wed, 11 Jan 2023 14:18:12 -0800 Subject: [PATCH 2/4] Update src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs Co-authored-by: Julien Couvreur --- src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs b/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs index 6175b934ca36a..18b55c7094774 100644 --- a/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs +++ b/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs @@ -2122,7 +2122,7 @@ void F(bool outer) var c = CompileAndVerify(source, targetFramework: TargetFramework.NetStandard20, options: TestOptions.DebugDll); // TODO: https://github.com/dotnet/roslyn/issues/32352 - // The inner closure allocation on IL_0018 should be withing the sequence point associated with opening brace of the if statement. + // The inner closure allocation on IL_0018 should be within the sequence point associated with opening brace of the if statement. // This sequence point is currently on IL_0025, which causes NRE when the instruction pointer is moved to the opening brace. c.VerifyIL("C.F", @" From 9d98795e462ba0149a2cd4b3c01999b825cfb6ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Matou=C5=A1ek?= Date: Thu, 12 Jan 2023 10:07:02 -0800 Subject: [PATCH 3/4] Feedback --- src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs b/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs index 18b55c7094774..eaedb86e7194c 100644 --- a/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs +++ b/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs @@ -2105,7 +2105,7 @@ static int F(int x, out int p) [WorkItem(32352, "https://github.com/dotnet/roslyn/issues/32352")] public void ClosureAllocationSequencePoints() { - var source = WithWindowsLineBreaks(@" + var source = @" using System; class C @@ -2118,8 +2118,8 @@ void F(bool outer) var f = new Func(() => inner & outer); } } -}"); - var c = CompileAndVerify(source, targetFramework: TargetFramework.NetStandard20, options: TestOptions.DebugDll); +}"; + var c = CompileAndVerify(source); // TODO: https://github.com/dotnet/roslyn/issues/32352 // The inner closure allocation on IL_0018 should be within the sequence point associated with opening brace of the if statement. From fcabf22df5efa2282df97e836f4166b71565ea6e Mon Sep 17 00:00:00 2001 From: tmat Date: Fri, 13 Jan 2023 09:07:55 -0800 Subject: [PATCH 4/4] Fix --- src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs b/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs index eaedb86e7194c..0c623da3363ab 100644 --- a/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs +++ b/src/Compilers/CSharp/Test/Emit/PDB/PDBLambdaTests.cs @@ -2119,7 +2119,7 @@ void F(bool outer) } } }"; - var c = CompileAndVerify(source); + var c = CompileAndVerify(source, options: TestOptions.DebugDll); // TODO: https://github.com/dotnet/roslyn/issues/32352 // The inner closure allocation on IL_0018 should be within the sequence point associated with opening brace of the if statement.