From 0b5d9082210da3c4e588348dfe23b3d495384ffa Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Tue, 11 Oct 2022 17:56:11 +0000 Subject: [PATCH] [analyzer] `NOT_ENOUGH_POSITIONAL_ARGUMENTS` To report at the token of the expected positional argument Fixes #50127 Change-Id: I5eb31c6d354fb15d482c2046f7faaa4505658f4e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/262603 Reviewed-by: Samuel Rawlins Reviewed-by: Brian Wilkerson Commit-Queue: Brian Wilkerson --- .../correction/bulk_fix_processor.dart | 5 +- .../services/correction/error_fix_status.yaml | 8 +- .../src/services/correction/fix_internal.dart | 5 +- pkg/analyzer/lib/error/error.dart | 4 +- pkg/analyzer/lib/src/error/codes.g.dart | 41 +++++- .../lib/src/error/error_code_values.g.dart | 5 +- pkg/analyzer/lib/src/generated/resolver.dart | 103 +++++++++++-- pkg/analyzer/messages.yaml | 41 ++++-- .../type_inference/function_test.dart | 3 +- .../test/src/diagnostics/ffi_native_test.dart | 3 +- .../not_enough_positional_arguments_test.dart | 135 ++++++++++++++++-- .../diagnostics/packed_annotation_test.dart | 3 +- pkg/analyzer/tool/diagnostics/diagnostics.md | 10 +- .../not_enough_positional_arguments_test.dart | 18 ++- .../method_implicit_invoke_local_test.dart | 3 +- .../compile_time_constant/arguments_test.dart | 3 +- .../constructor/bodyless_wrong_arg_test.dart | 3 +- .../call_wrong_argument_count_test.dart | 3 +- .../constructor/constructor13_test.dart | 3 +- tests/language/parameter/bad_named2_test.dart | 3 +- .../parameter/bad_named_parameters_test.dart | 3 +- .../parameter/named_aggregated_test.dart | 3 +- .../not_enough_positional_arguments_test.dart | 18 ++- .../method_implicit_invoke_local_test.dart | 3 +- .../compile_time_constant/arguments_test.dart | 3 +- .../constructor/bodyless_wrong_arg_test.dart | 3 +- .../call_wrong_argument_count_test.dart | 3 +- .../constructor/constructor13_test.dart | 3 +- .../language_2/parameter/bad_named2_test.dart | 3 +- .../parameter/bad_named_parameters_test.dart | 3 +- .../parameter/named_aggregated_test.dart | 3 +- .../compile_test.dart | 2 +- .../compile_test.dart | 2 +- 33 files changed, 384 insertions(+), 70 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart b/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart index 2af5b801525..bb6f643c6f5 100644 --- a/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart +++ b/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart @@ -78,7 +78,10 @@ class BulkFixProcessor { CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT: [ DataDriven.new, ], - CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS: [ + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR: [ + DataDriven.new, + ], + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR: [ DataDriven.new, ], CompileTimeErrorCode.UNDEFINED_CLASS: [ diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml index 9ffb4db1849..11938e6331a 100644 --- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml +++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml @@ -809,7 +809,13 @@ CompileTimeErrorCode.NOT_ASSIGNED_POTENTIALLY_NON_NULLABLE_LOCAL_VARIABLE: status: needsEvaluation CompileTimeErrorCode.NOT_BINARY_OPERATOR: status: needsEvaluation -CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS: +CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_PLURAL: + status: needsEvaluation +CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR: + status: hasFix +CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_PLURAL: + status: needsEvaluation +CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR: status: hasFix CompileTimeErrorCode.NOT_INITIALIZED_NON_NULLABLE_INSTANCE_FIELD: status: hasFix diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart index d110f4f4199..1542013d369 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -782,7 +782,10 @@ class FixProcessor extends BaseProcessor { CompileTimeErrorCode.NOT_A_TYPE: [ ImportLibrary.forType, ], - CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS: [ + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR: [ + DataDriven.new, + ], + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR: [ DataDriven.new, ], CompileTimeErrorCode.TYPE_TEST_WITH_UNDEFINED_NAME: [ diff --git a/pkg/analyzer/lib/error/error.dart b/pkg/analyzer/lib/error/error.dart index d839c56cb84..a0f329ba92b 100644 --- a/pkg/analyzer/lib/error/error.dart +++ b/pkg/analyzer/lib/error/error.dart @@ -103,7 +103,9 @@ class AnalysisError implements Diagnostic { assert( (arguments ?? const []).length == errorCode.numParameters, 'Message $errorCode requires ${errorCode.numParameters} ' - 'argument(s), but ${(arguments ?? const []).length} argument(s) were ' + 'argument${errorCode.numParameters == 1 ? '' : 's'}, but ' + '${(arguments ?? const []).length} ' + 'argument${(arguments ?? const []).length == 1 ? ' was' : 's were'} ' 'provided'); String problemMessage = formatList(errorCode.problemMessage, arguments); String? correctionTemplate = errorCode.correctionMessage; diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart index ff61c2e70b0..9eab0ab74db 100644 --- a/pkg/analyzer/lib/src/error/codes.g.dart +++ b/pkg/analyzer/lib/src/error/codes.g.dart @@ -3353,12 +3353,47 @@ class CompileTimeErrorCode extends AnalyzerErrorCode { /// Parameters: /// 0: the expected number of required arguments /// 1: the actual number of positional arguments given - static const CompileTimeErrorCode NOT_ENOUGH_POSITIONAL_ARGUMENTS = - CompileTimeErrorCode( + /// 2: name of the function or method + static const CompileTimeErrorCode + NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_PLURAL = CompileTimeErrorCode( 'NOT_ENOUGH_POSITIONAL_ARGUMENTS', - "{0} positional argument(s) expected, but {1} found.", + "{0} positional arguments expected by '{2}', but {1} found.", correctionMessage: "Try adding the missing arguments.", hasPublishedDocs: true, + uniqueName: 'NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_PLURAL', + ); + + /// Parameters: + /// 0: name of the function or method + static const CompileTimeErrorCode + NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR = CompileTimeErrorCode( + 'NOT_ENOUGH_POSITIONAL_ARGUMENTS', + "1 positional argument expected by '{0}', but 0 found.", + correctionMessage: "Try adding the missing argument.", + hasPublishedDocs: true, + uniqueName: 'NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR', + ); + + /// Parameters: + /// 0: the expected number of required arguments + /// 1: the actual number of positional arguments given + static const CompileTimeErrorCode NOT_ENOUGH_POSITIONAL_ARGUMENTS_PLURAL = + CompileTimeErrorCode( + 'NOT_ENOUGH_POSITIONAL_ARGUMENTS', + "{0} positional arguments expected, but {1} found.", + correctionMessage: "Try adding the missing arguments.", + hasPublishedDocs: true, + uniqueName: 'NOT_ENOUGH_POSITIONAL_ARGUMENTS_PLURAL', + ); + + /// No parameters. + static const CompileTimeErrorCode NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR = + CompileTimeErrorCode( + 'NOT_ENOUGH_POSITIONAL_ARGUMENTS', + "1 positional argument expected, but 0 found.", + correctionMessage: "Try adding the missing argument.", + hasPublishedDocs: true, + uniqueName: 'NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR', ); /// Parameters: diff --git a/pkg/analyzer/lib/src/error/error_code_values.g.dart b/pkg/analyzer/lib/src/error/error_code_values.g.dart index a989b00b74a..63716950929 100644 --- a/pkg/analyzer/lib/src/error/error_code_values.g.dart +++ b/pkg/analyzer/lib/src/error/error_code_values.g.dart @@ -342,7 +342,10 @@ const List errorCodeValues = [ CompileTimeErrorCode.NOT_ASSIGNED_POTENTIALLY_NON_NULLABLE_LOCAL_VARIABLE, CompileTimeErrorCode.NOT_A_TYPE, CompileTimeErrorCode.NOT_BINARY_OPERATOR, - CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_PLURAL, + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_PLURAL, + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR, CompileTimeErrorCode.NOT_INITIALIZED_NON_NULLABLE_INSTANCE_FIELD, CompileTimeErrorCode.NOT_INITIALIZED_NON_NULLABLE_INSTANCE_FIELD_CONSTRUCTOR, CompileTimeErrorCode.NOT_INITIALIZED_NON_NULLABLE_VARIABLE, diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart index 7d5ceb53ecc..5b8dd1e7713 100644 --- a/pkg/analyzer/lib/src/generated/resolver.dart +++ b/pkg/analyzer/lib/src/generated/resolver.dart @@ -1815,11 +1815,12 @@ class ResolverVisitor extends ThrowingAstVisitor .where((e) => e.isRequiredPositional) .length; if (requiredParameterCount != 0) { - errorReporter.reportErrorForToken( - CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, - node.name, - [requiredParameterCount, 0], - ); + _reportNotEnoughPositionalArguments( + token: node.name, + requiredParameterCount: requiredParameterCount, + actualArgumentCount: 0, + nameNode: node, + errorReporter: errorReporter); } } } @@ -3146,9 +3147,10 @@ class ResolverVisitor extends ThrowingAstVisitor int positionalArgumentCount = 0; bool noBlankArguments = true; Expression? firstUnresolvedArgument; + Expression? lastPositionalArgument; for (int i = 0; i < argumentCount; i++) { Expression argument = arguments[i]; - if (argument is! NamedExpressionImpl) { + if (argument is! NamedExpression) { if (argument is SimpleIdentifier && argument.name.isEmpty) { noBlankArguments = false; } @@ -3158,6 +3160,7 @@ class ResolverVisitor extends ThrowingAstVisitor } else { firstUnresolvedArgument ??= argument; } + lastPositionalArgument = argument; } } @@ -3196,10 +3199,18 @@ class ResolverVisitor extends ThrowingAstVisitor } if (positionalArgumentCount < requiredParameterCount && noBlankArguments) { - errorReporter?.reportErrorForNode( - CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, - argumentList, - [requiredParameterCount, positionalArgumentCount]); + var parent = argumentList.parent; + if (errorReporter != null && parent != null) { + var token = lastPositionalArgument?.endToken.next ?? + argumentList.leftParenthesis.next ?? + argumentList.rightParenthesis; + _reportNotEnoughPositionalArguments( + token: token, + requiredParameterCount: requiredParameterCount, + actualArgumentCount: positionalArgumentCount, + nameNode: parent, + errorReporter: errorReporter); + } } else if (positionalArgumentCount > unnamedParameterCount && noBlankArguments) { ErrorCode errorCode; @@ -3218,6 +3229,78 @@ class ResolverVisitor extends ThrowingAstVisitor } return resolvedParameters; } + + /// Report [CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS] or one of + /// its derivatives at the specified [token], considering the name of the + /// [nameNode]. + static void _reportNotEnoughPositionalArguments( + {required Token token, + required int requiredParameterCount, + required int actualArgumentCount, + required AstNode nameNode, + required ErrorReporter errorReporter}) { + String? name; + if (nameNode is InstanceCreationExpression) { + var constructorName = nameNode.constructorName; + name = + constructorName.name?.name ?? '${constructorName.type.name.name}.new'; + } else if (nameNode is SuperConstructorInvocation) { + name = nameNode.constructorName?.name; + if (name == null) { + var staticElement = nameNode.staticElement; + if (staticElement != null) { + name = + '${staticElement.returnType.getDisplayString(withNullability: true)}.new'; + } + } + } else if (nameNode is MethodInvocation) { + name = nameNode.methodName.name; + } else if (nameNode is FunctionExpressionInvocation) { + var function = nameNode.function; + if (function is SimpleIdentifier) { + name = function.name; + } + } else if (nameNode is EnumConstantArguments) { + var parent = nameNode.parent; + if (parent is EnumConstantDeclaration) { + var declaredElement = parent.declaredElement; + if (declaredElement is VariableElement) { + name = declaredElement.type.getDisplayString(withNullability: true); + } + } + } else if (nameNode is EnumConstantDeclaration) { + var declaredElement = nameNode.declaredElement; + if (declaredElement is VariableElement) { + name = declaredElement.type.getDisplayString(withNullability: true); + } + } else if (nameNode is Annotation) { + var nameNodeName = nameNode.name; + name = nameNodeName is PrefixedIdentifier + ? nameNodeName.identifier.name + : '${nameNodeName.name}.new'; + } else { + throw UnimplementedError('(${nameNode.runtimeType}) $nameNode'); + } + + var isPlural = requiredParameterCount - actualArgumentCount > 1; + var arguments = []; + if (isPlural) { + arguments.add(requiredParameterCount); + arguments.add(actualArgumentCount); + } + ErrorCode errorCode; + if (name == null) { + errorCode = isPlural + ? CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_PLURAL + : CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR; + } else { + errorCode = isPlural + ? CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_PLURAL + : CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR; + arguments.add(name); + } + errorReporter.reportErrorForToken(errorCode, token, arguments); + } } /// Override of [ResolverVisitorForMigration] that invokes methods of diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml index 39c228ff5e4..d33ed6aeaf5 100644 --- a/pkg/analyzer/messages.yaml +++ b/pkg/analyzer/messages.yaml @@ -10275,14 +10275,12 @@ CompileTimeErrorCode: ```dart var a = 5 - 3; ``` - NOT_ENOUGH_POSITIONAL_ARGUMENTS: - problemMessage: "{0} positional argument(s) expected, but {1} found." - correctionMessage: Try adding the missing arguments. + NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR: + sharedName: NOT_ENOUGH_POSITIONAL_ARGUMENTS + problemMessage: "1 positional argument expected, but 0 found." + correctionMessage: Try adding the missing argument. hasPublishedDocs: true - comment: |- - Parameters: - 0: the expected number of required arguments - 1: the actual number of positional arguments given + comment: No parameters. documentation: |- #### Description @@ -10298,7 +10296,7 @@ CompileTimeErrorCode: ```dart void f(int a, int b) {} void g() { - f[!(0)!]; + f(0[!)!]; } ``` @@ -10312,6 +10310,33 @@ CompileTimeErrorCode: f(0, 1); } ``` + NOT_ENOUGH_POSITIONAL_ARGUMENTS_PLURAL: + sharedName: NOT_ENOUGH_POSITIONAL_ARGUMENTS + problemMessage: "{0} positional arguments expected, but {1} found." + correctionMessage: Try adding the missing arguments. + hasPublishedDocs: true + comment: |- + Parameters: + 0: the expected number of required arguments + 1: the actual number of positional arguments given + NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR: + sharedName: NOT_ENOUGH_POSITIONAL_ARGUMENTS + problemMessage: "1 positional argument expected by '{0}', but 0 found." + correctionMessage: Try adding the missing argument. + hasPublishedDocs: true + comment: |- + Parameters: + 0: name of the function or method + NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_PLURAL: + sharedName: NOT_ENOUGH_POSITIONAL_ARGUMENTS + problemMessage: "{0} positional arguments expected by '{2}', but {1} found." + correctionMessage: Try adding the missing arguments. + hasPublishedDocs: true + comment: |- + Parameters: + 0: the expected number of required arguments + 1: the actual number of positional arguments given + 2: name of the function or method NOT_INITIALIZED_NON_NULLABLE_INSTANCE_FIELD: problemMessage: "Non-nullable instance field '{0}' must be initialized." correctionMessage: "Try adding an initializer expression, or a generative constructor that initializes it, or mark it 'late'." diff --git a/pkg/analyzer/test/src/dart/resolution/type_inference/function_test.dart b/pkg/analyzer/test/src/dart/resolution/type_inference/function_test.dart index 77f3d6b87cf..3f565544133 100644 --- a/pkg/analyzer/test/src/dart/resolution/type_inference/function_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/type_inference/function_test.dart @@ -53,7 +53,8 @@ f() { foo(1); } ''', [ - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 37, 3), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 39, 1), ]); assertTypeArgumentTypes( findNode.methodInvocation('foo('), diff --git a/pkg/analyzer/test/src/diagnostics/ffi_native_test.dart b/pkg/analyzer/test/src/diagnostics/ffi_native_test.dart index 02d46a3a184..e3baa7083b2 100644 --- a/pkg/analyzer/test/src/diagnostics/ffi_native_test.dart +++ b/pkg/analyzer/test/src/diagnostics/ffi_native_test.dart @@ -50,7 +50,8 @@ import 'dart:ffi'; @FfiNative() external int foo(); ''', [ - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 30, 2), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 31, 1), ]); } diff --git a/pkg/analyzer/test/src/diagnostics/not_enough_positional_arguments_test.dart b/pkg/analyzer/test/src/diagnostics/not_enough_positional_arguments_test.dart index 78a878226fd..adcad6b2c58 100644 --- a/pkg/analyzer/test/src/diagnostics/not_enough_positional_arguments_test.dart +++ b/pkg/analyzer/test/src/diagnostics/not_enough_positional_arguments_test.dart @@ -15,6 +15,53 @@ main() { @reflectiveTest class NotEnoughPositionalArgumentsTest extends PubPackageResolutionTest { + test_annotation_named() async { + await assertErrorsInCode(r''' +class A { + const A.named(int p); +} +@A.named() +void f() { +} +''', [ + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 45, 1, + messageContains: ["expected by 'named'"]), + ]); + } + + test_annotation_withArgumentList() async { + await assertErrorsInCode(r''' +class A { + const A(int p); +} +@A() +void f() { +} +''', [ + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 33, 1, + messageContains: ["expected by 'A.new'"]), + ]); + } + + test_annotation_withoutArgumentList() async { + await assertErrorsInCode(r''' +class A { + const A(int p); +} +const a = A(); +@a +void f() { +} +''', [ + error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 40, 3), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 42, 1, + messageContains: ["expected by 'A.new'"]), + ]); + } + test_const() async { await assertErrorsInCode(r''' class A { @@ -25,7 +72,9 @@ main() { } ''', [ error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 41, 9), - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 48, 2), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 49, 1, + messageContains: ["expected by 'A.new'"]), ]); } @@ -39,7 +88,8 @@ main() { } ''', [ error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 41, 13), - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 48, 6), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 49, 1), error(CompileTimeErrorCode.UNDEFINED_NAMED_PARAMETER, 49, 1), ]); } @@ -53,7 +103,56 @@ class B extends A { const B() : super(); } ''', [ - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 69, 2), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 70, 1, + messageContains: ["expected by 'A.new'"]), + ]); + } + + test_const_super_named() async { + await assertErrorsInCode(r''' +class A { + const A.named(int p); +} +class B extends A { + const B() : super.named(); +} +''', [ + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 82, 1, + messageContains: ["expected by 'named'"]), + ]); + } + + test_constructor_named() async { + await assertErrorsInCode(r''' +class A { + A.named(int x, int y, {int? n}); +} + +void f() { + A.named(5, n: 1); +} +''', [ + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 70, 1, + messageContains: ["expected by 'named'"]), + ]); + } + + test_constructor_positionalAndNamed() async { + await assertErrorsInCode(r''' +class A { + A(int x, int y, {int? n}); +} + +void f() { + A(5, n: 1); +} +''', [ + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 58, 1, + messageContains: ["expected by 'A.new'"]), ]); } @@ -65,7 +164,9 @@ enum E { } ''', [ error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 11, 3), - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 12, 2), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 13, 1, + messageContains: ["expected by 'E'"]), ]); } @@ -77,16 +178,28 @@ enum E { } ''', [ error(CompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, 11, 1), - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 11, 1), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 11, 1, + messageContains: ["expected by 'E'"]), ]); } - test_functionExpression() async { + test_functionExpression_plural() async { + await assertErrorsInCode(''' +main() { + (int x, int y) {} (); +}''', [ + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_PLURAL, 30, 1), + ]); + } + + test_functionExpression_singular() async { await assertErrorsInCode(''' main() { (int x) {} (); }''', [ - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 22, 2), + error( + CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_SINGULAR, 23, 1), ]); } @@ -96,7 +209,9 @@ f(int a, String b) {} main() { f(); }''', [ - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 34, 2), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_PLURAL, + 35, 1, + messageContains: ["expected by 'f'"]), ]); } @@ -107,7 +222,9 @@ Getter getter = (x) => x; main() { getter(); }''', [ - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 65, 2), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 66, 1, + messageContains: ["expected by 'getter'"]), ]); } diff --git a/pkg/analyzer/test/src/diagnostics/packed_annotation_test.dart b/pkg/analyzer/test/src/diagnostics/packed_annotation_test.dart index 88a67dbaf4f..016d1075ad6 100644 --- a/pkg/analyzer/test/src/diagnostics/packed_annotation_test.dart +++ b/pkg/analyzer/test/src/diagnostics/packed_annotation_test.dart @@ -41,7 +41,8 @@ class C extends Struct { } ''', [ error(FfiCode.PACKED_ANNOTATION_ALIGNMENT, 20, 9), - error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS, 27, 2), + error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR, + 28, 1), ]); } diff --git a/pkg/analyzer/tool/diagnostics/diagnostics.md b/pkg/analyzer/tool/diagnostics/diagnostics.md index 028dd86342e..2cdac0dd27b 100644 --- a/pkg/analyzer/tool/diagnostics/diagnostics.md +++ b/pkg/analyzer/tool/diagnostics/diagnostics.md @@ -13280,7 +13280,13 @@ var a = 5 - 3; ### not_enough_positional_arguments -_{0} positional argument(s) expected, but {1} found._ +_1 positional argument expected by '{0}', but 0 found._ + +_1 positional argument expected, but 0 found._ + +_{0} positional arguments expected by '{2}', but {1} found._ + +_{0} positional arguments expected, but {1} found._ #### Description @@ -13296,7 +13302,7 @@ required parameters, but only one argument is provided: {% prettify dart tag=pre+code %} void f(int a, int b) {} void g() { - f[!(0)!]; + f(0[!)!]; } {% endprettify %} diff --git a/tests/language/argument/not_enough_positional_arguments_test.dart b/tests/language/argument/not_enough_positional_arguments_test.dart index 9ebd09f84fc..d535ca668ff 100644 --- a/tests/language/argument/not_enough_positional_arguments_test.dart +++ b/tests/language/argument/not_enough_positional_arguments_test.dart @@ -23,8 +23,9 @@ class B { class C extends A { C() : super.test(b: 1) - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. // ^ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_NAMED_PARAMETER @@ -39,34 +40,39 @@ class D { class E extends D { E() : super.test(b: 1) - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. ; } main() { new A.test(b: 1); - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. // ^ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_NAMED_PARAMETER new B(); new C(); new D.test(b: 1); - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. new E(); foo(b: 1); - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. // ^ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_NAMED_PARAMETER bar(b: 1); - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. } diff --git a/tests/language/call/method_implicit_invoke_local_test.dart b/tests/language/call/method_implicit_invoke_local_test.dart index 0e15829e4e8..3950e1bd3e5 100644 --- a/tests/language/call/method_implicit_invoke_local_test.dart +++ b/tests/language/call/method_implicit_invoke_local_test.dart @@ -28,8 +28,9 @@ main() { Expect.equals(d2(1), 2); // Cannot invoke with the wrong signature. c2(); - //^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + //^^ // [cfe] Too few positional arguments: 1 required, 0 given. c2(3, 4); // ^ diff --git a/tests/language/compile_time_constant/arguments_test.dart b/tests/language/compile_time_constant/arguments_test.dart index 5640876201b..845239f89d8 100644 --- a/tests/language/compile_time_constant/arguments_test.dart +++ b/tests/language/compile_time_constant/arguments_test.dart @@ -11,8 +11,9 @@ class A { main() { const A(1); const A(); - // ^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^ // [cfe] Too few positional arguments: 1 required, 0 given. const A(1, 2); // ^ diff --git a/tests/language/constructor/bodyless_wrong_arg_test.dart b/tests/language/constructor/bodyless_wrong_arg_test.dart index 51f16f56c18..b3560d2ce82 100644 --- a/tests/language/constructor/bodyless_wrong_arg_test.dart +++ b/tests/language/constructor/bodyless_wrong_arg_test.dart @@ -11,8 +11,9 @@ class C extends Base { const C(String s) // Call super constructor with wrong argument count. : super(); - // ^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^ // [cfe] Too few positional arguments: 1 required, 0 given. } diff --git a/tests/language/constructor/call_wrong_argument_count_test.dart b/tests/language/constructor/call_wrong_argument_count_test.dart index 4830bcd5c3b..bb68d7e5fee 100644 --- a/tests/language/constructor/call_wrong_argument_count_test.dart +++ b/tests/language/constructor/call_wrong_argument_count_test.dart @@ -9,7 +9,8 @@ class Stockhorn { main() { new Stockhorn(1); new Stockhorn(); - // ^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^ // [cfe] Too few positional arguments: 1 required, 0 given. } diff --git a/tests/language/constructor/constructor13_test.dart b/tests/language/constructor/constructor13_test.dart index 24b8841bd20..40158adc7ca 100644 --- a/tests/language/constructor/constructor13_test.dart +++ b/tests/language/constructor/constructor13_test.dart @@ -11,8 +11,9 @@ class Klass { main() { new Klass(); - // ^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^ // [cfe] Too few positional arguments: 1 required, 0 given. new Klass(1); new Klass(1, 2); diff --git a/tests/language/parameter/bad_named2_test.dart b/tests/language/parameter/bad_named2_test.dart index 131909b66c6..f63a84ce740 100644 --- a/tests/language/parameter/bad_named2_test.dart +++ b/tests/language/parameter/bad_named2_test.dart @@ -16,8 +16,9 @@ main() { // No formal parameter named b. np.foo(b: 25); - // ^^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. // ^ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_NAMED_PARAMETER diff --git a/tests/language/parameter/bad_named_parameters_test.dart b/tests/language/parameter/bad_named_parameters_test.dart index 79c40c1fc77..6105c191d5b 100644 --- a/tests/language/parameter/bad_named_parameters_test.dart +++ b/tests/language/parameter/bad_named_parameters_test.dart @@ -49,7 +49,8 @@ main() { // Too few parameters. np.f42(b: 25); - // ^^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. } diff --git a/tests/language/parameter/named_aggregated_test.dart b/tests/language/parameter/named_aggregated_test.dart index 1ed33f2ac0f..0760e9f6e21 100644 --- a/tests/language/parameter/named_aggregated_test.dart +++ b/tests/language/parameter/named_aggregated_test.dart @@ -50,8 +50,9 @@ main() { // Expect compile-time error due to missing positional argument. NamedParametersAggregatedTests.F31(b: 25, c: 35); - // ^^^^^^^^^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^^^^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. new TypeTester(); diff --git a/tests/language_2/argument/not_enough_positional_arguments_test.dart b/tests/language_2/argument/not_enough_positional_arguments_test.dart index 2a0403aafd1..61d3de48069 100644 --- a/tests/language_2/argument/not_enough_positional_arguments_test.dart +++ b/tests/language_2/argument/not_enough_positional_arguments_test.dart @@ -25,8 +25,9 @@ class B { class C extends A { C() : super.test(b: 1) - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. // ^ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_NAMED_PARAMETER @@ -41,34 +42,39 @@ class D { class E extends D { E() : super.test(b: 1) - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. ; } main() { new A.test(b: 1); - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. // ^ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_NAMED_PARAMETER new B(); new C(); new D.test(b: 1); - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. new E(); foo(b: 1); - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. // ^ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_NAMED_PARAMETER bar(b: 1); - // ^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. } diff --git a/tests/language_2/call/method_implicit_invoke_local_test.dart b/tests/language_2/call/method_implicit_invoke_local_test.dart index f304eb41666..0f5b5208313 100644 --- a/tests/language_2/call/method_implicit_invoke_local_test.dart +++ b/tests/language_2/call/method_implicit_invoke_local_test.dart @@ -30,8 +30,9 @@ main() { Expect.equals(d2(1), 2); // Cannot invoke with the wrong signature. c2(); - //^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + //^^ // [cfe] Too few positional arguments: 1 required, 0 given. c2(3, 4); // ^ diff --git a/tests/language_2/compile_time_constant/arguments_test.dart b/tests/language_2/compile_time_constant/arguments_test.dart index 53083eba5dd..fc30343e28d 100644 --- a/tests/language_2/compile_time_constant/arguments_test.dart +++ b/tests/language_2/compile_time_constant/arguments_test.dart @@ -13,8 +13,9 @@ class A { main() { const A(1); const A(); - // ^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^ // [cfe] Too few positional arguments: 1 required, 0 given. const A(1, 2); // ^ diff --git a/tests/language_2/constructor/bodyless_wrong_arg_test.dart b/tests/language_2/constructor/bodyless_wrong_arg_test.dart index acbdde1db5b..a286ddb1f32 100644 --- a/tests/language_2/constructor/bodyless_wrong_arg_test.dart +++ b/tests/language_2/constructor/bodyless_wrong_arg_test.dart @@ -13,8 +13,9 @@ class C extends Base { const C(String s) // Call super constructor with wrong argument count. : super(); - // ^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^ // [cfe] Too few positional arguments: 1 required, 0 given. } diff --git a/tests/language_2/constructor/call_wrong_argument_count_test.dart b/tests/language_2/constructor/call_wrong_argument_count_test.dart index 1dbb91a0054..136e68cc115 100644 --- a/tests/language_2/constructor/call_wrong_argument_count_test.dart +++ b/tests/language_2/constructor/call_wrong_argument_count_test.dart @@ -11,7 +11,8 @@ class Stockhorn { main() { new Stockhorn(1); new Stockhorn(); - // ^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^ // [cfe] Too few positional arguments: 1 required, 0 given. } diff --git a/tests/language_2/constructor/constructor13_test.dart b/tests/language_2/constructor/constructor13_test.dart index b7051ea6e0f..59d051a5b7c 100644 --- a/tests/language_2/constructor/constructor13_test.dart +++ b/tests/language_2/constructor/constructor13_test.dart @@ -13,8 +13,9 @@ class Klass { main() { new Klass(); - // ^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^ // [cfe] Too few positional arguments: 1 required, 0 given. new Klass(1); new Klass(1, 2); diff --git a/tests/language_2/parameter/bad_named2_test.dart b/tests/language_2/parameter/bad_named2_test.dart index 7198a8a262d..0a32d4b819c 100644 --- a/tests/language_2/parameter/bad_named2_test.dart +++ b/tests/language_2/parameter/bad_named2_test.dart @@ -18,8 +18,9 @@ main() { // No formal parameter named b. np.foo(b: 25); - // ^^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. // ^ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_NAMED_PARAMETER diff --git a/tests/language_2/parameter/bad_named_parameters_test.dart b/tests/language_2/parameter/bad_named_parameters_test.dart index 94e841bcadd..05c4859a29c 100644 --- a/tests/language_2/parameter/bad_named_parameters_test.dart +++ b/tests/language_2/parameter/bad_named_parameters_test.dart @@ -51,7 +51,8 @@ main() { // Too few parameters. np.f42(b: 25); - // ^^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. } diff --git a/tests/language_2/parameter/named_aggregated_test.dart b/tests/language_2/parameter/named_aggregated_test.dart index 485af9b78ea..b7150b7f6ae 100644 --- a/tests/language_2/parameter/named_aggregated_test.dart +++ b/tests/language_2/parameter/named_aggregated_test.dart @@ -52,8 +52,9 @@ main() { // Expect compile-time error due to missing positional argument. NamedParametersAggregatedTests.F31(b: 25, c: 35); - // ^^^^^^^^^^^^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS + // ^^^^^^^^^^^^^^ // [cfe] Too few positional arguments: 1 required, 0 given. new TypeTester(); diff --git a/tests/lib/html/js_function_getter_trust_types/compile_test.dart b/tests/lib/html/js_function_getter_trust_types/compile_test.dart index 29f8669761f..a9ae402a6b0 100644 --- a/tests/lib/html/js_function_getter_trust_types/compile_test.dart +++ b/tests/lib/html/js_function_getter_trust_types/compile_test.dart @@ -41,7 +41,7 @@ main() { foo.bar.add(4); // ^ // [cfe] Error: Too few positional arguments: 2 required, 1 given. - // ^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS foo.bar.add(4, 5, 10); diff --git a/tests/lib_2/html/js_function_getter_trust_types/compile_test.dart b/tests/lib_2/html/js_function_getter_trust_types/compile_test.dart index b8653518746..24980285183 100644 --- a/tests/lib_2/html/js_function_getter_trust_types/compile_test.dart +++ b/tests/lib_2/html/js_function_getter_trust_types/compile_test.dart @@ -43,7 +43,7 @@ main() { foo.bar.add(4); // ^ // [cfe] Error: Too few positional arguments: 2 required, 1 given. - // ^^^ + // ^ // [analyzer] COMPILE_TIME_ERROR.NOT_ENOUGH_POSITIONAL_ARGUMENTS foo.bar.add(4, 5, 10);