Skip to content

Commit aa0cf44

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 38506. Don't attempt to use LibraryElement.metadata for not the first LibraryDirective.
[email protected] Bug: #38506 Change-Id: Ib76b1e81554f061622ec98fae21b4dd9a1280c24 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125521 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 6df2491 commit aa0cf44

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

pkg/analyzer/lib/src/dart/resolver/resolution_visitor.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
6161
final AstRewriter _astRewriter;
6262
final TypeNameResolver _typeNameResolver;
6363

64+
/// This index is incremented every time we visit a [LibraryDirective].
65+
/// There is just one [LibraryElement], so we can support only one node.
66+
int _libraryDirectiveIndex = 0;
67+
6468
/// The provider of pre-built children elements from the element being
6569
/// visited. For example when we visit a method, its element is resynthesized
6670
/// from the summary, and we get resynthesized elements for type parameters
@@ -735,8 +739,13 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
735739
@override
736740
void visitLibraryDirective(LibraryDirective node) {
737741
super.visitLibraryDirective(node);
738-
if (node.element != null) {
742+
++_libraryDirectiveIndex;
743+
if (node.element != null && _libraryDirectiveIndex == 1) {
739744
_setElementAnnotations(node.metadata, node.element.metadata);
745+
} else {
746+
for (var annotation in node.metadata) {
747+
annotation.elementAnnotation = ElementAnnotationImpl(_unitElement);
748+
}
740749
}
741750
}
742751

pkg/analyzer/test/generated/invalid_code_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ class B {}
202202
await _assertCanBeAnalyzed(r'c(=k(<)>');
203203
}
204204

205+
test_fuzz_38506() async {
206+
// https://github.com/dart-lang/sdk/issues/38506
207+
// We have only one LibraryElement to get resolved annotations.
208+
// Leave annotations node of other LibraryDirective(s) unresolved.
209+
await _assertCanBeAnalyzed(r'''
210+
library c;
211+
@foo
212+
library c;
213+
''');
214+
}
215+
205216
test_genericFunction_asTypeArgument_ofUnresolvedClass() async {
206217
await _assertCanBeAnalyzed(r'''
207218
C<int Function()> c;

0 commit comments

Comments
 (0)