Skip to content

Commit 235d6cb

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[model] Share inferTypeParameterFromAll between Analyzer and CFE
Part of #54902 Change-Id: I2b9da9868925dbdede49d5a11f8b0558400886e3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445060 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent f2123a8 commit 235d6cb

File tree

6 files changed

+121
-112
lines changed

6 files changed

+121
-112
lines changed

pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,29 @@ abstract interface class TypeAnalyzerOperations<
807807
required bool grounded,
808808
required bool isContravariant,
809809
});
810+
811+
/// Chooses types from all available sources at the final stage of inference.
812+
SharedType inferTypeParameterFromAll(
813+
SharedType? typeFromPreviousInference,
814+
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
815+
constraint,
816+
SharedType? extendsConstraint, {
817+
bool isContravariant = false,
818+
bool isLegacyCovariant = true,
819+
required Map<
820+
SharedTypeParameter,
821+
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
822+
>
823+
constraints,
824+
required SharedTypeParameter typeParameterToInfer,
825+
required List<SharedTypeParameter> typeParametersToInfer,
826+
required TypeConstraintGenerationDataForTesting<Variable, Object>?
827+
dataForTesting,
828+
required bool inferenceUsingBoundsIsEnabled,
829+
});
830+
831+
/// True if [typeParameter] doesn't have an explicit bound.
832+
bool isBoundOmitted(SharedTypeParameter typeParameter);
810833
}
811834

812835
mixin TypeAnalyzerOperationsMixin<
@@ -1178,6 +1201,66 @@ mixin TypeAnalyzerOperationsMixin<
11781201
}
11791202
}
11801203
}
1204+
1205+
@override
1206+
SharedType inferTypeParameterFromAll(
1207+
SharedType? typeFromPreviousInference,
1208+
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1209+
constraint,
1210+
SharedType? extendsConstraint, {
1211+
bool isContravariant = false,
1212+
bool isLegacyCovariant = true,
1213+
required Map<
1214+
SharedTypeParameter,
1215+
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1216+
>
1217+
constraints,
1218+
required SharedTypeParameter typeParameterToInfer,
1219+
required List<SharedTypeParameter> typeParametersToInfer,
1220+
required TypeConstraintGenerationDataForTesting<Variable, Object>?
1221+
dataForTesting,
1222+
required bool inferenceUsingBoundsIsEnabled,
1223+
}) {
1224+
// See if we already fixed this type in a previous inference step.
1225+
// If so, then we aren't allowed to change it unless [isLegacyCovariant] is
1226+
// false.
1227+
if (typeFromPreviousInference != null &&
1228+
isLegacyCovariant &&
1229+
isKnownType(new SharedTypeSchemaView(typeFromPreviousInference))) {
1230+
return typeFromPreviousInference;
1231+
}
1232+
1233+
if (inferenceUsingBoundsIsEnabled &&
1234+
constraint.lower is! SharedUnknownTypeSchemaView &&
1235+
!isBoundOmitted(typeParameterToInfer)) {
1236+
MergedTypeConstraint constraintFromBound = mergeInConstraintsFromBound(
1237+
typeParameterToInfer: typeParameterToInfer,
1238+
typeParametersToInfer:
1239+
typeParametersToInfer.cast<SharedTypeParameterView>(),
1240+
lower: constraint.lower.unwrapTypeSchemaView(),
1241+
inferencePhaseConstraints: constraints,
1242+
dataForTesting: dataForTesting,
1243+
inferenceUsingBoundsIsEnabled: inferenceUsingBoundsIsEnabled,
1244+
);
1245+
1246+
constraint.mergeInTypeSchemaUpper(constraintFromBound.upper, this);
1247+
constraint.mergeInTypeSchemaLower(constraintFromBound.lower, this);
1248+
}
1249+
1250+
if (extendsConstraint != null) {
1251+
constraint = constraint.clone();
1252+
constraint.mergeInTypeSchemaUpper(
1253+
new SharedTypeSchemaView(extendsConstraint),
1254+
this,
1255+
);
1256+
}
1257+
1258+
return chooseTypeFromConstraint(
1259+
constraint,
1260+
grounded: true,
1261+
isContravariant: isContravariant,
1262+
);
1263+
}
11811264
}
11821265

11831266
/// Abstract interface of a type constraint generator.

pkg/_fe_analyzer_shared/test/mini_ast.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,6 +3713,12 @@ class MiniAstOperations
37133713
// TODO(paulberry): Implement leastClosureOfSchema in mini ast.
37143714
throw UnimplementedError();
37153715
}
3716+
3717+
@override
3718+
bool isBoundOmitted(SharedTypeParameter typeParameter) {
3719+
// TODO(paulberry): Implement isBoundOmitted in mini ast.
3720+
throw UnimplementedError();
3721+
}
37163722
}
37173723

37183724
/// Representation of an expression or statement in the pseudo-Dart language

pkg/analyzer/lib/src/dart/element/generic_inferrer.dart

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,16 @@ class GenericInferrer {
530530
}
531531
} else {
532532
inferredTypes[i] =
533-
_inferTypeParameterFromAll(
533+
_typeSystemOperations.inferTypeParameterFromAll(
534+
previouslyInferredType,
534535
constraint,
535-
extendsClause,
536+
extendsClause?.upper.unwrapTypeSchemaView(),
536537
isContravariant: typeParam.variance.isContravariant,
537538
typeParameterToInfer: typeParam,
538-
inferencePhaseConstraints: inferencePhaseConstraints,
539+
constraints: inferencePhaseConstraints,
540+
dataForTesting: null,
541+
inferenceUsingBoundsIsEnabled: inferenceUsingBoundsIsEnabled,
542+
typeParametersToInfer: _typeFormals,
539543
)
540544
as TypeImpl;
541545
}
@@ -603,49 +607,6 @@ class GenericInferrer {
603607
'Consider passing explicit type argument(s) to the generic.\n\n';
604608
}
605609

606-
SharedType _inferTypeParameterFromAll(
607-
MergedTypeConstraint constraint,
608-
MergedTypeConstraint? extendsClause, {
609-
required bool isContravariant,
610-
required TypeParameterElementImpl typeParameterToInfer,
611-
required Map<TypeParameterElementImpl, MergedTypeConstraint>
612-
inferencePhaseConstraints,
613-
}) {
614-
if (extendsClause != null) {
615-
MergedTypeConstraint? boundConstraint;
616-
if (inferenceUsingBoundsIsEnabled) {
617-
if (!identical(
618-
constraint.lower.unwrapTypeSchemaView(),
619-
UnknownInferredType.instance,
620-
)) {
621-
boundConstraint = _typeSystemOperations.mergeInConstraintsFromBound(
622-
typeParameterToInfer: typeParameterToInfer,
623-
typeParametersToInfer: _typeFormals.cast<SharedTypeParameterView>(),
624-
lower: constraint.lower.unwrapTypeSchemaView(),
625-
inferencePhaseConstraints: inferencePhaseConstraints,
626-
dataForTesting: dataForTesting,
627-
inferenceUsingBoundsIsEnabled: inferenceUsingBoundsIsEnabled,
628-
);
629-
}
630-
}
631-
632-
constraint = _squashConstraints([
633-
constraint,
634-
extendsClause,
635-
if (boundConstraint != null &&
636-
!boundConstraint.isEmpty(_typeSystemOperations))
637-
boundConstraint,
638-
]);
639-
}
640-
641-
var choice = _typeSystemOperations.chooseTypeFromConstraint(
642-
constraint,
643-
grounded: true,
644-
isContravariant: isContravariant,
645-
);
646-
return choice;
647-
}
648-
649610
SharedType _inferTypeParameterFromContext(
650611
MergedTypeConstraint constraint,
651612
MergedTypeConstraint? extendsClause, {

pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ class TypeSystemOperations
636636
return type.unwrapTypeView<TypeImpl>().isBottom;
637637
}
638638

639+
@override
640+
bool isBoundOmitted(SharedTypeParameter typeParameter) {
641+
return typeParameter.boundShared == null;
642+
}
643+
639644
@override
640645
bool isDartCoreFunctionInternal(TypeImpl type) {
641646
return type.nullabilitySuffix == NullabilitySuffix.none &&

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,23 @@ class OperationsCfe
10551055
schema.unwrapTypeSchemaView(),
10561056
coreTypes: typeEnvironment.coreTypes));
10571057
}
1058+
1059+
@override
1060+
bool isBoundOmitted(covariant StructuralParameter structuralParameter) {
1061+
// If the bound was omitted by the programmer, the Kernel representation for
1062+
// the parameter will look similar to the following:
1063+
//
1064+
// T extends Object = dynamic
1065+
//
1066+
// Note that it's not possible to receive [Object] as [TypeParameter.bound]
1067+
// and `dynamic` as [TypeParameter.defaultType] from the front end in any
1068+
// other way.
1069+
DartType bound = structuralParameter.bound;
1070+
return bound is InterfaceType &&
1071+
identical(bound.classReference,
1072+
typeEnvironment.coreTypes.objectClass.reference) &&
1073+
structuralParameter.defaultType is DynamicType;
1074+
}
10581075
}
10591076

10601077
/// Type inference results used for testing.

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

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,6 @@ class TypeSchemaEnvironment extends HierarchyBasedTypeEnvironment
130130
return operandType;
131131
}
132132

133-
bool hasOmittedBound(StructuralParameter parameter) {
134-
// If the bound was omitted by the programmer, the Kernel representation for
135-
// the parameter will look similar to the following:
136-
//
137-
// T extends Object = dynamic
138-
//
139-
// Note that it's not possible to receive [Object] as [TypeParameter.bound]
140-
// and `dynamic` as [TypeParameter.defaultType] from the front end in any
141-
// other way.
142-
DartType bound = parameter.bound;
143-
return bound is InterfaceType &&
144-
identical(bound.classReference, coreTypes.objectClass.reference) &&
145-
parameter.defaultType is DynamicType;
146-
}
147-
148133
/// Use the given [constraints] to substitute for type parameters.
149134
///
150135
/// [typeParametersToInfer] is the set of type parameters that should be
@@ -175,7 +160,7 @@ class TypeSchemaEnvironment extends HierarchyBasedTypeEnvironment
175160

176161
DartType typeParamBound = typeParam.bound;
177162
DartType? extendsConstraint;
178-
if (!hasOmittedBound(typeParam)) {
163+
if (!operations.isBoundOmitted(typeParam)) {
179164
extendsConstraint = new FunctionTypeInstantiator.fromIterables(
180165
typeParametersToInfer, inferredTypes)
181166
.substitute(typeParamBound);
@@ -194,12 +179,11 @@ class TypeSchemaEnvironment extends HierarchyBasedTypeEnvironment
194179
inferenceUsingBoundsIsEnabled: inferenceUsingBoundsIsEnabled)
195180
as DartType;
196181
} else {
197-
inferredTypes[i] = _inferTypeParameterFromAll(
182+
inferredTypes[i] = operations.inferTypeParameterFromAll(
198183
previouslyInferredTypes?[i], constraint, extendsConstraint,
199184
isContravariant:
200185
typeParam.variance == shared.Variance.contravariant,
201186
isLegacyCovariant: typeParam.isLegacyCovariant,
202-
operations: operations,
203187
constraints: constraints,
204188
typeParameterToInfer: typeParam,
205189
typeParametersToInfer: typeParametersToInfer,
@@ -366,53 +350,6 @@ class TypeSchemaEnvironment extends HierarchyBasedTypeEnvironment
366350
return inferredTypes;
367351
}
368352

369-
SharedType _inferTypeParameterFromAll(DartType? typeFromPreviousInference,
370-
MergedTypeConstraint constraint, DartType? extendsConstraint,
371-
{bool isContravariant = false,
372-
bool isLegacyCovariant = true,
373-
required OperationsCfe operations,
374-
required Map<StructuralParameter, MergedTypeConstraint> constraints,
375-
required StructuralParameter typeParameterToInfer,
376-
required List<StructuralParameter> typeParametersToInfer,
377-
required InferenceDataForTesting? dataForTesting,
378-
required bool inferenceUsingBoundsIsEnabled}) {
379-
// See if we already fixed this type in a previous inference step.
380-
// If so, then we aren't allowed to change it unless [isLegacyCovariant] is
381-
// false.
382-
if (typeFromPreviousInference != null &&
383-
isLegacyCovariant &&
384-
operations
385-
.isKnownType(new SharedTypeSchemaView(typeFromPreviousInference))) {
386-
return typeFromPreviousInference;
387-
}
388-
389-
if (inferenceUsingBoundsIsEnabled &&
390-
constraint.lower is! SharedUnknownTypeSchemaView &&
391-
!hasOmittedBound(typeParameterToInfer)) {
392-
MergedTypeConstraint constraintFromBound =
393-
operations.mergeInConstraintsFromBound(
394-
typeParameterToInfer: typeParameterToInfer,
395-
typeParametersToInfer:
396-
typeParametersToInfer.cast<SharedTypeParameterView>(),
397-
lower: constraint.lower.unwrapTypeSchemaView<DartType>(),
398-
inferencePhaseConstraints: constraints,
399-
dataForTesting: dataForTesting,
400-
inferenceUsingBoundsIsEnabled: inferenceUsingBoundsIsEnabled);
401-
402-
constraint.mergeInTypeSchemaUpper(constraintFromBound.upper, operations);
403-
constraint.mergeInTypeSchemaLower(constraintFromBound.lower, operations);
404-
}
405-
406-
if (extendsConstraint != null) {
407-
constraint = constraint.clone();
408-
constraint.mergeInTypeSchemaUpper(
409-
new SharedTypeSchemaView(extendsConstraint), operations);
410-
}
411-
412-
return operations.chooseTypeFromConstraint(constraint,
413-
grounded: true, isContravariant: isContravariant);
414-
}
415-
416353
SharedType _inferTypeParameterFromContext(DartType? typeFromPreviousInference,
417354
MergedTypeConstraint constraint, DartType? extendsConstraint,
418355
{bool isLegacyCovariant = true,
@@ -448,7 +385,7 @@ class TypeSchemaEnvironment extends HierarchyBasedTypeEnvironment
448385

449386
if (inferenceUsingBoundsIsEnabled &&
450387
constraint.lower is! SharedUnknownTypeSchemaView &&
451-
!hasOmittedBound(typeParameterToInfer)) {
388+
!operations.isBoundOmitted(typeParameterToInfer)) {
452389
// Coverage-ignore-block(suite): Not run.
453390
MergedTypeConstraint constraintFromBound =
454391
operations.mergeInConstraintsFromBound(

0 commit comments

Comments
 (0)