mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 09:58:32 +00:00
Use WRONG_NUMBER_OF_TYPE_ARGUMENTS instead of two other equivalent error codes.
R=brianwilkerson@google.com Change-Id: Ibb5ea7d65df75655eaf2d43fba1110740be39912 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129833 Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
c941ad0cba
commit
7b7d8789ca
|
@ -117,7 +117,6 @@ const List<ErrorCode> errorCodeValues = [
|
|||
CompileTimeErrorCode.CONST_SET_ELEMENT_TYPE_IMPLEMENTS_EQUALS,
|
||||
CompileTimeErrorCode.CONST_SPREAD_EXPECTED_LIST_OR_SET,
|
||||
CompileTimeErrorCode.CONST_SPREAD_EXPECTED_MAP,
|
||||
CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS,
|
||||
CompileTimeErrorCode.CONST_WITH_NON_CONST,
|
||||
CompileTimeErrorCode.CONST_WITH_NON_CONSTANT_ARGUMENT,
|
||||
CompileTimeErrorCode.CONST_WITH_NON_TYPE,
|
||||
|
@ -747,7 +746,6 @@ const List<ErrorCode> errorCodeValues = [
|
|||
StaticWarningCode.MIXED_RETURN_TYPES,
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
StaticWarningCode.NEW_WITH_ABSTRACT_CLASS,
|
||||
StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS,
|
||||
StaticWarningCode.NEW_WITH_NON_TYPE,
|
||||
StaticWarningCode.NEW_WITH_UNDEFINED_CONSTRUCTOR,
|
||||
StaticWarningCode.NEW_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT,
|
||||
|
|
|
@ -89,7 +89,7 @@ typedef WorkToWaitAfterComputingResult = Future<void> Function(String path);
|
|||
/// TODO(scheglov) Clean up the list of implicitly analyzed files.
|
||||
class AnalysisDriver implements AnalysisDriverGeneric {
|
||||
/// The version of data format, should be incremented on every format change.
|
||||
static const int DATA_VERSION = 93;
|
||||
static const int DATA_VERSION = 94;
|
||||
|
||||
/// The length of the list returned by [_computeDeclaredVariablesSignature].
|
||||
static const int _declaredVariablesSignatureLength = 4;
|
||||
|
|
|
@ -364,7 +364,7 @@ class TypeNameResolver {
|
|||
|
||||
if (argumentCount != parameterCount) {
|
||||
reportErrorForNode(
|
||||
_getInvalidTypeParametersErrorCode(node),
|
||||
StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS,
|
||||
node,
|
||||
[node.name.name, parameterCount, argumentCount],
|
||||
);
|
||||
|
@ -409,24 +409,6 @@ class TypeNameResolver {
|
|||
return null; // Not found
|
||||
}
|
||||
|
||||
/// The number of type arguments in the given [typeName] does not match the
|
||||
/// number of parameters in the corresponding class element. Return the error
|
||||
/// code that should be used to report this error.
|
||||
ErrorCode _getInvalidTypeParametersErrorCode(TypeName typeName) {
|
||||
AstNode parent = typeName.parent;
|
||||
if (parent is ConstructorName) {
|
||||
parent = parent.parent;
|
||||
if (parent is InstanceCreationExpression) {
|
||||
if (parent.isConst) {
|
||||
return CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS;
|
||||
} else {
|
||||
return StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS;
|
||||
}
|
||||
}
|
||||
}
|
||||
return StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS;
|
||||
}
|
||||
|
||||
NullabilitySuffix _getNullability(bool hasQuestion) {
|
||||
if (isNonNullableByDefault) {
|
||||
if (hasQuestion) {
|
||||
|
@ -655,8 +637,11 @@ class TypeNameResolver {
|
|||
typeArguments[i] = _getType(argumentNodes[i]);
|
||||
}
|
||||
} else {
|
||||
reportErrorForNode(_getInvalidTypeParametersErrorCode(node), node,
|
||||
[typeName.name, parameterCount, argumentCount]);
|
||||
reportErrorForNode(
|
||||
StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS,
|
||||
node,
|
||||
[typeName.name, parameterCount, argumentCount],
|
||||
);
|
||||
for (int i = 0; i < parameterCount; i++) {
|
||||
typeArguments[i] = dynamicType;
|
||||
}
|
||||
|
|
|
@ -1116,28 +1116,6 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
|
|||
'CONST_SPREAD_EXPECTED_MAP', "A map is expected in this spread.",
|
||||
hasPublishedDocs: true);
|
||||
|
||||
/**
|
||||
* 16.12.2 Const: If <i>T</i> is a parameterized type <i>S<U<sub>1</sub>,
|
||||
* …, U<sub>m</sub>></i>, let <i>R = S</i>; It is a compile time
|
||||
* error if <i>S</i> is not a generic type with <i>m</i> type parameters.
|
||||
*
|
||||
* Parameters:
|
||||
* 0: the name of the type being referenced (<i>S</i>)
|
||||
* 1: the number of type parameters that were declared
|
||||
* 2: the number of type arguments provided
|
||||
*
|
||||
* See [StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS], and
|
||||
* [StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS].
|
||||
*/
|
||||
static const CompileTimeErrorCode CONST_WITH_INVALID_TYPE_PARAMETERS =
|
||||
CompileTimeErrorCode(
|
||||
'CONST_WITH_INVALID_TYPE_PARAMETERS',
|
||||
"The type '{0}' is declared with {1} type parameters, but {2} type "
|
||||
"arguments were given.",
|
||||
correction:
|
||||
"Try adjusting the number of type arguments to match the number "
|
||||
"of type parameters.");
|
||||
|
||||
/**
|
||||
* No parameters.
|
||||
*/
|
||||
|
@ -6724,7 +6702,9 @@ class StaticTypeWarningCode extends AnalyzerErrorCode {
|
|||
'WRONG_NUMBER_OF_TYPE_ARGUMENTS',
|
||||
"The type '{0}' is declared with {1} type parameters, "
|
||||
"but {2} type arguments were given.",
|
||||
correction: "Try adjusting the number of type arguments.",
|
||||
correction:
|
||||
"Try adjusting the number of type arguments to match the number "
|
||||
"of type parameters.",
|
||||
hasPublishedDocs: true);
|
||||
|
||||
/**
|
||||
|
@ -7876,46 +7856,6 @@ class StaticWarningCode extends AnalyzerErrorCode {
|
|||
static const StaticWarningCode NEW_WITH_ABSTRACT_CLASS =
|
||||
INSTANTIATE_ABSTRACT_CLASS;
|
||||
|
||||
/**
|
||||
* Parameters:
|
||||
* 0: the name of the type being referenced (<i>S</i>)
|
||||
* 1: the number of type parameters that were declared
|
||||
* 2: the number of type arguments provided
|
||||
*/
|
||||
// #### Description
|
||||
//
|
||||
// The analyzer produces this diagnostic when a constructor is invoked and the
|
||||
// number of type arguments doesn't match the number of type parameters
|
||||
// declared for the class.
|
||||
//
|
||||
// #### Example
|
||||
//
|
||||
// The following code produces this diagnostic because `C` declares one type
|
||||
// parameter, but two type arguments are given:
|
||||
//
|
||||
// ```dart
|
||||
// class C<E> {}
|
||||
//
|
||||
// var c = [!C<int, int>!]();
|
||||
// ```
|
||||
//
|
||||
// #### Common fixes
|
||||
//
|
||||
// Change the number of type arguments to match the number of type parameters
|
||||
// declared for the class:
|
||||
//
|
||||
// ```dart
|
||||
// class C<E> {}
|
||||
//
|
||||
// var c = C<int>();
|
||||
// ```
|
||||
static const StaticWarningCode NEW_WITH_INVALID_TYPE_PARAMETERS =
|
||||
StaticWarningCode(
|
||||
'NEW_WITH_INVALID_TYPE_PARAMETERS',
|
||||
"The type '{0}' is declared with {1} type arguments, but {2} type "
|
||||
"arguments were given.",
|
||||
correction: "Try adjusting the number of type arguments.");
|
||||
|
||||
/**
|
||||
* 12.11.1 New: It is a static warning if <i>T</i> is not a class accessible
|
||||
* in the current scope, optionally followed by type arguments.
|
||||
|
|
|
@ -742,45 +742,6 @@ class C {
|
|||
]);
|
||||
}
|
||||
|
||||
test_constWithInvalidTypeParameters() async {
|
||||
await assertErrorsInCode(r'''
|
||||
class A {
|
||||
const A();
|
||||
}
|
||||
f() { return const A<A>(); }
|
||||
''', [
|
||||
error(CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS, 44, 4),
|
||||
]);
|
||||
}
|
||||
|
||||
test_constWithInvalidTypeParameters_tooFew() async {
|
||||
await assertErrorsInCode(r'''
|
||||
class A {}
|
||||
class C<K, V> {
|
||||
const C();
|
||||
}
|
||||
f(p) {
|
||||
return const C<A>();
|
||||
}
|
||||
''', [
|
||||
error(CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS, 64, 4),
|
||||
]);
|
||||
}
|
||||
|
||||
test_constWithInvalidTypeParameters_tooMany() async {
|
||||
await assertErrorsInCode(r'''
|
||||
class A {}
|
||||
class C<E> {
|
||||
const C();
|
||||
}
|
||||
f(p) {
|
||||
return const C<A, A>();
|
||||
}
|
||||
''', [
|
||||
error(CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS, 61, 7),
|
||||
]);
|
||||
}
|
||||
|
||||
test_constWithNonConst() async {
|
||||
await assertErrorsInCode(r'''
|
||||
class T {
|
||||
|
|
|
@ -19,7 +19,7 @@ class InstanceCreationDriverResolutionTest extends DriverResolutionTest {
|
|||
await assertErrorsInCode(r'''
|
||||
final foo = Map<int>();
|
||||
''', [
|
||||
error(StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS, 12, 8),
|
||||
error(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS, 12, 8),
|
||||
]);
|
||||
|
||||
var creation = findNode.instanceCreation('Map<int>');
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:analyzer/src/error/codes.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import '../dart/resolution/driver_resolution.dart';
|
||||
|
||||
main() {
|
||||
defineReflectiveSuite(() {
|
||||
defineReflectiveTests(NewWithInvalidTypeParametersTest);
|
||||
});
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class NewWithInvalidTypeParametersTest extends DriverResolutionTest {
|
||||
test_nonGeneric() async {
|
||||
await assertErrorsInCode('''
|
||||
class A {}
|
||||
f() { return new A<A>(); }
|
||||
''', [
|
||||
error(StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS, 28, 4),
|
||||
]);
|
||||
}
|
||||
|
||||
test_tooFew() async {
|
||||
await assertErrorsInCode('''
|
||||
class A {}
|
||||
class C<K, V> {}
|
||||
f(p) {
|
||||
return new C<A>();
|
||||
}
|
||||
''', [
|
||||
error(StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS, 48, 4),
|
||||
]);
|
||||
}
|
||||
|
||||
test_tooMany() async {
|
||||
await assertErrorsInCode('''
|
||||
class A {}
|
||||
class C<E> {}
|
||||
f(p) {
|
||||
return new C<A, A>();
|
||||
}
|
||||
''', [
|
||||
error(StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS, 45, 7),
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -160,10 +160,10 @@ import 'inference_failure_on_untyped_parameter_test.dart'
|
|||
import 'instance_access_to_static_member_test.dart'
|
||||
as instance_access_to_static_member;
|
||||
import 'instantiate_abstract_class_test.dart' as instantiate_abstract_class;
|
||||
import 'invalid_annotation_test.dart' as invalid_annotation;
|
||||
import 'invalid_annotation_from_deferred_library_test.dart'
|
||||
as invalid_annotation_from_deferred_library;
|
||||
import 'invalid_annotation_getter_test.dart' as invalid_annotation_getter;
|
||||
import 'invalid_annotation_test.dart' as invalid_annotation;
|
||||
import 'invalid_assignment_test.dart' as invalid_assignment;
|
||||
import 'invalid_cast_new_expr_test.dart' as invalid_cast_new_expr;
|
||||
import 'invalid_constant_test.dart' as invalid_constant;
|
||||
|
@ -267,8 +267,6 @@ import 'must_be_a_native_function_type_test.dart'
|
|||
import 'must_be_a_subtype_test.dart' as must_be_a_subtype;
|
||||
import 'must_be_immutable_test.dart' as must_be_immutable;
|
||||
import 'must_call_super_test.dart' as must_call_super;
|
||||
import 'new_with_invalid_type_parameters_test.dart'
|
||||
as new_with_invalid_type_parameters;
|
||||
import 'new_with_non_type_test.dart' as new_with_non_type;
|
||||
import 'new_with_undefined_constructor_test.dart'
|
||||
as new_with_undefined_constructor;
|
||||
|
@ -471,6 +469,8 @@ import 'wrong_number_of_parameters_for_operator_test.dart'
|
|||
as wrong_number_of_parameters_for_operator;
|
||||
import 'wrong_number_of_parameters_for_setter_test.dart'
|
||||
as wrong_number_of_parameters_for_setter;
|
||||
import 'wrong_number_of_type_arguments_test.dart'
|
||||
as wrong_number_of_type_arguments;
|
||||
import 'wrong_type_parameter_variance_in_superinterface_test.dart'
|
||||
as wrong_type_parameter_variance_in_superinterface;
|
||||
import 'yield_each_in_non_generator_test.dart' as yield_each_in_non_generator;
|
||||
|
@ -657,7 +657,6 @@ main() {
|
|||
must_be_a_subtype.main();
|
||||
must_be_immutable.main();
|
||||
must_call_super.main();
|
||||
new_with_invalid_type_parameters.main();
|
||||
new_with_non_type.main();
|
||||
new_with_undefined_constructor.main();
|
||||
non_abstract_class_inherits_abstract_member.main();
|
||||
|
@ -800,6 +799,7 @@ main() {
|
|||
variable_type_mismatch.main();
|
||||
wrong_number_of_parameters_for_operator.main();
|
||||
wrong_number_of_parameters_for_setter.main();
|
||||
wrong_number_of_type_arguments.main();
|
||||
wrong_type_parameter_variance_in_superinterface.main();
|
||||
yield_each_in_non_generator.main();
|
||||
yield_in_non_generator.main();
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:analyzer/src/error/codes.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import '../dart/resolution/driver_resolution.dart';
|
||||
|
||||
main() {
|
||||
defineReflectiveSuite(() {
|
||||
defineReflectiveTests(WrongNumberOfTypeArgumentsTest);
|
||||
});
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class WrongNumberOfTypeArgumentsTest extends DriverResolutionTest {
|
||||
test_const_nonGeneric() async {
|
||||
await assertErrorsInCode('''
|
||||
class C {
|
||||
const C();
|
||||
}
|
||||
|
||||
f() {
|
||||
return const C<int>();
|
||||
}
|
||||
''', [
|
||||
error(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS, 47, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_const_tooFew() async {
|
||||
await assertErrorsInCode('''
|
||||
class C<K, V> {
|
||||
const C();
|
||||
}
|
||||
|
||||
f() {
|
||||
return const C<int>();
|
||||
}
|
||||
''', [
|
||||
error(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS, 53, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_const_tooMany() async {
|
||||
await assertErrorsInCode('''
|
||||
class C<E> {
|
||||
const C();
|
||||
}
|
||||
|
||||
f() {
|
||||
return const C<int, int>();
|
||||
}
|
||||
''', [
|
||||
error(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS, 50, 11),
|
||||
]);
|
||||
}
|
||||
|
||||
test_new_nonGeneric() async {
|
||||
await assertErrorsInCode('''
|
||||
class C {}
|
||||
|
||||
f() {
|
||||
return new C<int>();
|
||||
}
|
||||
''', [
|
||||
error(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS, 31, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_new_tooFew() async {
|
||||
await assertErrorsInCode('''
|
||||
class C<K, V> {}
|
||||
|
||||
f() {
|
||||
return new C<int>();
|
||||
}
|
||||
''', [
|
||||
error(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS, 37, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_new_tooMany() async {
|
||||
await assertErrorsInCode('''
|
||||
class C<E> {}
|
||||
|
||||
f() {
|
||||
return new C<int, int>();
|
||||
}
|
||||
''', [
|
||||
error(StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS, 34, 11),
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -3120,39 +3120,6 @@ class C {
|
|||
}
|
||||
{% endprettify %}
|
||||
|
||||
### new_with_invalid_type_parameters
|
||||
|
||||
_The type '{0}' is declared with {1} type arguments, but {2} type arguments were
|
||||
given._
|
||||
|
||||
#### Description
|
||||
|
||||
The analyzer produces this diagnostic when a constructor is invoked and the
|
||||
number of type arguments doesn't match the number of type parameters
|
||||
declared for the class.
|
||||
|
||||
#### Example
|
||||
|
||||
The following code produces this diagnostic because `C` declares one type
|
||||
parameter, but two type arguments are given:
|
||||
|
||||
{% prettify dart %}
|
||||
class C<E> {}
|
||||
|
||||
var c = [!C<int, int>!]();
|
||||
{% endprettify %}
|
||||
|
||||
#### Common fixes
|
||||
|
||||
Change the number of type arguments to match the number of type parameters
|
||||
declared for the class:
|
||||
|
||||
{% prettify dart %}
|
||||
class C<E> {}
|
||||
|
||||
var c = C<int>();
|
||||
{% endprettify %}
|
||||
|
||||
### new_with_undefined_constructor_default
|
||||
|
||||
_The class '{0}' doesn't have a default constructor._
|
||||
|
|
|
@ -16,7 +16,7 @@ main() {
|
|||
foo = null;
|
||||
var bar = new Map<String>();
|
||||
// ^^^^^^^^^^^
|
||||
// [analyzer] STATIC_WARNING.NEW_WITH_INVALID_TYPE_PARAMETERS
|
||||
// [analyzer] STATIC_TYPE_WARNING.WRONG_NUMBER_OF_TYPE_ARGUMENTS
|
||||
// [cfe] Expected 2 type arguments.
|
||||
baz = new Map();
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
main() {
|
||||
var map = new Map<int>{ "a": 1, "b": 2, "c": 3 };
|
||||
// ^^^^^^^^
|
||||
// [analyzer] STATIC_WARNING.NEW_WITH_INVALID_TYPE_PARAMETERS
|
||||
// [analyzer] STATIC_TYPE_WARNING.WRONG_NUMBER_OF_TYPE_ARGUMENTS
|
||||
// [cfe] Expected 2 type arguments.
|
||||
// ^
|
||||
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
|
||||
|
|
|
@ -32,11 +32,11 @@ main() {
|
|||
a = new A<int>();
|
||||
a = new A<String, String>();
|
||||
// ^^^^^^^^^^^^^^^^^
|
||||
// [analyzer] STATIC_WARNING.NEW_WITH_INVALID_TYPE_PARAMETERS
|
||||
// [analyzer] STATIC_TYPE_WARNING.WRONG_NUMBER_OF_TYPE_ARGUMENTS
|
||||
// [cfe] Expected 1 type arguments.
|
||||
a = new F<int>();
|
||||
a = new F<int, String>();
|
||||
// ^^^^^^^^^^^^^^
|
||||
// [analyzer] STATIC_WARNING.NEW_WITH_INVALID_TYPE_PARAMETERS
|
||||
// [analyzer] STATIC_TYPE_WARNING.WRONG_NUMBER_OF_TYPE_ARGUMENTS
|
||||
// [cfe] Expected 1 type arguments.
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
final foo = Map<int>();
|
||||
// ^^^^^^^^
|
||||
// [analyzer] STATIC_WARNING.NEW_WITH_INVALID_TYPE_PARAMETERS
|
||||
// [analyzer] STATIC_TYPE_WARNING.WRONG_NUMBER_OF_TYPE_ARGUMENTS
|
||||
// [cfe] Expected 2 type arguments.
|
||||
|
||||
main() {}
|
||||
|
|
Loading…
Reference in a new issue