Skip to content

Commit

Permalink
[_fe_analyzer_shared] Move TypeConstraintGatherer to its own file.
Browse files Browse the repository at this point in the history
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
stereotype441 authored and Commit Queue committed Jan 14, 2025
1 parent 61336f3 commit e1ba3ea
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 181 deletions.
3 changes: 1 addition & 2 deletions pkg/_fe_analyzer_shared/test/mini_ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ import 'package:_fe_analyzer_shared/src/type_inference/variable_bindings.dart';
import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
import 'package:test/test.dart';

import 'type_inference/type_constraint_gatherer_test.dart';

import 'mini_ir.dart';
import 'mini_type_constraint_gatherer.dart';
import 'mini_types.dart';

final RegExp _locationRegExp =
Expand Down
112 changes: 112 additions & 0 deletions pkg/_fe_analyzer_shared/test/mini_type_constraint_gatherer.dart
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;
}
}
Loading

0 comments on commit e1ba3ea

Please sign in to comment.