Skip to content

Commit

Permalink
[analyzer] Resolve annotations on local variable elements
Browse files Browse the repository at this point in the history
Metadata is attached to a `VariableDeclarationList`, not the individual
declarations therein.
Even though annotations from the list are copied down into the local
variable elements, the resolver did not set the `element` property on
those annotations which is required for computing the constant value in
a later step.
This is fixed by making the resolver visit the declaration list instead
of individual declarations.

Closes dart-lang#47502

Change-Id: Id6e47e6fa878e61be5279fa4127262deb73ab1c1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217363
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
simolus3 authored and commit-bot@chromium.org committed Oct 20, 2021
1 parent 6dd4ff6 commit 02b8407
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/generated/element_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ class ElementResolver extends SimpleAstVisitor<void> {
}

@override
void visitVariableDeclaration(VariableDeclaration node) {
void visitVariableDeclarationList(VariableDeclarationList node) {
_resolveAnnotations(node.metadata);
}

Expand Down
40 changes: 40 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/metadata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,46 @@ A
''');
}

test_onLocalVariable() async {
await assertNoErrorsInCode(r'''
class A {
final int a;
const A(this.a);
}
void f() {
@A(3)
int? x;
print(x);
}
''');

var annotation = findNode.annotation('@A');
_assertResolvedNodeText(annotation, '''
Annotation
arguments: ArgumentList
arguments
IntegerLiteral
literal: 3
staticType: int
leftParenthesis: (
rightParenthesis: )
atSign: @
element: self::@class::A::@constructor::•
name: SimpleIdentifier
staticElement: self::@class::A
staticType: null
token: A
''');

final localVariable = findElement.localVar('x');
final annotationOnElement = localVariable.metadata.single;
_assertElementAnnotationValueText(annotationOnElement, '''
A
a: int 3
''');
}

test_optIn_fromOptOut_class() async {
newFile('$testPackageLibPath/a.dart', content: r'''
class A {
Expand Down

0 comments on commit 02b8407

Please sign in to comment.