Unify duplicate_field_formal_parameter and final_initialized_multiple_times

I started doing this the normal way, but the two codes appear to differ
only based on whether the field was final, and I don't think that factor
is important to the nature of the error, so I think a single code is
sufficient.

Change-Id: I693dcfb1a2a4b47a8b82fe4860e7a61c82ca062e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196880
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson
2021-04-27 17:32:18 +00:00
committed by commit-bot@chromium.org
parent 605b211c89
commit d92ed4d72f
9 changed files with 28 additions and 75 deletions
-1
View File
@@ -186,7 +186,6 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR,
CompileTimeErrorCode.FIELD_INITIALIZING_FORMAL_NOT_ASSIGNABLE,
CompileTimeErrorCode.FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR,
CompileTimeErrorCode.FINAL_INITIALIZED_MULTIPLE_TIMES,
CompileTimeErrorCode.FINAL_NOT_INITIALIZED,
CompileTimeErrorCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_1,
CompileTimeErrorCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_2,
+2 -15
View File
@@ -3168,8 +3168,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
static const CompileTimeErrorCode DUPLICATE_FIELD_FORMAL_PARAMETER =
CompileTimeErrorCode(
'DUPLICATE_FIELD_FORMAL_PARAMETER',
"The field '{0}' can't be referenced in multiple initializing "
"parameters in the same constructor.",
"The field '{0}' can't be initialized by multiple parameters in the "
"same constructor.",
correction: "Try removing one of the parameters, or "
"using different fields.");
@@ -4653,19 +4653,6 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
correction: "Try removing one of the initializations.",
hasPublishedDocs: true);
/**
* 5 Variables: It is a compile-time error if a final instance variable that
* has is initialized by means of an initializing formal of a constructor is
* also initialized elsewhere in the same constructor.
*
* Parameters:
* 0: the name of the field in question
*/
static const CompileTimeErrorCode FINAL_INITIALIZED_MULTIPLE_TIMES =
CompileTimeErrorCode('FINAL_INITIALIZED_MULTIPLE_TIMES',
"'{0}' is a final field and so can only be set once.",
correction: "Try removing all but one of the initializations.");
/**
* Parameters:
* 0: the name of the uninitialized final variable
@@ -219,13 +219,7 @@ class ConstructorFieldsVerifier {
);
}
} else if (state == _InitState.initInFieldFormal) {
if (fieldElement.isFinal || fieldElement.isConst) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FINAL_INITIALIZED_MULTIPLE_TIMES,
parameter.identifier,
[fieldElement.displayName],
);
}
// Reported in DuplicateDefinitionVerifier._checkDuplicateIdentifier
}
}
}
@@ -384,14 +384,7 @@ class DuplicateDefinitionVerifier {
var previous = getterScope[name];
if (previous != null) {
if (_isGetterSetterPair(element, previous)) {
// OK
} else if (element is FieldFormalParameterElement &&
previous is FieldFormalParameterElement &&
element.field != null &&
element.field!.isFinal) {
// Reported as CompileTimeErrorCode.FINAL_INITIALIZED_MULTIPLE_TIMES.
} else {
if (!_isGetterSetterPair(element, previous)) {
_errorReporter.reportErrorForNode(
getError(previous, element),
identifier,
@@ -37,6 +37,17 @@ class A {
]);
}
test_optional_positional_final() async {
await assertErrorsInCode(r'''
class A {
final x;
A([this.x = 1, this.x = 2]) {}
}
''', [
error(CompileTimeErrorCode.DUPLICATE_FIELD_FORMAL_PARAMETER, 43, 1),
]);
}
test_required_named() async {
await assertErrorsInCode(r'''
class A {
@@ -58,4 +69,15 @@ class A {
error(CompileTimeErrorCode.DUPLICATE_FIELD_FORMAL_PARAMETER, 36, 1),
]);
}
test_required_positional_final() async {
await assertErrorsInCode(r'''
class A {
final x;
A(this.x, this.x) {}
}
''', [
error(CompileTimeErrorCode.DUPLICATE_FIELD_FORMAL_PARAMETER, 38, 1),
]);
}
}
@@ -1,39 +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/context_collection_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(FinalInitializedMultipleTimesTest);
});
}
@reflectiveTest
class FinalInitializedMultipleTimesTest extends PubPackageResolutionTest {
test_initializingFormals_withDefaultValues() async {
await assertErrorsInCode(r'''
class A {
final x;
A([this.x = 1, this.x = 2]) {}
}
''', [
error(CompileTimeErrorCode.FINAL_INITIALIZED_MULTIPLE_TIMES, 43, 1),
]);
}
test_initializingFormals_withoutDefaultValues() async {
await assertErrorsInCode(r'''
class A {
final x;
A(this.x, this.x) {}
}
''', [
error(CompileTimeErrorCode.FINAL_INITIALIZED_MULTIPLE_TIMES, 38, 1),
]);
}
}
@@ -207,8 +207,6 @@ import 'field_initializing_formal_not_assignable_test.dart'
as field_initializing_formal_not_assignable;
import 'final_initialized_in_delcaration_and_constructor_test.dart'
as final_initialized_in_declaration_and_constructor;
import 'final_initialized_multiple_times_test.dart'
as final_initialized_multiple_times;
import 'final_not_initialized_constructor_test.dart'
as final_not_initialized_constructor;
import 'final_not_initialized_test.dart' as final_not_initialized;
@@ -820,7 +818,6 @@ main() {
final_initialized_in_declaration_and_constructor.main();
field_initialized_in_initializer_and_declaration.main();
field_initialized_in_parameter_and_initializer.main();
final_initialized_multiple_times.main();
field_initializer_factory_constructor.main();
field_initializer_in_struct.main();
field_initializer_not_assignable.main();
@@ -25,7 +25,7 @@
Class.two_fields(this.field_
, this.field_
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.FINAL_INITIALIZED_MULTIPLE_TIMES
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_FIELD_FORMAL_PARAMETER
// [cfe] 'field_' was already initialized by this constructor.
// ^
// [cfe] Duplicated parameter name 'field_'.
@@ -27,7 +27,7 @@
Class.two_fields(this.field_
, this.field_
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.FINAL_INITIALIZED_MULTIPLE_TIMES
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_FIELD_FORMAL_PARAMETER
// [cfe] 'field_' was already initialized by this constructor.
// ^
// [cfe] Duplicated parameter name 'field_'.