[analyzer] Clean up casting for variance and related tests.

Change-Id: Ieb73919e41ee500c29c17d64f6b4525978378a00
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125141
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
This commit is contained in:
Kallen Tu 2019-11-13 19:26:41 +00:00 committed by commit-bot@chromium.org
parent 39194779c1
commit df2093362d
3 changed files with 29 additions and 55 deletions

View file

@ -2142,14 +2142,13 @@ class AstBuilder extends StackListener {
// Peek to leave type parameters on top of stack.
List<TypeParameter> 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

View file

@ -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<DartType>(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.');
}
}

View file

@ -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<int>", strT1: "A<num>");
isSubtype(A_num, A_num, strT0: "A<num>", strT1: "A<num>");
isNotSubtype(A_num, A_int, strT0: "A<num>", strT1: "A<int>");
}
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<num>", strT1: "A<num>");
}
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<num>", strT1: "A<int>");
isNotSubtype(A_int, A_num, strT0: "A<int>", strT1: "A<num>");
}
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<num>", strT1: "A<num>");
isNotSubtype(A_int, A_num, strT0: "A<int>", strT1: "A<num>");
}
test_interfaceType_invariant() {