Skip to content

Commit

Permalink
[front end] Unify methods for making a type nullable.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Dec 3, 2024
1 parent 1baa8ca commit 3a67a7d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pkg/front_end/lib/src/type_inference/inference_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6824,8 +6824,8 @@ class InferenceVisitorImpl extends InferenceVisitorBase
instrumentation!.record(uriForInstrumentation, fileOffset, 'target',
new InstrumentationValueForMember(equalsTarget.member!));
}
DartType rightType =
operations.getNullableType(equalsTarget.getBinaryOperandType(this));
DartType rightType = operations
.makeNullableInternal(equalsTarget.getBinaryOperandType(this));
DartType contextType =
rightType.withDeclaredNullability(Nullability.nullable);
rightResult = ensureAssignableResult(contextType, rightResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,8 @@ abstract class InferenceVisitorBase implements InferenceVisitor {
return greatestClosure(type, topType: coreTypes.objectNullableRawType);
}

DartType computeNullable(DartType type) {
if (type is NullType || type is NeverType) {
return const NullType();
}
return cfeOperations.getNullableType(type);
}
DartType computeNullable(DartType type) =>
cfeOperations.makeNullableInternal(type);

// Coverage-ignore(suite): Not run.
Expression createReachabilityError(int fileOffset, Message errorMessage) {
Expand Down
33 changes: 16 additions & 17 deletions pkg/front_end/lib/src/type_inference/type_inference_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -636,22 +636,6 @@ class OperationsCfe
return new SharedTypeView(result);
}

DartType getNullableType(DartType type) {
// Note that the [IntersectionType.withDeclaredNullability] is special so
// we don't trust it.
if (type.declaredNullability == Nullability.nullable &&
type is! IntersectionType) {
return type;
}
DartType? cached = typeCacheNullable[type];
if (cached != null) {
return cached;
}
DartType result = type.withDeclaredNullability(Nullability.nullable);
typeCacheNullable[type] = result;
return result;
}

@override
SharedTypeView<DartType> variableType(VariableDeclaration variable) {
if (variable is VariableDeclarationImpl) {
Expand Down Expand Up @@ -763,7 +747,22 @@ class OperationsCfe

@override
DartType makeNullableInternal(DartType type) {
return type.withDeclaredNullability(Nullability.nullable);
if (type is NullType || type is NeverType) {
return const NullType();
}
// Note that the [IntersectionType.withDeclaredNullability] is special so
// we don't trust it.
if (type.declaredNullability == Nullability.nullable &&
type is! IntersectionType) {
return type;
}
DartType? cached = typeCacheNullable[type];
if (cached != null) {
return cached;
}
DartType result = type.withDeclaredNullability(Nullability.nullable);
typeCacheNullable[type] = result;
return result;
}

@override
Expand Down

0 comments on commit 3a67a7d

Please sign in to comment.