Skip to content

Commit

Permalink
[analyzer] Refine type of InterfaceTypeImpl.element
Browse files Browse the repository at this point in the history
The type of `InterfaceTypeImpl.element` is changed from
`InterfaceElement` to `InterfaceElementImpl`.

This allows eliminating casts from a number of call sites, at the
expense of adding a single cast to the `InterfaceTypeImpl`
constructor. This cast is safe because all concrete implementations of
`InterfaceElement` are subtypes of `InterfaceElementImpl`.

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: I5d0ffa3b14788965589a57a3c061acac2f3c6930
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403942
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Paul Berry <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 12, 2025
1 parent bdf8c21 commit 7d8c34c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
4 changes: 1 addition & 3 deletions pkg/analyzer/lib/src/dart/element/least_upper_bound.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:_fe_analyzer_shared/src/type_inference/type_analyzer_operations.
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/extensions.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_schema.dart';
Expand Down Expand Up @@ -73,8 +72,7 @@ class InterfaceLeastUpperBoundHelper {
for (int i = 0; i < args1.length; i++) {
// TODO(kallentu): : Clean up TypeParameterElementImpl casting once
// variance is added to the interface.
Variance parameterVariance =
(params[i] as TypeParameterElementImpl).variance;
Variance parameterVariance = params[i].variance;
if (parameterVariance.isCovariant) {
args.add(typeSystem.leastUpperBound(args1[i], args2[i]));
} else if (parameterVariance.isContravariant) {
Expand Down
15 changes: 6 additions & 9 deletions pkg/analyzer/lib/src/dart/element/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_visitor.dart';
import 'package:analyzer/src/dart/analysis/session.dart';
import 'package:analyzer/src/dart/element/display_string_builder.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
Expand Down Expand Up @@ -574,7 +573,7 @@ class InstantiatedTypeAliasElementImpl implements InstantiatedTypeAliasElement {
/// A concrete implementation of an [InterfaceType].
class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
@override
final InterfaceElement element;
final InterfaceElementImpl element;

@override
final List<DartType> typeArguments;
Expand All @@ -596,6 +595,8 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
required List<DartType> typeArguments,
required NullabilitySuffix nullabilitySuffix,
InstantiatedTypeAliasElement? alias}) {
// TODO(paulberry): avoid this cast by changing the type of `element`
element as InterfaceElementImpl;
if (element.name == 'FutureOr' && element.library.isDartAsync) {
return FutureOrTypeImpl(
element: element,
Expand Down Expand Up @@ -696,8 +697,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
.toList();

@override
InterfaceElementImpl2 get element3 =>
(element as InterfaceElementImpl).element;
InterfaceElementImpl2 get element3 => element.element;

@override
List<GetterElement> get getters => accessors
Expand Down Expand Up @@ -744,7 +744,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
@override
bool get isDartCoreEnum {
var element = this.element;
return element is ClassElement && element.isDartCoreEnum;
return element is ClassElementImpl && element.isDartCoreEnum;
}

@override
Expand Down Expand Up @@ -881,7 +881,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
}

InheritanceManager3 get _inheritanceManager =>
(element.library.session as AnalysisSessionImpl).inheritanceManager;
element.library.session.inheritanceManager;

@override
bool operator ==(Object other) {
Expand Down Expand Up @@ -1021,7 +1021,6 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
}

if (recoveryStatic) {
var element = this.element as InterfaceElementImpl;
return element.lookupStaticGetter(name, library);
}

Expand Down Expand Up @@ -1077,7 +1076,6 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
}

if (recoveryStatic) {
var element = this.element as InterfaceElementImpl;
return element.lookupStaticMethod(name, library);
}

Expand Down Expand Up @@ -1133,7 +1131,6 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
}

if (recoveryStatic) {
var element = this.element as InterfaceElementImpl;
return element.lookupStaticSetter(name, library);
}

Expand Down

0 comments on commit 7d8c34c

Please sign in to comment.