@@ -14,6 +14,7 @@ import 'package:analyzer/src/dart/constant/utilities.dart';
14
14
import 'package:analyzer/src/dart/element/element.dart' ;
15
15
import 'package:analyzer/src/dart/element/member.dart' ;
16
16
import 'package:analyzer/src/dart/element/type.dart' ;
17
+ import 'package:analyzer/src/dart/resolver/invocation_inference_helper.dart' ;
17
18
import 'package:analyzer/src/error/codes.dart' ;
18
19
import 'package:analyzer/src/generated/resolver.dart' ;
19
20
@@ -46,6 +47,38 @@ class AnnotationResolver {
46
47
}
47
48
}
48
49
50
+ void _classConstructorInvocation (
51
+ AnnotationImpl node,
52
+ ClassElement classElement,
53
+ SimpleIdentifierImpl ? constructorName,
54
+ ArgumentList argumentList,
55
+ List <Map <DartType , NonPromotionReason > Function ()> whyNotPromotedList,
56
+ ) {
57
+ ConstructorElement ? constructorElement;
58
+ if (constructorName != null ) {
59
+ constructorElement = classElement.getNamedConstructor (
60
+ constructorName.name,
61
+ );
62
+ } else {
63
+ constructorElement = classElement.unnamedConstructor;
64
+ }
65
+
66
+ _constructorInvocation (
67
+ node,
68
+ constructorName,
69
+ classElement.typeParameters,
70
+ constructorElement,
71
+ argumentList,
72
+ (typeArguments) {
73
+ return classElement.instantiate (
74
+ typeArguments: typeArguments,
75
+ nullabilitySuffix: _resolver.noneOrStarSuffix,
76
+ );
77
+ },
78
+ whyNotPromotedList,
79
+ );
80
+ }
81
+
49
82
void _classGetter (
50
83
AnnotationImpl node,
51
84
ClassElement classElement,
@@ -80,20 +113,13 @@ class AnnotationResolver {
80
113
81
114
void _constructorInvocation (
82
115
AnnotationImpl node,
83
- ClassElement classElement,
84
116
SimpleIdentifierImpl ? constructorName,
117
+ List <TypeParameterElement > typeParameters,
118
+ ConstructorElement ? constructorElement,
85
119
ArgumentList argumentList,
120
+ InterfaceType Function (List <DartType > typeArguments) instantiateElement,
86
121
List <Map <DartType , NonPromotionReason > Function ()> whyNotPromotedList,
87
122
) {
88
- ConstructorElement ? constructorElement;
89
- if (constructorName != null ) {
90
- constructorElement = classElement.getNamedConstructor (
91
- constructorName.name,
92
- );
93
- } else {
94
- constructorElement = classElement.unnamedConstructor;
95
- }
96
-
97
123
constructorElement = _resolver.toLegacyElement (constructorElement);
98
124
constructorName? .staticElement = constructorElement;
99
125
node.element = constructorElement;
@@ -108,8 +134,6 @@ class AnnotationResolver {
108
134
return ;
109
135
}
110
136
111
- var typeParameters = classElement.typeParameters;
112
-
113
137
// If no type parameters, the elements are correct.
114
138
if (typeParameters.isEmpty) {
115
139
_resolveConstructorInvocationArguments (node);
@@ -123,10 +147,7 @@ class AnnotationResolver {
123
147
List <DartType > typeArguments,
124
148
ConstructorElement constructorElement,
125
149
) {
126
- var type = classElement.instantiate (
127
- typeArguments: typeArguments,
128
- nullabilitySuffix: _resolver.noneOrStarSuffix,
129
- );
150
+ var type = instantiateElement (typeArguments);
130
151
constructorElement = ConstructorMember .from (constructorElement, type);
131
152
constructorName? .staticElement = constructorElement;
132
153
node.element = constructorElement;
@@ -166,8 +187,11 @@ class AnnotationResolver {
166
187
_resolver.visitArgumentList (argumentList,
167
188
whyNotPromotedList: whyNotPromotedList);
168
189
169
- var constructorRawType = _resolver.typeAnalyzer
170
- .constructorToGenericFunctionType (constructorElement);
190
+ var elementToInfer = ConstructorElementToInfer (
191
+ typeParameters,
192
+ constructorElement,
193
+ );
194
+ var constructorRawType = elementToInfer.asType;
171
195
172
196
var inferred = _resolver.inferenceHelper.inferGenericInvoke (
173
197
node, constructorRawType, typeArgumentList, argumentList, node,
@@ -264,7 +288,7 @@ class AnnotationResolver {
264
288
// Class(args) or Class.CONST
265
289
if (element1 is ClassElement ) {
266
290
if (argumentList != null ) {
267
- _constructorInvocation (
291
+ _classConstructorInvocation (
268
292
node, element1, name2, argumentList, whyNotPromotedList);
269
293
} else {
270
294
_classGetter (node, element1, name2, whyNotPromotedList);
@@ -286,7 +310,7 @@ class AnnotationResolver {
286
310
// prefix.Class(args) or prefix.Class.CONST
287
311
if (element2 is ClassElement ) {
288
312
if (argumentList != null ) {
289
- _constructorInvocation (
313
+ _classConstructorInvocation (
290
314
node, element2, name3, argumentList, whyNotPromotedList);
291
315
} else {
292
316
_classGetter (node, element2, name3, whyNotPromotedList);
@@ -303,6 +327,19 @@ class AnnotationResolver {
303
327
_propertyAccessorElement (node, name2, element2, whyNotPromotedList);
304
328
return ;
305
329
}
330
+
331
+ // prefix.TypeAlias(args) or prefix.TypeAlias.CONST
332
+ if (element2 is TypeAliasElement ) {
333
+ var aliasedType = element2.aliasedType;
334
+ var argumentList = node.arguments;
335
+ if (aliasedType is InterfaceType && argumentList != null ) {
336
+ _typeAliasConstructorInvocation (node, element2, name3, aliasedType,
337
+ argumentList, whyNotPromotedList);
338
+ } else {
339
+ _typeAliasGetter (node, element2, name3, whyNotPromotedList);
340
+ }
341
+ return ;
342
+ }
306
343
// undefined
307
344
if (element2 == null ) {
308
345
_errorReporter.reportErrorForNode (
@@ -322,6 +359,19 @@ class AnnotationResolver {
322
359
return ;
323
360
}
324
361
362
+ // TypeAlias(args) or TypeAlias.CONST
363
+ if (element1 is TypeAliasElement ) {
364
+ var aliasedType = element1.aliasedType;
365
+ var argumentList = node.arguments;
366
+ if (aliasedType is InterfaceType && argumentList != null ) {
367
+ _typeAliasConstructorInvocation (node, element1, name2, aliasedType,
368
+ argumentList, whyNotPromotedList);
369
+ } else {
370
+ _typeAliasGetter (node, element1, name2, whyNotPromotedList);
371
+ }
372
+ return ;
373
+ }
374
+
325
375
// TODO(scheglov) Must be const.
326
376
if (element1 is VariableElement ) {
327
377
return ;
@@ -393,6 +443,67 @@ class AnnotationResolver {
393
443
}
394
444
}
395
445
446
+ void _typeAliasConstructorInvocation (
447
+ AnnotationImpl node,
448
+ TypeAliasElement typeAliasElement,
449
+ SimpleIdentifierImpl ? constructorName,
450
+ InterfaceType aliasedType,
451
+ ArgumentList argumentList,
452
+ List <Map <DartType , NonPromotionReason > Function ()> whyNotPromotedList,
453
+ ) {
454
+ var constructorElement = aliasedType.lookUpConstructor (
455
+ constructorName? .name,
456
+ _definingLibrary,
457
+ );
458
+
459
+ _constructorInvocation (
460
+ node,
461
+ constructorName,
462
+ typeAliasElement.typeParameters,
463
+ constructorElement,
464
+ argumentList,
465
+ (typeArguments) {
466
+ return typeAliasElement.instantiate (
467
+ typeArguments: typeArguments,
468
+ nullabilitySuffix: _resolver.noneOrStarSuffix,
469
+ ) as InterfaceType ;
470
+ },
471
+ whyNotPromotedList,
472
+ );
473
+ }
474
+
475
+ void _typeAliasGetter (
476
+ AnnotationImpl node,
477
+ TypeAliasElement typeAliasElement,
478
+ SimpleIdentifierImpl ? getterName,
479
+ List <Map <DartType , NonPromotionReason > Function ()> whyNotPromotedList,
480
+ ) {
481
+ ExecutableElement ? getter;
482
+ var aliasedType = typeAliasElement.aliasedType;
483
+ if (aliasedType is InterfaceType ) {
484
+ var classElement = aliasedType.element;
485
+ if (getterName != null ) {
486
+ getter = classElement.getGetter (getterName.name);
487
+ getter = _resolver.toLegacyElement (getter);
488
+ }
489
+ }
490
+
491
+ getterName? .staticElement = getter;
492
+ node.element = getter;
493
+
494
+ if (getterName != null && getter is PropertyAccessorElement ) {
495
+ _propertyAccessorElement (node, getterName, getter, whyNotPromotedList);
496
+ _resolveAnnotationElementGetter (node, getter);
497
+ } else if (getter is ! ConstructorElement ) {
498
+ _errorReporter.reportErrorForNode (
499
+ CompileTimeErrorCode .INVALID_ANNOTATION ,
500
+ node,
501
+ );
502
+ }
503
+
504
+ _visitArguments (node, whyNotPromotedList);
505
+ }
506
+
396
507
void _visitArguments (AnnotationImpl node,
397
508
List <Map <DartType , NonPromotionReason > Function ()> whyNotPromotedList) {
398
509
var arguments = node.arguments;
0 commit comments