Fix for annotations on library element after import.

This is not valid, but we should not crash.

Change-Id: I7fb0e5a705072af1f91c3674fe3e0b40e6a7a34a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/205620
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2021-06-30 21:12:44 +00:00 committed by commit-bot@chromium.org
parent b0310477d9
commit 911fe966dc
2 changed files with 22 additions and 8 deletions

View file

@ -81,14 +81,17 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
_libraryElement.imports = _imports;
_libraryElement.hasExtUri = _hasExtUri;
var firstDirective = unit.directives.firstOrNull;
if (firstDirective != null) {
_libraryElement.documentationComment = getCommentNodeRawText(
firstDirective.documentationComment,
);
var firstDirectiveMetadata = firstDirective.element?.metadata;
if (firstDirectiveMetadata != null) {
_libraryElement.metadata = firstDirectiveMetadata;
if (_isFirstLibraryDirective) {
_isFirstLibraryDirective = false;
var firstDirective = unit.directives.firstOrNull;
if (firstDirective != null) {
_libraryElement.documentationComment = getCommentNodeRawText(
firstDirective.documentationComment,
);
var firstDirectiveMetadata = firstDirective.element?.metadata;
if (firstDirectiveMetadata != null) {
_libraryElement.metadata = firstDirectiveMetadata;
}
}
}
}
@ -626,6 +629,9 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
if (_isFirstLibraryDirective) {
_isFirstLibraryDirective = false;
node.element = _libraryElement;
_libraryElement.documentationComment = getCommentNodeRawText(
node.documentationComment,
);
_libraryElement.metadata = _buildAnnotations(node.metadata);
}
}

View file

@ -368,6 +368,14 @@ class C {
''');
}
test_libraryAfterImport() async {
await _assertCanBeAnalyzed(r'''
import 'dart:async';
@foo
library my;
''');
}
test_localFunction_defaultFieldFormalParameter_metadata() async {
await _assertCanBeAnalyzed(r'''
const my = 0;