From adcc21238329cb5fc70b0601b4784f05d2bc314a Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Sat, 11 Jan 2025 07:03:59 -0800 Subject: [PATCH] [analyzer] Changes related to ExecutableElementImpl._parameters. Now that the type of `ExecutableElementImpl._parameters` has been changed to `List`, the type of `ExecutableElementImpl.formalParameters` can also be safely changed from `List` to `List`, 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`, 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`. (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` rather than `SharedTypeStructure`. 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. Change-Id: I49153cebed389f549c22d41bebee1f9b85c173d0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404041 Commit-Queue: Paul Berry Reviewed-by: Konstantin Shcheglov --- pkg/analyzer/lib/src/dart/element/element.dart | 3 +-- pkg/analyzer/lib/src/dart/element/extensions.dart | 11 ----------- .../lib/src/dart/element/inheritance_manager3.dart | 12 ++++++++++-- pkg/analyzer/lib/src/dart/element/top_merge.dart | 2 +- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 18ea7c824e3b..4b00ef1c788a 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -3679,8 +3679,7 @@ abstract class ExecutableElementImpl extends _ExistingElementImpl } @override - List get formalParameters => - parameters.cast(); + List get formalParameters => parameters; @override bool get hasImplicitReturnType { diff --git a/pkg/analyzer/lib/src/dart/element/extensions.dart b/pkg/analyzer/lib/src/dart/element/extensions.dart index dc912c8d4e24..fa396172ea27 100644 --- a/pkg/analyzer/lib/src/dart/element/extensions.dart +++ b/pkg/analyzer/lib/src/dart/element/extensions.dart @@ -205,17 +205,6 @@ extension LibraryExtension2 on LibraryElement2? { this?.featureSet.isEnabled(Feature.wildcard_variables) ?? false; } -extension ListOfParameterElementExtension on List { - /// Returns `this` as `List`, converting if it isn't - /// one already. - List toImpl() { - return switch (this) { - List already => already, - _ => [for (var p in this) p.toImpl()], - }; - } -} - extension ParameterElementExtension on ParameterElement { /// Return [ParameterElement] with the specified properties replaced. ParameterElementImpl copyWith({ diff --git a/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart b/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart index 52b616bee6ca..c4b87a4eb81e 100644 --- a/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart +++ b/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart @@ -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`. + result.parameters = resultType.parameters as List; result.element = MethodElementImpl2( Reference.root(), // TODO(scheglov): wrong firstMethod.name, @@ -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`. + result.parameters = resultType.parameters as List; var field = FieldElementImpl(variableName, -1); field.enclosingElement3 = targetClass; diff --git a/pkg/analyzer/lib/src/dart/element/top_merge.dart b/pkg/analyzer/lib/src/dart/element/top_merge.dart index 0b0498dc6c36..ce15c589f1ad 100644 --- a/pkg/analyzer/lib/src/dart/element/top_merge.dart +++ b/pkg/analyzer/lib/src/dart/element/top_merge.dart @@ -172,7 +172,7 @@ class TopMergeHelper { throw _TopMergeStateError(T, S, 'Different number of formal parameters'); } - var R_parameters = []; + var R_parameters = []; for (var i = 0; i < T_parameters.length; i++) { var T_parameter = T_parameters[i]; var S_parameter = S_parameters[i];