Skip to content

Commit fc6092f

Browse files
authored
Fix DeclaringSyntaxReferences returning empty for VB catch variables (#81151)
1 parent 16c94d5 commit fc6092f

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/Compilers/VisualBasic/Portable/Symbols/Source/LocalSymbol.vb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
836836

837837
MyBase.New(container, binder, declaringIdentifier, declarationKind, Nothing)
838838

839+
Debug.Assert(modifiedIdentifierOpt IsNot Nothing OrElse declarationKind = LocalDeclarationKind.Catch,
840+
"Only catch variables should have Nothing for modifiedIdentifierOpt")
841+
839842
_modifiedIdentifierOpt = modifiedIdentifierOpt
840843
_asClauseOpt = asClauseOpt
841844
_initializerOpt = initializerOpt
@@ -920,17 +923,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
920923

921924
Public Overrides ReadOnly Property DeclaringSyntaxReferences As ImmutableArray(Of SyntaxReference)
922925
Get
923-
Select Case DeclarationKind
924-
Case LocalDeclarationKind.None, LocalDeclarationKind.FunctionValue
925-
Return ImmutableArray(Of SyntaxReference).Empty
926-
927-
Case Else
928-
If _modifiedIdentifierOpt IsNot Nothing Then
929-
Return ImmutableArray.Create(Of SyntaxReference)(_modifiedIdentifierOpt.GetReference())
930-
Else
931-
Return ImmutableArray(Of SyntaxReference).Empty
932-
End If
933-
End Select
926+
If _modifiedIdentifierOpt IsNot Nothing Then
927+
Return ImmutableArray.Create(_modifiedIdentifierOpt.GetReference())
928+
Else
929+
Return MyBase.DeclaringSyntaxReferences
930+
End If
934931
End Get
935932
End Property
936933
End Class

src/Compilers/VisualBasic/Test/Semantic/DeclaringSyntaxNodeTests.vb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,40 @@ End Class
640640
CheckDeclaringSyntaxIsNoDeclaration(Of ForEachStatementSyntax)(comp, tree, "loc10")
641641
End Sub
642642

643+
<Fact, WorkItem("https://github.com/dotnet/roslyn/issues/6209")>
644+
Public Sub CatchVariableDeclaringSyntax()
645+
Dim comp = CompilationUtils.CreateCompilationWithMscorlib40(
646+
<compilation name="CatchVariableDeclaringSyntax">
647+
<file name="a.vb">
648+
Imports System
649+
650+
Class C1
651+
Sub m()
652+
Try
653+
Console.WriteLine()
654+
Catch exc As Exception
655+
Console.WriteLine(exc.ToString())
656+
End Try
657+
End Sub
658+
End Class
659+
</file>
660+
</compilation>)
661+
662+
Dim tree = comp.SyntaxTrees(0)
663+
Dim model = comp.GetSemanticModel(tree)
664+
Dim root = tree.GetRoot()
665+
Dim identifierName = root.DescendantNodes().OfType(Of IdentifierNameSyntax).First(Function(i) i.Identifier.ValueText = "exc")
666+
667+
Dim localSymbol As ISymbol = model.GetSymbolInfo(identifierName).Symbol
668+
Assert.NotNull(localSymbol)
669+
Assert.Equal(SymbolKind.Local, localSymbol.Kind)
670+
Assert.Equal("exc", localSymbol.Name)
671+
672+
Dim declaredSyntax = localSymbol.DeclaringSyntaxReferences.Single().GetSyntax()
673+
Assert.IsType(Of IdentifierNameSyntax)(declaredSyntax)
674+
Assert.Equal("exc", DirectCast(declaredSyntax, IdentifierNameSyntax).Identifier.ValueText)
675+
End Sub
676+
643677
<Fact>
644678
Public Sub LabelDeclaringSyntax()
645679
Dim comp = CompilationUtils.CreateCompilationWithMscorlib40(

0 commit comments

Comments
 (0)