Skip to content

Commit 3a67a7d

Browse files
stereotype441Commit Queue
authored andcommitted
[front end] Unify methods for making a type nullable.
Previously, there were three methods with slightly different behaviors: - `OperationsCfe.makeNullableInternal`, which simply added `?` to the type. - `OperationsCfe.getNullableType`, which contained special logic for intersection types, and also maintained a cache to minimize the number of type objects that needed to be allocated. - `InferenceVisitorBase.computeNullable`, which also coerced `Never?` to `Null`. With this change, there is just one core implementation, `OperationsCfe.makeNullableInternal`, with all of these behaviors, and then `InferenceVisitorBase.computeNullable` is a convenience method that calls it. Change-Id: I7b6a442d0f3ba66e89d39c50cf240f021344e4aa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398420 Commit-Queue: Chloe Stefantsova <[email protected]> Auto-Submit: Paul Berry <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 1baa8ca commit 3a67a7d

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6824,8 +6824,8 @@ class InferenceVisitorImpl extends InferenceVisitorBase
68246824
instrumentation!.record(uriForInstrumentation, fileOffset, 'target',
68256825
new InstrumentationValueForMember(equalsTarget.member!));
68266826
}
6827-
DartType rightType =
6828-
operations.getNullableType(equalsTarget.getBinaryOperandType(this));
6827+
DartType rightType = operations
6828+
.makeNullableInternal(equalsTarget.getBinaryOperandType(this));
68296829
DartType contextType =
68306830
rightType.withDeclaredNullability(Nullability.nullable);
68316831
rightResult = ensureAssignableResult(contextType, rightResult,

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,8 @@ abstract class InferenceVisitorBase implements InferenceVisitor {
194194
return greatestClosure(type, topType: coreTypes.objectNullableRawType);
195195
}
196196

197-
DartType computeNullable(DartType type) {
198-
if (type is NullType || type is NeverType) {
199-
return const NullType();
200-
}
201-
return cfeOperations.getNullableType(type);
202-
}
197+
DartType computeNullable(DartType type) =>
198+
cfeOperations.makeNullableInternal(type);
203199

204200
// Coverage-ignore(suite): Not run.
205201
Expression createReachabilityError(int fileOffset, Message errorMessage) {

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -636,22 +636,6 @@ class OperationsCfe
636636
return new SharedTypeView(result);
637637
}
638638

639-
DartType getNullableType(DartType type) {
640-
// Note that the [IntersectionType.withDeclaredNullability] is special so
641-
// we don't trust it.
642-
if (type.declaredNullability == Nullability.nullable &&
643-
type is! IntersectionType) {
644-
return type;
645-
}
646-
DartType? cached = typeCacheNullable[type];
647-
if (cached != null) {
648-
return cached;
649-
}
650-
DartType result = type.withDeclaredNullability(Nullability.nullable);
651-
typeCacheNullable[type] = result;
652-
return result;
653-
}
654-
655639
@override
656640
SharedTypeView<DartType> variableType(VariableDeclaration variable) {
657641
if (variable is VariableDeclarationImpl) {
@@ -763,7 +747,22 @@ class OperationsCfe
763747

764748
@override
765749
DartType makeNullableInternal(DartType type) {
766-
return type.withDeclaredNullability(Nullability.nullable);
750+
if (type is NullType || type is NeverType) {
751+
return const NullType();
752+
}
753+
// Note that the [IntersectionType.withDeclaredNullability] is special so
754+
// we don't trust it.
755+
if (type.declaredNullability == Nullability.nullable &&
756+
type is! IntersectionType) {
757+
return type;
758+
}
759+
DartType? cached = typeCacheNullable[type];
760+
if (cached != null) {
761+
return cached;
762+
}
763+
DartType result = type.withDeclaredNullability(Nullability.nullable);
764+
typeCacheNullable[type] = result;
765+
return result;
767766
}
768767

769768
@override

0 commit comments

Comments
 (0)