Improve the messaging around conflicting constructors
Fixes: https://github.com/dart-lang/sdk/issues/46803
Change-Id: I0435ea15cfb4c57dfb865f66d251afbe1d9459a6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/208921
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/error/error.dart b/pkg/analyzer/lib/error/error.dart
index 7d50e7e..a8fcbf3 100644
--- a/pkg/analyzer/lib/error/error.dart
+++ b/pkg/analyzer/lib/error/error.dart
@@ -95,7 +95,9 @@
CompileTimeErrorCode.CAST_TO_NON_TYPE,
CompileTimeErrorCode.CONCRETE_CLASS_WITH_ABSTRACT_MEMBER,
CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD,
+ CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER,
CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_METHOD,
+ CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER,
CompileTimeErrorCode.CONFLICTING_FIELD_AND_METHOD,
CompileTimeErrorCode.CONFLICTING_GENERIC_INTERFACES,
CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD,
diff --git a/pkg/analyzer/lib/src/error/codes.dart b/pkg/analyzer/lib/src/error/codes.dart
index fb2881f..29f4fe2 100644
--- a/pkg/analyzer/lib/src/error/codes.dart
+++ b/pkg/analyzer/lib/src/error/codes.dart
@@ -1565,7 +1565,7 @@
/**
* Parameters:
- * 0: the name of the field
+ * 0: the name of the constructor and field
*/
// #### Description
//
@@ -1610,6 +1610,19 @@
/**
* Parameters:
+ * 0: the name of the constructor and getter
+ */
+ static const CompileTimeErrorCode CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER =
+ CompileTimeErrorCode(
+ 'CONFLICTING_CONSTRUCTOR_AND_STATIC_MEMBER',
+ "'{0}' can't be used to name both a constructor and a static getter "
+ "in this class.",
+ correction: "Try renaming either the constructor or the getter.",
+ hasPublishedDocs: true,
+ uniqueName: 'CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER');
+
+ /**
+ * Parameters:
* 0: the name of the constructor
*/
static const CompileTimeErrorCode CONFLICTING_CONSTRUCTOR_AND_STATIC_METHOD =
@@ -1622,6 +1635,19 @@
uniqueName: 'CONFLICTING_CONSTRUCTOR_AND_STATIC_METHOD');
/**
+ * Parameters:
+ * 0: the name of the constructor and setter
+ */
+ static const CompileTimeErrorCode CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER =
+ CompileTimeErrorCode(
+ 'CONFLICTING_CONSTRUCTOR_AND_STATIC_MEMBER',
+ "'{0}' can't be used to name both a constructor and a static setter "
+ "in this class.",
+ correction: "Try renaming either the constructor or the setter.",
+ hasPublishedDocs: true,
+ uniqueName: 'CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER');
+
+ /**
* 10.11 Class Member Conflicts: Let `C` be a class. It is a compile-time
* error if `C` declares a getter or a setter with basename `n`, and has a
* method named `n`.
diff --git a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
index 0947fbe..7ad7ac9 100644
--- a/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
+++ b/pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
@@ -302,11 +302,18 @@
var staticMember = staticGetters[name] ?? staticSetters[name];
if (staticMember != null) {
if (staticMember is PropertyAccessorElement) {
- _errorReporter.reportErrorForNode(
- CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD,
- nameNode,
- [name],
- );
+ CompileTimeErrorCode errorCode;
+ if (staticMember.isSynthetic) {
+ errorCode = CompileTimeErrorCode
+ .CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD;
+ } else if (staticMember.isGetter) {
+ errorCode = CompileTimeErrorCode
+ .CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER;
+ } else {
+ errorCode = CompileTimeErrorCode
+ .CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER;
+ }
+ _errorReporter.reportErrorForNode(errorCode, nameNode, [name]);
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_METHOD,
diff --git a/pkg/analyzer/test/src/dart/resolution/class_test.dart b/pkg/analyzer/test/src/dart/resolution/class_test.dart
index d5bb61e..c59ee4f 100644
--- a/pkg/analyzer/test/src/dart/resolution/class_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/class_test.dart
@@ -180,8 +180,8 @@
static int get foo => 0;
}
''', [
- error(
- CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD, 14, 3),
+ error(CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_GETTER, 14,
+ 3),
]);
}
@@ -212,8 +212,8 @@
static void set foo(_) {}
}
''', [
- error(
- CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_FIELD, 14, 3),
+ error(CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_AND_STATIC_SETTER, 14,
+ 3),
]);
}
diff --git a/pkg/analyzer/tool/diagnostics/diagnostics.md b/pkg/analyzer/tool/diagnostics/diagnostics.md
index f6857fc..0cdb816 100644
--- a/pkg/analyzer/tool/diagnostics/diagnostics.md
+++ b/pkg/analyzer/tool/diagnostics/diagnostics.md
@@ -1845,9 +1845,15 @@
_'{0}' can't be used to name both a constructor and a static field in this
class._
+_'{0}' can't be used to name both a constructor and a static getter in this
+class._
+
_'{0}' can't be used to name both a constructor and a static method in this
class._
+_'{0}' can't be used to name both a constructor and a static setter in this
+class._
+
#### Description
The analyzer produces this diagnostic when a named constructor and either a