Skip to content

Commit 9dea0a8

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Add InterfaceType.asInstanceOf(ClassElement) to API.
[email protected] Change-Id: I91c61aea37bb4ccfb300083e1b017b8e71ef6d6d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153703 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent d4ffb92 commit 9dea0a8

File tree

11 files changed

+32
-31
lines changed

11 files changed

+32
-31
lines changed

pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:analysis_server/src/services/correction/assist.dart';
66
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
77
import 'package:analysis_server/src/services/correction/fix.dart';
88
import 'package:analyzer/dart/ast/ast.dart';
9-
import 'package:analyzer/src/dart/element/type.dart';
9+
import 'package:analyzer/dart/element/type.dart';
1010
import 'package:analyzer_plugin/utilities/assist/assist.dart';
1111
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
1212
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -127,7 +127,7 @@ class ReplaceWithVar extends CorrectionProducer {
127127
return false;
128128
}
129129
final iterableType = parent.iterable.staticType;
130-
if (iterableType is InterfaceTypeImpl) {
130+
if (iterableType is InterfaceType) {
131131
var instantiatedType =
132132
iterableType.asInstanceOf(typeProvider.iterableElement);
133133
if (instantiatedType?.typeArguments?.first == staticType) {

pkg/analyzer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Added `DynamicType`, `NeverType`, and `VoidType` interfaces.
44
* Added `TypeVisitor` and `DartType.accept(TypeVisitor)`.
55
* Changed `ConstructorElement.returnType` to `InterfaceType`.
6+
* Added `InterfaceType.asInstanceOf(ClassElement)`.
67

78
## 0.39.12
89
* Deprecated `canUseSummaries` in `DartSdkManager` constructor.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ abstract class InterfaceType implements ParameterizedType {
295295
/// represent a mixin declaration.
296296
List<InterfaceType> get superclassConstraints;
297297

298+
/// Return the canonical interface that this type implements for [element],
299+
/// or `null` if such an interface does not exist.
300+
///
301+
/// For example, given the following definitions
302+
/// ```
303+
/// class A<E> {}
304+
/// class B<E> implements A<E> {}
305+
/// class C implements A<String> {}
306+
/// ```
307+
/// Asking the type `B<int>` for the type associated with `A` will return the
308+
/// type `A<int>`. Asking the type `C` for the type associated with `A` will
309+
/// return the type `A<String>`.
310+
InterfaceType asInstanceOf(ClassElement element);
311+
298312
/// Return the element representing the getter with the given [name] that is
299313
/// declared in this class, or `null` if this class does not declare a getter
300314
/// with the given name.

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -894,20 +894,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
894894
builder.writeInterfaceType(this);
895895
}
896896

897-
/// Return either this type or a supertype of this type that is defined by the
898-
/// [targetElement], or `null` if such a type does not exist. If this type
899-
/// inherits from the target element along multiple paths, then the returned
900-
/// type is arbitrary.
901-
///
902-
/// For example, given the following definitions
903-
/// ```
904-
/// class A<E> {}
905-
/// class B<E> implements A<E> {}
906-
/// class C implements A<String> {}
907-
/// ```
908-
/// Asking the type `B<int>` for the type associated with `A` will return the
909-
/// type `A<int>`. Asking the type `C` for the type associated with `A` will
910-
/// return the type `A<String>`.
897+
@override
911898
InterfaceType asInstanceOf(ClassElement targetElement) {
912899
if (element == targetElement) {
913900
return this;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ abstract class TypeSystem implements public.TypeSystem {
273273
if (type is! InterfaceType) return null;
274274
if (genericType is! InterfaceType) return null;
275275

276-
var asInstanceOf = (type as InterfaceTypeImpl)
276+
var asInstanceOf = (type as InterfaceType)
277277
.asInstanceOf((genericType as InterfaceType).element);
278278

279279
if (asInstanceOf != null) {

pkg/analyzer/lib/src/dart/resolver/body_inference_context.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class BodyInferenceContext {
138138
}
139139

140140
static DartType _argumentOf(DartType type, ClassElement element) {
141-
if (type is InterfaceTypeImpl) {
141+
if (type is InterfaceType) {
142142
var elementType = type.asInstanceOf(element);
143143
if (elementType != null) {
144144
return elementType.typeArguments[0];

pkg/analyzer/lib/src/dart/resolver/for_resolver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ForResolver {
5858
? _resolver.typeProvider.streamElement
5959
: _resolver.typeProvider.iterableElement;
6060

61-
InterfaceType iteratedType = iterableType is InterfaceTypeImpl
61+
InterfaceType iteratedType = iterableType is InterfaceType
6262
? iterableType.asInstanceOf(iteratedElement)
6363
: null;
6464

pkg/analyzer/lib/src/dart/resolver/typed_literal_resolver.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class TypedLiteralResolver {
178178
var expressionType = element.expression.staticType;
179179
if (expressionType.isDynamic) {
180180
return expressionType;
181-
} else if (expressionType is InterfaceTypeImpl) {
181+
} else if (expressionType is InterfaceType) {
182182
if (expressionType.isDartCoreNull) {
183183
if (element.isNullAware) {
184184
return expressionType;
@@ -336,16 +336,16 @@ class TypedLiteralResolver {
336336
if (!isNull && expressionType is InterfaceType) {
337337
if (_typeSystem.isSubtypeOf2(
338338
expressionType, _typeProvider.iterableForSetMapDisambiguation)) {
339-
InterfaceType iterableType = (expressionType as InterfaceTypeImpl)
340-
.asInstanceOf(_typeProvider.iterableElement);
339+
InterfaceType iterableType =
340+
expressionType.asInstanceOf(_typeProvider.iterableElement);
341341
return _InferredCollectionElementTypeInformation(
342342
elementType: iterableType.typeArguments[0],
343343
keyType: null,
344344
valueType: null);
345345
} else if (_typeSystem.isSubtypeOf2(
346346
expressionType, _typeProvider.mapForSetMapDisambiguation)) {
347-
InterfaceType mapType = (expressionType as InterfaceTypeImpl)
348-
.asInstanceOf(_typeProvider.mapElement);
347+
InterfaceType mapType =
348+
expressionType.asInstanceOf(_typeProvider.mapElement);
349349
List<DartType> typeArguments = mapType.typeArguments;
350350
return _InferredCollectionElementTypeInformation(
351351
elementType: null,

pkg/analyzer/lib/src/error/literal_element_verifier.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/element/type.dart';
88
import 'package:analyzer/dart/element/type_provider.dart';
99
import 'package:analyzer/error/listener.dart';
10-
import 'package:analyzer/src/dart/element/type.dart';
1110
import 'package:analyzer/src/dart/element/type_system.dart';
1211
import 'package:analyzer/src/error/codes.dart';
1312

@@ -145,7 +144,7 @@ class LiteralElementVerifier {
145144
expressionType = typeSystem.resolveToBound(expressionType);
146145

147146
InterfaceType iterableType;
148-
if (expressionType is InterfaceTypeImpl) {
147+
if (expressionType is InterfaceType) {
149148
iterableType = expressionType.asInstanceOf(typeProvider.iterableElement);
150149
}
151150

@@ -188,7 +187,7 @@ class LiteralElementVerifier {
188187
expressionType = typeSystem.resolveToBound(expressionType);
189188

190189
InterfaceType mapType;
191-
if (expressionType is InterfaceTypeImpl) {
190+
if (expressionType is InterfaceType) {
192191
mapType = expressionType.asInstanceOf(typeProvider.mapElement);
193192
}
194193

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
22282228
}
22292229

22302230
DartType sequenceElementType;
2231-
if (iterableType is InterfaceTypeImpl) {
2231+
if (iterableType is InterfaceType) {
22322232
var sequenceElement = awaitKeyword != null
22332233
? _typeProvider.streamElement
22342234
: _typeProvider.iterableElement;

pkg/analyzer/lib/src/task/strong/checker.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class CodeChecker extends RecursiveAstVisitor {
174174
checkAssignment(element.expression, expressionCastType);
175175

176176
var exprType = element.expression.staticType;
177-
var asIterableType = exprType is InterfaceTypeImpl
177+
var asIterableType = exprType is InterfaceType
178178
? exprType.asInstanceOf(typeProvider.iterableElement)
179179
: null;
180180
var elementType =
@@ -213,7 +213,7 @@ class CodeChecker extends RecursiveAstVisitor {
213213
checkAssignment(element.expression, expressionCastType);
214214

215215
var exprType = element.expression.staticType;
216-
var asMapType = exprType is InterfaceTypeImpl
216+
var asMapType = exprType is InterfaceType
217217
? exprType.asInstanceOf(typeProvider.mapElement)
218218
: null;
219219

@@ -845,7 +845,7 @@ class CodeChecker extends RecursiveAstVisitor {
845845

846846
DartType _getInstanceTypeArgument(
847847
DartType expressionType, ClassElement instanceType) {
848-
if (expressionType is InterfaceTypeImpl) {
848+
if (expressionType is InterfaceType) {
849849
var asInstanceType = expressionType.asInstanceOf(instanceType);
850850
if (asInstanceType != null) {
851851
return asInstanceType.typeArguments[0];

0 commit comments

Comments
 (0)