提交 93fe7532 编写于 作者: T Ty Overby 提交者: GitHub

allow use of a byref parameter inside of nameof (#16596)

* allow use of a byref parameter inside of nameof

* remove random whitespace changes

* add handling for linq queries

* And -> AndAlso

* change name of linq query

* add linq assembly ref
上级 8a056f2f
......@@ -22,6 +22,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Private _containingSymbol As MethodSymbol
Private _withExpressionPlaceholderMap As Dictionary(Of BoundValuePlaceholderBase, BoundWithStatement)
Private _expressionsBeingVisited As Stack(Of BoundExpression)
Private _insideNameof As Boolean = False
Private _inExpressionLambda As Boolean
......@@ -66,7 +67,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
' cannot access ByRef parameters in Lambdas
Dim parameterSymbolContainingSymbol As Symbol = parameterSymbol.ContainingSymbol
If _containingSymbol IsNot parameterSymbolContainingSymbol Then
If _containingSymbol IsNot parameterSymbolContainingSymbol AndAlso Not _insideNameof Then
' Need to go up the chain of containers and see if the last lambda we see
' is a QueryLambda, before we reach parameter's container.
If Binder.IsTopMostEnclosingLambdaAQueryLambda(_containingSymbol, parameterSymbolContainingSymbol) Then
......@@ -183,6 +184,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return Nothing
End Function
Public Overrides Function VisitNameOfOperator(node As BoundNameOfOperator) As BoundNode
_insideNameof = True
Dim r = MyBase.VisitNameOfOperator(node)
_insideNameof = False
Return r
End Function
Public Overrides Function Visit(node As BoundNode) As BoundNode
Dim pop As Boolean = False
......
......@@ -3544,5 +3544,51 @@ TP
]]>).VerifyDiagnostics()
End Sub
<Fact, WorkItem(10839, "https://github.com/dotnet/roslyn/issues/10839")>
Public Sub NameOfByRefInLambda()
Dim compilationDef =
<compilation>
<file name="a.vb">
Module Program
Sub DoSomething(ByRef x As Integer)
Dim f = Function()
Return NameOf(x)
End Function
System.Console.WriteLine(f())
End Sub
Sub Main()
Dim x = 5
DoSomething(x)
End Sub
End Module
</file>
</compilation>
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(compilationDef, TestOptions.DebugExe)
CompileAndVerify(comp, expectedOutput:="x").VerifyDiagnostics()
End Sub
<Fact, WorkItem(10839, "https://github.com/dotnet/roslyn/issues/10839")>
Public Sub NameOfByRefInQuery()
Dim compilationDef =
<compilation>
<file name="a.vb">
Imports System.Linq
Module Program
Sub DoSomething(ByRef x As Integer)
Dim f = from y in {1, 2, 3}
select nameof(x)
System.Console.WriteLine(f.Aggregate("", Function(a, b) a + b))
End Sub
Sub Main()
Dim x = 5
DoSomething(x)
End Sub
End Module
</file>
</compilation>
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(compilationDef, options:=TestOptions.DebugExe, additionalRefs:={LinqAssemblyRef})
CompileAndVerify(comp, expectedOutput:="xxx").VerifyDiagnostics()
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册