Skip to content

Commit e1ba3ea

Browse files
stereotype441Commit Queue
authored andcommitted
[_fe_analyzer_shared] Move TypeConstraintGatherer to its own file.
Since the class `TypeConstraintGatherer` (which is only used in `_fe_analyzer_shared`'s unit tests) is now used both by `type_constraint_gatherer_test.dart` and by `mini_ast.dart`, it makes sense for it to live in its own file. Change-Id: I7c8a58cfbf3724e2c8313b701345bc47ce032522 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404060 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 61336f3 commit e1ba3ea

File tree

3 files changed

+189
-181
lines changed

3 files changed

+189
-181
lines changed

pkg/_fe_analyzer_shared/test/mini_ast.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ import 'package:_fe_analyzer_shared/src/type_inference/variable_bindings.dart';
3434
import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
3535
import 'package:test/test.dart';
3636

37-
import 'type_inference/type_constraint_gatherer_test.dart';
38-
3937
import 'mini_ir.dart';
38+
import 'mini_type_constraint_gatherer.dart';
4039
import 'mini_types.dart';
4140

4241
final RegExp _locationRegExp =
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:_fe_analyzer_shared/src/type_inference/type_analyzer_operations.dart';
6+
import 'package:_fe_analyzer_shared/src/type_inference/type_constraint.dart';
7+
import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
8+
9+
import 'mini_ast.dart';
10+
import 'mini_types.dart';
11+
12+
class TypeConstraintGatherer extends TypeConstraintGenerator<Type,
13+
NamedFunctionParameter, Var, TypeParameter, Type, String, Node>
14+
with
15+
TypeConstraintGeneratorMixin<Type, NamedFunctionParameter, Var,
16+
TypeParameter, Type, String, Node> {
17+
@override
18+
final Set<TypeParameter> typeParametersToConstrain = <TypeParameter>{};
19+
20+
@override
21+
final bool enableDiscrepantObliviousnessOfNullabilitySuffixOfFutureOr;
22+
23+
@override
24+
final MiniAstOperations typeAnalyzerOperations = MiniAstOperations();
25+
26+
final constraints = <String>[];
27+
28+
TypeConstraintGatherer(Set<String> typeVariablesBeingConstrained,
29+
{this.enableDiscrepantObliviousnessOfNullabilitySuffixOfFutureOr = false})
30+
: super(inferenceUsingBoundsIsEnabled: false) {
31+
for (var typeVariableName in typeVariablesBeingConstrained) {
32+
typeParametersToConstrain
33+
.add(TypeRegistry.addTypeParameter(typeVariableName));
34+
}
35+
}
36+
37+
@override
38+
TypeConstraintGeneratorState get currentState =>
39+
TypeConstraintGeneratorState(constraints.length);
40+
41+
@override
42+
void addLowerConstraintForParameter(TypeParameter typeParameter, Type lower,
43+
{required Node? astNodeForTesting}) {
44+
constraints.add('$lower <: $typeParameter');
45+
}
46+
47+
@override
48+
void addUpperConstraintForParameter(TypeParameter typeParameter, Type upper,
49+
{required Node? astNodeForTesting}) {
50+
constraints.add('$typeParameter <: $upper');
51+
}
52+
53+
@override
54+
Map<TypeParameter,
55+
MergedTypeConstraint<Type, TypeParameter, Var, Type, String>>
56+
computeConstraints() {
57+
// TODO(cstefantsova): implement computeConstraints
58+
throw UnimplementedError();
59+
}
60+
61+
@override
62+
void eliminateTypeParametersInGeneratedConstraints(
63+
Object eliminator, TypeConstraintGeneratorState eliminationStartState,
64+
{required Node? astNodeForTesting}) {
65+
// TODO(paulberry): implement eliminateTypeParametersInGeneratedConstraints
66+
}
67+
68+
@override
69+
List<Type>? getTypeArgumentsAsInstanceOf(Type type, String typeDeclaration) {
70+
// We just have a few cases hardcoded here to make the tests work.
71+
// TODO(paulberry): if this gets too unwieldy, replace it with a more
72+
// general implementation.
73+
switch ((type, typeDeclaration)) {
74+
case (PrimaryType(name: 'List', :var args), 'Iterable'):
75+
// List<T> inherits from Iterable<T>
76+
return args;
77+
case (PrimaryType(name: 'MyListOfInt'), 'List'):
78+
// MyListOfInt inherits from List<int>
79+
return [Type('int')];
80+
case (PrimaryType(name: 'Future'), 'int'):
81+
case (PrimaryType(name: 'int'), 'String'):
82+
case (PrimaryType(name: 'List'), 'Future'):
83+
case (PrimaryType(name: 'String'), 'int'):
84+
case (PrimaryType(name: 'Future'), 'String'):
85+
// Unrelated types
86+
return null;
87+
default:
88+
throw UnimplementedError(
89+
'getTypeArgumentsAsInstanceOf($type, $typeDeclaration)');
90+
}
91+
}
92+
93+
@override
94+
(
95+
Type,
96+
Type, {
97+
List<TypeParameter> typeParametersToEliminate
98+
}) instantiateFunctionTypesAndProvideFreshTypeParameters(
99+
SharedFunctionTypeStructure<Type, TypeParameter, NamedFunctionParameter>
100+
p,
101+
SharedFunctionTypeStructure<Type, TypeParameter, NamedFunctionParameter>
102+
q,
103+
{required bool leftSchema}) {
104+
// TODO(paulberry): implement instantiateFunctionTypesAndProvideEliminator
105+
throw UnimplementedError();
106+
}
107+
108+
@override
109+
void restoreState(TypeConstraintGeneratorState state) {
110+
constraints.length = state.count;
111+
}
112+
}

0 commit comments

Comments
 (0)