[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
https://github.com/dart-lang/sdk/issues/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 <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/analyzer/lib/dart/element/type.dart b/pkg/analyzer/lib/dart/element/type.dart
index f7f1f51..d1b1d13 100644
--- a/pkg/analyzer/lib/dart/element/type.dart
+++ b/pkg/analyzer/lib/dart/element/type.dart
@@ -28,13 +28,14 @@
 import 'package:analyzer/dart/element/element2.dart';
 import 'package:analyzer/dart/element/nullability_suffix.dart';
 import 'package:analyzer/dart/element/type_visitor.dart';
-import 'package:analyzer/src/dart/element/type.dart' show RecordTypeImpl;
+import 'package:analyzer/src/dart/element/type.dart'
+    show RecordTypeImpl, TypeImpl;
 import 'package:meta/meta.dart';
 
 /// The type associated with elements in the element model.
 ///
 /// Clients may not extend, implement or mix-in this class.
-abstract class DartType implements SharedTypeStructure<DartType> {
+abstract class DartType implements SharedTypeStructure<TypeImpl> {
   /// If this type is an instantiation of a type alias, information about
   /// the alias element, and the type arguments.
   /// Otherwise return `null`.
@@ -208,6 +209,15 @@
     @Deprecated('Only non-nullable by default mode is supported')
     bool withNullability = true,
   });
+
+  /// Determines whether this type is the same as [other].
+  ///
+  /// Deprecated: this getter is a part of the analyzer's private
+  /// implementation, and was exposed by accident (see
+  /// https://github.com/dart-lang/sdk/issues/59763). Please use `==` instead.
+  @override
+  @Deprecated('Use `==` instead')
+  bool isStructurallyEqualTo(covariant DartType other);
 }
 
 /// The type `dynamic` is a type which is a supertype of all other types, just
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index 10747ed..22376af92 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -4631,7 +4631,7 @@
   List<TypeParameterElement2> get typeParameters2 => const [];
 
   @override
-  DartType get typeShared => type;
+  TypeImpl get typeShared => type;
 
   @override
   Element? get _enclosingFunction => wrappedElement._enclosingElement3;
@@ -4676,7 +4676,7 @@
 abstract class FormalParameterElementOrMember
     implements
         FormalParameterElement,
-        SharedNamedFunctionParameterStructure<DartType> {}
+        SharedNamedFunctionParameterStructure<TypeImpl> {}
 
 mixin FragmentedAnnotatableElementMixin<E extends Fragment>
     implements FragmentedElementMixin<E> {
@@ -11317,7 +11317,7 @@
         FragmentedAnnotatableElementMixin<TypeParameterFragment>,
         FragmentedElementMixin<TypeParameterFragment>,
         _NonTopLevelVariableOrParameter
-    implements TypeParameterElement2, SharedTypeParameterStructure<DartType> {
+    implements TypeParameterElement2, SharedTypeParameterStructure<TypeImpl> {
   @override
   final TypeParameterElementImpl firstFragment;
 
@@ -11343,7 +11343,9 @@
   TypeParameterElement2 get baseElement => this;
 
   @override
-  DartType? get boundShared => bound;
+  TypeImpl? get boundShared =>
+      // TODO(paulberry): get rid of this cast by changing the type of `bound`.
+      bound as TypeImpl?;
 
   @override
   List<TypeParameterElementImpl> get fragments {
diff --git a/pkg/analyzer/lib/src/dart/element/member.dart b/pkg/analyzer/lib/src/dart/element/member.dart
index a9f3eb6..55a0675 100644
--- a/pkg/analyzer/lib/src/dart/element/member.dart
+++ b/pkg/analyzer/lib/src/dart/element/member.dart
@@ -1261,7 +1261,7 @@
   List<TypeParameterElement2> get typeParameters2 => _element2.typeParameters2;
 
   @override
-  DartType get typeShared => type;
+  TypeImpl get typeShared => type;
 
   @override
   FormalParameterElement get _element2 => declaration.asElement2;
diff --git a/pkg/analyzer/lib/src/dart/element/type.dart b/pkg/analyzer/lib/src/dart/element/type.dart
index bd50bec..daf05b8 100644
--- a/pkg/analyzer/lib/src/dart/element/type.dart
+++ b/pkg/analyzer/lib/src/dart/element/type.dart
@@ -35,7 +35,7 @@
 
 /// The [Type] representing the type `dynamic`.
 class DynamicTypeImpl extends TypeImpl
-    implements DynamicType, SharedDynamicTypeStructure<DartType> {
+    implements DynamicType, SharedDynamicTypeStructure<TypeImpl> {
   /// The unique instance of this class.
   static final DynamicTypeImpl instance = DynamicTypeImpl._();
 
@@ -90,7 +90,7 @@
 class FunctionTypeImpl extends TypeImpl
     implements
         FunctionType,
-        SharedFunctionTypeStructure<DartType, TypeParameterElementImpl2,
+        SharedFunctionTypeStructure<TypeImpl, TypeParameterElementImpl2,
             FormalParameterElementOrMember> {
   @override
   late int hashCode = _computeHashCode();
@@ -108,7 +108,7 @@
   final NullabilitySuffix nullabilitySuffix;
 
   @override
-  final List<DartType> positionalParameterTypes;
+  final List<TypeImpl> positionalParameterTypes;
 
   @override
   final int requiredPositionalParameterCount;
@@ -125,7 +125,7 @@
   }) {
     int? firstNamedParameterIndex;
     var requiredPositionalParameterCount = 0;
-    var positionalParameterTypes = <DartType>[];
+    var positionalParameterTypes = <TypeImpl>[];
     List<ParameterElement> sortedNamedParameters;
 
     // Check if already sorted.
@@ -142,7 +142,9 @@
         }
         lastNamedParameterName = name;
       } else {
-        positionalParameterTypes.add(parameter.type);
+        // TODO(paulberry): get rid of this cast by changing the type of
+        // `parameters` to `List<ParameterElementMixin>`.
+        positionalParameterTypes.add(parameter.type as TypeImpl);
         if (parameter.isRequiredPositional) {
           requiredPositionalParameterCount++;
         }
@@ -232,10 +234,10 @@
       positionalParameterTypes.sublist(requiredPositionalParameterCount);
 
   @override
-  List<DartType> get positionalParameterTypesShared => positionalParameterTypes;
+  List<TypeImpl> get positionalParameterTypesShared => positionalParameterTypes;
 
   @override
-  DartType get returnTypeShared => returnType;
+  TypeImpl get returnTypeShared => returnType;
 
   @override
   // TODO(paulberry): see if this type can be changed to
@@ -1207,7 +1209,7 @@
 }
 
 class InvalidTypeImpl extends TypeImpl
-    implements InvalidType, SharedInvalidTypeStructure<DartType> {
+    implements InvalidType, SharedInvalidTypeStructure<TypeImpl> {
   /// The unique instance of this class.
   static final InvalidTypeImpl instance = InvalidTypeImpl._();
 
@@ -1332,7 +1334,7 @@
 /// A concrete implementation of [DartType] representing the type `Null`, with
 /// no type parameters and no nullability suffix.
 class NullTypeImpl extends InterfaceTypeImpl
-    implements SharedNullTypeStructure<DartType> {
+    implements SharedNullTypeStructure<TypeImpl> {
   NullTypeImpl({
     required super.element3,
     super.alias,
@@ -1358,7 +1360,7 @@
 }
 
 class RecordTypeImpl extends TypeImpl
-    implements RecordType, SharedRecordTypeStructure<DartType> {
+    implements RecordType, SharedRecordTypeStructure<TypeImpl> {
   @override
   final List<RecordTypePositionalFieldImpl> positionalFields;
 
@@ -1369,7 +1371,7 @@
   final NullabilitySuffix nullabilitySuffix;
 
   @override
-  late final List<DartType> positionalTypes = [
+  late final List<TypeImpl> positionalTypes = [
     for (var field in positionalFields) field.type
   ];
 
@@ -1418,13 +1420,13 @@
   List<RecordTypeNamedFieldImpl> get namedTypes => namedFields;
 
   @override
-  List<DartType> get positionalTypesShared => positionalTypes;
+  List<TypeImpl> get positionalTypesShared => positionalTypes;
 
   @override
   List<RecordTypeNamedFieldImpl> get sortedNamedTypes => namedTypes;
 
   @override
-  List<SharedNamedTypeStructure<DartType>> get sortedNamedTypesShared =>
+  List<SharedNamedTypeStructure<TypeImpl>> get sortedNamedTypesShared =>
       sortedNamedTypes;
 
   @override
@@ -1525,7 +1527,7 @@
 }
 
 class RecordTypeNamedFieldImpl extends RecordTypeFieldImpl
-    implements RecordTypeNamedField, SharedNamedTypeStructure<DartType> {
+    implements RecordTypeNamedField, SharedNamedTypeStructure<TypeImpl> {
   @override
   final String name;
 
@@ -1538,7 +1540,7 @@
   String get nameShared => name;
 
   @override
-  DartType get typeShared => type;
+  TypeImpl get typeShared => type;
 }
 
 class RecordTypePositionalFieldImpl extends RecordTypeFieldImpl
@@ -1846,7 +1848,7 @@
 
 /// A concrete implementation of a [VoidType].
 class VoidTypeImpl extends TypeImpl
-    implements VoidType, SharedVoidTypeStructure<DartType> {
+    implements VoidType, SharedVoidTypeStructure<TypeImpl> {
   /// The unique instance of this class, with indeterminate nullability.
   static final VoidTypeImpl instance = VoidTypeImpl._();
 
diff --git a/pkg/analyzer/lib/src/dart/element/type_schema.dart b/pkg/analyzer/lib/src/dart/element/type_schema.dart
index dc50adb..8c98df8 100644
--- a/pkg/analyzer/lib/src/dart/element/type_schema.dart
+++ b/pkg/analyzer/lib/src/dart/element/type_schema.dart
@@ -22,7 +22,7 @@
 /// example `List<_>`. This is distinct from `List<dynamic>`. These types will
 /// never appear in the final resolved AST.
 class UnknownInferredType extends TypeImpl
-    implements SharedUnknownTypeStructure<DartType> {
+    implements SharedUnknownTypeStructure<TypeImpl> {
   static const UnknownInferredType instance = UnknownInferredType._();
 
   const UnknownInferredType._();