Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1522)

Unified Diff: pkg/analyzer/lib/src/dart/element/type.dart

Issue 2954523002: fix #27259, implement covariance checking for strong mode and DDC (Closed)
Patch Set: rebase Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/dart/element/type.dart
diff --git a/pkg/analyzer/lib/src/dart/element/type.dart b/pkg/analyzer/lib/src/dart/element/type.dart
index 5462ac6010094e38988f3a7404b4ceb26d12e95f..7444744eaa1070659ed6b0620da8d8f92a00a4e2 100644
--- a/pkg/analyzer/lib/src/dart/element/type.dart
+++ b/pkg/analyzer/lib/src/dart/element/type.dart
@@ -718,7 +718,7 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
// variables, and see if the result is equal.
if (typeFormals.isNotEmpty) {
List<DartType> freshVariables =
- relateTypeFormals(this, object, (t, s) => t == s);
+ relateTypeFormals(this, object, (t, s, _, __) => t == s);
if (freshVariables == null) {
return false;
}
@@ -895,7 +895,7 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
return relate(
this,
type,
- (DartType t, DartType s, _, __) =>
+ (DartType t, DartType s) =>
(t as TypeImpl).isMoreSpecificThan(s, withDynamic),
new TypeSystemImpl(null).instantiateToBounds);
}
@@ -906,7 +906,7 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
return relate(
typeSystem.instantiateToBounds(this),
typeSystem.instantiateToBounds(type),
- (DartType t, DartType s, _, __) => t.isAssignableTo(s),
+ (DartType t, DartType s) => t.isAssignableTo(s),
typeSystem.instantiateToBounds);
}
@@ -1076,16 +1076,16 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
* structural rules for handling optional parameters and arity, but use their
* own relation for comparing corresponding parameters or return types.
*
- * If [returnRelation] is omitted, uses [parameterRelation] for both.
+ * If [parameterRelation] is omitted, uses [returnRelation] for both. This
+ * is convenient for Dart 1 type system methods.
*/
static bool relate(
FunctionType t,
DartType other,
- bool parameterRelation(
- DartType t, DartType s, bool tIsCovariant, bool sIsCovariant),
+ bool returnRelation(DartType t, DartType s),
DartType instantiateToBounds(DartType t),
- {bool returnRelation(DartType t, DartType s)}) {
- returnRelation ??= (t, s) => parameterRelation(t, s, false, false);
+ {bool parameterRelation(ParameterElement t, ParameterElement s)}) {
+ parameterRelation ??= (t, s) => returnRelation(t.type, s.type);
// Trivial base cases.
if (other == null) {
@@ -1102,7 +1102,8 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
// This type cast is safe, because we checked it above.
FunctionType s = other as FunctionType;
if (t.typeFormals.isNotEmpty) {
- List<DartType> freshVariables = relateTypeFormals(t, s, returnRelation);
+ List<DartType> freshVariables =
+ relateTypeFormals(t, s, (s, t, _, __) => returnRelation(s, t));
if (freshVariables == null) {
return false;
}
@@ -1119,14 +1120,28 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
}
// Test the parameter types.
+ return relateParameters(t.parameters, s.parameters, parameterRelation);
+ }
+ /**
+ * Compares two function types [t] and [s] to see if their corresponding
Leaf 2017/06/28 18:12:30 Nit: [t] and [s] aren't really parameters to this
Jennifer Messerly 2017/07/05 20:11:21 Done.
+ * parameter types match [parameterRelation].
+ *
+ * Used for the various relations on function types which have the same
+ * structural rules for handling optional parameters and arity, but use their
+ * own relation for comparing corresponding parameter types.
+ */
+ static bool relateParameters(
+ List<ParameterElement> tParams,
+ List<ParameterElement> sParams,
+ bool parameterRelation(ParameterElement p1, ParameterElement p2)) {
// TODO(jmesserly): this could be implemented with less allocation if we
// wanted, by taking advantage of the fact that positional arguments must
// appear before named ones.
var tRequired = <ParameterElement>[];
var tOptional = <ParameterElement>[];
var tNamed = <String, ParameterElement>{};
- for (var p in t.parameters) {
+ for (var p in tParams) {
var kind = p.parameterKind;
if (kind == ParameterKind.REQUIRED) {
tRequired.add(p);
@@ -1141,7 +1156,7 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
var sRequired = <ParameterElement>[];
var sOptional = <ParameterElement>[];
var sNamed = <String, ParameterElement>{};
- for (var p in s.parameters) {
+ for (var p in sParams) {
var kind = p.parameterKind;
if (kind == ParameterKind.REQUIRED) {
sRequired.add(p);
@@ -1174,8 +1189,7 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
return false;
}
var sParam = sNamed[key];
- if (!parameterRelation(
- tParam.type, sParam.type, tParam.isCovariant, sParam.isCovariant)) {
+ if (!parameterRelation(tParam, sParam)) {
return false;
}
}
@@ -1204,10 +1218,7 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
}
for (int i = 0; i < sPositional.length; i++) {
- var tParam = tPositional[i];
- var sParam = sPositional[i];
- if (!parameterRelation(
- tParam.type, sParam.type, tParam.isCovariant, sParam.isCovariant)) {
+ if (!parameterRelation(tPositional[i], sPositional[i])) {
return false;
}
}
@@ -1227,7 +1238,10 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
* `F` to get `F -> F` and `F -> F`, which we can see are equal.
*/
static List<DartType> relateTypeFormals(
- FunctionType f1, FunctionType f2, bool relation(DartType t, DartType s)) {
+ FunctionType f1,
+ FunctionType f2,
+ bool relation(DartType bound2, DartType bound1,
+ TypeParameterElement formal2, TypeParameterElement formal1)) {
List<TypeParameterElement> params1 = f1.typeFormals;
List<TypeParameterElement> params2 = f2.typeFormals;
int count = params1.length;
@@ -1258,7 +1272,7 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
bound1 = bound1.substitute2(variablesFresh, variables1);
bound2 = bound2.substitute2(variablesFresh, variables2);
pFresh.bound = bound2;
- if (!relation(bound2, bound1)) {
+ if (!relation(bound2, bound1, p2, p1)) {
return null;
}
}
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/resolver.dart » ('j') | pkg/analyzer/lib/src/generated/type_system.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698