Skip to content

Commit 8a28378

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[cfe] Fix value of the top type in least closure of a type schema
Previously, the algorithm for the least closure was abstracted over the top type. However, all call sites only pass `Object?` as the top type, and this CL fixes that value, which is expected in null-safe Dart. Change-Id: I67cea1ccf910e96d4441dd0fbee9579f5cf22155 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400080 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent dbee089 commit 8a28378

File tree

5 files changed

+12
-13
lines changed

5 files changed

+12
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mixin TypeSchemaStandardBounds on StandardBounds {
2121
DartType leastClosureForUpperBound(DartType typeSchema) {
2222
// - We replace all uses of `T1 <: T2` in the `UP` algorithm by `S1 <: S2`
2323
// where `Si` is the least closure of `Ti` with respect to `_`.
24-
return leastClosure(typeSchema, topType: coreTypes.objectNullableRawType);
24+
return leastClosure(typeSchema, coreTypes: coreTypes);
2525
}
2626

2727
@override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:kernel/ast.dart';
6+
import 'package:kernel/core_types.dart';
67
import 'package:kernel/src/replacement_visitor.dart';
78

89
import 'type_schema.dart' show UnknownType;
@@ -38,9 +39,9 @@ DartType greatestClosure(DartType schema, {required DartType topType}) {
3839
///
3940
/// Note that the least closure of a type schema is always a subtype of any type
4041
/// which matches the schema.
41-
DartType leastClosure(DartType schema, {required DartType topType}) {
42+
DartType leastClosure(DartType schema, {required CoreTypes coreTypes}) {
4243
return _TypeSchemaEliminationVisitor.run(schema,
43-
computeLeastClosure: true, topType: topType);
44+
computeLeastClosure: true, topType: coreTypes.objectNullableRawType);
4445
}
4546

4647
/// Visitor that computes least and greatest closures of a type schema.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ class TypeSchemaEnvironment extends HierarchyBasedTypeEnvironment
409409
if (constraint.lower is! SharedUnknownTypeSchemaView<DartType>) {
410410
return grounded
411411
? leastClosure(constraint.lower.unwrapTypeSchemaView(),
412-
topType: topType)
412+
coreTypes: coreTypes)
413413
: constraint.lower.unwrapTypeSchemaView();
414414
} else if (constraint.upper is! UnknownType) {
415415
return grounded
@@ -440,7 +440,7 @@ class TypeSchemaEnvironment extends HierarchyBasedTypeEnvironment
440440
} else if (constraint.lower is! UnknownType) {
441441
return grounded
442442
? leastClosure(constraint.lower.unwrapTypeSchemaView(),
443-
topType: topType)
443+
coreTypes: coreTypes)
444444
:
445445
// Coverage-ignore(suite): Not run.
446446
constraint.lower.unwrapTypeSchemaView();

pkg/front_end/test/type_inference/type_schema_elimination_nnbd_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class TypeSchemaEliminationTest {
2929
}
3030

3131
DartType leastClosure(DartType schema) {
32-
return typeSchemaElimination.leastClosure(schema,
33-
topType: env.coreTypes.objectNullableRawType);
32+
return typeSchemaElimination.leastClosure(schema, coreTypes: env.coreTypes);
3433
}
3534

3635
void testGreatest(String type, String expectedClosure) {

pkg/front_end/test/type_inference/type_schema_elimination_test.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class TypeSchemaEliminationTest {
2929
}
3030

3131
DartType leastClosure(DartType schema) {
32-
return typeSchemaElimination.leastClosure(schema,
33-
topType: new DynamicType());
32+
return typeSchemaElimination.leastClosure(schema, coreTypes: env.coreTypes);
3433
}
3534

3635
void testGreatest(String type, String expectedClosure) {
@@ -69,13 +68,13 @@ class TypeSchemaEliminationTest {
6968
}
7069

7170
void test_leastClosure_contravariant() {
72-
testLeast("(UNKNOWN) ->* dynamic", "(dynamic) ->* dynamic");
73-
testLeast("({UNKNOWN foo}) ->* dynamic", "({dynamic foo}) ->* dynamic");
71+
testLeast("(UNKNOWN) ->* dynamic", "(Object?) ->* dynamic");
72+
testLeast("({UNKNOWN foo}) ->* dynamic", "({Object? foo}) ->* dynamic");
7473
}
7574

7675
void test_leastClosure_contravariant_contravariant() {
7776
testLeast("((UNKNOWN) ->* UNKNOWN) ->* dynamic",
78-
"((Never) ->* dynamic) ->* dynamic");
77+
"((Never) ->* Object?) ->* dynamic");
7978
}
8079

8180
void test_leastClosure_covariant() {
@@ -85,7 +84,7 @@ class TypeSchemaEliminationTest {
8584

8685
void test_leastClosure_function_multipleUnknown() {
8786
testLeast("(UNKNOWN, UNKNOWN, {UNKNOWN a, UNKNOWN b}) ->* UNKNOWN",
88-
"(dynamic, dynamic, {dynamic a, dynamic b}) ->* Never");
87+
"(Object?, Object?, {Object? a, Object? b}) ->* Never");
8988
}
9089

9190
void test_leastClosure_simple() {

0 commit comments

Comments
 (0)