diff --git a/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/Rewriters/CapturedVariableRewriter.cs b/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/Rewriters/CapturedVariableRewriter.cs index fe31ce63c8b6de6cfce79168044bf77f7a916601..652f13b5227b1d220c53c2a51d0c81f7366bcc34 100644 --- a/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/Rewriters/CapturedVariableRewriter.cs +++ b/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/Rewriters/CapturedVariableRewriter.cs @@ -140,7 +140,7 @@ private BoundExpression RewriteParameter(CSharpSyntaxNode syntax, ParameterSymbo { var typeNameKind = GeneratedNames.GetKind(symbol.Type.Name); if (typeNameKind != GeneratedNameKind.None && - GeneratedNames.GetKind(symbol.Name) != GeneratedNameKind.TransparentIdentifier) + typeNameKind != GeneratedNameKind.AnonymousType) { // The state machine case is for async lambdas. The state machine // will have a hoisted "this" field if it needs to access the diff --git a/src/ExpressionEvaluator/CSharp/Test/ExpressionCompiler/LocalsTests.cs b/src/ExpressionEvaluator/CSharp/Test/ExpressionCompiler/LocalsTests.cs index 63532c16f1493911d7ed5ff2b3243bf3520cec53..0bee0c9d6b68aa015c44217b88b721f136e08a1e 100644 --- a/src/ExpressionEvaluator/CSharp/Test/ExpressionCompiler/LocalsTests.cs +++ b/src/ExpressionEvaluator/CSharp/Test/ExpressionCompiler/LocalsTests.cs @@ -2963,6 +2963,59 @@ .maxstack 1 testData.GetMethodData("<>x.<>m0").VerifyIL(yIL); } + [WorkItem(3236, "https://github.com/dotnet/roslyn/pull/3236")] + [Fact] + public void AnonymousTypeParameter() + { + const string source = @" +using System.Linq; + +class C +{ + static void Main(string[] args) + { + var anonymousTypes = + from a in args + select new { Value = a, Length = a.Length }; + var values = + from t in anonymousTypes + select t.Value; + } +} +"; + + const string methodName = "C.<>c.
b__0_1"; + + const string tIL = @" +{ + // Code size 2 (0x2) + .maxstack 1 + IL_0000: ldarg.1 + IL_0001: ret +} +"; + + var comp = CreateCompilationWithMscorlib(source, new[] { SystemCoreRef }, TestOptions.DebugDll); + var runtime = CreateRuntimeInstance(comp); + + string typeName; + var locals = ArrayBuilder.GetInstance(); + CompilationTestData testData; + GetLocals(runtime, methodName, argumentsOnly: false, locals: locals, count: 1, typeName: out typeName, testData: out testData); + + VerifyLocal(testData, typeName, locals[0], "<>m0", "t", expectedILOpt: tIL); + + locals.Free(); + + var context = CreateMethodContext(runtime, methodName); + string error; + + testData = new CompilationTestData(); + context.CompileExpression("t", out error, testData); + Assert.Null(error); + testData.GetMethodData("<>x.<>m0").VerifyIL(tIL); + } + private static void GetLocals(RuntimeInstance runtime, string methodName, bool argumentsOnly, ArrayBuilder locals, int count, out string typeName, out CompilationTestData testData) { var context = CreateMethodContext(runtime, methodName); diff --git a/src/ExpressionEvaluator/VisualBasic/Test/ExpressionCompiler/LocalsTests.vb b/src/ExpressionEvaluator/VisualBasic/Test/ExpressionCompiler/LocalsTests.vb index b93e74c02f20a14385839fcf5fb0948dce586044..72f7ca03378e8d500c50ca14cc5e2533c9c4e62d 100644 --- a/src/ExpressionEvaluator/VisualBasic/Test/ExpressionCompiler/LocalsTests.vb +++ b/src/ExpressionEvaluator/VisualBasic/Test/ExpressionCompiler/LocalsTests.vb @@ -3145,6 +3145,55 @@ End Class testData.GetMethodData("<>x.<>m0").VerifyIL(xIL) End Sub + + + Public Sub AnonymousTypeParameter() + Const source = " +Imports System.Linq + +Class C + Shared Sub Main(args As String()) + Dim anonymousTypes = + From a In args + Select New With {.Value = a, .Length = a.Length} + Dim values = + From t In anonymousTypes + Select t.Value + End Sub +End Class +" + + Const methodName = "C._Closure$__._Lambda$__1-1" + + Const xIL = " +{ + // Code size 2 (0x2) + .maxstack 1 + IL_0000: ldarg.1 + IL_0001: ret +} +" + + Dim comp = CreateCompilationWithMscorlib({source}, {SystemCoreRef, MsvbRef}, TestOptions.DebugDll) + Dim runtime = CreateRuntimeInstance(comp) + + Dim typeName As String = Nothing + Dim locals = ArrayBuilder(Of LocalAndMethod).GetInstance() + Dim testData As CompilationTestData = Nothing + GetLocals(runtime, methodName, argumentsOnly:=False, locals:=locals, count:=1, typeName:=typeName, testData:=testData) + + VerifyLocal(testData, typeName, locals(0), "<>m0", "t", expectedILOpt:=xIL) + locals.Free() + + Dim context = CreateMethodContext(runtime, methodName) + Dim errorMessage As String = Nothing + + testData = New CompilationTestData() + context.CompileExpression("t", errorMessage, testData) + Assert.Null(errorMessage) + testData.GetMethodData("<>x.<>m0").VerifyIL(xIL) + End Sub + Private Shared Sub GetLocals(runtime As RuntimeInstance, methodName As String, argumentsOnly As Boolean, locals As ArrayBuilder(Of LocalAndMethod), count As Integer, ByRef typeName As String, ByRef testData As CompilationTestData) Dim context = CreateMethodContext(runtime, methodName) testData = New CompilationTestData()