From 74be6670c9b5fff68caec4112f13216031c4b0fc Mon Sep 17 00:00:00 2001 From: Johnni Winther Date: Fri, 4 Dec 2020 10:03:40 +0000 Subject: [PATCH] [kernel] Remove CoreTypes from legacyErasure Change-Id: I9692c01ed3bf9982834144dd9eb477a97db5536f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175041 Reviewed-by: Dmitry Stefantsov Commit-Queue: Johnni Winther --- .../lib/src/fasta/builder/field_builder.dart | 3 +-- .../fasta/kernel/class_hierarchy_builder.dart | 7 +++--- .../kernel/combined_member_signature.dart | 2 +- .../src/fasta/kernel/constant_evaluator.dart | 25 ++++++++----------- .../src/fasta/kernel/implicit_field_type.dart | 6 ++--- .../src/fasta/kernel/inference_visitor.dart | 10 ++++---- .../fasta/source/source_class_builder.dart | 5 ++-- .../fasta/type_inference/type_inferrer.dart | 12 ++++----- pkg/kernel/lib/class_hierarchy.dart | 8 +++--- pkg/kernel/lib/src/bounds_checks.dart | 4 +-- pkg/kernel/lib/src/legacy_erasure.dart | 18 ++++++------- pkg/kernel/lib/src/standard_bounds.dart | 8 ++---- pkg/kernel/test/legacy_erasure_test.dart | 2 +- 13 files changed, 46 insertions(+), 64 deletions(-) diff --git a/pkg/front_end/lib/src/fasta/builder/field_builder.dart b/pkg/front_end/lib/src/fasta/builder/field_builder.dart index eb037e7ae86b..833753cd2614 100644 --- a/pkg/front_end/lib/src/fasta/builder/field_builder.dart +++ b/pkg/front_end/lib/src/fasta/builder/field_builder.dart @@ -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); diff --git a/pkg/front_end/lib/src/fasta/kernel/class_hierarchy_builder.dart b/pkg/front_end/lib/src/fasta/kernel/class_hierarchy_builder.dart index 5c5223c591b2..d56fa0d41d37 100644 --- a/pkg/front_end/lib/src/fasta/kernel/class_hierarchy_builder.dart +++ b/pkg/front_end/lib/src/fasta/kernel/class_hierarchy_builder.dart @@ -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 { @@ -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]); } } @@ -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; diff --git a/pkg/front_end/lib/src/fasta/kernel/combined_member_signature.dart b/pkg/front_end/lib/src/fasta/kernel/combined_member_signature.dart index 1478cf0cf9c8..c97fc4ea900c 100644 --- a/pkg/front_end/lib/src/fasta/kernel/combined_member_signature.dart +++ b/pkg/front_end/lib/src/fasta/kernel/combined_member_signature.dart @@ -301,7 +301,7 @@ abstract class CombinedMemberSignatureBase { "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); diff --git a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart index 1d4af852c42b..ed46fca98ddb 100644 --- a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart +++ b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart @@ -174,8 +174,6 @@ class ConstantWeakener extends ComputeOnceConstantVisitor { ConstantWeakener(this._evaluator); - CoreTypes get _coreTypes => _evaluator.coreTypes; - Constant processValue(Constant node, Constant value) { if (value != null) { value = _evaluator.canonicalize(value); @@ -207,8 +205,8 @@ class ConstantWeakener extends ComputeOnceConstantVisitor { @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 entries; for (int index = 0; index < node.entries.length; index++) { ConstantMapEntry entry = node.entries[index]; @@ -229,7 +227,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor { @override Constant visitListConstant(ListConstant node) { - DartType typeArgument = rawLegacyErasure(_coreTypes, node.typeArgument); + DartType typeArgument = rawLegacyErasure(node.typeArgument); List entries; for (int index = 0; index < node.entries.length; index++) { Constant entry = visitConstant(node.entries[index]); @@ -247,7 +245,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor { @override Constant visitSetConstant(SetConstant node) { - DartType typeArgument = rawLegacyErasure(_coreTypes, node.typeArgument); + DartType typeArgument = rawLegacyErasure(node.typeArgument); List entries; for (int index = 0; index < node.entries.length; index++) { Constant entry = visitConstant(node.entries[index]); @@ -267,8 +265,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor { Constant visitInstanceConstant(InstanceConstant node) { List 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; @@ -294,7 +291,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor { PartialInstantiationConstant node) { List 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; @@ -311,7 +308,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor { @override Constant visitTypeLiteralConstant(TypeLiteralConstant node) { - DartType type = rawLegacyErasure(_coreTypes, node.type); + DartType type = rawLegacyErasure(node.type); if (type != null) { return new TypeLiteralConstant(type); } @@ -840,7 +837,7 @@ class ConstantEvaluator extends RecursiveVisitor { case EvaluationMode.agnostic: return type; case EvaluationMode.weak: - return legacyErasure(coreTypes, type); + return legacyErasure(type); } throw new UnsupportedError( "Unexpected evaluation mode: ${evaluationMode}."); @@ -852,9 +849,7 @@ class ConstantEvaluator extends RecursiveVisitor { 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}."); @@ -2608,7 +2603,7 @@ class ConstantEvaluator extends RecursiveVisitor { 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) { diff --git a/pkg/front_end/lib/src/fasta/kernel/implicit_field_type.dart b/pkg/front_end/lib/src/fasta/kernel/implicit_field_type.dart index 971b0e55e73e..a636968678a4 100644 --- a/pkg/front_end/lib/src/fasta/kernel/implicit_field_type.dart +++ b/pkg/front_end/lib/src/fasta/kernel/implicit_field_type.dart @@ -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; @@ -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; diff --git a/pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart b/pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart index de26ae2876ed..360130c306de 100644 --- a/pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart +++ b/pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart @@ -3837,7 +3837,7 @@ class InferenceVisitor } if (!inferrer.isNonNullableByDefault) { - binaryType = legacyErasure(inferrer.coreTypes, binaryType); + binaryType = legacyErasure(binaryType); } if (!inferrer.isTopLevel && binaryTarget.isNullable) { @@ -3934,7 +3934,7 @@ class InferenceVisitor } if (!inferrer.isNonNullableByDefault) { - unaryType = legacyErasure(inferrer.coreTypes, unaryType); + unaryType = legacyErasure(unaryType); } if (!inferrer.isTopLevel && unaryTarget.isNullable) { @@ -4022,7 +4022,7 @@ class InferenceVisitor } if (!inferrer.isNonNullableByDefault) { - readType = legacyErasure(inferrer.coreTypes, readType); + readType = legacyErasure(readType); } if (!inferrer.isTopLevel && readTarget.isNullable) { @@ -4220,7 +4220,7 @@ class InferenceVisitor } if (!inferrer.isNonNullableByDefault) { - readType = legacyErasure(inferrer.coreTypes, readType); + readType = legacyErasure(readType); } readResult ??= new ExpressionInferenceResult(readType, read); @@ -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) { diff --git a/pkg/front_end/lib/src/fasta/source/source_class_builder.dart b/pkg/front_end/lib/src/fasta/source/source_class_builder.dart index 8536d8cb1e24..b2645ddca3d6 100644 --- a/pkg/front_end/lib/src/fasta/source/source_class_builder.dart +++ b/pkg/front_end/lib/src/fasta/source/source_class_builder.dart @@ -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( @@ -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; diff --git a/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart b/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart index d6ef1129d340..43f7c3def38f 100644 --- a/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart +++ b/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart @@ -1985,7 +1985,7 @@ class TypeInferrerImpl implements TypeInferrer { typeSchemaEnvironment.inferGenericFunctionOrType( isNonNullableByDefault ? calleeType.returnType - : legacyErasure(coreTypes, calleeType.returnType), + : legacyErasure(calleeType.returnType), calleeTypeParameters, null, null, @@ -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; @@ -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; @@ -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); diff --git a/pkg/kernel/lib/class_hierarchy.dart b/pkg/kernel/lib/class_hierarchy.dart index d14cfc3f0291..fcec1e639442 100644 --- a/pkg/kernel/lib/class_hierarchy.dart +++ b/pkg/kernel/lib/class_hierarchy.dart @@ -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( @@ -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; } @@ -1679,7 +1679,7 @@ class _ClassInfo { genericSuperType[cls] = result; } } else { - type = legacyErasureSupertype(coreTypes, type); + type = legacyErasureSupertype(type); if (type != canonical) { onAmbiguousSupertypes(classNode, canonical, type); } diff --git a/pkg/kernel/lib/src/bounds_checks.dart b/pkg/kernel/lib/src/bounds_checks.dart index 00763eef3ad8..c9aecc6dfeb2 100644 --- a/pkg/kernel/lib/src/bounds_checks.dart +++ b/pkg/kernel/lib/src/bounds_checks.dart @@ -345,7 +345,7 @@ List 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 @@ -454,7 +454,7 @@ List 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 ??= []; diff --git a/pkg/kernel/lib/src/legacy_erasure.dart b/pkg/kernel/lib/src/legacy_erasure.dart index 83c32ce565bd..362836890cde 100644 --- a/pkg/kernel/lib/src/legacy_erasure.dart +++ b/pkg/kernel/lib/src/legacy_erasure.dart @@ -3,15 +3,14 @@ // 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 @@ -19,22 +18,21 @@ DartType legacyErasure(CoreTypes coreTypes, DartType type) { /// 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 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; @@ -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) { diff --git a/pkg/kernel/lib/src/standard_bounds.dart b/pkg/kernel/lib/src/standard_bounds.dart index 64a17552309f..92fb5eeb4688 100644 --- a/pkg/kernel/lib/src/standard_bounds.dart +++ b/pkg/kernel/lib/src/standard_bounds.dart @@ -271,9 +271,7 @@ mixin StandardBounds { type1, type2, clientLibrary); } return _getNullabilityObliviousStandardLowerBound( - legacyErasure(coreTypes, type1), - legacyErasure(coreTypes, type2), - clientLibrary); + legacyErasure(type1), legacyErasure(type2), clientLibrary); } DartType _getNullabilityAwareStandardLowerBound( @@ -594,9 +592,7 @@ mixin StandardBounds { type1, type2, clientLibrary); } return _getNullabilityObliviousStandardUpperBound( - legacyErasure(coreTypes, type1), - legacyErasure(coreTypes, type2), - clientLibrary); + legacyErasure(type1), legacyErasure(type2), clientLibrary); } DartType _getNullabilityAwareStandardUpperBound( diff --git a/pkg/kernel/test/legacy_erasure_test.dart b/pkg/kernel/test/legacy_erasure_test.dart index 4f82dea76a41..0a0902b9ffe5 100644 --- a/pkg/kernel/test/legacy_erasure_test.dart +++ b/pkg/kernel/test/legacy_erasure_test.dart @@ -58,7 +58,7 @@ main() { data.forEach((String input, String output) { DartType inputType = env.parseType(input); DartType expectedOutputType = env.parseType(output); - DartType actualOutputType = legacyErasure(env.coreTypes, inputType); + DartType actualOutputType = legacyErasure(inputType); print('legacyErasure($inputType) = $actualOutputType: $expectedOutputType'); Expect.equals( expectedOutputType,