Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 92704cf

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Don't set types for class/constructor/getter identifiers of annotations.
They are not expressions, and so don't have types. Actually setting a type for a constructor of a generic class breaks summary serialization because `T Function()` type of the constructor `class A<T> { const A.named()l }` is not valid - `T` is not in scope. So, this crashed the linker. Change-Id: I25a6e5eb6902faa479e4dcb683ef3936af07b5b9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168420 Reviewed-by: Samuel Rawlins <srawlins@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 891a8a1 commit 92704cf

File tree

5 files changed

+140
-43
lines changed

5 files changed

+140
-43
lines changed

pkg/analyzer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Deprecated `GenericTypeAliasElement`. Use `FunctionTypeAliasElement`.
33
* Read imports, exports, and parts on demand in `AnalysisDriver`.
44
Specifically, `parseFileSync` will not read any referenced files.
5+
* Types are not set anymore for classes/constructors/getters of
6+
identifiers in metadata (still set in arguments).
57

68
## 0.40.4
79
* Deprecated `IndexExpression.auxiliaryElements` and

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class AnnotationResolver {
7676
element = _resolver.toLegacyElement(element);
7777

7878
identifier.staticElement = element;
79-
identifier.staticType = element?.type ?? DynamicTypeImpl.instance;
8079
// TODO(scheglov) error?
8180
} else if (prefixElement is PrefixElement) {
8281
var resolver = PropertyElementResolver(_resolver);
@@ -108,14 +107,6 @@ class AnnotationResolver {
108107

109108
var element = result.readElement;
110109
identifier.staticElement = element;
111-
112-
DartType type;
113-
if (element is PropertyAccessorElement && element.isGetter) {
114-
type = element.returnType;
115-
} else {
116-
type = DynamicTypeImpl.instance;
117-
}
118-
identifier.staticType = type;
119110
}
120111
} else {
121112
var identifier = nodeName as SimpleIdentifier;
@@ -137,16 +128,6 @@ class AnnotationResolver {
137128
[identifier.name],
138129
);
139130
}
140-
141-
DartType type;
142-
if (element is ClassElement) {
143-
type = _resolver.typeProvider.typeType;
144-
} else if (element is PropertyAccessorElement && element.isGetter) {
145-
type = element.returnType;
146-
} else {
147-
type = DynamicTypeImpl.instance;
148-
}
149-
identifier.staticType = type;
150131
}
151132

152133
_resolveAnnotationElement(node);

pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void topLevelFunction() {}
151151

152152
SimpleIdentifier identifier_1 = annotation.name;
153153
expect(identifier_1.staticElement, same(myElement.getter));
154-
expect(identifier_1.staticType, typeProvider.intType);
154+
expect(identifier_1.staticType, isNull);
155155
}
156156

157157
{
@@ -202,7 +202,7 @@ const a = 1;
202202

203203
SimpleIdentifier aRef = annotation.name;
204204
assertElement(aRef, findElement.topGet('a'));
205-
assertType(aRef, 'int');
205+
assertTypeNull(aRef);
206206
}
207207

208208
test_annotation_onDirective_import() async {
@@ -222,7 +222,7 @@ const a = 1;
222222

223223
SimpleIdentifier aRef = annotation.name;
224224
assertElement(aRef, findElement.topGet('a'));
225-
assertType(aRef, 'int');
225+
assertTypeNull(aRef);
226226
}
227227

228228
test_annotation_onDirective_library() async {
@@ -242,7 +242,7 @@ const a = 1;
242242

243243
SimpleIdentifier aRef = annotation.name;
244244
assertElement(aRef, findElement.topGet('a'));
245-
assertType(aRef, 'int');
245+
assertTypeNull(aRef);
246246
}
247247

248248
test_annotation_onDirective_part() async {
@@ -265,7 +265,7 @@ const a = 1;
265265

266266
SimpleIdentifier aRef = annotation.name;
267267
assertElement(aRef, findElement.topGet('a'));
268-
assertType(aRef, 'int');
268+
assertTypeNull(aRef);
269269
}
270270

271271
test_annotation_onDirective_partOf() async {
@@ -288,7 +288,7 @@ const a = 1;
288288

289289
SimpleIdentifier aRef = annotation.name;
290290
assertElement(aRef, findElement.topGet('a'));
291-
assertType(aRef, 'int');
291+
assertTypeNull(aRef);
292292
}
293293

294294
test_annotation_onFormalParameter_redirectingFactory() async {
@@ -310,7 +310,7 @@ const c = 2;
310310

311311
SimpleIdentifier ref = annotation.name;
312312
assertElement(ref, getter);
313-
assertType(ref, 'int');
313+
assertTypeNull(ref);
314314
}
315315

316316
{
@@ -386,7 +386,7 @@ class C {
386386

387387
SimpleIdentifier identifier_1 = annotation.name;
388388
expect(identifier_1.staticElement, same(myElement.getter));
389-
expect(identifier_1.staticType, typeProvider.intType);
389+
assertTypeNull(identifier_1);
390390
}
391391

392392
test_annotation_prefixed_classField() async {
@@ -570,7 +570,7 @@ class A {
570570
assertTypeNull(prefixed.prefix);
571571

572572
expect(prefixed.identifier.staticElement, same(aGetter));
573-
expect(prefixed.identifier.staticType, typeProvider.intType);
573+
assertTypeNull(prefixed.identifier);
574574

575575
expect(annotation.constructorName, isNull);
576576
expect(annotation.arguments, isNull);
@@ -630,7 +630,7 @@ class A {
630630
assertTypeNull(prefixed.prefix);
631631

632632
expect(prefixed.identifier.staticElement, same(constructor));
633-
assertType(prefixed.identifier, 'A Function(int, {int b})');
633+
assertTypeNull(prefixed.identifier);
634634

635635
expect(annotation.constructorName, isNull);
636636

@@ -699,14 +699,14 @@ void main() {
699699

700700
SimpleIdentifier identifier_1 = annotation_1.name;
701701
expect(identifier_1.staticElement, same(element_1.getter));
702-
expect(identifier_1.staticType, typeProvider.intType);
702+
assertTypeNull(identifier_1);
703703

704704
Annotation annotation_2 = main.metadata[1];
705705
expect(annotation_2.element, same(element_2.getter));
706706

707707
SimpleIdentifier identifier_2 = annotation_2.name;
708708
expect(identifier_2.staticElement, same(element_2.getter));
709-
expect(identifier_2.staticType, typeProvider.intType);
709+
assertTypeNull(identifier_2);
710710
}
711711

712712
test_asExpression() async {

pkg/analyzer/test/src/dart/resolution/metadata_test.dart

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,120 @@ main() {
2222

2323
@reflectiveTest
2424
class MetadataResolutionTest extends PubPackageResolutionTest {
25+
test_genericClass_instanceGetter() async {
26+
await resolveTestCode(r'''
27+
class A<T> {
28+
T get foo {}
29+
}
30+
31+
@A.foo
32+
void f() {}
33+
''');
34+
35+
_assertResolvedNodeText(findNode.annotation('@A'), r'''
36+
Annotation
37+
element: self::@class::A::@getter::foo
38+
name: PrefixedIdentifier
39+
identifier: SimpleIdentifier
40+
staticElement: self::@class::A::@getter::foo
41+
staticType: null
42+
token: foo
43+
period: .
44+
prefix: SimpleIdentifier
45+
staticElement: self::@class::A
46+
staticType: null
47+
token: A
48+
staticElement: self::@class::A::@getter::foo
49+
staticType: null
50+
''');
51+
}
52+
53+
test_genericClass_namedConstructor() async {
54+
await assertNoErrorsInCode(r'''
55+
class A<T> {
56+
const A.named();
57+
}
58+
59+
@A.named()
60+
void f() {}
61+
''');
62+
63+
_assertResolvedNodeText(findNode.annotation('@A'), r'''
64+
Annotation
65+
arguments: ArgumentList
66+
element: ConstructorMember
67+
base: self::@class::A::@constructor::named
68+
substitution: {T: dynamic}
69+
name: PrefixedIdentifier
70+
identifier: SimpleIdentifier
71+
staticElement: ConstructorMember
72+
base: self::@class::A::@constructor::named
73+
substitution: {T: dynamic}
74+
staticType: null
75+
token: named
76+
period: .
77+
prefix: SimpleIdentifier
78+
staticElement: self::@class::A
79+
staticType: null
80+
token: A
81+
staticElement: ConstructorMember
82+
base: self::@class::A::@constructor::named
83+
substitution: {T: dynamic}
84+
staticType: null
85+
''');
86+
}
87+
88+
test_genericClass_staticGetter() async {
89+
await resolveTestCode(r'''
90+
class A<T> {
91+
static T get foo {}
92+
}
93+
94+
@A.foo
95+
void f() {}
96+
''');
97+
98+
_assertResolvedNodeText(findNode.annotation('@A'), r'''
99+
Annotation
100+
element: self::@class::A::@getter::foo
101+
name: PrefixedIdentifier
102+
identifier: SimpleIdentifier
103+
staticElement: self::@class::A::@getter::foo
104+
staticType: null
105+
token: foo
106+
period: .
107+
prefix: SimpleIdentifier
108+
staticElement: self::@class::A
109+
staticType: null
110+
token: A
111+
staticElement: self::@class::A::@getter::foo
112+
staticType: null
113+
''');
114+
}
115+
116+
test_genericClass_unnamedConstructor() async {
117+
await assertNoErrorsInCode(r'''
118+
class A<T> {
119+
const A();
120+
}
121+
122+
@A()
123+
void f() {}
124+
''');
125+
126+
_assertResolvedNodeText(findNode.annotation('@A'), r'''
127+
Annotation
128+
arguments: ArgumentList
129+
element: ConstructorMember
130+
base: self::@class::A::@constructor::•
131+
substitution: {T: dynamic}
132+
name: SimpleIdentifier
133+
staticElement: self::@class::A
134+
staticType: null
135+
token: A
136+
''');
137+
}
138+
25139
test_onFieldFormal() async {
26140
await assertNoErrorsInCode(r'''
27141
class A {
@@ -55,7 +169,7 @@ Annotation
55169
element: self::@class::A::@constructor::•
56170
name: SimpleIdentifier
57171
staticElement: self::@class::A
58-
staticType: Type
172+
staticType: null
59173
token: A
60174
''');
61175
}

pkg/analyzer/test/src/summary/resynthesize_common.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8884,15 +8884,15 @@ class C {
88848884
element: self::@class::C::@getter::foo
88858885
name: SimpleIdentifier
88868886
staticElement: self::@class::C::@getter::foo
8887-
staticType: int
8887+
staticType: null
88888888
token: foo
88898889
}
88908890
metadata
88918891
Annotation
88928892
element: self::@getter::foo
88938893
name: SimpleIdentifier
88948894
staticElement: self::@getter::foo
8895-
staticType: int
8895+
staticType: null
88968896
token: foo
88978897
typeParameters
88988898
T
@@ -8903,7 +8903,7 @@ class C {
89038903
element: self::@getter::foo
89048904
name: SimpleIdentifier
89058905
staticElement: self::@getter::foo
8906-
staticType: int
8906+
staticType: null
89078907
token: foo
89088908
const int foo;
89098909
constantInitializer
@@ -9121,15 +9121,15 @@ extension E on int {
91219121
element: self::@extension::E::@getter::foo
91229122
name: SimpleIdentifier
91239123
staticElement: self::@extension::E::@getter::foo
9124-
staticType: int
9124+
staticType: null
91259125
token: foo
91269126
}
91279127
metadata
91289128
Annotation
91299129
element: self::@getter::foo
91309130
name: SimpleIdentifier
91319131
staticElement: self::@getter::foo
9132-
staticType: int
9132+
staticType: null
91339133
token: foo
91349134
typeParameters
91359135
T
@@ -9140,7 +9140,7 @@ extension E on int {
91409140
element: self::@getter::foo
91419141
name: SimpleIdentifier
91429142
staticElement: self::@getter::foo
9143-
staticType: int
9143+
staticType: null
91449144
token: foo
91459145
const int foo;
91469146
constantInitializer
@@ -9430,15 +9430,15 @@ mixin M on Object {
94309430
element: self::@mixin::M::@getter::foo
94319431
name: SimpleIdentifier
94329432
staticElement: self::@mixin::M::@getter::foo
9433-
staticType: int
9433+
staticType: null
94349434
token: foo
94359435
}
94369436
metadata
94379437
Annotation
94389438
element: self::@getter::foo
94399439
name: SimpleIdentifier
94409440
staticElement: self::@getter::foo
9441-
staticType: int
9441+
staticType: null
94429442
token: foo
94439443
typeParameters
94449444
T
@@ -9449,7 +9449,7 @@ mixin M on Object {
94499449
element: self::@getter::foo
94509450
name: SimpleIdentifier
94519451
staticElement: self::@getter::foo
9452-
staticType: int
9452+
staticType: null
94539453
token: foo
94549454
const int foo;
94559455
constantInitializer
@@ -11789,7 +11789,7 @@ class C {
1178911789
element: <null>
1179011790
name: SimpleIdentifier
1179111791
staticElement: <null>
11792-
staticType: dynamic
11792+
staticType: null
1179311793
token: foo
1179411794
''',
1179511795
withFullyResolvedAst: true);
@@ -11817,7 +11817,7 @@ class C {
1181711817
element: <null>
1181811818
name: SimpleIdentifier
1181911819
staticElement: <null>
11820-
staticType: dynamic
11820+
staticType: null
1182111821
token: v
1182211822
''',
1182311823
withFullyResolvedAst: true);

0 commit comments

Comments
 (0)