Skip to content

Commit

Permalink
[analyzer] Change return type of some TypeSystemImpl methods.
Browse files Browse the repository at this point in the history
The following methods of `TypeSystemImpl` have their return type
changed from `DartType` to `TypeImpl`:

- `flatten`
- `greatestClosureOfSchema`
- `leastClosureOfSchema`
- `resolveToBound`

This is part of a larger arc of work to change the analyzer's use of
the shared code so that the type parameters it supplies are not part
of the analyzer public API. See
#59763.

Change-Id: I9cf82a644b944f18bae9435ba68c896d45723281
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405062
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Paul Berry <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 17, 2025
1 parent 54981a4 commit f61ecec
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
29 changes: 19 additions & 10 deletions pkg/analyzer/lib/src/dart/element/type_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,18 @@ class TypeSystemImpl implements TypeSystem {
}

@override
DartType flatten(DartType T) {
if (identical(T, UnknownInferredType.instance)) {
TypeImpl flatten(DartType T) {
// TODO(paulberry): remove this cast by making the parameter `T` covariant
// and changing its type to `TypeImpl`.
if (identical(T as TypeImpl, UnknownInferredType.instance)) {
return T;
}

// if T is S? then flatten(T) = flatten(S)?
var nullabilitySuffix = T.nullabilitySuffix;
if (nullabilitySuffix != NullabilitySuffix.none) {
var S = (T as TypeImpl).withNullability(NullabilitySuffix.none);
return (flatten(S) as TypeImpl).withNullability(nullabilitySuffix);
var S = T.withNullability(NullabilitySuffix.none);
return flatten(S).withNullability(nullabilitySuffix);
}

// If T is X & S for some type variable X and type S then:
Expand Down Expand Up @@ -629,13 +631,15 @@ class TypeSystemImpl implements TypeSystem {
///
/// Note that the greatest closure of a type schema is always a supertype of
/// any type which matches the schema.
DartType greatestClosureOfSchema(DartType schema) {
TypeImpl greatestClosureOfSchema(DartType schema) {
// TODO(paulberry): remove this cast by making `ReplacementVisitor`
// implement `TypeVisitor<TypeImpl?>`.
return TypeSchemaEliminationVisitor.run(
topType: objectQuestion,
bottomType: NeverTypeImpl.instance,
isLeastClosure: false,
schema: schema,
);
) as TypeImpl;
}

@override
Expand Down Expand Up @@ -1413,13 +1417,15 @@ class TypeSystemImpl implements TypeSystem {
///
/// Note that the least closure of a type schema is always a subtype of any
/// type which matches the schema.
DartType leastClosureOfSchema(DartType schema) {
TypeImpl leastClosureOfSchema(DartType schema) {
// TODO(paulberry): remove this cast by making `ReplacementVisitor`
// implement `TypeVisitor<TypeImpl?>`.
return TypeSchemaEliminationVisitor.run(
topType: objectQuestion,
bottomType: NeverTypeImpl.instance,
isLeastClosure: true,
schema: schema,
);
) as TypeImpl;
}

@override
Expand Down Expand Up @@ -1717,7 +1723,10 @@ class TypeSystemImpl implements TypeSystem {
}

@override
DartType resolveToBound(DartType type) {
TypeImpl resolveToBound(DartType type) {
// TODO(paulberry): remove this cast by making the parameter `T` covariant
// and changing its type to `TypeImpl`.
type as TypeImpl;
if (type is TypeParameterTypeImpl) {
var promotedBound = type.promotedBound;
if (promotedBound != null) {
Expand All @@ -1729,7 +1738,7 @@ class TypeSystemImpl implements TypeSystem {
return objectQuestion;
}

var resolved = resolveToBound(bound) as TypeImpl;
var resolved = resolveToBound(bound);

var newNullabilitySuffix = uniteNullabilities(
uniteNullabilities(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_provider.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/dart/resolver/type_property_resolver.dart';
Expand Down Expand Up @@ -165,7 +166,7 @@ class CommentReferenceResolver {
var extendedType = _typeSystem.resolveToBound(
enclosingExtension.extendedType,
);
if (extendedType is InterfaceType) {
if (extendedType is InterfaceTypeImpl) {
enclosingType = extendedType;
} else if (extendedType is FunctionType) {
enclosingType = _typeProvider.functionType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/extensions.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_provider.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/dart/resolver/extension_member_resolver.dart';
Expand Down Expand Up @@ -180,7 +181,7 @@ class TypePropertyResolver {
} else {
var receiverTypeResolved = _typeSystem.resolveToBound(receiverType);

if (receiverTypeResolved is InterfaceType) {
if (receiverTypeResolved is InterfaceTypeImpl) {
_lookupInterfaceType(receiverTypeResolved);
if (_hasGetterOrSetter) {
return _toResult();
Expand All @@ -193,7 +194,7 @@ class TypePropertyResolver {
}
}

if (receiverTypeResolved is FunctionType &&
if (receiverTypeResolved is FunctionTypeImpl &&
_name == FunctionElement.CALL_METHOD_NAME) {
return ResolutionResult(
needsGetterError: false,
Expand All @@ -209,7 +210,7 @@ class TypePropertyResolver {
return _toResult();
}

if (receiverTypeResolved is RecordType) {
if (receiverTypeResolved is RecordTypeImpl) {
var field = receiverTypeResolved.fieldByName(name);
if (field != null) {
return ResolutionResult(
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
}

var context = typeSystem.flatten(contextType);
if (context is! FunctionType || context.typeFormals.isNotEmpty) {
if (context is! FunctionTypeImpl || context.typeFormals.isNotEmpty) {
return expression;
}

Expand Down

0 comments on commit f61ecec

Please sign in to comment.