Skip to content

Commit a1c8054

Browse files
stereotype441Commit Queue
authored andcommitted
[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 <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]> Auto-Submit: Paul Berry <[email protected]>
1 parent 3735413 commit a1c8054

File tree

4 files changed

+30
-50
lines changed

4 files changed

+30
-50
lines changed

pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -903,14 +903,14 @@ abstract class TypeConstraintGenerator<
903903
/// If [p] and [q] are both non-generic function types, and [p] is a subtype
904904
/// of [q] under some constraints, the constraints making the relation
905905
/// possible are recorded, and `true` is returned. Otherwise, the constraint
906-
/// state is unchanged (or rolled back using [restoreState]), and `null` is
906+
/// state is unchanged (or rolled back using [restoreState]), and `false` is
907907
/// returned.
908908
///
909909
/// An invariant of the type inference is that only [p] or [q] may be a
910910
/// schema (in other words, may contain the unknown type `_`); the other must
911911
/// be simply a type. If [leftSchema] is `true`, [p] may contain `_`; if it is
912912
/// `false`, [q] may contain `_`.
913-
bool? performSubtypeConstraintGenerationForFunctionTypes(
913+
bool performSubtypeConstraintGenerationForFunctionTypes(
914914
TypeStructure p, TypeStructure q,
915915
{required bool leftSchema, required AstNode? astNodeForTesting}) {
916916
if (p is SharedFunctionTypeStructure<TypeStructure, TypeParameterStructure,
@@ -938,14 +938,14 @@ abstract class TypeConstraintGenerator<
938938
if (!performSubtypeConstraintGenerationInternal(
939939
p.returnType, q.returnType,
940940
leftSchema: leftSchema, astNodeForTesting: astNodeForTesting)) {
941-
return null;
941+
return false;
942942
}
943943
for (int i = 0; i < q.positionalParameterTypes.length; ++i) {
944944
if (!performSubtypeConstraintGenerationInternal(
945945
q.positionalParameterTypes[i], p.positionalParameterTypes[i],
946946
leftSchema: !leftSchema, astNodeForTesting: astNodeForTesting)) {
947947
restoreState(state);
948-
return null;
948+
return false;
949949
}
950950
}
951951
return true;
@@ -967,14 +967,14 @@ abstract class TypeConstraintGenerator<
967967
if (!performSubtypeConstraintGenerationInternal(
968968
p.returnType, q.returnType,
969969
leftSchema: leftSchema, astNodeForTesting: astNodeForTesting)) {
970-
return null;
970+
return false;
971971
}
972972
for (int i = 0; i < p.positionalParameterTypes.length; ++i) {
973973
if (!performSubtypeConstraintGenerationInternal(
974974
q.positionalParameterTypes[i], p.positionalParameterTypes[i],
975975
leftSchema: !leftSchema, astNodeForTesting: astNodeForTesting)) {
976976
restoreState(state);
977-
return null;
977+
return false;
978978
}
979979
}
980980
// Consume parameter names from p and q in order. Since the named
@@ -1011,13 +1011,13 @@ abstract class TypeConstraintGenerator<
10111011
if (comparisonResult > 0) {
10121012
// Extra parameter in q that q that doesn't exist in p. No match.
10131013
restoreState(state);
1014-
return null;
1014+
return false;
10151015
} else if (comparisonResult < 0) {
10161016
// Extra parameter in p that doesn't exist in q. Ok if not
10171017
// required.
10181018
if (p.sortedNamedParameters[i].isRequired) {
10191019
restoreState(state);
1020-
return null;
1020+
return false;
10211021
} else {
10221022
i++;
10231023
}
@@ -1029,7 +1029,7 @@ abstract class TypeConstraintGenerator<
10291029
leftSchema: !leftSchema,
10301030
astNodeForTesting: astNodeForTesting)) {
10311031
restoreState(state);
1032-
return null;
1032+
return false;
10331033
}
10341034
i++;
10351035
j++;
@@ -1038,7 +1038,7 @@ abstract class TypeConstraintGenerator<
10381038
}
10391039
}
10401040

1041-
return null;
1041+
return false;
10421042
}
10431043

10441044
/// Matches [p] against [q].

pkg/_fe_analyzer_shared/test/type_inference/type_constraint_gatherer_test.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ main() {
6262
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
6363
Type('int Function(int)'), Type('String Function(int)'),
6464
leftSchema: false, astNodeForTesting: Node.placeholder()))
65-
.equals(null);
65+
.isFalse();
6666
check(tcg._constraints).isEmpty();
6767
});
6868

@@ -71,7 +71,7 @@ main() {
7171
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
7272
Type('void Function(int)'), Type('void Function(String)'),
7373
leftSchema: false, astNodeForTesting: Node.placeholder()))
74-
.equals(null);
74+
.isFalse();
7575
check(tcg._constraints).isEmpty();
7676
});
7777

@@ -80,7 +80,7 @@ main() {
8080
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
8181
Type('void Function()'), Type('void Function([int])'),
8282
leftSchema: false, astNodeForTesting: Node.placeholder()))
83-
.equals(null);
83+
.isFalse();
8484
check(tcg._constraints).isEmpty();
8585
});
8686

@@ -89,7 +89,7 @@ main() {
8989
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
9090
Type('void Function(int)'), Type('void Function([int])'),
9191
leftSchema: false, astNodeForTesting: Node.placeholder()))
92-
.equals(null);
92+
.isFalse();
9393
check(tcg._constraints).isEmpty();
9494
});
9595
});
@@ -146,7 +146,7 @@ main() {
146146
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
147147
Type('int Function({int x})'), Type('String Function({int x})'),
148148
leftSchema: false, astNodeForTesting: Node.placeholder()))
149-
.equals(null);
149+
.isFalse();
150150
check(tcg._constraints).isEmpty();
151151
});
152152

@@ -157,7 +157,7 @@ main() {
157157
Type('void Function({String x})'),
158158
leftSchema: false,
159159
astNodeForTesting: Node.placeholder()))
160-
.equals(null);
160+
.isFalse();
161161
check(tcg._constraints).isEmpty();
162162
});
163163

@@ -168,7 +168,7 @@ main() {
168168
Type('void Function()'),
169169
leftSchema: false,
170170
astNodeForTesting: Node.placeholder()))
171-
.equals(null);
171+
.isFalse();
172172
check(tcg._constraints).isEmpty();
173173
});
174174

@@ -177,7 +177,7 @@ main() {
177177
check(tcg.performSubtypeConstraintGenerationForFunctionTypes(
178178
Type('void Function()'), Type('void Function({int x})'),
179179
leftSchema: false, astNodeForTesting: Node.placeholder()))
180-
.equals(null);
180+
.isFalse();
181181
check(tcg._constraints).isEmpty();
182182
});
183183

@@ -189,7 +189,7 @@ main() {
189189
Type('void Function({int z})'),
190190
leftSchema: false,
191191
astNodeForTesting: Node.placeholder()))
192-
.equals(null);
192+
.isFalse();
193193
check(tcg._constraints).isEmpty();
194194
});
195195
});
@@ -215,7 +215,7 @@ main() {
215215
Type('void Function(int, [String])'),
216216
leftSchema: false,
217217
astNodeForTesting: Node.placeholder()))
218-
.equals(null);
218+
.isFalse();
219219
check(tcg._constraints).isEmpty();
220220
});
221221

@@ -226,7 +226,7 @@ main() {
226226
Type('void Function(int, String)'),
227227
leftSchema: false,
228228
astNodeForTesting: Node.placeholder()))
229-
.equals(null);
229+
.isFalse();
230230
check(tcg._constraints).isEmpty();
231231
});
232232
});

pkg/analyzer/lib/src/dart/element/type_constraint_gatherer.dart

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
387387
}
388388

389389
if (P_typeFormals.isEmpty && Q_typeFormals.isEmpty) {
390-
return _functionType0(P, Q, leftSchema, nodeForTesting: nodeForTesting);
390+
return performSubtypeConstraintGenerationForFunctionTypes(P, Q,
391+
leftSchema: leftSchema, astNodeForTesting: nodeForTesting);
391392
}
392393

393394
// We match two generic function types:
@@ -438,8 +439,9 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
438439
.toList();
439440
var P_instantiated = P.instantiate(typeArguments);
440441
var Q_instantiated = Q.instantiate(typeArguments);
441-
if (!_functionType0(P_instantiated, Q_instantiated, leftSchema,
442-
nodeForTesting: nodeForTesting)) {
442+
if (!performSubtypeConstraintGenerationForFunctionTypes(
443+
P_instantiated, Q_instantiated,
444+
leftSchema: leftSchema, astNodeForTesting: nodeForTesting)) {
443445
_constraints.length = rewind;
444446
return false;
445447
}
@@ -452,26 +454,6 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
452454
return true;
453455
}
454456

455-
/// Matches [P] against [Q], where [P] and [Q] are both non-generic function
456-
/// types.
457-
///
458-
/// If [P] is a subtype of [Q] under some constraints, the constraints making
459-
/// the relation possible are recorded to [_constraints], and `true` is
460-
/// returned. Otherwise, [_constraints] is left unchanged (or rolled back),
461-
/// and `false` is returned.
462-
bool _functionType0(FunctionType f, FunctionType g, bool leftSchema,
463-
{required AstNode? nodeForTesting}) {
464-
// A function type `(M0,..., Mn, [M{n+1}, ..., Mm]) -> R0` is a subtype
465-
// match for a function type `(N0,..., Nk, [N{k+1}, ..., Nr]) -> R1` with
466-
// respect to `L` under constraints `C0 + ... + Cr + C`.
467-
bool? result = performSubtypeConstraintGenerationForFunctionTypes(f, g,
468-
leftSchema: leftSchema, astNodeForTesting: nodeForTesting);
469-
if (result != null) {
470-
return result;
471-
}
472-
return false;
473-
}
474-
475457
/// Matches [P] against [Q], where [P] and [Q] are both record types.
476458
///
477459
/// If [P] is a subtype of [Q] under some constraints, the constraints making

pkg/front_end/lib/src/type_inference/type_constraint_gatherer.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,10 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
448448
return true;
449449
}
450450

451-
constraintGenerationResult =
452-
performSubtypeConstraintGenerationForFunctionTypes(p, q,
453-
leftSchema: constrainSupertype,
454-
astNodeForTesting: treeNodeForTesting);
455-
if (constraintGenerationResult != null) {
456-
return constraintGenerationResult;
451+
if (performSubtypeConstraintGenerationForFunctionTypes(p, q,
452+
leftSchema: constrainSupertype,
453+
astNodeForTesting: treeNodeForTesting)) {
454+
return true;
457455
}
458456

459457
// A generic function type <T0 extends B00, ..., Tn extends B0n>F0 is a

0 commit comments

Comments
 (0)