Skip to content

Commit

Permalink
Fix for crash when annotation references synthtic constructor of a ge…
Browse files Browse the repository at this point in the history
…neric class alias.

FAILURE: mixi@:class<s=a(=m
[NoSuchMethodError: The getter 'enclosingElement' was called on null.
Receiver: null
Tried calling: enclosingElement, #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
flutter#1      LinkedResolutionReader.nextElement (package:analyzer/src/summary2/bundle_reader.dart:602:22)
flutter#2      ApplyResolutionVisitor._nextElement (package:analyzer/src/summary2/apply_resolution.dart:1106:24)
flutter#3      ApplyResolutionVisitor.visitAnnotation (package:analyzer/src/summary2/apply_resolution.dart:83:20)
flutter#4      AnnotationImpl.accept (package:analyzer/src/dart/ast/ast.dart:280:49)
#5      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
flutter#6      ApplyResolutionVisitor.visitClassTypeAlias (package:analyzer/src/summary2/apply_resolution.dart:167:20)
flutter#7      ClassTypeAliasImpl.accept (package:analyzer/src/dart/ast/ast.dart:1794:49)
flutter#8      LinkedContext.applyResolution (package:analyzer/src/summary2/bundle_reader.dart:512:11)
flutter#9      LinkedUnitContext.applyResolution (package:analyzer/src/summary2/linked_unit_context.dart:93:25)
flutter#10     ElementImpl.metadata (package:analyzer/src/dart/element/element.dart:2893:21)
flutter#11     ResolutionVisitor.visitClassTypeAlias (package:analyzer/src/dart/resolver/resolution_visitor.dart:256:51)
flutter#12     ClassTypeAliasImpl.accept (package:analyzer/src/dart/ast/ast.dart:1794:49)
flutter#13     NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
flutter#14     CompilationUnitImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:2141:21)
flutter#15     RecursiveAstVisitor.visitCompilationUnit (package:analyzer/dart/ast/visitor.dart:731:10)
flutter#16     CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2134:49)
flutter#17     LibraryAnalyzer._resolveFile (package:analyzer/src/dart/analysis/library_analyzer.dart:680:10)
flutter#18     LibraryAnalyzer.analyzeSync.<anonymous closure> (package:analyzer/src/dart/analysis/library_analyzer.dart:139:7)
flutter#19     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:387:8)
flutter#20     LibraryAnalyzer.analyzeSync (package:analyzer/src/dart/analysis/library_analyzer.dart:138:11)
#21     LibraryAnalyzer.analyze (package:analyzer/src/dart/analysis/library_analyzer.dart:107:12)
#22     AnalysisDriver._computeResolvedLibrary2.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1414:65)
#23     PerformanceLog.run (package:analyzer/src/dart/analysis/performance_logger.dart:32:15)
#24     AnalysisDriver._computeResolvedLibrary2 (package:analyzer/src/dart/analysis/driver.dart:1400:20)
#25     AnalysisDriver._computeResolvedLibrary.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1393:15)


Change-Id: I6f04a325b30d9dc61e7eb1a4bba91ed8b9bcd1d3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176060
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and [email protected] committed Dec 14, 2020
1 parent 9dad187 commit d2e5d47
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -956,9 +956,13 @@ class ClassElementImpl extends AbstractClassElementImpl
// substituting type parameters as appropriate.
return constructorsToForward
.map((ConstructorElement superclassConstructor) {
ConstructorElementImpl implicitConstructor =
ConstructorElementImpl(superclassConstructor.name, -1);
var containerRef = reference.getChild('@constructor');
var name = superclassConstructor.name;
var implicitConstructor = ConstructorElementImpl.forLinkedNode(
this, containerRef.getChild(name), null);
implicitConstructor.isSynthetic = true;
implicitConstructor.name = name;
implicitConstructor.nameOffset = -1;
implicitConstructor.redirectedConstructor = superclassConstructor;
var hasMixinWithInstanceVariables = mixins.any(typeHasInstanceVariables);
implicitConstructor.isConst =
Expand Down
100 changes: 100 additions & 0 deletions pkg/analyzer/test/src/summary/resynthesize_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9547,6 +9547,62 @@ class C {
''');
}

test_metadata_constructor_call_named_synthetic_ofClassAlias_generic() async {
var library = await checkLibrary('''
class A {
const A.named();
}
mixin B {}
class C<T> = A with B;
@C.named()
class D {}
''');
checkElementText(
library,
r'''
class A {
const A.named();
}
class alias C extends A with B {
synthetic const C.named() = A.named;
}
typeParameters
T
bound: null
defaultType: dynamic
class D {
}
metadata
Annotation
arguments: ArgumentList
element: ConstructorMember
base: self::@class::C::@constructor::named
substitution: {T: dynamic}
name: PrefixedIdentifier
identifier: SimpleIdentifier
staticElement: ConstructorMember
base: self::@class::C::@constructor::named
substitution: {T: dynamic}
staticType: null
token: named
period: .
prefix: SimpleIdentifier
staticElement: self::@class::C
staticType: null
token: C
staticElement: ConstructorMember
base: self::@class::C::@constructor::named
substitution: {T: dynamic}
staticType: null
mixin B on Object {
}
''',
withFullyResolvedAst: true);
}

test_metadata_constructor_call_unnamed() async {
var library = await checkLibrary('class A { const A(); } @A() class C {}');
checkElementText(library, r'''
Expand Down Expand Up @@ -9574,6 +9630,50 @@ class C {
''');
}

test_metadata_constructor_call_unnamed_synthetic_ofClassAlias_generic() async {
var library = await checkLibrary('''
class A {
const A();
}
mixin B {}
class C<T> = A with B;
@C()
class D {}
''');
checkElementText(
library,
r'''
class A {
const A();
}
class alias C extends A with B {
synthetic const C() = A;
}
typeParameters
T
bound: null
defaultType: dynamic
class D {
}
metadata
Annotation
arguments: ArgumentList
element: ConstructorMember
base: self::@class::C::@constructor::•
substitution: {T: dynamic}
name: SimpleIdentifier
staticElement: self::@class::C
staticType: null
token: C
mixin B on Object {
}
''',
withFullyResolvedAst: true);
}

test_metadata_constructor_call_with_args() async {
var library =
await checkLibrary('class A { const A(x); } @A(null) class C {}');
Expand Down

0 comments on commit d2e5d47

Please sign in to comment.