-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[_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]>
- Loading branch information
1 parent
61336f3
commit e1ba3ea
Showing
3 changed files
with
189 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
pkg/_fe_analyzer_shared/test/mini_type_constraint_gatherer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:_fe_analyzer_shared/src/type_inference/type_analyzer_operations.dart'; | ||
import 'package:_fe_analyzer_shared/src/type_inference/type_constraint.dart'; | ||
import 'package:_fe_analyzer_shared/src/types/shared_type.dart'; | ||
|
||
import 'mini_ast.dart'; | ||
import 'mini_types.dart'; | ||
|
||
class TypeConstraintGatherer extends TypeConstraintGenerator<Type, | ||
NamedFunctionParameter, Var, TypeParameter, Type, String, Node> | ||
with | ||
TypeConstraintGeneratorMixin<Type, NamedFunctionParameter, Var, | ||
TypeParameter, Type, String, Node> { | ||
@override | ||
final Set<TypeParameter> typeParametersToConstrain = <TypeParameter>{}; | ||
|
||
@override | ||
final bool enableDiscrepantObliviousnessOfNullabilitySuffixOfFutureOr; | ||
|
||
@override | ||
final MiniAstOperations typeAnalyzerOperations = MiniAstOperations(); | ||
|
||
final constraints = <String>[]; | ||
|
||
TypeConstraintGatherer(Set<String> typeVariablesBeingConstrained, | ||
{this.enableDiscrepantObliviousnessOfNullabilitySuffixOfFutureOr = false}) | ||
: super(inferenceUsingBoundsIsEnabled: false) { | ||
for (var typeVariableName in typeVariablesBeingConstrained) { | ||
typeParametersToConstrain | ||
.add(TypeRegistry.addTypeParameter(typeVariableName)); | ||
} | ||
} | ||
|
||
@override | ||
TypeConstraintGeneratorState get currentState => | ||
TypeConstraintGeneratorState(constraints.length); | ||
|
||
@override | ||
void addLowerConstraintForParameter(TypeParameter typeParameter, Type lower, | ||
{required Node? astNodeForTesting}) { | ||
constraints.add('$lower <: $typeParameter'); | ||
} | ||
|
||
@override | ||
void addUpperConstraintForParameter(TypeParameter typeParameter, Type upper, | ||
{required Node? astNodeForTesting}) { | ||
constraints.add('$typeParameter <: $upper'); | ||
} | ||
|
||
@override | ||
Map<TypeParameter, | ||
MergedTypeConstraint<Type, TypeParameter, Var, Type, String>> | ||
computeConstraints() { | ||
// TODO(cstefantsova): implement computeConstraints | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
void eliminateTypeParametersInGeneratedConstraints( | ||
Object eliminator, TypeConstraintGeneratorState eliminationStartState, | ||
{required Node? astNodeForTesting}) { | ||
// TODO(paulberry): implement eliminateTypeParametersInGeneratedConstraints | ||
} | ||
|
||
@override | ||
List<Type>? getTypeArgumentsAsInstanceOf(Type type, String typeDeclaration) { | ||
// We just have a few cases hardcoded here to make the tests work. | ||
// TODO(paulberry): if this gets too unwieldy, replace it with a more | ||
// general implementation. | ||
switch ((type, typeDeclaration)) { | ||
case (PrimaryType(name: 'List', :var args), 'Iterable'): | ||
// List<T> inherits from Iterable<T> | ||
return args; | ||
case (PrimaryType(name: 'MyListOfInt'), 'List'): | ||
// MyListOfInt inherits from List<int> | ||
return [Type('int')]; | ||
case (PrimaryType(name: 'Future'), 'int'): | ||
case (PrimaryType(name: 'int'), 'String'): | ||
case (PrimaryType(name: 'List'), 'Future'): | ||
case (PrimaryType(name: 'String'), 'int'): | ||
case (PrimaryType(name: 'Future'), 'String'): | ||
// Unrelated types | ||
return null; | ||
default: | ||
throw UnimplementedError( | ||
'getTypeArgumentsAsInstanceOf($type, $typeDeclaration)'); | ||
} | ||
} | ||
|
||
@override | ||
( | ||
Type, | ||
Type, { | ||
List<TypeParameter> typeParametersToEliminate | ||
}) instantiateFunctionTypesAndProvideFreshTypeParameters( | ||
SharedFunctionTypeStructure<Type, TypeParameter, NamedFunctionParameter> | ||
p, | ||
SharedFunctionTypeStructure<Type, TypeParameter, NamedFunctionParameter> | ||
q, | ||
{required bool leftSchema}) { | ||
// TODO(paulberry): implement instantiateFunctionTypesAndProvideEliminator | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
void restoreState(TypeConstraintGeneratorState state) { | ||
constraints.length = state.count; | ||
} | ||
} |
Oops, something went wrong.