Skip to content

Commit

Permalink
Specially handle tuple methods in CodeGenerator.EmitMethodInfoExpress…
Browse files Browse the repository at this point in the history
…ion for VB.

Fixes dotnet#27322.
  • Loading branch information
AlekseyTs committed Apr 22, 2020
1 parent c7c78a9 commit dd93c55
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Compilers/VisualBasic/Portable/CodeGen/EmitExpression.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2178,15 +2178,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGen
End Sub

Private Sub EmitMethodInfoExpression(node As BoundMethodInfo, used As Boolean)
Dim method As MethodSymbol = node.Method

If method.IsTupleMethod Then
method = method.TupleUnderlyingMethod
End If

_builder.EmitOpCode(ILOpCode.Ldtoken)
EmitSymbolToken(node.Method, node.Syntax)
EmitSymbolToken(method, node.Syntax)
Dim getMethod As MethodSymbol
If Not node.Method.ContainingType.IsGenericType AndAlso Not node.Method.ContainingType.IsAnonymousType Then ' anonymous types are generic under the hood.
If Not method.ContainingType.IsGenericType AndAlso Not method.ContainingType.IsAnonymousType Then ' anonymous types are generic under the hood.
_builder.EmitOpCode(ILOpCode.Call, stackAdjustment:=0) ' argument off, return value on
getMethod = DirectCast(Me._module.Compilation.GetWellKnownTypeMember(WellKnownMember.System_Reflection_MethodBase__GetMethodFromHandle), MethodSymbol)
Else
_builder.EmitOpCode(ILOpCode.Ldtoken)
EmitSymbolToken(node.Method.ContainingType, node.Syntax)
EmitSymbolToken(method.ContainingType, node.Syntax)
_builder.EmitOpCode(ILOpCode.Call, stackAdjustment:=-1) ' 2 arguments off, return value on
getMethod = DirectCast(Me._module.Compilation.GetWellKnownTypeMember(WellKnownMember.System_Reflection_MethodBase__GetMethodFromHandle2), MethodSymbol)
End If
Expand Down
40 changes: 40 additions & 0 deletions src/Compilers/VisualBasic/Test/Emit/CodeGen/CodeGenTuples.vb
Original file line number Diff line number Diff line change
Expand Up @@ -23050,6 +23050,46 @@ End Class"
End Select
End Sub

<Fact>
<WorkItem(27322, "https://github.com/dotnet/roslyn/issues/27322")>
Public Sub Issue27322()
Dim source0 = "
Imports System
Imports System.Collections.Generic
Imports System.Linq.Expressions

Module Module1

Sub Main()
Dim tupleA = (1, 3)
Dim tupleB = (1, ""123"".Length)

Dim ok1 As Expression(Of Func(Of Integer)) = Function() tupleA.Item1
Dim ok2 As Expression(Of Func(Of Integer)) = Function() tupleA.GetHashCode()
Dim ok3 As Expression(Of Func(Of Tuple(Of Integer, Integer))) = Function() tupleA.ToTuple()
Dim ok4 As Expression(Of Func(Of Boolean)) = Function() Equals(tupleA, tupleB)
Dim ok5 As Expression(Of Func(Of Integer)) = Function() Comparer(Of (Integer, Integer)).Default.Compare(tupleA, tupleB)
ok1.Compile()()
ok2.Compile()()
ok3.Compile()()
ok4.Compile()()
ok5.Compile()()

Dim err1 As Expression(Of Func(Of Boolean)) = Function() tupleA.Equals(tupleB)
Dim err2 As Expression(Of Func(Of Integer)) = Function() tupleA.CompareTo(tupleB)

err1.Compile()()
err2.Compile()()

System.Console.WriteLine(""Done"")
End Sub

End Module
"

Dim comp1 = CreateCompilation(source0, options:=TestOptions.DebugExe)
CompileAndVerify(comp1, expectedOutput:="Done")
End Sub
End Class

End Namespace
Expand Down

0 comments on commit dd93c55

Please sign in to comment.