@@ -474,74 +474,6 @@ class GenericInferrer {
474474 }
475475 }
476476
477- /// Choose the bound that was implied by the return type, if any.
478- ///
479- /// Which bound this is depends on what positions the type parameter
480- /// appears in. If the type only appears only in a contravariant position,
481- /// we will choose the lower bound instead.
482- ///
483- /// For example given:
484- ///
485- /// Func1<T, bool> makeComparer<T>(T x) => (T y) => x() == y;
486- ///
487- /// main() {
488- /// Func1<num, bool> t = makeComparer/* infer <num> */(42);
489- /// print(t(42.0)); /// false, no error.
490- /// }
491- ///
492- /// The constraints we collect are:
493- ///
494- /// * `num <: T`
495- /// * `int <: T`
496- ///
497- /// ... and no upper bound. Therefore the lower bound is the best choice.
498- ///
499- /// If [isContravariant] is `true` , then we are solving for a contravariant
500- /// type parameter which means we choose the upper bound rather than the
501- /// lower bound for normally covariant type parameters.
502- TypeImpl _chooseTypeFromConstraint (
503- MergedTypeConstraint constraint, {
504- bool toKnownType = false ,
505- required bool isContravariant,
506- }) {
507- TypeImpl upper = constraint.upper.unwrapTypeSchemaView ();
508- TypeImpl lower = constraint.lower.unwrapTypeSchemaView ();
509- // Prefer the known bound, if any.
510- // Otherwise take whatever bound has partial information, e.g. `Iterable<?>`
511- //
512- // For both of those, prefer the lower bound (arbitrary heuristic) or upper
513- // bound if [isContravariant] is `true`
514- if (isContravariant) {
515- if (_typeSystemOperations.isKnownType (SharedTypeSchemaView (upper))) {
516- return upper;
517- }
518- if (_typeSystemOperations.isKnownType (SharedTypeSchemaView (lower))) {
519- return lower;
520- }
521- if (! identical (UnknownInferredType .instance, upper)) {
522- return toKnownType ? _typeSystem.greatestClosureOfSchema (upper) : upper;
523- }
524- if (! identical (UnknownInferredType .instance, lower)) {
525- return toKnownType ? _typeSystem.leastClosureOfSchema (lower) : lower;
526- }
527- return upper;
528- } else {
529- if (_typeSystemOperations.isKnownType (SharedTypeSchemaView (lower))) {
530- return lower;
531- }
532- if (_typeSystemOperations.isKnownType (SharedTypeSchemaView (upper))) {
533- return upper;
534- }
535- if (! identical (UnknownInferredType .instance, lower)) {
536- return toKnownType ? _typeSystem.leastClosureOfSchema (lower) : lower;
537- }
538- if (! identical (UnknownInferredType .instance, upper)) {
539- return toKnownType ? _typeSystem.greatestClosureOfSchema (upper) : upper;
540- }
541- return lower;
542- }
543- }
544-
545477 /// Computes (or recomputes) a set of inferred types based on the constraints
546478 /// that have been recorded so far.
547479 List <TypeImpl > _chooseTypes ({required bool preliminary}) {
@@ -579,13 +511,15 @@ class GenericInferrer {
579511 if (previouslyInferredType != null ) {
580512 inferredTypes[i] = previouslyInferredType;
581513 } else if (preliminary) {
582- var inferredType = _inferTypeParameterFromContext (
583- constraint,
584- extendsClause,
585- isContravariant: typeParam.variance.isContravariant,
586- typeParameterToInfer: typeParam,
587- inferencePhaseConstraints: inferencePhaseConstraints,
588- );
514+ var inferredType =
515+ _inferTypeParameterFromContext (
516+ constraint,
517+ extendsClause,
518+ isContravariant: typeParam.variance.isContravariant,
519+ typeParameterToInfer: typeParam,
520+ inferencePhaseConstraints: inferencePhaseConstraints,
521+ )
522+ as TypeImpl ;
589523
590524 inferredTypes[i] = inferredType;
591525 if (typeParam.isLegacyCovariant &&
@@ -595,13 +529,15 @@ class GenericInferrer {
595529 _typesInferredSoFar[typeParam] = inferredType;
596530 }
597531 } else {
598- inferredTypes[i] = _inferTypeParameterFromAll (
599- constraint,
600- extendsClause,
601- isContravariant: typeParam.variance.isContravariant,
602- typeParameterToInfer: typeParam,
603- inferencePhaseConstraints: inferencePhaseConstraints,
604- );
532+ inferredTypes[i] =
533+ _inferTypeParameterFromAll (
534+ constraint,
535+ extendsClause,
536+ isContravariant: typeParam.variance.isContravariant,
537+ typeParameterToInfer: typeParam,
538+ inferencePhaseConstraints: inferencePhaseConstraints,
539+ )
540+ as TypeImpl ;
605541 }
606542 }
607543
@@ -667,7 +603,7 @@ class GenericInferrer {
667603 'Consider passing explicit type argument(s) to the generic.\n\n ' ;
668604 }
669605
670- TypeImpl _inferTypeParameterFromAll (
606+ SharedType _inferTypeParameterFromAll (
671607 MergedTypeConstraint constraint,
672608 MergedTypeConstraint ? extendsClause, {
673609 required bool isContravariant,
@@ -702,15 +638,15 @@ class GenericInferrer {
702638 ]);
703639 }
704640
705- var choice = _chooseTypeFromConstraint (
641+ var choice = _typeSystemOperations. chooseTypeFromConstraint (
706642 constraint,
707- toKnownType : true ,
643+ grounded : true ,
708644 isContravariant: isContravariant,
709645 );
710646 return choice;
711647 }
712648
713- TypeImpl _inferTypeParameterFromContext (
649+ SharedType _inferTypeParameterFromContext (
714650 MergedTypeConstraint constraint,
715651 MergedTypeConstraint ? extendsClause, {
716652 required bool isContravariant,
@@ -721,8 +657,9 @@ class GenericInferrer {
721657 // Both bits of the bound information should be available at the same time.
722658 assert (extendsClause == null || typeParameterToInfer.bound != null );
723659
724- TypeImpl t = _chooseTypeFromConstraint (
660+ SharedType t = _typeSystemOperations. chooseTypeFromConstraint (
725661 constraint,
662+ grounded: false ,
726663 isContravariant: isContravariant,
727664 );
728665 if (! _typeSystemOperations.isKnownType (SharedTypeSchemaView (t))) {
@@ -761,8 +698,9 @@ class GenericInferrer {
761698 ! boundConstraint.isEmpty (_typeSystemOperations))
762699 boundConstraint,
763700 ]);
764- return _chooseTypeFromConstraint (
701+ return _typeSystemOperations. chooseTypeFromConstraint (
765702 constraint,
703+ grounded: false ,
766704 isContravariant: isContravariant,
767705 );
768706 }
0 commit comments