@@ -4762,7 +4762,6 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
47624762 InterfaceTypeImpl get thisType {
47634763 if (_thisType == null ) {
47644764 List <TypeImpl > typeArguments;
4765- var typeParameters = firstFragment.typeParameters;
47664765 if (typeParameters.isNotEmpty) {
47674766 typeArguments =
47684767 typeParameters.map <TypeImpl >((t) {
@@ -9877,6 +9876,23 @@ class TypeParameterElementImpl extends ElementImpl
98779876 @override
98789877 final String ? name;
98799878
9879+ /// The value representing the variance modifier keyword, or `null` if
9880+ /// there is no explicit variance modifier, meaning legacy covariance.
9881+ shared.Variance ? _variance;
9882+
9883+ /// The type representing the bound associated with this type parameter,
9884+ /// or `null` if this type parameter does not have an explicit bound.
9885+ ///
9886+ /// Being able to distinguish between an implicit and explicit bound is
9887+ /// needed by the instantiate to bounds algorithm.
9888+ @override
9889+ TypeImpl ? bound;
9890+
9891+ /// The default value of the type parameter. It is used to provide the
9892+ /// corresponding missing type argument in type annotations and as the
9893+ /// fall-back type value in type inference.
9894+ TypeImpl ? defaultType;
9895+
98809896 TypeParameterElementImpl ({required this .firstFragment})
98819897 : name = firstFragment.name {
98829898 firstFragment.element = this ;
@@ -9890,21 +9906,9 @@ class TypeParameterElementImpl extends ElementImpl
98909906 @override
98919907 TypeParameterElementImpl get baseElement => this ;
98929908
9893- @override
9894- TypeImpl ? get bound => firstFragment.bound;
9895-
9896- set bound (TypeImpl ? value) {
9897- firstFragment.bound = value;
9898- }
9899-
99009909 @override
99019910 TypeImpl ? get boundShared => bound;
99029911
9903- /// The default value of the type parameter. It is used to provide the
9904- /// corresponding missing type argument in type annotations and as the
9905- /// fall-back type value in type inference.
9906- TypeImpl ? get defaultType => firstFragment.defaultType;
9907-
99089912 @override
99099913 Element ? get enclosingElement {
99109914 return firstFragment.enclosingFragment? .element;
@@ -9926,7 +9930,9 @@ class TypeParameterElementImpl extends ElementImpl
99269930 ];
99279931 }
99289932
9929- bool get isLegacyCovariant => firstFragment.isLegacyCovariant;
9933+ bool get isLegacyCovariant {
9934+ return _variance == null ;
9935+ }
99309936
99319937 @override
99329938 bool get isSynthetic {
@@ -9957,12 +9963,12 @@ class TypeParameterElementImpl extends ElementImpl
99579963 @override
99589964 String ? get name3 => name;
99599965
9960- shared.Variance get variance => firstFragment.variance;
9961-
9962- set variance (shared.Variance ? value) {
9963- firstFragment.variance = value;
9966+ shared.Variance get variance {
9967+ return _variance ?? shared.Variance .covariant ;
99649968 }
99659969
9970+ set variance (shared.Variance ? newVariance) => _variance = newVariance;
9971+
99669972 @override
99679973 T ? accept <T >(ElementVisitor2 <T > visitor) {
99689974 return visitor.visitTypeParameterElement (this );
@@ -9977,6 +9983,52 @@ class TypeParameterElementImpl extends ElementImpl
99779983 builder.writeTypeParameterElement (this );
99789984 }
99799985
9986+ /// Computes the variance of the type parameters in the [type] .
9987+ shared.Variance computeVarianceInType (DartType type) {
9988+ if (type is TypeParameterTypeImpl ) {
9989+ if (type.element == this ) {
9990+ return shared.Variance .covariant ;
9991+ } else {
9992+ return shared.Variance .unrelated;
9993+ }
9994+ } else if (type is InterfaceTypeImpl ) {
9995+ var result = shared.Variance .unrelated;
9996+ for (int i = 0 ; i < type.typeArguments.length; ++ i) {
9997+ var argument = type.typeArguments[i];
9998+ var parameter = type.element.typeParameters[i];
9999+
10000+ var parameterVariance = parameter.variance;
10001+ result = result.meet (
10002+ parameterVariance.combine (computeVarianceInType (argument)),
10003+ );
10004+ }
10005+ return result;
10006+ } else if (type is FunctionType ) {
10007+ var result = computeVarianceInType (type.returnType);
10008+
10009+ for (var parameter in type.typeParameters) {
10010+ // If [parameter] is referenced in the bound at all, it makes the
10011+ // variance of [parameter] in the entire type invariant. The invocation
10012+ // of [computeVariance] below is made to simply figure out if [variable]
10013+ // occurs in the bound.
10014+ var bound = parameter.bound;
10015+ if (bound != null && ! computeVarianceInType (bound).isUnrelated) {
10016+ result = shared.Variance .invariant;
10017+ }
10018+ }
10019+
10020+ for (var formalParameter in type.formalParameters) {
10021+ result = result.meet (
10022+ shared.Variance .contravariant.combine (
10023+ computeVarianceInType (formalParameter.type),
10024+ ),
10025+ );
10026+ }
10027+ return result;
10028+ }
10029+ return shared.Variance .unrelated;
10030+ }
10031+
998010032 @override
998110033 TypeParameterTypeImpl instantiate ({
998210034 required NullabilitySuffix nullabilitySuffix,
@@ -10003,19 +10055,6 @@ class TypeParameterFragmentImpl extends FragmentImpl
1000310055 @override
1000410056 int ? nameOffset;
1000510057
10006- /// The default value of the type parameter. It is used to provide the
10007- /// corresponding missing type argument in type annotations and as the
10008- /// fall-back type value in type inference.
10009- TypeImpl ? defaultType;
10010-
10011- /// The type representing the bound associated with this parameter, or `null`
10012- /// if this parameter does not have an explicit bound.
10013- TypeImpl ? _bound;
10014-
10015- /// The value representing the variance modifier keyword, or `null` if
10016- /// there is no explicit variance modifier, meaning legacy covariance.
10017- shared.Variance ? _variance;
10018-
1001910058 /// The element corresponding to this fragment.
1002010059 TypeParameterElementImpl ? _element;
1002110060
@@ -10030,25 +10069,6 @@ class TypeParameterFragmentImpl extends FragmentImpl
1003010069 isSynthetic = true ;
1003110070 }
1003210071
10033- /// The type representing the bound associated with this parameter, or `null`
10034- /// if this parameter does not have an explicit bound. Being able to
10035- /// distinguish between an implicit and explicit bound is needed by the
10036- /// instantiate to bounds algorithm.
10037- TypeImpl ? get bound {
10038- return _bound;
10039- }
10040-
10041- set bound (DartType ? bound) {
10042- // TODO(paulberry): Change the type of the parameter `bound` so that this
10043- // cast isn't needed.
10044- _bound = bound as TypeImpl ? ;
10045- if (_element case var element? ) {
10046- if (! identical (element.bound, bound)) {
10047- element.bound = bound;
10048- }
10049- }
10050- }
10051-
1005210072 @override
1005310073 List <Fragment > get children => const [];
1005410074
@@ -10082,10 +10102,6 @@ class TypeParameterFragmentImpl extends FragmentImpl
1008210102 _element = element;
1008310103 }
1008410104
10085- bool get isLegacyCovariant {
10086- return _variance == null ;
10087- }
10088-
1008910105 @override
1009010106 LibraryFragment ? get libraryFragment {
1009110107 return enclosingFragment? .libraryFragment;
@@ -10105,66 +10121,6 @@ class TypeParameterFragmentImpl extends FragmentImpl
1010510121 @override
1010610122 // TODO(augmentations): Support chaining between the fragments.
1010710123 TypeParameterFragmentImpl ? get previousFragment => null ;
10108-
10109- shared.Variance get variance {
10110- return _variance ?? shared.Variance .covariant ;
10111- }
10112-
10113- set variance (shared.Variance ? newVariance) => _variance = newVariance;
10114-
10115- /// Computes the variance of the type parameters in the [type] .
10116- shared.Variance computeVarianceInType (DartType type) {
10117- if (type is TypeParameterTypeImpl ) {
10118- if (type.element == element) {
10119- return shared.Variance .covariant ;
10120- } else {
10121- return shared.Variance .unrelated;
10122- }
10123- } else if (type is InterfaceTypeImpl ) {
10124- var result = shared.Variance .unrelated;
10125- for (int i = 0 ; i < type.typeArguments.length; ++ i) {
10126- var argument = type.typeArguments[i];
10127- var parameter = type.element.typeParameters[i];
10128-
10129- var parameterVariance = parameter.variance;
10130- result = result.meet (
10131- parameterVariance.combine (computeVarianceInType (argument)),
10132- );
10133- }
10134- return result;
10135- } else if (type is FunctionType ) {
10136- var result = computeVarianceInType (type.returnType);
10137-
10138- for (var parameter in type.typeParameters) {
10139- // If [parameter] is referenced in the bound at all, it makes the
10140- // variance of [parameter] in the entire type invariant. The invocation
10141- // of [computeVariance] below is made to simply figure out if [variable]
10142- // occurs in the bound.
10143- var bound = parameter.bound;
10144- if (bound != null && ! computeVarianceInType (bound).isUnrelated) {
10145- result = shared.Variance .invariant;
10146- }
10147- }
10148-
10149- for (var formalParameter in type.formalParameters) {
10150- result = result.meet (
10151- shared.Variance .contravariant.combine (
10152- computeVarianceInType (formalParameter.type),
10153- ),
10154- );
10155- }
10156- return result;
10157- }
10158- return shared.Variance .unrelated;
10159- }
10160-
10161- /// Creates the [TypeParameterType] with the given [nullabilitySuffix] for
10162- /// this type parameter.
10163- TypeParameterTypeImpl instantiate ({
10164- required NullabilitySuffix nullabilitySuffix,
10165- }) {
10166- return element.instantiate (nullabilitySuffix: nullabilitySuffix);
10167- }
1016810124}
1016910125
1017010126/// Mixin representing an element which can have type parameters.
0 commit comments