-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix DeclaringSyntaxReferences returning empty for VB catch variables #81151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: CyrusNajmabadi <[email protected]>
| Return ImmutableArray.Create(Of SyntaxReference)(_modifiedIdentifierOpt.GetReference()) | ||
| Else | ||
| Return ImmutableArray(Of SyntaxReference).Empty | ||
| ' For catch variables (and potentially other cases), there is no ModifiedIdentifierSyntax. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot determine what other cases there may be that hit this. if there are none, assert that this is a catch variable. If tehre are others, enumerate them here and add tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Investigated all usages of VariableLocalSymbol and confirmed that only LocalDeclarationKind.Catch passes Nothing for modifiedIdentifierOpt. Added Debug.Assert(DeclarationKind = LocalDeclarationKind.Catch) in commit 8d731e6.
Analysis:
Catch: PassesNothinginCatchBlockBinder.vb:55Variable,Constant,Static,Using: Always provideModifiedIdentifierSyntaxFor,ForEach: Always provideModifiedIdentifierSyntaxImplicitVariable,FunctionValue: UseSourceLocalSymbol(different type), notVariableLocalSymbol
All tests pass with the assertion.
Co-authored-by: CyrusNajmabadi <[email protected]>
src/Compilers/VisualBasic/Test/Semantic/DeclaringSyntaxNodeTests.vb
Outdated
Show resolved
Hide resolved
| Dim token = tree.GetCompilationUnitRoot().FindToken(position) | ||
| Dim identifierName = token.Parent | ||
|
|
||
| ' Get the symbol for the catch variable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot no need to add test comments that just restate what the test is already doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed redundant comments in commit 8566ad4.
|
|
||
| ' Check that DeclaringSyntaxReferences returns a non-empty list | ||
| Dim declaringRefs = localSymbol.DeclaringSyntaxReferences | ||
| Assert.Equal(1, declaringRefs.Length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot remove unneeded asserts. Instead, use declaringRefs.Single(). That way it will crash if we don't have exactly one item. Similarly, if we have an IsType check, we don't need a NotNull check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplified assertions in commit 6e0ec0c:
- Used
.Single()instead of checking length and indexing - Removed redundant
Assert.NotNull(declaredSyntax)sinceAssert.IsTypealready implies not null
Co-authored-by: CyrusNajmabadi <[email protected]>
Co-authored-by: CyrusNajmabadi <[email protected]>
src/Compilers/VisualBasic/Test/Semantic/DeclaringSyntaxNodeTests.vb
Outdated
Show resolved
Hide resolved
|
@dotnet/roslyn-compiler this is ready for review. |
| Return ImmutableArray(Of SyntaxReference).Empty | ||
|
|
||
| Case Else | ||
| If _modifiedIdentifierOpt IsNot Nothing Then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the first case is unreachable and the entire method implementation could be:
If _modifiedIdentifierOpt IsNot Nothing Then
Return ImmutableArray.Create(Of SyntaxReference)(_modifiedIdentifierOpt.GetReference())
Else
Return MyBase.DeclaringSyntaxReferences
End If
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Dim code As String = tree.GetText().ToString() | ||
| Dim position As Integer = code.IndexOf("exc", StringComparison.Ordinal) | ||
| Dim token = tree.GetCompilationUnitRoot().FindToken(position) | ||
| Dim identifierName = token.Parent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Done with review pass (commit 7) |
…r-catch-variable-symbol
Plan: Fix DeclaringSyntaxReferences for Catch Variables ✅
Summary
Fixed an issue where
DeclaringSyntaxReferencesreturned an empty list for catch statement variables in Visual Basic.Solution
Modified the
VariableLocalSymbol.DeclaringSyntaxReferencesproperty inLocalSymbol.vbto fall back to using_identifierToken.Parent.GetReference()when_modifiedIdentifierOptisNothing.Code Review Response
VariableLocalSymbolcreation and confirmed that onlyLocalDeclarationKind.CatchpassesNothingasmodifiedIdentifierOpt. AddedDebug.Assert(DeclarationKind = LocalDeclarationKind.Catch)to enforce this invariant..Single()instead of checking length and indexing, and removed redundantNotNullcheck sinceIsTypealready implies not null.Analysis of all LocalDeclarationKind cases:
Catch: UsesNothingfor modifiedIdentifierOpt ✓Variable,Constant,Static: Always provide ModifiedIdentifierSyntaxUsing: Always provides ModifiedIdentifierSyntaxFor,ForEach: Always provide ModifiedIdentifierSyntaxImplicitVariable,FunctionValue: Use different LocalSymbol types (SourceLocalSymbol), not VariableLocalSymbolTesting Results ✅
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.