Use named parameters in resolveArgumentsToParameters().

Change-Id: Icbcab6c35ec326df54376a8ebad938373118c3f9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/226020
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart
index 0391099..192c94c 100644
--- a/pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart
@@ -432,35 +432,6 @@
     }
   }
 
-  /// Given an [argumentList] and the [executableElement] that will be invoked
-  /// using those argument, compute the list of parameters that correspond to
-  /// the list of arguments. An error will be reported if any of the arguments
-  /// cannot be matched to a parameter. Return the parameters that correspond to
-  /// the arguments, or `null` if no correspondence could be computed.
-  ///
-  /// TODO(scheglov) this is duplicate
-  List<ParameterElement?>? _resolveArgumentsToFunction(
-      ArgumentList argumentList, ExecutableElement? executableElement) {
-    if (executableElement == null) {
-      return null;
-    }
-    List<ParameterElement> parameters = executableElement.parameters;
-    return _resolveArgumentsToParameters(argumentList, parameters);
-  }
-
-  /// Given an [argumentList] and the [parameters] related to the element that
-  /// will be invoked using those arguments, compute the list of parameters that
-  /// correspond to the list of arguments. An error will be reported if any of
-  /// the arguments cannot be matched to a parameter. Return the parameters that
-  /// correspond to the arguments.
-  ///
-  /// TODO(scheglov) this is duplicate
-  List<ParameterElement?> _resolveArgumentsToParameters(
-      ArgumentList argumentList, List<ParameterElement> parameters) {
-    return ResolverVisitor.resolveArgumentsToParameters(
-        argumentList, parameters, _errorReporter.reportErrorForNode);
-  }
-
   void _resolveConstructorInvocationArguments(AnnotationImpl node) {
     var argumentList = node.arguments;
     // error will be reported in ConstantVerifier
@@ -470,10 +441,12 @@
     // resolve arguments to parameters
     var constructor = node.element;
     if (constructor is ConstructorElement) {
-      var parameters = _resolveArgumentsToFunction(argumentList, constructor);
-      if (parameters != null) {
-        argumentList.correspondingStaticParameters = parameters;
-      }
+      argumentList.correspondingStaticParameters =
+          ResolverVisitor.resolveArgumentsToParameters(
+        argumentList: argumentList,
+        parameters: constructor.parameters,
+        errorReporter: _errorReporter,
+      );
     }
   }
 
diff --git a/pkg/analyzer/lib/src/dart/resolver/instance_creation_expression_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/instance_creation_expression_resolver.dart
index f824a1e..481ceb7 100644
--- a/pkg/analyzer/lib/src/dart/resolver/instance_creation_expression_resolver.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/instance_creation_expression_resolver.dart
@@ -88,7 +88,9 @@
         // Fix up the parameter elements based on inferred method.
         arguments.correspondingStaticParameters =
             ResolverVisitor.resolveArgumentsToParameters(
-                arguments, inferred.parameters, null);
+          argumentList: arguments,
+          parameters: inferred.parameters,
+        );
 
         constructorName.type2.type = inferred.returnType;
 
diff --git a/pkg/analyzer/lib/src/dart/resolver/invocation_inference_helper.dart b/pkg/analyzer/lib/src/dart/resolver/invocation_inference_helper.dart
index d7a8fd2..901f6f8 100644
--- a/pkg/analyzer/lib/src/dart/resolver/invocation_inference_helper.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/invocation_inference_helper.dart
@@ -190,7 +190,9 @@
       // Fix up the parameter elements based on inferred method.
       arguments.correspondingStaticParameters =
           ResolverVisitor.resolveArgumentsToParameters(
-              arguments, inferred.parameters, null);
+        argumentList: arguments,
+        parameters: inferred.parameters,
+      );
       node.staticInvokeType = inferred;
     }
   }
@@ -381,7 +383,9 @@
     // Get the parameters that correspond to the uninstantiated generic.
     List<ParameterElement?> rawParameters =
         ResolverVisitor.resolveArgumentsToParameters(
-            argumentList, rawType.parameters, null);
+      argumentList: argumentList,
+      parameters: rawType.parameters,
+    );
 
     List<ParameterElement> params = <ParameterElement>[];
     List<DartType> argTypes = <DartType>[];
@@ -537,9 +541,9 @@
     FunctionType invokeType,
   ) {
     var parameters = ResolverVisitor.resolveArgumentsToParameters(
-      argumentList,
-      invokeType.parameters,
-      _errorReporter.reportErrorForNode,
+      argumentList: argumentList,
+      parameters: invokeType.parameters,
+      errorReporter: _errorReporter,
     );
     argumentList.correspondingStaticParameters = parameters;
   }
diff --git a/pkg/analyzer/lib/src/generated/element_resolver.dart b/pkg/analyzer/lib/src/generated/element_resolver.dart
index 344c5e7..2dc6017 100644
--- a/pkg/analyzer/lib/src/generated/element_resolver.dart
+++ b/pkg/analyzer/lib/src/generated/element_resolver.dart
@@ -457,28 +457,10 @@
     if (executableElement == null) {
       return null;
     }
-    List<ParameterElement> parameters = executableElement.parameters;
-    return _resolveArgumentsToParameters(
-      argumentList,
-      parameters,
-      enclosingConstructor: enclosingConstructor,
-    );
-  }
-
-  /// Given an [argumentList] and the [parameters] related to the element that
-  /// will be invoked using those arguments, compute the list of parameters that
-  /// correspond to the list of arguments. An error will be reported if any of
-  /// the arguments cannot be matched to a parameter. Return the parameters that
-  /// correspond to the arguments.
-  List<ParameterElement?> _resolveArgumentsToParameters(
-    ArgumentList argumentList,
-    List<ParameterElement> parameters, {
-    ConstructorDeclaration? enclosingConstructor,
-  }) {
     return ResolverVisitor.resolveArgumentsToParameters(
-      argumentList,
-      parameters,
-      _errorReporter.reportErrorForNode,
+      argumentList: argumentList,
+      parameters: executableElement.parameters,
+      errorReporter: _errorReporter,
       enclosingConstructor: enclosingConstructor,
     );
   }
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index ad495d9..aef17ef 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -2324,17 +2324,12 @@
   /// will be invoked using those arguments, compute the list of parameters that
   /// correspond to the list of arguments.
   ///
-  /// An error will be reported to [onError] if any of the arguments cannot be
-  /// matched to a parameter. onError will be provided the node of the first
-  /// argument that is not matched. onError can be null to ignore the error.
-  ///
   /// Returns the parameters that correspond to the arguments. If no parameter
   /// matched an argument, that position will be `null` in the list.
-  static List<ParameterElement?> resolveArgumentsToParameters(
-    ArgumentList argumentList,
-    List<ParameterElement> parameters,
-    void Function(ErrorCode errorCode, AstNode node, [List<Object> arguments])?
-        onError, {
+  static List<ParameterElement?> resolveArgumentsToParameters({
+    required ArgumentList argumentList,
+    required List<ParameterElement> parameters,
+    ErrorReporter? errorReporter,
     ConstructorDeclaration? enclosingConstructor,
   }) {
     if (parameters.isEmpty && argumentList.arguments.isEmpty) {
@@ -2375,20 +2370,16 @@
         String name = nameNode.name;
         var element = namedParameters != null ? namedParameters[name] : null;
         if (element == null) {
-          if (onError != null) {
-            onError(CompileTimeErrorCode.UNDEFINED_NAMED_PARAMETER, nameNode,
-                [name]);
-          }
+          errorReporter?.reportErrorForNode(
+              CompileTimeErrorCode.UNDEFINED_NAMED_PARAMETER, nameNode, [name]);
         } else {
           resolvedParameters[i] = element;
           nameNode.staticElement = element;
         }
         usedNames ??= HashSet<String>();
         if (!usedNames.add(name)) {
-          if (onError != null) {
-            onError(CompileTimeErrorCode.DUPLICATE_NAMED_ARGUMENT, nameNode,
-                [name]);
-          }
+          errorReporter?.reportErrorForNode(
+              CompileTimeErrorCode.DUPLICATE_NAMED_ARGUMENT, nameNode, [name]);
         }
       } else {
         if (argument is SimpleIdentifier && argument.name.isEmpty) {
@@ -2411,8 +2402,8 @@
           var element = formalParameter.declaredElement
               as SuperFormalParameterElementImpl;
           if (formalParameter.isNamed) {
-            if (onError != null && element.superConstructorParameter == null) {
-              onError(
+            if (element.superConstructorParameter == null) {
+              errorReporter?.reportErrorForNode(
                 CompileTimeErrorCode
                     .SUPER_FORMAL_PARAMETER_WITHOUT_ASSOCIATED_NAMED,
                 formalParameter.identifier,
@@ -2420,21 +2411,19 @@
             }
           } else {
             positionalArgumentCount++;
-            if (onError != null) {
-              if (hasExplicitPositionalArguments) {
-                onError(
-                  CompileTimeErrorCode
-                      .POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT,
-                  formalParameter.identifier,
-                );
-              }
-              if (element.superConstructorParameter == null) {
-                onError(
-                  CompileTimeErrorCode
-                      .SUPER_FORMAL_PARAMETER_WITHOUT_ASSOCIATED_POSITIONAL,
-                  formalParameter.identifier,
-                );
-              }
+            if (hasExplicitPositionalArguments) {
+              errorReporter?.reportErrorForNode(
+                CompileTimeErrorCode
+                    .POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT,
+                formalParameter.identifier,
+              );
+            }
+            if (element.superConstructorParameter == null) {
+              errorReporter?.reportErrorForNode(
+                CompileTimeErrorCode
+                    .SUPER_FORMAL_PARAMETER_WITHOUT_ASSOCIATED_POSITIONAL,
+                formalParameter.identifier,
+              );
             }
           }
         }
@@ -2442,10 +2431,10 @@
     }
 
     if (positionalArgumentCount < requiredParameterCount && noBlankArguments) {
-      if (onError != null) {
-        onError(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS,
-            argumentList, [requiredParameterCount, positionalArgumentCount]);
-      }
+      errorReporter?.reportErrorForNode(
+          CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS,
+          argumentList,
+          [requiredParameterCount, positionalArgumentCount]);
     } else if (positionalArgumentCount > unnamedParameterCount &&
         noBlankArguments) {
       ErrorCode errorCode;
@@ -2457,8 +2446,8 @@
       } else {
         errorCode = CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS;
       }
-      if (onError != null && firstUnresolvedArgument != null) {
-        onError(errorCode, firstUnresolvedArgument,
+      if (firstUnresolvedArgument != null) {
+        errorReporter?.reportErrorForNode(errorCode, firstUnresolvedArgument,
             [unnamedParameterCount, positionalArgumentCount]);
       }
     }
diff --git a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
index bcd3487..11579a3 100644
--- a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
+++ b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
@@ -378,7 +378,9 @@
       // Fix up the parameter elements based on inferred method.
       arguments.correspondingStaticParameters =
           ResolverVisitor.resolveArgumentsToParameters(
-              arguments, inferred.parameters, null);
+        argumentList: arguments,
+        parameters: inferred.parameters,
+      );
       typeName.type = inferred.returnType;
       // Update the static element as well. This is used in some cases, such as
       // computing constant values. It is stored in two places.