[cfe][analyzer] Change performSubtypeConstraintGenerationForFunctionTypes to return a `bool`.
Previously, this method returned `bool?`, however the only two
possible return values were `true` and `null`.
Change-Id: Ib111d6b3da87f8d04de884635fbec9ec7046dd8d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/388860
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Auto-Submit: Paul Berry <paulberry@google.com>
diff --git a/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart b/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart
index 1f3ad2b..fb7fcf3 100644
--- a/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart
+++ b/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart
@@ -903,14 +903,14 @@
/// If [p] and [q] are both non-generic function types, and [p] is a subtype
/// of [q] under some constraints, the constraints making the relation
/// possible are recorded, and `true` is returned. Otherwise, the constraint
- /// state is unchanged (or rolled back using [restoreState]), and `null` is
+ /// state is unchanged (or rolled back using [restoreState]), and `false` is
/// returned.
///
/// An invariant of the type inference is that only [p] or [q] may be a
/// schema (in other words, may contain the unknown type `_`); the other must
/// be simply a type. If [leftSchema] is `true`, [p] may contain `_`; if it is
/// `false`, [q] may contain `_`.
- bool? performSubtypeConstraintGenerationForFunctionTypes(
+ bool performSubtypeConstraintGenerationForFunctionTypes(
TypeStructure p, TypeStructure q,
{required bool leftSchema, required AstNode? astNodeForTesting}) {
if (p is SharedFunctionTypeStructure<TypeStructure, TypeParameterStructure,
@@ -938,14 +938,14 @@
if (!performSubtypeConstraintGenerationInternal(
p.returnType, q.returnType,
leftSchema: leftSchema, astNodeForTesting: astNodeForTesting)) {
- return null;
+ return false;
}
for (int i = 0; i < q.positionalParameterTypes.length; ++i) {
if (!performSubtypeConstraintGenerationInternal(
q.positionalParameterTypes[i], p.positionalParameterTypes[i],
leftSchema: !leftSchema, astNodeForTesting: astNodeForTesting)) {
restoreState(state);
- return null;
+ return false;
}
}
return true;
@@ -967,14 +967,14 @@
if (!performSubtypeConstraintGenerationInternal(
p.returnType, q.returnType,
leftSchema: leftSchema, astNodeForTesting: astNodeForTesting)) {
- return null;
+ return false;
}
for (int i = 0; i < p.positionalParameterTypes.length; ++i) {
if (!performSubtypeConstraintGenerationInternal(
q.positionalParameterTypes[i], p.positionalParameterTypes[i],
leftSchema: !leftSchema, astNodeForTesting: astNodeForTesting)) {
restoreState(state);
- return null;
+ return false;
}
}
// Consume parameter names from p and q in order. Since the named
@@ -1011,13 +1011,13 @@
if (comparisonResult > 0) {
// Extra parameter in q that q that doesn't exist in p. No match.
restoreState(state);
- return null;
+ return false;
} else if (comparisonResult < 0) {
// Extra parameter in p that doesn't exist in q. Ok if not
// required.
if (p.sortedNamedParameters[i].isRequired) {
restoreState(state);
- return null;
+ return false;
} else {
i++;
}
@@ -1029,7 +1029,7 @@
leftSchema: !leftSchema,
astNodeForTesting: astNodeForTesting)) {
restoreState(state);
- return null;
+ return false;
}
i++;
j++;
@@ -1038,7 +1038,7 @@
}
}
- return null;
+ return false;
}
/// Matches [p] against [q].
diff --git a/pkg/_fe_analyzer_shared/test/type_inference/type_constraint_gatherer_test.dart b/pkg/_fe_analyzer_shared/test/type_inference/type_constraint_gatherer_test.dart
index c860637..dbcfb0f 100644
--- a/pkg/_fe_analyzer_shared/test/type_inference/type_constraint_gatherer_test.dart
+++ b/pkg/_fe_analyzer_shared/test/type_inference/type_constraint_gatherer_test.dart
@@ -62,7 +62,7 @@
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
Type('int Function(int)'), Type('String Function(int)'),
leftSchema: false, astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
@@ -71,7 +71,7 @@
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
Type('void Function(int)'), Type('void Function(String)'),
leftSchema: false, astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
@@ -80,7 +80,7 @@
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
Type('void Function()'), Type('void Function([int])'),
leftSchema: false, astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
@@ -89,7 +89,7 @@
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
Type('void Function(int)'), Type('void Function([int])'),
leftSchema: false, astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
});
@@ -146,7 +146,7 @@
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
Type('int Function({int x})'), Type('String Function({int x})'),
leftSchema: false, astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
@@ -157,7 +157,7 @@
Type('void Function({String x})'),
leftSchema: false,
astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
@@ -168,7 +168,7 @@
Type('void Function()'),
leftSchema: false,
astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
@@ -177,7 +177,7 @@
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
Type('void Function()'), Type('void Function({int x})'),
leftSchema: false, astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
@@ -189,7 +189,7 @@
Type('void Function({int z})'),
leftSchema: false,
astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
});
@@ -215,7 +215,7 @@
Type('void Function(int, [String])'),
leftSchema: false,
astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
@@ -226,7 +226,7 @@
Type('void Function(int, String)'),
leftSchema: false,
astNodeForTesting: Node.placeholder()))
- .equals(null);
+ .isFalse();
check(tcg._constraints).isEmpty();
});
});
diff --git a/pkg/analyzer/lib/src/dart/element/type_constraint_gatherer.dart b/pkg/analyzer/lib/src/dart/element/type_constraint_gatherer.dart
index 23b3a8e..9655ad0 100644
--- a/pkg/analyzer/lib/src/dart/element/type_constraint_gatherer.dart
+++ b/pkg/analyzer/lib/src/dart/element/type_constraint_gatherer.dart
@@ -387,7 +387,8 @@
}
if (P_typeFormals.isEmpty && Q_typeFormals.isEmpty) {
- return _functionType0(P, Q, leftSchema, nodeForTesting: nodeForTesting);
+ return performSubtypeConstraintGenerationForFunctionTypes(P, Q,
+ leftSchema: leftSchema, astNodeForTesting: nodeForTesting);
}
// We match two generic function types:
@@ -438,8 +439,9 @@
.toList();
var P_instantiated = P.instantiate(typeArguments);
var Q_instantiated = Q.instantiate(typeArguments);
- if (!_functionType0(P_instantiated, Q_instantiated, leftSchema,
- nodeForTesting: nodeForTesting)) {
+ if (!performSubtypeConstraintGenerationForFunctionTypes(
+ P_instantiated, Q_instantiated,
+ leftSchema: leftSchema, astNodeForTesting: nodeForTesting)) {
_constraints.length = rewind;
return false;
}
@@ -452,26 +454,6 @@
return true;
}
- /// Matches [P] against [Q], where [P] and [Q] are both non-generic function
- /// types.
- ///
- /// If [P] is a subtype of [Q] under some constraints, the constraints making
- /// the relation possible are recorded to [_constraints], and `true` is
- /// returned. Otherwise, [_constraints] is left unchanged (or rolled back),
- /// and `false` is returned.
- bool _functionType0(FunctionType f, FunctionType g, bool leftSchema,
- {required AstNode? nodeForTesting}) {
- // A function type `(M0,..., Mn, [M{n+1}, ..., Mm]) -> R0` is a subtype
- // match for a function type `(N0,..., Nk, [N{k+1}, ..., Nr]) -> R1` with
- // respect to `L` under constraints `C0 + ... + Cr + C`.
- bool? result = performSubtypeConstraintGenerationForFunctionTypes(f, g,
- leftSchema: leftSchema, astNodeForTesting: nodeForTesting);
- if (result != null) {
- return result;
- }
- return false;
- }
-
/// Matches [P] against [Q], where [P] and [Q] are both record types.
///
/// If [P] is a subtype of [Q] under some constraints, the constraints making
diff --git a/pkg/front_end/lib/src/type_inference/type_constraint_gatherer.dart b/pkg/front_end/lib/src/type_inference/type_constraint_gatherer.dart
index b031c12..320b39e 100644
--- a/pkg/front_end/lib/src/type_inference/type_constraint_gatherer.dart
+++ b/pkg/front_end/lib/src/type_inference/type_constraint_gatherer.dart
@@ -448,12 +448,10 @@
return true;
}
- constraintGenerationResult =
- performSubtypeConstraintGenerationForFunctionTypes(p, q,
- leftSchema: constrainSupertype,
- astNodeForTesting: treeNodeForTesting);
- if (constraintGenerationResult != null) {
- return constraintGenerationResult;
+ if (performSubtypeConstraintGenerationForFunctionTypes(p, q,
+ leftSchema: constrainSupertype,
+ astNodeForTesting: treeNodeForTesting)) {
+ return true;
}
// A generic function type <T0 extends B00, ..., Tn extends B0n>F0 is a