Skip to content

Commit 297c90b

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[analyzer][cfe] Share the constraint collecting procedures
Part of #54902 Change-Id: I98dae68a61536cc3292f2a4f05cab22a8014486d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392581 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent ff3d7a1 commit 297c90b

File tree

4 files changed

+82
-52
lines changed

4 files changed

+82
-52
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,16 @@ abstract class TypeConstraintGenerator<
906906

907907
TypeConstraintGenerator({required this.inferenceUsingBoundsIsEnabled});
908908

909+
/// Add constraint: [lower] <: [typeParameter] <: TOP.
910+
void addLowerConstraintForParameter(
911+
TypeParameterStructure typeParameter, TypeStructure lower,
912+
{required AstNode? nodeForTesting});
913+
914+
/// Add constraint: BOTTOM <: [typeParameter] <: [upper].
915+
void addUpperConstraintForParameter(
916+
TypeParameterStructure typeParameter, TypeStructure upper,
917+
{required AstNode? nodeForTesting});
918+
909919
/// Returns the type arguments of the supertype of [type] that is an
910920
/// instantiation of [typeDeclaration]. If none of the supertypes of [type]
911921
/// are instantiations of [typeDeclaration], returns null.

pkg/_fe_analyzer_shared/test/type_inference/type_constraint_gatherer_test.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ class _TypeConstraintGatherer extends TypeConstraintGenerator<Type,
599599
case var typeVar?
600600
when p.nullabilitySuffix == NullabilitySuffix.none &&
601601
_typeVariablesBeingConstrained.contains(typeVar)) {
602-
_constraints.add('$typeVar <: $q');
602+
addUpperConstraintForParameter(typeVar, q, nodeForTesting: null);
603603
return true;
604604
}
605605

@@ -609,7 +609,7 @@ class _TypeConstraintGatherer extends TypeConstraintGenerator<Type,
609609
case var typeVar?
610610
when q.nullabilitySuffix == NullabilitySuffix.none &&
611611
_typeVariablesBeingConstrained.contains(typeVar)) {
612-
_constraints.add('$p <: $typeVar');
612+
addLowerConstraintForParameter(typeVar, p, nodeForTesting: null);
613613
return true;
614614
}
615615

@@ -635,4 +635,16 @@ class _TypeConstraintGatherer extends TypeConstraintGenerator<Type,
635635
void restoreState(TypeConstraintGeneratorState state) {
636636
_constraints.length = state.count;
637637
}
638+
639+
@override
640+
void addUpperConstraintForParameter(TypeParameter typeParameter, Type upper,
641+
{required Node? nodeForTesting}) {
642+
_constraints.add('$typeParameter <: $upper');
643+
}
644+
645+
@override
646+
void addLowerConstraintForParameter(TypeParameter typeParameter, Type lower,
647+
{required Node? nodeForTesting}) {
648+
_constraints.add('$lower <: $typeParameter');
649+
}
638650
}

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

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
7171
@override
7272
TypeSystemOperations get typeAnalyzerOperations => _typeSystemOperations;
7373

74+
@override
75+
void addLowerConstraintForParameter(
76+
TypeParameterElement element, DartType lower,
77+
{required AstNode? nodeForTesting}) {
78+
GeneratedTypeConstraint<DartType, TypeParameterElement, PromotableElement>
79+
generatedTypeConstraint = GeneratedTypeConstraint<
80+
DartType,
81+
TypeParameterElement,
82+
PromotableElement>.lower(element, SharedTypeSchemaView(lower));
83+
_constraints.add(generatedTypeConstraint);
84+
if (dataForTesting != null && nodeForTesting != null) {
85+
(dataForTesting!.generatedTypeConstraints[nodeForTesting] ??= [])
86+
.add(generatedTypeConstraint);
87+
}
88+
}
89+
90+
@override
91+
void addUpperConstraintForParameter(
92+
TypeParameterElement element, DartType upper,
93+
{required AstNode? nodeForTesting}) {
94+
GeneratedTypeConstraint<DartType, TypeParameterElement, PromotableElement>
95+
generatedTypeConstraint = GeneratedTypeConstraint<
96+
DartType,
97+
TypeParameterElement,
98+
PromotableElement>.upper(element, SharedTypeSchemaView(upper));
99+
_constraints.add(generatedTypeConstraint);
100+
if (dataForTesting != null && nodeForTesting != null) {
101+
(dataForTesting!.generatedTypeConstraints[nodeForTesting] ??= [])
102+
.add(generatedTypeConstraint);
103+
}
104+
}
105+
74106
/// Returns the set of type constraints that was gathered.
75107
Map<
76108
TypeParameterElement,
@@ -148,7 +180,8 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
148180
case var P_element?
149181
when P_nullability == NullabilitySuffix.none &&
150182
_typeParameters.contains(P_element)) {
151-
_addUpper(P_element, Q, nodeForTesting: nodeForTesting);
183+
addUpperConstraintForParameter(P_element, Q,
184+
nodeForTesting: nodeForTesting);
152185
return true;
153186
}
154187

@@ -165,7 +198,8 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
165198
P,
166199
_typeSystemOperations.greatestClosureOfTypeInternal(
167200
Q_element.bound!, [..._typeParameters]))))) {
168-
_addLower(Q_element, P, nodeForTesting: nodeForTesting);
201+
addLowerConstraintForParameter(Q_element, P,
202+
nodeForTesting: nodeForTesting);
169203
return true;
170204
}
171205

@@ -343,34 +377,6 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
343377
return false;
344378
}
345379

346-
void _addLower(TypeParameterElement element, DartType lower,
347-
{required AstNode? nodeForTesting}) {
348-
GeneratedTypeConstraint<DartType, TypeParameterElement, PromotableElement>
349-
generatedTypeConstraint = GeneratedTypeConstraint<
350-
DartType,
351-
TypeParameterElement,
352-
PromotableElement>.lower(element, SharedTypeSchemaView(lower));
353-
_constraints.add(generatedTypeConstraint);
354-
if (dataForTesting != null && nodeForTesting != null) {
355-
(dataForTesting!.generatedTypeConstraints[nodeForTesting] ??= [])
356-
.add(generatedTypeConstraint);
357-
}
358-
}
359-
360-
void _addUpper(TypeParameterElement element, DartType upper,
361-
{required AstNode? nodeForTesting}) {
362-
GeneratedTypeConstraint<DartType, TypeParameterElement, PromotableElement>
363-
generatedTypeConstraint = GeneratedTypeConstraint<
364-
DartType,
365-
TypeParameterElement,
366-
PromotableElement>.upper(element, SharedTypeSchemaView(upper));
367-
_constraints.add(generatedTypeConstraint);
368-
if (dataForTesting != null && nodeForTesting != null) {
369-
(dataForTesting!.generatedTypeConstraints[nodeForTesting] ??= [])
370-
.add(generatedTypeConstraint);
371-
}
372-
}
373-
374380
/// Matches [P] against [Q], where [P] and [Q] are both function types.
375381
///
376382
/// If [P] is a subtype of [Q] under some constraints, the constraints making

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,33 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
135135
constrainSupertype: false, treeNodeForTesting: treeNodeForTesting);
136136
}
137137

138-
/// Add constraint: [lower] <: [parameter] <: TOP.
139-
void _constrainParameterLower(StructuralParameter parameter, DartType lower,
140-
{required TreeNode? treeNodeForTesting}) {
138+
@override
139+
void addLowerConstraintForParameter(
140+
StructuralParameter parameter, DartType lower,
141+
{required TreeNode? nodeForTesting}) {
141142
GeneratedTypeConstraint generatedTypeConstraint =
142143
new GeneratedTypeConstraint.lower(
143144
parameter, new SharedTypeSchemaView(lower));
144-
if (treeNodeForTesting != null && _inferenceResultForTesting != null) {
145+
if (nodeForTesting != null && _inferenceResultForTesting != null) {
145146
// Coverage-ignore-block(suite): Not run.
146-
(_inferenceResultForTesting
147-
.generatedTypeConstraints[treeNodeForTesting] ??= [])
147+
(_inferenceResultForTesting.generatedTypeConstraints[nodeForTesting] ??=
148+
[])
148149
.add(generatedTypeConstraint);
149150
}
150151
_protoConstraints.add(generatedTypeConstraint);
151152
}
152153

153-
/// Add constraint: BOTTOM <: [parameter] <: [upper].
154-
void _constrainParameterUpper(StructuralParameter parameter, DartType upper,
155-
{required TreeNode? treeNodeForTesting}) {
154+
@override
155+
void addUpperConstraintForParameter(
156+
StructuralParameter parameter, DartType upper,
157+
{required TreeNode? nodeForTesting}) {
156158
GeneratedTypeConstraint generatedTypeConstraint =
157159
new GeneratedTypeConstraint.upper(
158160
parameter, new SharedTypeSchemaView(upper));
159-
if (treeNodeForTesting != null && _inferenceResultForTesting != null) {
161+
if (nodeForTesting != null && _inferenceResultForTesting != null) {
160162
// Coverage-ignore-block(suite): Not run.
161-
(_inferenceResultForTesting
162-
.generatedTypeConstraints[treeNodeForTesting] ??= [])
163+
(_inferenceResultForTesting.generatedTypeConstraints[nodeForTesting] ??=
164+
[])
163165
.add(generatedTypeConstraint);
164166
}
165167
_protoConstraints.add(generatedTypeConstraint);
@@ -255,8 +257,8 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
255257
case StructuralParameter pParameter?
256258
when pNullability == NullabilitySuffix.none &&
257259
_parametersToConstrain.contains(pParameter)) {
258-
_constrainParameterUpper(pParameter, q,
259-
treeNodeForTesting: treeNodeForTesting);
260+
addUpperConstraintForParameter(pParameter, q,
261+
nodeForTesting: treeNodeForTesting);
260262
return true;
261263
}
262264

@@ -273,8 +275,8 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
273275
p,
274276
typeOperations.greatestClosureOfTypeInternal(
275277
qParameter.bound, _parametersToConstrain)))) {
276-
_constrainParameterLower(qParameter, p,
277-
treeNodeForTesting: treeNodeForTesting);
278+
addLowerConstraintForParameter(qParameter, p,
279+
nodeForTesting: treeNodeForTesting);
278280
return true;
279281
}
280282

@@ -526,17 +528,17 @@ class TypeConstraintGatherer extends shared.TypeConstraintGenerator<
526528
"Unsupported type '${type.runtimeType}'."));
527529
for (GeneratedTypeConstraint constraint in constraints) {
528530
if (constraint.isUpper) {
529-
_constrainParameterUpper(
531+
addUpperConstraintForParameter(
530532
constraint.typeParameter,
531533
eliminator.eliminateToLeast(
532534
constraint.constraint.unwrapTypeSchemaView()),
533-
treeNodeForTesting: treeNodeForTesting);
535+
nodeForTesting: treeNodeForTesting);
534536
} else {
535-
_constrainParameterLower(
537+
addLowerConstraintForParameter(
536538
constraint.typeParameter,
537539
eliminator.eliminateToGreatest(
538540
constraint.constraint.unwrapTypeSchemaView()),
539-
treeNodeForTesting: treeNodeForTesting);
541+
nodeForTesting: treeNodeForTesting);
540542
}
541543
}
542544
return true;

0 commit comments

Comments
 (0)