Skip to content

Commit

Permalink
[kernel] Remove CoreTypes from legacyErasure
Browse files Browse the repository at this point in the history
Change-Id: I9692c01ed3bf9982834144dd9eb477a97db5536f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175041
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
  • Loading branch information
johnniwinther authored and commit-bot@chromium.org committed Dec 4, 2020
1 parent f7ae22a commit 74be667
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 64 deletions.
3 changes: 1 addition & 2 deletions pkg/front_end/lib/src/fasta/builder/field_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ class SourceFieldBuilder extends MemberBuilderImpl implements FieldBuilder {
// `fieldType` may have changed if a circularity was detected when
// [inferredType] was computed.
if (!library.isNonNullableByDefault) {
inferredType = legacyErasure(
library.loader.typeInferenceEngine.coreTypes, inferredType);
inferredType = legacyErasure(inferredType);
}
fieldType = implicitFieldType.checkInferred(inferredType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ class ClassHierarchyNodeBuilder {
norm(hierarchy.coreTypes, inheritedType));
}
} else {
inheritedType = legacyErasure(hierarchy.coreTypes, inheritedType);
inheritedType = legacyErasure(inheritedType);
if (inferredType == null) {
return inheritedType;
} else {
Expand Down Expand Up @@ -1703,8 +1703,7 @@ class ClassHierarchyNodeBuilder {
if (!classBuilder.library.isNonNullableByDefault &&
supernode.classBuilder.library.isNonNullableByDefault) {
for (int i = 0; i < superclasses.length; i++) {
superclasses[i] =
legacyErasureSupertype(hierarchy.coreTypes, superclasses[i]);
superclasses[i] = legacyErasureSupertype(superclasses[i]);
}
}

Expand Down Expand Up @@ -2377,7 +2376,7 @@ class ClassHierarchyNodeBuilder {
Supertype type) {
if (type == null) return null;
if (!classBuilder.library.isNonNullableByDefault) {
type = legacyErasureSupertype(hierarchy.coreTypes, type);
type = legacyErasureSupertype(type);
}
ClassHierarchyNode node = hierarchy.getNodeFromClass(type.classNode);
if (node == null) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ abstract class CombinedMemberSignatureBase<T> {
"No member computed for index ${index} in ${members}");
candidateType = _computeMemberType(thisType, target);
if (!classBuilder.library.isNonNullableByDefault) {
DartType legacyErasure = rawLegacyErasure(_coreTypes, candidateType);
DartType legacyErasure = rawLegacyErasure(candidateType);
if (legacyErasure != null) {
_neededLegacyErasureIndices ??= {};
_neededLegacyErasureIndices.add(index);
Expand Down
25 changes: 10 additions & 15 deletions pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ class ConstantWeakener extends ComputeOnceConstantVisitor<Constant> {

ConstantWeakener(this._evaluator);

CoreTypes get _coreTypes => _evaluator.coreTypes;

Constant processValue(Constant node, Constant value) {
if (value != null) {
value = _evaluator.canonicalize(value);
Expand Down Expand Up @@ -207,8 +205,8 @@ class ConstantWeakener extends ComputeOnceConstantVisitor<Constant> {

@override
Constant visitMapConstant(MapConstant node) {
DartType keyType = rawLegacyErasure(_coreTypes, node.keyType);
DartType valueType = rawLegacyErasure(_coreTypes, node.valueType);
DartType keyType = rawLegacyErasure(node.keyType);
DartType valueType = rawLegacyErasure(node.valueType);
List<ConstantMapEntry> entries;
for (int index = 0; index < node.entries.length; index++) {
ConstantMapEntry entry = node.entries[index];
Expand All @@ -229,7 +227,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor<Constant> {

@override
Constant visitListConstant(ListConstant node) {
DartType typeArgument = rawLegacyErasure(_coreTypes, node.typeArgument);
DartType typeArgument = rawLegacyErasure(node.typeArgument);
List<Constant> entries;
for (int index = 0; index < node.entries.length; index++) {
Constant entry = visitConstant(node.entries[index]);
Expand All @@ -247,7 +245,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor<Constant> {

@override
Constant visitSetConstant(SetConstant node) {
DartType typeArgument = rawLegacyErasure(_coreTypes, node.typeArgument);
DartType typeArgument = rawLegacyErasure(node.typeArgument);
List<Constant> entries;
for (int index = 0; index < node.entries.length; index++) {
Constant entry = visitConstant(node.entries[index]);
Expand All @@ -267,8 +265,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor<Constant> {
Constant visitInstanceConstant(InstanceConstant node) {
List<DartType> typeArguments;
for (int index = 0; index < node.typeArguments.length; index++) {
DartType typeArgument =
rawLegacyErasure(_coreTypes, node.typeArguments[index]);
DartType typeArgument = rawLegacyErasure(node.typeArguments[index]);
if (typeArgument != null) {
typeArguments ??= node.typeArguments.toList(growable: false);
typeArguments[index] = typeArgument;
Expand All @@ -294,7 +291,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor<Constant> {
PartialInstantiationConstant node) {
List<DartType> types;
for (int index = 0; index < node.types.length; index++) {
DartType type = rawLegacyErasure(_coreTypes, node.types[index]);
DartType type = rawLegacyErasure(node.types[index]);
if (type != null) {
types ??= node.types.toList(growable: false);
types[index] = type;
Expand All @@ -311,7 +308,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor<Constant> {

@override
Constant visitTypeLiteralConstant(TypeLiteralConstant node) {
DartType type = rawLegacyErasure(_coreTypes, node.type);
DartType type = rawLegacyErasure(node.type);
if (type != null) {
return new TypeLiteralConstant(type);
}
Expand Down Expand Up @@ -840,7 +837,7 @@ class ConstantEvaluator extends RecursiveVisitor<Constant> {
case EvaluationMode.agnostic:
return type;
case EvaluationMode.weak:
return legacyErasure(coreTypes, type);
return legacyErasure(type);
}
throw new UnsupportedError(
"Unexpected evaluation mode: ${evaluationMode}.");
Expand All @@ -852,9 +849,7 @@ class ConstantEvaluator extends RecursiveVisitor<Constant> {
case EvaluationMode.agnostic:
return types;
case EvaluationMode.weak:
return types
.map((DartType type) => legacyErasure(coreTypes, type))
.toList();
return types.map((DartType type) => legacyErasure(type)).toList();
}
throw new UnsupportedError(
"Unexpected evaluation mode: ${evaluationMode}.");
Expand Down Expand Up @@ -2608,7 +2603,7 @@ class ConstantEvaluator extends RecursiveVisitor<Constant> {
bool isSubtype(Constant constant, DartType type, SubtypeCheckMode mode) {
DartType constantType = constant.getType(_staticTypeContext);
if (mode == SubtypeCheckMode.ignoringNullabilities) {
constantType = rawLegacyErasure(coreTypes, constantType) ?? constantType;
constantType = rawLegacyErasure(constantType) ?? constantType;
}
bool result = typeEnvironment.isSubtypeOf(constantType, type, mode);
if (targetingJavaScript && !result) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/front_end/lib/src/fasta/kernel/implicit_field_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ class _ImplicitFieldTypeRoot extends ImplicitFieldType {
for (ImplicitFieldType overridden in _overriddenFields) {
DartType overriddenType = overridden.inferType();
if (!fieldBuilder.library.isNonNullableByDefault) {
overriddenType = legacyErasure(
fieldBuilder.library.loader.coreTypes, overriddenType);
overriddenType = legacyErasure(overriddenType);
}
if (inferredType == null) {
inferredType = overriddenType;
Expand Down Expand Up @@ -166,8 +165,7 @@ class _ImplicitFieldTypeRoot extends ImplicitFieldType {
for (ImplicitFieldType overridden in _overriddenFields) {
DartType overriddenType = overridden.inferType();
if (!fieldBuilder.library.isNonNullableByDefault) {
overriddenType = legacyErasure(
fieldBuilder.library.loader.coreTypes, overriddenType);
overriddenType = legacyErasure(overriddenType);
}
if (type != overriddenType) {
String name = fieldBuilder.fullNameForErrors;
Expand Down
10 changes: 5 additions & 5 deletions pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3837,7 +3837,7 @@ class InferenceVisitor
}

if (!inferrer.isNonNullableByDefault) {
binaryType = legacyErasure(inferrer.coreTypes, binaryType);
binaryType = legacyErasure(binaryType);
}

if (!inferrer.isTopLevel && binaryTarget.isNullable) {
Expand Down Expand Up @@ -3934,7 +3934,7 @@ class InferenceVisitor
}

if (!inferrer.isNonNullableByDefault) {
unaryType = legacyErasure(inferrer.coreTypes, unaryType);
unaryType = legacyErasure(unaryType);
}

if (!inferrer.isTopLevel && unaryTarget.isNullable) {
Expand Down Expand Up @@ -4022,7 +4022,7 @@ class InferenceVisitor
}

if (!inferrer.isNonNullableByDefault) {
readType = legacyErasure(inferrer.coreTypes, readType);
readType = legacyErasure(readType);
}

if (!inferrer.isTopLevel && readTarget.isNullable) {
Expand Down Expand Up @@ -4220,7 +4220,7 @@ class InferenceVisitor
}

if (!inferrer.isNonNullableByDefault) {
readType = legacyErasure(inferrer.coreTypes, readType);
readType = legacyErasure(readType);
}

readResult ??= new ExpressionInferenceResult(readType, read);
Expand Down Expand Up @@ -5275,7 +5275,7 @@ class InferenceVisitor
DartType type = target.getterType;

if (!inferrer.isNonNullableByDefault) {
type = legacyErasure(inferrer.coreTypes, type);
type = legacyErasure(type);
}

if (target is Procedure && target.kind == ProcedureKind.Method) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/front_end/lib/src/fasta/source/source_class_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,7 @@ class SourceClassBuilder extends ClassBuilderImpl
}
DartType computedBound = substitution.substituteType(interfaceBound);
if (!library.isNonNullableByDefault) {
computedBound =
legacyErasure(types.hierarchy.coreTypes, computedBound);
computedBound = legacyErasure(computedBound);
}
if (!types
.performNullabilityAwareMutualSubtypesCheck(
Expand Down Expand Up @@ -1305,7 +1304,7 @@ class SourceClassBuilder extends ClassBuilderImpl

if (!declaredMember.isNonNullableByDefault &&
interfaceMember.isNonNullableByDefault) {
interfaceType = legacyErasure(types.hierarchy.coreTypes, interfaceType);
interfaceType = legacyErasure(interfaceType);
}

bool inParameter = declaredParameter != null || asIfDeclaredParameter;
Expand Down
12 changes: 6 additions & 6 deletions pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,7 @@ class TypeInferrerImpl implements TypeInferrer {
typeSchemaEnvironment.inferGenericFunctionOrType(
isNonNullableByDefault
? calleeType.returnType
: legacyErasure(coreTypes, calleeType.returnType),
: legacyErasure(calleeType.returnType),
calleeTypeParameters,
null,
null,
Expand Down Expand Up @@ -2034,14 +2034,14 @@ class TypeInferrerImpl implements TypeInferrer {
arguments.positional[position],
isNonNullableByDefault
? inferredFormalType
: legacyErasure(coreTypes, inferredFormalType),
: legacyErasure(inferredFormalType),
inferenceNeeded ||
isSpecialCasedBinaryOperator ||
isSpecialCasedTernaryOperator ||
typeChecksNeeded);
inferredType = result.inferredType == null || isNonNullableByDefault
? result.inferredType
: legacyErasure(coreTypes, result.inferredType);
: legacyErasure(result.inferredType);
Expression expression =
_hoist(result.expression, inferredType, hoistedExpressions);
arguments.positional[position] = expression..parent = arguments;
Expand Down Expand Up @@ -2073,12 +2073,12 @@ class TypeInferrerImpl implements TypeInferrer {
namedArgument.value,
isNonNullableByDefault
? inferredFormalType
: legacyErasure(coreTypes, inferredFormalType),
: legacyErasure(inferredFormalType),
inferenceNeeded || isSpecialCasedBinaryOperator || typeChecksNeeded);
DartType inferredType =
result.inferredType == null || isNonNullableByDefault
? result.inferredType
: legacyErasure(coreTypes, result.inferredType);
: legacyErasure(result.inferredType);
Expression expression =
_hoist(result.expression, inferredType, hoistedExpressions);
namedArgument.value = expression..parent = namedArgument;
Expand Down Expand Up @@ -2216,7 +2216,7 @@ class TypeInferrerImpl implements TypeInferrer {
"Inferred function type: $calleeType.");

if (!isNonNullableByDefault) {
inferredType = legacyErasure(coreTypes, inferredType);
inferredType = legacyErasure(inferredType);
}

return new SuccessfulInferenceResult(inferredType);
Expand Down
8 changes: 4 additions & 4 deletions pkg/kernel/lib/class_hierarchy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,8 @@ class ClosedWorldClassHierarchy implements ClassHierarchy {
: Substitution.fromInterfaceType(type2).substituteType(
info2.genericSuperType[next.classNode].asInterfaceType);
if (!clientLibrary.isNonNullableByDefault) {
superType1 = legacyErasure(coreTypes, superType1);
superType2 = legacyErasure(coreTypes, superType2);
superType1 = legacyErasure(superType1);
superType2 = legacyErasure(superType2);
}
if (superType1 == superType2) {
candidate = superType1.withDeclaredNullability(
Expand Down Expand Up @@ -1657,7 +1657,7 @@ class _ClassInfo {
Supertype canonical = genericSuperType[cls];
if (canonical == null) {
if (!classNode.enclosingLibrary.isNonNullableByDefault) {
canonical = legacyErasureSupertype(coreTypes, type);
canonical = legacyErasureSupertype(type);
} else {
canonical = type;
}
Expand All @@ -1679,7 +1679,7 @@ class _ClassInfo {
genericSuperType[cls] = result;
}
} else {
type = legacyErasureSupertype(coreTypes, type);
type = legacyErasureSupertype(type);
if (type != canonical) {
onAmbiguousSupertypes(classNode, canonical, type);
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kernel/lib/src/bounds_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ List<TypeArgumentIssue> findTypeArgumentIssues(
} else if (variables[i].bound is! InvalidType) {
DartType bound = substitute(variables[i].bound, substitutionMap);
if (!library.isNonNullableByDefault) {
bound = legacyErasure(typeEnvironment.coreTypes, bound);
bound = legacyErasure(bound);
}
if (!typeEnvironment.isSubtypeOf(argument, bound, subtypeCheckMode)) {
// If the bound is InvalidType it's not checked, because an error was
Expand Down Expand Up @@ -454,7 +454,7 @@ List<TypeArgumentIssue> findTypeArgumentIssuesForInvocation(
} else if (parameters[i].bound is! InvalidType) {
DartType bound = substitute(parameters[i].bound, substitutionMap);
if (!library.isNonNullableByDefault) {
bound = legacyErasure(typeEnvironment.coreTypes, bound);
bound = legacyErasure(bound);
}
if (!typeEnvironment.isSubtypeOf(argument, bound, subtypeCheckMode)) {
result ??= <TypeArgumentIssue>[];
Expand Down
18 changes: 7 additions & 11 deletions pkg/kernel/lib/src/legacy_erasure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,36 @@
// BSD-style license that can be found in the LICENSE.md file.

import '../ast.dart' hide MapEntry;
import '../core_types.dart';

import 'replacement_visitor.dart';

/// Returns legacy erasure of [type], that is, the type in which all nnbd
/// nullabilities have been replaced with legacy nullability, and all required
/// named parameters are not required.
DartType legacyErasure(CoreTypes coreTypes, DartType type) {
return rawLegacyErasure(coreTypes, type) ?? type;
DartType legacyErasure(DartType type) {
return rawLegacyErasure(type) ?? type;
}

/// Returns legacy erasure of [type], that is, the type in which all nnbd
/// nullabilities have been replaced with legacy nullability, and all required
/// named parameters are not required.
///
/// Returns `null` if the type wasn't changed.
DartType rawLegacyErasure(CoreTypes coreTypes, DartType type) {
return type.accept(new _LegacyErasure(coreTypes));
DartType rawLegacyErasure(DartType type) {
return type.accept(const _LegacyErasure());
}

/// Returns legacy erasure of [supertype], that is, the type in which all nnbd
/// nullabilities have been replaced with legacy nullability, and all required
/// named parameters are not required.
Supertype legacyErasureSupertype(CoreTypes coreTypes, Supertype supertype) {
Supertype legacyErasureSupertype(Supertype supertype) {
if (supertype.typeArguments.isEmpty) {
return supertype;
}
List<DartType> newTypeArguments;
for (int i = 0; i < supertype.typeArguments.length; i++) {
DartType typeArgument = supertype.typeArguments[i];
DartType newTypeArgument =
typeArgument.accept(new _LegacyErasure(coreTypes));
DartType newTypeArgument = typeArgument.accept(const _LegacyErasure());
if (newTypeArgument != null) {
newTypeArguments ??= supertype.typeArguments.toList(growable: false);
newTypeArguments[i] = newTypeArgument;
Expand All @@ -51,9 +49,7 @@ Supertype legacyErasureSupertype(CoreTypes coreTypes, Supertype supertype) {
///
/// The visitor returns `null` if the type wasn't changed.
class _LegacyErasure extends ReplacementVisitor {
final CoreTypes coreTypes;

_LegacyErasure(this.coreTypes);
const _LegacyErasure();

Nullability visitNullability(DartType node) {
if (node.declaredNullability != Nullability.legacy) {
Expand Down
Loading

0 comments on commit 74be667

Please sign in to comment.