55import 'dart:math' as math;
66
77import 'package:_fe_analyzer_shared/src/type_inference/shared_inference_log.dart' ;
8- import 'package:_fe_analyzer_shared/src/type_inference/type_constraint.dart' ;
98import 'package:_fe_analyzer_shared/src/types/shared_type.dart' ;
109import 'package:analyzer/dart/ast/ast.dart'
1110 show
@@ -60,15 +59,7 @@ import 'package:collection/collection.dart';
6059class GenericInferrer {
6160 final TypeSystemImpl _typeSystem;
6261 final Set <TypeParameterElement > _typeParameters = Set .identity ();
63- final Map <
64- TypeParameterElement ,
65- List <
66- MergedTypeConstraint <
67- DartType ,
68- TypeParameterElement ,
69- PromotableElement ,
70- InterfaceType ,
71- InterfaceElement >>> _constraints = {};
62+ final Map <TypeParameterElement , List <MergedTypeConstraint >> _constraints = {};
7263
7364 /// The list of type parameters being inferred.
7465 final List <TypeParameterElement > _typeFormals;
@@ -159,8 +150,7 @@ class GenericInferrer {
159150 void constrainArgument (
160151 DartType argumentType, DartType parameterType, String parameterName,
161152 {InterfaceElement ? genericClass, required AstNode ? nodeForTesting}) {
162- var origin = TypeConstraintFromArgument <DartType , PromotableElement ,
163- TypeParameterElement , InterfaceType , InterfaceElement >(
153+ var origin = TypeConstraintFromArgument (
164154 argumentType: SharedTypeView (argumentType),
165155 parameterType: SharedTypeView (parameterType),
166156 parameterName: parameterName,
@@ -199,14 +189,8 @@ class GenericInferrer {
199189 void constrainGenericFunctionInContext (
200190 FunctionType fnType, DartType contextType,
201191 {required AstNode ? nodeForTesting}) {
202- var origin = TypeConstraintFromFunctionContext <
203- DartType ,
204- DartType ,
205- DartType ,
206- PromotableElement ,
207- TypeParameterElement ,
208- InterfaceType ,
209- InterfaceElement >(functionType: fnType, contextType: contextType);
192+ var origin = TypeConstraintFromFunctionContext (
193+ functionType: fnType, contextType: contextType);
210194
211195 // Since we're trying to infer the instantiation, we want to ignore type
212196 // formals as we check the parameters and return type.
@@ -229,14 +213,8 @@ class GenericInferrer {
229213 /// is a subtype of the [contextType] .
230214 void constrainReturnType (DartType declaredType, DartType contextType,
231215 {required AstNode ? nodeForTesting}) {
232- var origin = TypeConstraintFromReturnType <
233- DartType ,
234- DartType ,
235- DartType ,
236- PromotableElement ,
237- TypeParameterElement ,
238- InterfaceType ,
239- InterfaceElement >(declaredType: declaredType, contextType: contextType);
216+ var origin = TypeConstraintFromReturnType (
217+ declaredType: declaredType, contextType: contextType);
240218 inferenceLogWriter? .enterConstraintGeneration (
241219 ConstraintGenerationSource .returnType, declaredType, contextType);
242220 _tryMatchSubtypeOf (declaredType, contextType, origin,
@@ -267,12 +245,7 @@ class GenericInferrer {
267245 var parameterBound =
268246 Substitution .fromPairs (_typeFormals, inferredTypes)
269247 .substituteType (parameterBoundRaw);
270- var extendsConstraint = MergedTypeConstraint <
271- DartType ,
272- TypeParameterElement ,
273- PromotableElement ,
274- InterfaceType ,
275- InterfaceElement >.fromExtends (
248+ var extendsConstraint = MergedTypeConstraint .fromExtends (
276249 typeParameterName: parameter.name,
277250 boundType: SharedTypeView (parameterBoundRaw),
278251 extendsType: SharedTypeView (parameterBound),
@@ -445,10 +418,7 @@ class GenericInferrer {
445418 /// type parameter which means we choose the upper bound rather than the
446419 /// lower bound for normally covariant type parameters.
447420 DartType _chooseTypeFromConstraints (
448- Iterable <
449- MergedTypeConstraint <DartType , TypeParameterElement ,
450- PromotableElement , InterfaceType , InterfaceElement >>
451- constraints,
421+ Iterable <MergedTypeConstraint > constraints,
452422 {bool toKnownType = false ,
453423 required bool isContravariant}) {
454424 var (: lower, : upper) =
@@ -503,12 +473,10 @@ class GenericInferrer {
503473 // TODO(kallentu): : Clean up TypeParameterElementImpl casting once
504474 // variance is added to the interface.
505475 var typeParam = _typeFormals[i] as TypeParameterElementImpl ;
506- MergedTypeConstraint <DartType , TypeParameterElement , PromotableElement ,
507- InterfaceType , InterfaceElement >? extendsClause;
476+ MergedTypeConstraint ? extendsClause;
508477 var bound = typeParam.bound;
509478 if (bound != null ) {
510- extendsClause = MergedTypeConstraint <DartType , TypeParameterElement ,
511- PromotableElement , InterfaceType , InterfaceElement >.fromExtends (
479+ extendsClause = MergedTypeConstraint .fromExtends (
512480 typeParameterName: typeParam.name,
513481 boundType: SharedTypeView (bound),
514482 extendsType: SharedTypeView (
@@ -546,10 +514,7 @@ class GenericInferrer {
546514 }
547515
548516 ({DartType lower, DartType upper}) _computeLowerAndUpperBoundsOfConstraints (
549- Iterable <
550- MergedTypeConstraint <DartType , TypeParameterElement ,
551- PromotableElement , InterfaceType , InterfaceElement >>
552- constraints) {
517+ Iterable <MergedTypeConstraint > constraints) {
553518 DartType lower = UnknownInferredType .instance;
554519 DartType upper = UnknownInferredType .instance;
555520 for (var constraint in constraints) {
@@ -584,30 +549,20 @@ class GenericInferrer {
584549 return element.getDisplayString ();
585550 }
586551
587- String _formatError (
588- TypeParameterElement typeParam,
589- DartType inferred,
590- Iterable <
591- MergedTypeConstraint <DartType , TypeParameterElement ,
592- PromotableElement , InterfaceType , InterfaceElement >>
593- constraints) {
552+ String _formatError (TypeParameterElement typeParam, DartType inferred,
553+ Iterable <MergedTypeConstraint > constraints) {
594554 var inferredStr = inferred.getDisplayString ();
595555 var intro = "Tried to infer '$inferredStr ' for '${typeParam .name }'"
596556 " which doesn't work:" ;
597557
598- var constraintsByOrigin = < TypeConstraintOrigin <DartType , PromotableElement ,
599- TypeParameterElement , InterfaceType , InterfaceElement >,
600- List <
601- MergedTypeConstraint <DartType , TypeParameterElement ,
602- PromotableElement , InterfaceType , InterfaceElement >>> {};
558+ var constraintsByOrigin =
559+ < TypeConstraintOrigin , List <MergedTypeConstraint >> {};
603560 for (var c in constraints) {
604561 constraintsByOrigin.putIfAbsent (c.origin, () => []).add (c);
605562 }
606563
607564 // Only report unique constraint origins.
608- Iterable <
609- MergedTypeConstraint <DartType , TypeParameterElement , PromotableElement ,
610- InterfaceType , InterfaceElement >> isSatisfied (bool expected) =>
565+ Iterable <MergedTypeConstraint > isSatisfied (bool expected) =>
611566 constraintsByOrigin.values
612567 .where ((l) =>
613568 l.every ((c) => c.isSatisfiedBy (
@@ -629,28 +584,17 @@ class GenericInferrer {
629584 'Consider passing explicit type argument(s) to the generic.\n\n ' ;
630585 }
631586
632- DartType _inferTypeParameterFromAll (
633- List <
634- MergedTypeConstraint <DartType , TypeParameterElement ,
635- PromotableElement , InterfaceType , InterfaceElement >>
636- constraints,
637- MergedTypeConstraint <DartType , TypeParameterElement , PromotableElement ,
638- InterfaceType , InterfaceElement >?
639- extendsClause,
587+ DartType _inferTypeParameterFromAll (List <MergedTypeConstraint > constraints,
588+ MergedTypeConstraint ? extendsClause,
640589 {required bool isContravariant,
641590 required TypeParameterElement typeParameterToInfer,
642- required Map <
643- TypeParameterElement ,
644- List <
645- MergedTypeConstraint <DartType , TypeParameterElement ,
646- PromotableElement , InterfaceType , InterfaceElement >>>
591+ required Map <TypeParameterElement , List <MergedTypeConstraint >>
647592 inferencePhaseConstraints}) {
648593 if (extendsClause != null ) {
649594 var (: lower, upper: _) =
650595 _computeLowerAndUpperBoundsOfConstraints (constraints);
651596
652- MergedTypeConstraint <DartType , TypeParameterElement , PromotableElement ,
653- InterfaceType , InterfaceElement >? boundConstraint;
597+ MergedTypeConstraint ? boundConstraint;
654598 if (inferenceUsingBoundsIsEnabled) {
655599 if (! identical (lower, UnknownInferredType .instance)) {
656600 boundConstraint = _mergeInConstraintsFromBound (
@@ -675,20 +619,11 @@ class GenericInferrer {
675619 }
676620
677621 DartType _inferTypeParameterFromContext (
678- Iterable <
679- MergedTypeConstraint <DartType , TypeParameterElement ,
680- PromotableElement , InterfaceType , InterfaceElement >>
681- constraints,
682- MergedTypeConstraint <DartType , TypeParameterElement , PromotableElement ,
683- InterfaceType , InterfaceElement >?
684- extendsClause,
622+ Iterable <MergedTypeConstraint > constraints,
623+ MergedTypeConstraint ? extendsClause,
685624 {required bool isContravariant,
686625 required TypeParameterElement typeParameterToInfer,
687- required Map <
688- TypeParameterElement ,
689- List <
690- MergedTypeConstraint <DartType , TypeParameterElement ,
691- PromotableElement , InterfaceType , InterfaceElement >>>
626+ required Map <TypeParameterElement , List <MergedTypeConstraint >>
692627 inferencePhaseConstraints}) {
693628 // Both bits of the bound information should be available at the same time.
694629 assert (extendsClause == null || typeParameterToInfer.bound != null );
@@ -710,8 +645,7 @@ class GenericInferrer {
710645 var (: lower, upper: _) =
711646 _computeLowerAndUpperBoundsOfConstraints (constraints);
712647
713- MergedTypeConstraint <DartType , TypeParameterElement , PromotableElement ,
714- InterfaceType , InterfaceElement >? boundConstraint;
648+ MergedTypeConstraint ? boundConstraint;
715649 if (inferenceUsingBoundsIsEnabled) {
716650 if (! identical (lower, UnknownInferredType .instance)) {
717651 boundConstraint = _mergeInConstraintsFromBound (
@@ -734,17 +668,11 @@ class GenericInferrer {
734668 return t;
735669 }
736670
737- MergedTypeConstraint <DartType , TypeParameterElement , PromotableElement ,
738- InterfaceType , InterfaceElement >
739- _mergeInConstraintsFromBound (
740- {required TypeParameterElement typeParameterToInfer,
741- required DartType lower,
742- required Map <
743- TypeParameterElement ,
744- List <
745- MergedTypeConstraint <DartType , TypeParameterElement ,
746- PromotableElement , InterfaceType , InterfaceElement >>>
747- inferencePhaseConstraints}) {
671+ MergedTypeConstraint _mergeInConstraintsFromBound (
672+ {required TypeParameterElement typeParameterToInfer,
673+ required DartType lower,
674+ required Map <TypeParameterElement , List <MergedTypeConstraint >>
675+ inferencePhaseConstraints}) {
748676 // The type parameter's bound may refer to itself (or other type
749677 // parameters), so we might have to create an additional constraint.
750678 // Consider this example from
@@ -877,14 +805,8 @@ class GenericInferrer {
877805 /// The return value indicates whether the match was successful. If it was
878806 /// unsuccessful, any constraints that were accumulated during the match
879807 /// attempt have been rewound.
880- bool _tryMatchSubtypeOf (
881- DartType t1,
882- DartType t2,
883- TypeConstraintOrigin <DartType , PromotableElement , TypeParameterElement ,
884- InterfaceType , InterfaceElement >
885- origin,
886- {required bool covariant ,
887- required AstNode ? nodeForTesting}) {
808+ bool _tryMatchSubtypeOf (DartType t1, DartType t2, TypeConstraintOrigin origin,
809+ {required bool covariant , required AstNode ? nodeForTesting}) {
888810 var gatherer = TypeConstraintGatherer (
889811 typeParameters: _typeParameters,
890812 typeSystemOperations: _typeSystemOperations,
@@ -911,21 +833,12 @@ class GenericInferrer {
911833 return type.getDisplayString ();
912834 }
913835
914- static String _formatConstraints (
915- Iterable <
916- MergedTypeConstraint <DartType , TypeParameterElement ,
917- PromotableElement , InterfaceType , InterfaceElement >>
918- constraints,
836+ static String _formatConstraints (Iterable <MergedTypeConstraint > constraints,
919837 TypeSystemOperations typeSystemOperations) {
920- List <List <String >> lineParts = Set <
921- TypeConstraintOrigin <
922- DartType ,
923- PromotableElement ,
924- TypeParameterElement ,
925- InterfaceType ,
926- InterfaceElement >>.from (constraints.map ((c) => c.origin))
927- .map ((o) => o.formatError (typeSystemOperations))
928- .toList ();
838+ List <List <String >> lineParts =
839+ Set <TypeConstraintOrigin >.from (constraints.map ((c) => c.origin))
840+ .map ((o) => o.formatError (typeSystemOperations))
841+ .toList ();
929842
930843 int prefixMax = lineParts.map ((p) => p[0 ].length).fold (0 , math.max);
931844
0 commit comments