Skip to content

Commit 7c8c061

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate TypeArgumentsVerifier.
Change-Id: I520578b93bd577faaa4b409412de4dc8e1d1ccaa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395046 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent aa3eb29 commit 7c8c061

File tree

4 files changed

+98
-37
lines changed

4 files changed

+98
-37
lines changed

pkg/analyzer/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ lib/src/error/must_call_super_verifier.dart
117117
lib/src/error/nullable_dereference_verifier.dart
118118
lib/src/error/override_verifier.dart
119119
lib/src/error/required_parameters_verifier.dart
120-
lib/src/error/type_arguments_verifier.dart
121120
lib/src/error/unused_local_elements_verifier.dart
122121
lib/src/error/use_result_verifier.dart
123122
lib/src/generated/element_resolver.dart

pkg/analyzer/lib/src/dart/element/type_algebra.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/dart/element/element2.dart';
67
import 'package:analyzer/dart/element/nullability_suffix.dart';
78
import 'package:analyzer/dart/element/type.dart';
89
import 'package:analyzer/dart/element/type_visitor.dart';
@@ -15,6 +16,7 @@ import 'package:analyzer/src/summary2/function_type_builder.dart';
1516
import 'package:analyzer/src/summary2/named_type_builder.dart';
1617
import 'package:analyzer/src/summary2/record_type_builder.dart';
1718
import 'package:analyzer/src/utilities/extensions/collection.dart';
19+
import 'package:analyzer/src/utilities/extensions/element.dart';
1820

1921
/// Generates a fresh copy of the given type parameters, with their bounds
2022
/// substituted to reference the new parameters.
@@ -214,6 +216,16 @@ abstract class Substitution {
214216
);
215217
}
216218

219+
/// Substitutes the Nth parameter in [parameters] with the Nth type in
220+
/// [types].
221+
static MapSubstitution fromPairs2(
222+
List<TypeParameterElement2> parameters,
223+
List<DartType> types,
224+
) {
225+
var fragments = parameters.map((e) => e.asElement).toList();
226+
return fromPairs(fragments, types);
227+
}
228+
217229
/// Substitutes all occurrences of the given type parameters with the
218230
/// corresponding upper or lower bound, depending on the variance of the
219231
/// context where it occurs.

pkg/analyzer/lib/src/error/type_arguments_verifier.dart

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "dart:math" as math;
77
import 'package:analyzer/dart/analysis/analysis_options.dart';
88
import 'package:analyzer/dart/analysis/features.dart';
99
import 'package:analyzer/dart/ast/ast.dart';
10-
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/dart/element/element2.dart';
1111
import 'package:analyzer/dart/element/type.dart';
1212
import 'package:analyzer/diagnostic/diagnostic.dart';
1313
import 'package:analyzer/error/error.dart';
@@ -17,10 +17,11 @@ import 'package:analyzer/src/dart/element/type_algebra.dart';
1717
import 'package:analyzer/src/dart/element/type_system.dart';
1818
import 'package:analyzer/src/diagnostic/diagnostic.dart';
1919
import 'package:analyzer/src/error/codes.dart';
20+
import 'package:analyzer/src/utilities/extensions/object.dart';
2021

2122
class TypeArgumentsVerifier {
2223
final AnalysisOptions _options;
23-
final LibraryElement _libraryElement;
24+
final LibraryElement2 _libraryElement;
2425
final ErrorReporter _errorReporter;
2526

2627
TypeArgumentsVerifier(
@@ -33,19 +34,26 @@ class TypeArgumentsVerifier {
3334
_libraryElement.typeSystem as TypeSystemImpl;
3435

3536
void checkConstructorReference(ConstructorReference node) {
36-
var classElement = node.constructorName.type.element;
37-
List<TypeParameterElement> typeParameters;
38-
if (classElement is TypeAliasElement) {
39-
typeParameters = classElement.typeParameters;
40-
} else if (classElement is InterfaceElement) {
41-
typeParameters = classElement.typeParameters;
37+
var classElement = node.constructorName.type.element2;
38+
List<TypeParameterElement2> typeParameters;
39+
if (classElement is TypeAliasElement2) {
40+
typeParameters = classElement.typeParameters2;
41+
} else if (classElement is InterfaceElement2) {
42+
typeParameters = classElement.typeParameters2;
4243
} else {
4344
return;
4445
}
4546

4647
if (typeParameters.isEmpty) {
4748
return;
4849
}
50+
51+
for (var typeParameter in typeParameters) {
52+
if (typeParameter.name3 == null) {
53+
return;
54+
}
55+
}
56+
4957
var typeArgumentList = node.constructorName.type.typeArguments;
5058
if (typeArgumentList == null) {
5159
return;
@@ -66,7 +74,7 @@ class TypeArgumentsVerifier {
6674
return;
6775
}
6876
var typeArgumentListLength = typeArgumentList.arguments.length;
69-
var substitution = Substitution.fromPairs(typeParameters, typeArguments);
77+
var substitution = Substitution.fromPairs2(typeParameters, typeArguments);
7078
for (var i = 0; i < typeArguments.length; i++) {
7179
var typeParameter = typeParameters[i];
7280
var typeArgument = typeArguments[i];
@@ -84,20 +92,26 @@ class TypeArgumentsVerifier {
8492
_errorReporter.atNode(
8593
errorNode,
8694
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,
87-
arguments: [typeArgument, typeParameter.name, bound],
95+
arguments: [typeArgument, typeParameter.name3!, bound],
8896
);
8997
}
9098
}
9199
}
92100

93101
void checkEnumConstantDeclaration(EnumConstantDeclaration node) {
94-
var constructorElement = node.constructorElement;
102+
var constructorElement = node.constructorElement2;
95103
if (constructorElement == null) {
96104
return;
97105
}
98106

99-
var enumElement = constructorElement.enclosingElement3;
100-
var typeParameters = enumElement.typeParameters;
107+
var enumElement = constructorElement.enclosingElement2;
108+
var typeParameters = enumElement.typeParameters2;
109+
110+
for (var typeParameter in typeParameters) {
111+
if (typeParameter.name3 == null) {
112+
return;
113+
}
114+
}
101115

102116
var typeArgumentList = node.arguments?.typeArguments;
103117
var typeArgumentNodes = typeArgumentList?.arguments;
@@ -117,7 +131,7 @@ class TypeArgumentsVerifier {
117131

118132
// Check that type arguments are regular-bounded.
119133
var typeArguments = constructorElement.returnType.typeArguments;
120-
var substitution = Substitution.fromPairs(typeParameters, typeArguments);
134+
var substitution = Substitution.fromPairs2(typeParameters, typeArguments);
121135
for (var i = 0; i < typeArguments.length; i++) {
122136
var typeParameter = typeParameters[i];
123137
var typeArgument = typeArguments[i];
@@ -134,7 +148,7 @@ class TypeArgumentsVerifier {
134148
_errorReporter.atEntity(
135149
errorTarget,
136150
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,
137-
arguments: [typeArgument, typeParameter.name, bound],
151+
arguments: [typeArgument, typeParameter.name3!, bound],
138152
);
139153
}
140154
}
@@ -246,7 +260,7 @@ class TypeArgumentsVerifier {
246260
return;
247261
}
248262
var type = node.typeOrThrow;
249-
if (_isMissingTypeArguments(node, type, node.element)) {
263+
if (_isMissingTypeArguments(node, type, node.element2)) {
250264
AstNode unwrappedParent = parentEscapingTypeArguments(node);
251265
if (unwrappedParent is AsExpression ||
252266
unwrappedParent is CastPattern ||
@@ -272,34 +286,44 @@ class TypeArgumentsVerifier {
272286
return;
273287
}
274288

275-
List<TypeParameterElement> typeParameters;
276-
String elementName;
289+
List<TypeParameterElement2> typeParameters;
290+
String? elementName;
277291
List<DartType> typeArguments;
278292
var alias = type.alias;
279293
if (alias != null) {
280-
elementName = alias.element.name;
281-
typeParameters = alias.element.typeParameters;
294+
elementName = alias.element2.name3;
295+
typeParameters = alias.element2.typeParameters2;
282296
typeArguments = alias.typeArguments;
283297
} else if (type is InterfaceType) {
284-
elementName = type.element.name;
285-
typeParameters = type.element.typeParameters;
298+
elementName = type.element3.name3;
299+
typeParameters = type.element3.typeParameters2;
286300
typeArguments = type.typeArguments;
287301
} else {
288302
return;
289303
}
290304

305+
if (elementName == null) {
306+
return;
307+
}
308+
291309
if (typeParameters.isEmpty) {
292310
return;
293311
}
294312

295313
// Check for regular-bounded.
296314
List<_TypeArgumentIssue>? issues;
297-
var substitution = Substitution.fromPairs(typeParameters, typeArguments);
315+
var substitution = Substitution.fromPairs2(typeParameters, typeArguments);
298316
for (var i = 0; i < typeArguments.length; i++) {
299317
var typeParameter = typeParameters[i];
318+
var typeParameterName = typeParameter.name3;
319+
if (typeParameterName == null) {
320+
return;
321+
}
322+
300323
var typeArgument = typeArguments[i];
301324

302-
if (typeArgument is FunctionType && typeArgument.typeFormals.isNotEmpty) {
325+
if (typeArgument is FunctionType &&
326+
typeArgument.typeParameters.isNotEmpty) {
303327
if (!_libraryElement.featureSet.isEnabled(Feature.generic_metadata)) {
304328
_errorReporter.atNode(
305329
_typeArgumentErrorNode(namedType, i),
@@ -319,7 +343,8 @@ class TypeArgumentsVerifier {
319343
if (!_typeSystem.isSubtypeOf(typeArgument, bound)) {
320344
issues ??= <_TypeArgumentIssue>[];
321345
issues.add(
322-
_TypeArgumentIssue(i, typeParameter, bound, typeArgument),
346+
_TypeArgumentIssue(
347+
i, typeParameter, typeParameterName, bound, typeArgument),
323348
);
324349
}
325350
}
@@ -378,7 +403,7 @@ class TypeArgumentsVerifier {
378403
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,
379404
arguments: [
380405
issue.argument,
381-
issue.parameter.name,
406+
issue.parameterName,
382407
issue.parameterBound
383408
],
384409
contextMessages: buildContextMessages(),
@@ -400,12 +425,17 @@ class TypeArgumentsVerifier {
400425
}
401426

402427
// Check for super-bounded.
403-
var invertedSubstitution = Substitution.fromPairs(
428+
var invertedSubstitution = Substitution.fromPairs2(
404429
typeParameters,
405430
invertedTypeArguments,
406431
);
407432
for (var i = 0; i < invertedTypeArguments.length; i++) {
408433
var typeParameter = typeParameters[i];
434+
var typeParameterName = typeParameter.name3;
435+
if (typeParameterName == null) {
436+
return;
437+
}
438+
409439
var typeArgument = invertedTypeArguments[i];
410440

411441
var bound = typeParameter.bound;
@@ -419,7 +449,7 @@ class TypeArgumentsVerifier {
419449
_errorReporter.atNode(
420450
_typeArgumentErrorNode(namedType, i),
421451
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,
422-
arguments: [typeArgument, typeParameter.name, bound],
452+
arguments: [typeArgument, typeParameterName, bound],
423453
contextMessages: buildContextMessages(
424454
invertedTypeArguments: invertedTypeArguments,
425455
),
@@ -443,7 +473,7 @@ class TypeArgumentsVerifier {
443473
return;
444474
}
445475

446-
var fnTypeParams = genericType.typeFormals;
476+
var fnTypeParams = genericType.typeParameters;
447477
var typeArgs = typeArgumentList.map((t) => t.typeOrThrow).toList();
448478

449479
// If the amount mismatches, clean up the lists to be substitutable. The
@@ -464,7 +494,7 @@ class TypeArgumentsVerifier {
464494
//
465495
DartType argType = typeArgs[i];
466496

467-
if (argType is FunctionType && argType.typeFormals.isNotEmpty) {
497+
if (argType is FunctionType && argType.typeParameters.isNotEmpty) {
468498
if (!_libraryElement.featureSet.isEnabled(Feature.generic_metadata)) {
469499
_errorReporter.atNode(
470500
typeArgumentList[i],
@@ -475,18 +505,23 @@ class TypeArgumentsVerifier {
475505
}
476506

477507
var fnTypeParam = fnTypeParams[i];
508+
var fnTypeParamName = fnTypeParam.name3;
509+
if (fnTypeParamName == null) {
510+
continue;
511+
}
512+
478513
var rawBound = fnTypeParam.bound;
479514
if (rawBound == null) {
480515
continue;
481516
}
482517

483-
var substitution = Substitution.fromPairs(fnTypeParams, typeArgs);
518+
var substitution = Substitution.fromPairs2(fnTypeParams, typeArgs);
484519
var bound = substitution.substituteType(rawBound);
485520
if (!_typeSystem.isSubtypeOf(argType, bound)) {
486521
_errorReporter.atNode(
487522
typeArgumentList[i],
488523
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,
489-
arguments: [argType, fnTypeParam.name, bound],
524+
arguments: [argType, fnTypeParamName, bound],
490525
);
491526
}
492527
}
@@ -572,7 +607,12 @@ class TypeArgumentsVerifier {
572607
///
573608
/// - [type] does not have any `dynamic` type arguments.
574609
/// - the element is marked with `@optionalTypeArgs` from "package:meta".
575-
bool _isMissingTypeArguments(AstNode node, DartType type, Element? element) {
610+
bool _isMissingTypeArguments(AstNode node, DartType type, Element2? element) {
611+
var elementMetadata = element.ifTypeOrNull<Annotatable>()?.metadata2;
612+
if (elementMetadata == null) {
613+
return false;
614+
}
615+
576616
List<DartType> typeArguments;
577617
var alias = type.alias;
578618
if (alias != null) {
@@ -586,7 +626,7 @@ class TypeArgumentsVerifier {
586626
// Check if this type has type arguments and at least one is dynamic.
587627
// If so, we may need to issue a strict-raw-types error.
588628
if (typeArguments.any((t) => t is DynamicType)) {
589-
if (element != null && element.hasOptionalTypeArgs) {
629+
if (element != null && elementMetadata.hasOptionalTypeArgs) {
590630
return false;
591631
}
592632
return true;
@@ -608,7 +648,7 @@ class TypeArgumentsVerifier {
608648
return false;
609649
}
610650

611-
if (namedType.type?.element is ExtensionTypeElement) {
651+
if (namedType.type?.element3 is ExtensionTypeElement2) {
612652
return false;
613653
}
614654

@@ -630,7 +670,10 @@ class _TypeArgumentIssue {
630670
final int index;
631671

632672
/// The type parameter with the bound that was violated.
633-
final TypeParameterElement parameter;
673+
final TypeParameterElement2 parameter;
674+
675+
/// The non-null name of the [parameter].
676+
final String parameterName;
634677

635678
/// The substituted bound of the [parameter].
636679
final DartType parameterBound;
@@ -641,6 +684,7 @@ class _TypeArgumentIssue {
641684
_TypeArgumentIssue(
642685
this.index,
643686
this.parameter,
687+
this.parameterName,
644688
this.parameterBound,
645689
this.argument,
646690
);

pkg/analyzer/lib/src/utilities/extensions/element.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,9 @@ extension ParameterElementExtension on ParameterElement {
283283
return declaration as ParameterElementImpl;
284284
}
285285
}
286+
287+
extension TypeParameterElement2Extension on TypeParameterElement2 {
288+
TypeParameterElement get asElement {
289+
return firstFragment as TypeParameterElement;
290+
}
291+
}

0 commit comments

Comments
 (0)