Skip to content

Commit

Permalink
[analyzer] Changes related to ExecutableElementImpl._parameters.
Browse files Browse the repository at this point in the history
Now that the type of `ExecutableElementImpl._parameters` has been
changed to `List<ParameterElementImpl>`, the type of
`ExecutableElementImpl.formalParameters` can also be safely changed
from `List<FormalParameterFragment>` to `List<ParameterElementImpl>`,
and no longer requires a cast. (This works because
`ParameterElementImpl` is a subtype of `FormalParameterFragment`.)

Also, in `InheritanceManager3._topMerge`, it isn't necessary to use
`toImpl()` to convert `resultType.parameters` to
`List<ParameterElementImpl>`, because `resultType` is guaranteed to
have been produced by `TypeSystemImpl.topMerge`. Instead, we can
change `TypeSystemImpl.topMerge` so that when it produces a
`FunctionType`, it always makes their parameter list a
`List<ParameterElementImpl>`. (It was already the case that all the
parameters in a `FunctionType` produced by `TypeSystemImpl.topMerge`
are instances of `ParameterElementImpl`, so this is a straightforward
change.)

The change to `ExecutableElementImpl.formalParameters` will allow some
types in various element model Impl classes to be changed from
`DartType` to `TypeImpl`, which will in turn pave the way for changing
the analyzer's `DartType` class so that it implements
`SharedTypeStructure<TypeImpl>` rather than
`SharedTypeStructure<DartType>`.

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: I49153cebed389f549c22d41bebee1f9b85c173d0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404041
Commit-Queue: Paul Berry <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 11, 2025
1 parent 8bbe6fb commit adcc212
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
3 changes: 1 addition & 2 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3679,8 +3679,7 @@ abstract class ExecutableElementImpl extends _ExistingElementImpl
}

@override
List<FormalParameterFragment> get formalParameters =>
parameters.cast<FormalParameterFragment>();
List<ParameterElementImpl> get formalParameters => parameters;

@override
bool get hasImplicitReturnType {
Expand Down
11 changes: 0 additions & 11 deletions pkg/analyzer/lib/src/dart/element/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,6 @@ extension LibraryExtension2 on LibraryElement2? {
this?.featureSet.isEnabled(Feature.wildcard_variables) ?? false;
}

extension ListOfParameterElementExtension on List<ParameterElement> {
/// Returns `this` as `List<ParameterElementImpl>`, converting if it isn't
/// one already.
List<ParameterElementImpl> toImpl() {
return switch (this) {
List<ParameterElementImpl> already => already,
_ => [for (var p in this) p.toImpl()],
};
}
}

extension ParameterElementExtension on ParameterElement {
/// Return [ParameterElement] with the specified properties replaced.
ParameterElementImpl copyWith({
Expand Down
12 changes: 10 additions & 2 deletions pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,11 @@ class InheritanceManager3 {
result.name2 = fragmentName;
result.typeParameters = resultType.typeFormals.cast();
result.returnType = resultType.returnType;
result.parameters = resultType.parameters.toImpl();
// `resultType` is guaranteed to have been produced by
// `TypeSystemImpl.topMerge`; when that function merges function types, it
// always produces a `FunctionType` whose parameter list is a
// `List<ParameterElementImpl>`.
result.parameters = resultType.parameters as List<ParameterElementImpl>;
result.element = MethodElementImpl2(
Reference.root(), // TODO(scheglov): wrong
firstMethod.name,
Expand All @@ -1160,7 +1164,11 @@ class InheritanceManager3 {
result.isGetter = firstAccessor.isGetter;
result.isSetter = firstAccessor.isSetter;
result.returnType = resultType.returnType;
result.parameters = resultType.parameters.toImpl();
// `resultType` is guaranteed to have been produced by
// `TypeSystemImpl.topMerge`; when that function merges function types, it
// always produces a `FunctionType` whose parameter list is a
// `List<ParameterElementImpl>`.
result.parameters = resultType.parameters as List<ParameterElementImpl>;

var field = FieldElementImpl(variableName, -1);
field.enclosingElement3 = targetClass;
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/element/top_merge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class TopMergeHelper {
throw _TopMergeStateError(T, S, 'Different number of formal parameters');
}

var R_parameters = <ParameterElement>[];
var R_parameters = <ParameterElementImpl>[];
for (var i = 0; i < T_parameters.length; i++) {
var T_parameter = T_parameters[i];
var S_parameter = S_parameters[i];
Expand Down

0 comments on commit adcc212

Please sign in to comment.