Skip to content

Commit 058dd33

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[model] Share interface of chooseTypes between Analyzer and CFE
The implementation isn't shared yet, but is extracted into the implementations of `TypeAnalyzerOperations` class, to ensure the internal dependencies on the other tool-specific functions and classes is weakened. This is a preliminary step to sharing `chooseTypes` between the Analyzer and the CFE. Part of #54902 Change-Id: I4441a9b5dacffd6567ed7e5a3937f64a119fa10f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/446080 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent eed7f7e commit 058dd33

File tree

15 files changed

+653
-348
lines changed

15 files changed

+653
-348
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ abstract interface class SharedInferenceLogWriter {
246246
/// type schema to another.
247247
void recordGeneratedConstraint(
248248
SharedTypeParameter parameter,
249-
MergedTypeConstraint<Object, SharedType, Object> constraint,
249+
MergedTypeConstraint<Object, SharedType, Object, Object> constraint,
250250
);
251251

252252
/// Records that type inference has resolved a method name.
@@ -751,7 +751,7 @@ abstract class SharedInferenceLogWriterImpl
751751
@override
752752
void recordGeneratedConstraint(
753753
SharedTypeParameter parameter,
754-
MergedTypeConstraint<Object, SharedType, Object> constraint,
754+
MergedTypeConstraint<Object, SharedType, Object, Object> constraint,
755755
) {
756756
checkCall(
757757
method: 'recordGeneratedConstraint',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ mixin TypeAnalyzer<
311311
bool get isDotShorthandContextEmpty => _dotShorthands.isEmpty;
312312

313313
@override
314-
TypeAnalyzerOperations<Variable, TypeDeclarationType, TypeDeclaration>
314+
TypeAnalyzerOperations<Variable, TypeDeclarationType, TypeDeclaration, Node>
315315
get operations;
316316

317317
/// Options affecting the behavior of [TypeAnalyzer].

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

Lines changed: 141 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import 'type_constraint.dart';
2222
abstract interface class TypeAnalyzerOperations<
2323
Variable extends Object,
2424
TypeDeclarationType extends Object,
25-
TypeDeclaration extends Object
25+
TypeDeclaration extends Object,
26+
AstNode extends Object
2627
>
2728
implements FlowAnalysisOperations<Variable, SharedTypeView> {
2829
/// Returns the type `double`.
@@ -56,16 +57,17 @@ abstract interface class TypeAnalyzerOperations<
5657
Variable,
5758
TypeDeclarationType,
5859
TypeDeclaration,
59-
Object
60+
AstNode
6061
>
6162
createTypeConstraintGenerator({
62-
required TypeConstraintGenerationDataForTesting<Variable, Object>?
63+
required TypeConstraintGenerationDataForTesting<Variable, AstNode>?
6364
typeConstraintGenerationDataForTesting,
6465
required List<SharedTypeParameterView> typeParametersToInfer,
6566
required TypeAnalyzerOperations<
6667
Variable,
6768
TypeDeclarationType,
68-
TypeDeclaration
69+
TypeDeclaration,
70+
AstNode
6971
>
7072
typeAnalyzerOperations,
7173
required bool inferenceUsingBoundsIsEnabled,
@@ -577,17 +579,22 @@ abstract interface class TypeAnalyzerOperations<
577579
SharedTypeSchemaView typeSchema,
578580
);
579581

580-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
582+
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration, AstNode>
581583
mergeInConstraintsFromBound({
582584
required SharedTypeParameter typeParameterToInfer,
583585
required List<SharedTypeParameterView> typeParametersToInfer,
584586
required SharedType lower,
585587
required Map<
586588
SharedTypeParameter,
587-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
589+
MergedTypeConstraint<
590+
Variable,
591+
TypeDeclarationType,
592+
TypeDeclaration,
593+
AstNode
594+
>
588595
>
589596
inferencePhaseConstraints,
590-
required TypeConstraintGenerationDataForTesting<Variable, Object>?
597+
required TypeConstraintGenerationDataForTesting<Variable, AstNode>?
591598
dataForTesting,
592599
required bool inferenceUsingBoundsIsEnabled,
593600
});
@@ -802,7 +809,12 @@ abstract interface class TypeAnalyzerOperations<
802809
/// is described in
803810
/// https://github.com/dart-lang/language/blob/main/resources/type-system/inference.md#constraint-solution-for-a-type-variable.
804811
SharedType chooseTypeFromConstraint(
805-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
812+
MergedTypeConstraint<
813+
Variable,
814+
TypeDeclarationType,
815+
TypeDeclaration,
816+
AstNode
817+
>
806818
constraint, {
807819
required bool grounded,
808820
required bool isContravariant,
@@ -811,54 +823,102 @@ abstract interface class TypeAnalyzerOperations<
811823
/// Chooses types from all available sources at the final stage of inference.
812824
SharedType inferTypeParameterFromAll(
813825
SharedType? typeFromPreviousInference,
814-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
826+
MergedTypeConstraint<
827+
Variable,
828+
TypeDeclarationType,
829+
TypeDeclaration,
830+
AstNode
831+
>
815832
constraint,
816833
SharedType? extendsConstraint, {
817834
bool isContravariant = false,
818835
bool isLegacyCovariant = true,
819836
required Map<
820837
SharedTypeParameter,
821-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
838+
MergedTypeConstraint<
839+
Variable,
840+
TypeDeclarationType,
841+
TypeDeclaration,
842+
AstNode
843+
>
822844
>
823845
constraints,
824846
required SharedTypeParameter typeParameterToInfer,
825847
required List<SharedTypeParameter> typeParametersToInfer,
826-
required TypeConstraintGenerationDataForTesting<Variable, Object>?
848+
required TypeConstraintGenerationDataForTesting<Variable, AstNode>?
827849
dataForTesting,
828850
required bool inferenceUsingBoundsIsEnabled,
829851
});
830852

831853
/// Chooses types from the current inference context in preliminary stages.
832854
SharedType inferTypeParameterFromContext(
833855
SharedType? typeFromPreviousInference,
834-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
856+
MergedTypeConstraint<
857+
Variable,
858+
TypeDeclarationType,
859+
TypeDeclaration,
860+
AstNode
861+
>
835862
constraint,
836863
SharedType? extendsConstraint, {
837864
required bool isContravariant,
838865
bool isLegacyCovariant = true,
839866
required Map<
840867
SharedTypeParameter,
841-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
868+
MergedTypeConstraint<
869+
Variable,
870+
TypeDeclarationType,
871+
TypeDeclaration,
872+
AstNode
873+
>
842874
>
843875
constraints,
844876
required List<SharedTypeParameter> typeParametersToInfer,
845877
required SharedTypeParameter typeParameterToInfer,
846-
required TypeConstraintGenerationDataForTesting<Variable, Object>?
878+
required TypeConstraintGenerationDataForTesting<Variable, AstNode>?
847879
dataForTesting,
848880
required bool inferenceUsingBoundsIsEnabled,
849881
});
850882

851883
/// True if [typeParameter] doesn't have an explicit bound.
852884
bool isBoundOmitted(SharedTypeParameter typeParameter);
885+
886+
/// Computes (or recomputes) a set of inferred types based on the constraints
887+
/// that have been recorded so far.
888+
List<SharedType> chooseTypes(
889+
List<SharedTypeParameter> typeParametersToInfer,
890+
Map<
891+
SharedTypeParameter,
892+
MergedTypeConstraint<
893+
Variable,
894+
TypeDeclarationType,
895+
TypeDeclaration,
896+
AstNode
897+
>
898+
>
899+
constraints,
900+
List<SharedType>? previouslyInferredTypes, {
901+
required bool preliminary,
902+
required bool inferenceUsingBoundsIsEnabled,
903+
required TypeConstraintGenerationDataForTesting<Variable, AstNode>?
904+
dataForTesting,
905+
required AstNode? treeNodeForTesting,
906+
});
853907
}
854908

855909
mixin TypeAnalyzerOperationsMixin<
856910
Variable extends Object,
857911
TypeDeclarationType extends Object,
858-
TypeDeclaration extends Object
912+
TypeDeclaration extends Object,
913+
AstNode extends Object
859914
>
860915
implements
861-
TypeAnalyzerOperations<Variable, TypeDeclarationType, TypeDeclaration> {
916+
TypeAnalyzerOperations<
917+
Variable,
918+
TypeDeclarationType,
919+
TypeDeclaration,
920+
AstNode
921+
> {
862922
@override
863923
SharedTypeView futureType(SharedTypeView argumentType) {
864924
return new SharedTypeView(
@@ -990,17 +1050,22 @@ mixin TypeAnalyzerOperationsMixin<
9901050
}
9911051

9921052
@override
993-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1053+
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration, AstNode>
9941054
mergeInConstraintsFromBound({
9951055
required SharedTypeParameter typeParameterToInfer,
9961056
required List<SharedTypeParameterView> typeParametersToInfer,
9971057
required SharedType lower,
9981058
required Map<
9991059
SharedTypeParameter,
1000-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1060+
MergedTypeConstraint<
1061+
Variable,
1062+
TypeDeclarationType,
1063+
TypeDeclaration,
1064+
AstNode
1065+
>
10011066
>
10021067
inferencePhaseConstraints,
1003-
required TypeConstraintGenerationDataForTesting<Variable, Object>?
1068+
required TypeConstraintGenerationDataForTesting<Variable, AstNode>?
10041069
dataForTesting,
10051070
required bool inferenceUsingBoundsIsEnabled,
10061071
}) {
@@ -1034,7 +1099,7 @@ mixin TypeAnalyzerOperationsMixin<
10341099
Variable,
10351100
TypeDeclarationType,
10361101
TypeDeclaration,
1037-
Object
1102+
AstNode
10381103
>
10391104
typeConstraintGatherer = createTypeConstraintGenerator(
10401105
typeConstraintGenerationDataForTesting: null,
@@ -1050,11 +1115,21 @@ mixin TypeAnalyzerOperationsMixin<
10501115
);
10511116
Map<
10521117
SharedTypeParameter,
1053-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1118+
MergedTypeConstraint<
1119+
Variable,
1120+
TypeDeclarationType,
1121+
TypeDeclaration,
1122+
AstNode
1123+
>
10541124
>
10551125
constraintsPerTypeVariable = typeConstraintGatherer.computeConstraints();
10561126
for (SharedTypeParameter typeParameter in constraintsPerTypeVariable.keys) {
1057-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1127+
MergedTypeConstraint<
1128+
Variable,
1129+
TypeDeclarationType,
1130+
TypeDeclaration,
1131+
AstNode
1132+
>
10581133
constraint = constraintsPerTypeVariable[typeParameter]!;
10591134
constraint.origin = new TypeConstraintFromExtendsClause(
10601135
typeParameterName: typeParameterToInfer.displayName,
@@ -1167,7 +1242,12 @@ mixin TypeAnalyzerOperationsMixin<
11671242

11681243
@override
11691244
SharedType chooseTypeFromConstraint(
1170-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1245+
MergedTypeConstraint<
1246+
Variable,
1247+
TypeDeclarationType,
1248+
TypeDeclaration,
1249+
AstNode
1250+
>
11711251
constraint, {
11721252
required bool grounded,
11731253
required bool isContravariant,
@@ -1225,19 +1305,29 @@ mixin TypeAnalyzerOperationsMixin<
12251305
@override
12261306
SharedType inferTypeParameterFromAll(
12271307
SharedType? typeFromPreviousInference,
1228-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1308+
MergedTypeConstraint<
1309+
Variable,
1310+
TypeDeclarationType,
1311+
TypeDeclaration,
1312+
AstNode
1313+
>
12291314
constraint,
12301315
SharedType? extendsConstraint, {
12311316
bool isContravariant = false,
12321317
bool isLegacyCovariant = true,
12331318
required Map<
12341319
SharedTypeParameter,
1235-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1320+
MergedTypeConstraint<
1321+
Variable,
1322+
TypeDeclarationType,
1323+
TypeDeclaration,
1324+
AstNode
1325+
>
12361326
>
12371327
constraints,
12381328
required SharedTypeParameter typeParameterToInfer,
12391329
required List<SharedTypeParameter> typeParametersToInfer,
1240-
required TypeConstraintGenerationDataForTesting<Variable, Object>?
1330+
required TypeConstraintGenerationDataForTesting<Variable, AstNode>?
12411331
dataForTesting,
12421332
required bool inferenceUsingBoundsIsEnabled,
12431333
}) {
@@ -1285,19 +1375,29 @@ mixin TypeAnalyzerOperationsMixin<
12851375
@override
12861376
SharedType inferTypeParameterFromContext(
12871377
SharedType? typeFromPreviousInference,
1288-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1378+
MergedTypeConstraint<
1379+
Variable,
1380+
TypeDeclarationType,
1381+
TypeDeclaration,
1382+
AstNode
1383+
>
12891384
constraint,
12901385
SharedType? extendsConstraint, {
12911386
required bool isContravariant,
12921387
bool isLegacyCovariant = true,
12931388
required Map<
12941389
SharedTypeParameter,
1295-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1390+
MergedTypeConstraint<
1391+
Variable,
1392+
TypeDeclarationType,
1393+
TypeDeclaration,
1394+
AstNode
1395+
>
12961396
>
12971397
constraints,
12981398
required List<SharedTypeParameter> typeParametersToInfer,
12991399
required SharedTypeParameter typeParameterToInfer,
1300-
required TypeConstraintGenerationDataForTesting<Variable, Object>?
1400+
required TypeConstraintGenerationDataForTesting<Variable, AstNode>?
13011401
dataForTesting,
13021402
required bool inferenceUsingBoundsIsEnabled,
13031403
}) {
@@ -1391,7 +1491,12 @@ abstract class TypeConstraintGenerator<
13911491
bool get enableDiscrepantObliviousnessOfNullabilitySuffixOfFutureOr;
13921492

13931493
/// Abstract type operations to be used in the matching methods.
1394-
TypeAnalyzerOperations<Variable, TypeDeclarationType, TypeDeclaration>
1494+
TypeAnalyzerOperations<
1495+
Variable,
1496+
TypeDeclarationType,
1497+
TypeDeclaration,
1498+
AstNode
1499+
>
13951500
get typeAnalyzerOperations;
13961501

13971502
/// Type parameters being constrained by [TypeConstraintGenerator].
@@ -1414,7 +1519,12 @@ abstract class TypeConstraintGenerator<
14141519
/// Returns the set of type constraints that was gathered.
14151520
Map<
14161521
SharedTypeParameter,
1417-
MergedTypeConstraint<Variable, TypeDeclarationType, TypeDeclaration>
1522+
MergedTypeConstraint<
1523+
Variable,
1524+
TypeDeclarationType,
1525+
TypeDeclaration,
1526+
AstNode
1527+
>
14181528
>
14191529
computeConstraints();
14201530

0 commit comments

Comments
 (0)