Skip to content

Commit bb7106a

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Change DartType to implement SharedTypeStructure<TypeImpl>.
The type `DartType` is changed so that instead of implementing `SharedTypeStructure<DartType>`, it implements `SharedTypeStructure<TypeImpl>`. 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. To ensure that this change doesn't expose the private `TypeImpl` type through the analyzer public API, an abstract override of `SharedTypeStructure.isStructurallyEqualTo` is added to `DartType`, with a parameter type of `DartType`. (Without the override, the parameter type would be `SharedTypeStructure<TypeImpl>`.) This method has been marked as deprecated, because it was not intentional for it to be exposed through the analyzer's public API. The following types, derived from `DartType`, require similar changes due to the fact that they implement shared types that are subtypes of `SharedTypeStructure`: - `DynamicTypeImpl` now implements `SharedDynamicTypeStructure<TypeImpl>`. - `FunctionTypeImpl` now implements `SharedFunctionTypeStructure<TypeImpl, TypeParameterElementImpl2, FormalParameterElementOrMember>`. - `InvalidTypeImpl` now implements `SharedInvalidTypeStructure<TypeImpl>`. - `NullTypeImpl` now implements `SharedNullTypeStructure<TypeImpl>`. - `RecordTypeImpl` now implements `SharedRecordTypeStructure<TypeImpl>`. - `VoidTypeImpl` now implements `SharedVoidTypeStructure<TypeImpl>`. - `UnknownInferredType` now implements `SharedUnknownTypeStructure<TypeImpl>`. Since the type `SharedNamedFunctionParameterStructure` is tightly integrated with `SharedFunctionTypeStructure`, `FormalParameterElementOrMember` must also be changed, so that it implements `SharedNamedFunctionParameterStructure<TypeImpl>` rather than `SharedNamedFunctionParameterStructure<DartType>`. Similarly, `TypeParameterElementImpl2` must be changed so that it implements `SharedTypeParameterStructure<TypeImpl>` rather than `SharedTypeParameterStructure<DartType>`. Follow-up CLs will change other uses of shared generic types so that they supply a type argument of `TypeImpl` rather than `DartType`. Change-Id: Ib36491056d46e4cd706c0dc2b85adc5314cb0b2c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403944 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent dc709bb commit bb7106a

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

pkg/analyzer/lib/dart/element/type.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ import 'package:analyzer/dart/element/element.dart';
2828
import 'package:analyzer/dart/element/element2.dart';
2929
import 'package:analyzer/dart/element/nullability_suffix.dart';
3030
import 'package:analyzer/dart/element/type_visitor.dart';
31-
import 'package:analyzer/src/dart/element/type.dart' show RecordTypeImpl;
31+
import 'package:analyzer/src/dart/element/type.dart'
32+
show RecordTypeImpl, TypeImpl;
3233
import 'package:meta/meta.dart';
3334

3435
/// The type associated with elements in the element model.
3536
///
3637
/// Clients may not extend, implement or mix-in this class.
37-
abstract class DartType implements SharedTypeStructure<DartType> {
38+
abstract class DartType implements SharedTypeStructure<TypeImpl> {
3839
/// If this type is an instantiation of a type alias, information about
3940
/// the alias element, and the type arguments.
4041
/// Otherwise return `null`.
@@ -208,6 +209,15 @@ abstract class DartType implements SharedTypeStructure<DartType> {
208209
@Deprecated('Only non-nullable by default mode is supported')
209210
bool withNullability = true,
210211
});
212+
213+
/// Determines whether this type is the same as [other].
214+
///
215+
/// Deprecated: this getter is a part of the analyzer's private
216+
/// implementation, and was exposed by accident (see
217+
/// https://github.com/dart-lang/sdk/issues/59763). Please use `==` instead.
218+
@override
219+
@Deprecated('Use `==` instead')
220+
bool isStructurallyEqualTo(covariant DartType other);
211221
}
212222

213223
/// The type `dynamic` is a type which is a supertype of all other types, just

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4631,7 +4631,7 @@ class FormalParameterElementImpl extends PromotableElementImpl2
46314631
List<TypeParameterElement2> get typeParameters2 => const [];
46324632

46334633
@override
4634-
DartType get typeShared => type;
4634+
TypeImpl get typeShared => type;
46354635

46364636
@override
46374637
Element? get _enclosingFunction => wrappedElement._enclosingElement3;
@@ -4676,7 +4676,7 @@ mixin FormalParameterElementMixin implements FormalParameterElement {
46764676
abstract class FormalParameterElementOrMember
46774677
implements
46784678
FormalParameterElement,
4679-
SharedNamedFunctionParameterStructure<DartType> {}
4679+
SharedNamedFunctionParameterStructure<TypeImpl> {}
46804680

46814681
mixin FragmentedAnnotatableElementMixin<E extends Fragment>
46824682
implements FragmentedElementMixin<E> {
@@ -11317,7 +11317,7 @@ class TypeParameterElementImpl2 extends TypeDefiningElementImpl2
1131711317
FragmentedAnnotatableElementMixin<TypeParameterFragment>,
1131811318
FragmentedElementMixin<TypeParameterFragment>,
1131911319
_NonTopLevelVariableOrParameter
11320-
implements TypeParameterElement2, SharedTypeParameterStructure<DartType> {
11320+
implements TypeParameterElement2, SharedTypeParameterStructure<TypeImpl> {
1132111321
@override
1132211322
final TypeParameterElementImpl firstFragment;
1132311323

@@ -11343,7 +11343,9 @@ class TypeParameterElementImpl2 extends TypeDefiningElementImpl2
1134311343
TypeParameterElement2 get baseElement => this;
1134411344

1134511345
@override
11346-
DartType? get boundShared => bound;
11346+
TypeImpl? get boundShared =>
11347+
// TODO(paulberry): get rid of this cast by changing the type of `bound`.
11348+
bound as TypeImpl?;
1134711349

1134811350
@override
1134911351
List<TypeParameterElementImpl> get fragments {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ class ParameterMember extends VariableMember
12611261
List<TypeParameterElement2> get typeParameters2 => _element2.typeParameters2;
12621262

12631263
@override
1264-
DartType get typeShared => type;
1264+
TypeImpl get typeShared => type;
12651265

12661266
@override
12671267
FormalParameterElement get _element2 => declaration.asElement2;

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ List<DartType> fixedTypeList(DartType e1, [DartType? e2]) {
3535

3636
/// The [Type] representing the type `dynamic`.
3737
class DynamicTypeImpl extends TypeImpl
38-
implements DynamicType, SharedDynamicTypeStructure<DartType> {
38+
implements DynamicType, SharedDynamicTypeStructure<TypeImpl> {
3939
/// The unique instance of this class.
4040
static final DynamicTypeImpl instance = DynamicTypeImpl._();
4141

@@ -90,7 +90,7 @@ class DynamicTypeImpl extends TypeImpl
9090
class FunctionTypeImpl extends TypeImpl
9191
implements
9292
FunctionType,
93-
SharedFunctionTypeStructure<DartType, TypeParameterElementImpl2,
93+
SharedFunctionTypeStructure<TypeImpl, TypeParameterElementImpl2,
9494
FormalParameterElementOrMember> {
9595
@override
9696
late int hashCode = _computeHashCode();
@@ -108,7 +108,7 @@ class FunctionTypeImpl extends TypeImpl
108108
final NullabilitySuffix nullabilitySuffix;
109109

110110
@override
111-
final List<DartType> positionalParameterTypes;
111+
final List<TypeImpl> positionalParameterTypes;
112112

113113
@override
114114
final int requiredPositionalParameterCount;
@@ -125,7 +125,7 @@ class FunctionTypeImpl extends TypeImpl
125125
}) {
126126
int? firstNamedParameterIndex;
127127
var requiredPositionalParameterCount = 0;
128-
var positionalParameterTypes = <DartType>[];
128+
var positionalParameterTypes = <TypeImpl>[];
129129
List<ParameterElement> sortedNamedParameters;
130130

131131
// Check if already sorted.
@@ -142,7 +142,9 @@ class FunctionTypeImpl extends TypeImpl
142142
}
143143
lastNamedParameterName = name;
144144
} else {
145-
positionalParameterTypes.add(parameter.type);
145+
// TODO(paulberry): get rid of this cast by changing the type of
146+
// `parameters` to `List<ParameterElementMixin>`.
147+
positionalParameterTypes.add(parameter.type as TypeImpl);
146148
if (parameter.isRequiredPositional) {
147149
requiredPositionalParameterCount++;
148150
}
@@ -232,10 +234,10 @@ class FunctionTypeImpl extends TypeImpl
232234
positionalParameterTypes.sublist(requiredPositionalParameterCount);
233235

234236
@override
235-
List<DartType> get positionalParameterTypesShared => positionalParameterTypes;
237+
List<TypeImpl> get positionalParameterTypesShared => positionalParameterTypes;
236238

237239
@override
238-
DartType get returnTypeShared => returnType;
240+
TypeImpl get returnTypeShared => returnType;
239241

240242
@override
241243
// TODO(paulberry): see if this type can be changed to
@@ -1207,7 +1209,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
12071209
}
12081210

12091211
class InvalidTypeImpl extends TypeImpl
1210-
implements InvalidType, SharedInvalidTypeStructure<DartType> {
1212+
implements InvalidType, SharedInvalidTypeStructure<TypeImpl> {
12111213
/// The unique instance of this class.
12121214
static final InvalidTypeImpl instance = InvalidTypeImpl._();
12131215

@@ -1332,7 +1334,7 @@ class NeverTypeImpl extends TypeImpl implements NeverType {
13321334
/// A concrete implementation of [DartType] representing the type `Null`, with
13331335
/// no type parameters and no nullability suffix.
13341336
class NullTypeImpl extends InterfaceTypeImpl
1335-
implements SharedNullTypeStructure<DartType> {
1337+
implements SharedNullTypeStructure<TypeImpl> {
13361338
NullTypeImpl({
13371339
required super.element3,
13381340
super.alias,
@@ -1358,7 +1360,7 @@ abstract class RecordTypeFieldImpl implements RecordTypeField {
13581360
}
13591361

13601362
class RecordTypeImpl extends TypeImpl
1361-
implements RecordType, SharedRecordTypeStructure<DartType> {
1363+
implements RecordType, SharedRecordTypeStructure<TypeImpl> {
13621364
@override
13631365
final List<RecordTypePositionalFieldImpl> positionalFields;
13641366

@@ -1369,7 +1371,7 @@ class RecordTypeImpl extends TypeImpl
13691371
final NullabilitySuffix nullabilitySuffix;
13701372

13711373
@override
1372-
late final List<DartType> positionalTypes = [
1374+
late final List<TypeImpl> positionalTypes = [
13731375
for (var field in positionalFields) field.type
13741376
];
13751377

@@ -1418,13 +1420,13 @@ class RecordTypeImpl extends TypeImpl
14181420
List<RecordTypeNamedFieldImpl> get namedTypes => namedFields;
14191421

14201422
@override
1421-
List<DartType> get positionalTypesShared => positionalTypes;
1423+
List<TypeImpl> get positionalTypesShared => positionalTypes;
14221424

14231425
@override
14241426
List<RecordTypeNamedFieldImpl> get sortedNamedTypes => namedTypes;
14251427

14261428
@override
1427-
List<SharedNamedTypeStructure<DartType>> get sortedNamedTypesShared =>
1429+
List<SharedNamedTypeStructure<TypeImpl>> get sortedNamedTypesShared =>
14281430
sortedNamedTypes;
14291431

14301432
@override
@@ -1525,7 +1527,7 @@ class RecordTypeImpl extends TypeImpl
15251527
}
15261528

15271529
class RecordTypeNamedFieldImpl extends RecordTypeFieldImpl
1528-
implements RecordTypeNamedField, SharedNamedTypeStructure<DartType> {
1530+
implements RecordTypeNamedField, SharedNamedTypeStructure<TypeImpl> {
15291531
@override
15301532
final String name;
15311533

@@ -1538,7 +1540,7 @@ class RecordTypeNamedFieldImpl extends RecordTypeFieldImpl
15381540
String get nameShared => name;
15391541

15401542
@override
1541-
DartType get typeShared => type;
1543+
TypeImpl get typeShared => type;
15421544
}
15431545

15441546
class RecordTypePositionalFieldImpl extends RecordTypeFieldImpl
@@ -1846,7 +1848,7 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
18461848

18471849
/// A concrete implementation of a [VoidType].
18481850
class VoidTypeImpl extends TypeImpl
1849-
implements VoidType, SharedVoidTypeStructure<DartType> {
1851+
implements VoidType, SharedVoidTypeStructure<TypeImpl> {
18501852
/// The unique instance of this class, with indeterminate nullability.
18511853
static final VoidTypeImpl instance = VoidTypeImpl._();
18521854

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import 'package:analyzer/src/dart/element/type_visitor.dart';
2222
/// example `List<_>`. This is distinct from `List<dynamic>`. These types will
2323
/// never appear in the final resolved AST.
2424
class UnknownInferredType extends TypeImpl
25-
implements SharedUnknownTypeStructure<DartType> {
25+
implements SharedUnknownTypeStructure<TypeImpl> {
2626
static const UnknownInferredType instance = UnknownInferredType._();
2727

2828
const UnknownInferredType._();

0 commit comments

Comments
 (0)