diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart index c40153a2214..acccfc36aee 100644 --- a/pkg/analyzer/lib/src/fasta/ast_builder.dart +++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart @@ -2142,14 +2142,13 @@ class AstBuilder extends StackListener { // Peek to leave type parameters on top of stack. List typeParameters = peek(); - TypeParameter typeParameter = typeParameters[index]; - typeParameter - ..extendsKeyword = extendsOrSuper - ..bound = bound; - if (typeParameter is TypeParameterImpl) { - typeParameter.varianceKeyword = variance; - } + // TODO (kallentu) : Clean up TypeParameterImpl casting once variance is + // added to the interface. + (typeParameters[index] as TypeParameterImpl) + ..extendsKeyword = extendsOrSuper + ..bound = bound + ..varianceKeyword = variance; } @override diff --git a/pkg/analyzer/lib/src/generated/type_system.dart b/pkg/analyzer/lib/src/generated/type_system.dart index 1b841ecaff4..8ff916677d4 100644 --- a/pkg/analyzer/lib/src/generated/type_system.dart +++ b/pkg/analyzer/lib/src/generated/type_system.dart @@ -1117,15 +1117,14 @@ class Dart2TypeSystem extends TypeSystem { DartType t1 = tArgs1[i]; DartType t2 = tArgs2[i]; - // TODO (kallentu) : Clean up TypeParameterElementImpl checks and - // casting once variance is added to the interface. - TypeParameterElement parameterElement = tParams[i]; - Variance variance = Variance.covariant; - if (parameterElement is TypeParameterElementImpl) { - variance = parameterElement.variance; - } - - if (variance.isContravariant) { + // TODO (kallentu) : Clean up TypeParameterElementImpl casting once + // variance is added to the interface. + Variance variance = (tParams[i] as TypeParameterElementImpl).variance; + if (variance.isCovariant) { + if (!isSubtypeOf(t1, t2)) { + return false; + } + } else if (variance.isContravariant) { if (!isSubtypeOf(t2, t1)) { return false; } @@ -1134,9 +1133,8 @@ class Dart2TypeSystem extends TypeSystem { return false; } } else { - if (!isSubtypeOf(t1, t2)) { - return false; - } + throw new StateError('Type parameter ${tParams[i]} has unknown ' + 'variance $variance for subtype checking.'); } } return true; @@ -2173,15 +2171,13 @@ class InterfaceLeastUpperBoundHelper { var args = List(args1.length); for (int i = 0; i < args1.length; i++) { - // TODO (kallentu) : Clean up TypeParameterElementImpl checks and - // casting once variance is added to the interface. - TypeParameterElement parameter = params[i]; - Variance parameterVariance = Variance.covariant; - if (parameter is TypeParameterElementImpl) { - parameterVariance = parameter.variance; - } - - if (parameterVariance.isContravariant) { + // TODO (kallentu) : Clean up TypeParameterElementImpl casting once + // variance is added to the interface. + Variance parameterVariance = + (params[i] as TypeParameterElementImpl).variance; + if (parameterVariance.isCovariant) { + args[i] = typeSystem.getLeastUpperBound(args1[i], args2[i]); + } else if (parameterVariance.isContravariant) { if (typeSystem is Dart2TypeSystem) { args[i] = (typeSystem as Dart2TypeSystem) .getGreatestLowerBound(args1[i], args2[i]); @@ -2201,7 +2197,8 @@ class InterfaceLeastUpperBoundHelper { // parameters. args[i] = args1[i]; } else { - args[i] = typeSystem.getLeastUpperBound(args1[i], args2[i]); + throw new StateError('Type parameter ${params[i]} has unknown ' + 'variance $parameterVariance for bounds calculation.'); } } diff --git a/pkg/analyzer/test/src/dart/element/subtype_test.dart b/pkg/analyzer/test/src/dart/element/subtype_test.dart index 354f34e90f1..8d7b391e3f8 100644 --- a/pkg/analyzer/test/src/dart/element/subtype_test.dart +++ b/pkg/analyzer/test/src/dart/element/subtype_test.dart @@ -2846,7 +2846,7 @@ class SubtypeTest extends _SubtypingTestBase { ); } - test_interfaceType_covariant_01() { + test_interfaceType_covariant() { var T = typeParameter('T', variance: Variance.covariant); var A = class_(name: 'A', typeParameters: [T]); @@ -2861,22 +2861,11 @@ class SubtypeTest extends _SubtypingTestBase { ); isSubtype(A_int, A_num, strT0: "A", strT1: "A"); + isSubtype(A_num, A_num, strT0: "A", strT1: "A"); isNotSubtype(A_num, A_int, strT0: "A", strT1: "A"); } - test_interfaceType_covariant_02() { - var T = typeParameter('T', variance: Variance.covariant); - var A = class_(name: 'A', typeParameters: [T]); - - var A_num = A.instantiate( - typeArguments: [numNone], - nullabilitySuffix: NullabilitySuffix.none, - ); - - isSubtype(A_num, A_num, strT0: "A", strT1: "A"); - } - - test_interfaceType_contravariant_01() { + test_interfaceType_contravariant() { var T = typeParameter('T', variance: Variance.contravariant); var A = class_(name: 'A', typeParameters: [T]); @@ -2891,19 +2880,8 @@ class SubtypeTest extends _SubtypingTestBase { ); isSubtype(A_num, A_int, strT0: "A", strT1: "A"); - isNotSubtype(A_int, A_num, strT0: "A", strT1: "A"); - } - - test_interfaceType_contravariant_02() { - var T = typeParameter('T', variance: Variance.contravariant); - var A = class_(name: 'A', typeParameters: [T]); - - var A_num = A.instantiate( - typeArguments: [numNone], - nullabilitySuffix: NullabilitySuffix.none, - ); - isSubtype(A_num, A_num, strT0: "A", strT1: "A"); + isNotSubtype(A_int, A_num, strT0: "A", strT1: "A"); } test_interfaceType_invariant() {