[analyzer] Adds new message for `INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR`
Fixes: https://github.com/dart-lang/sdk/issues/61680
Change-Id: I3f5e0e722125a05bcc0f7ff4f9f785c584b7d94b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/453360
Auto-Submit: Felipe Morschel <git@fmorschel.dev>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
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 39a1a4f..454e765 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
@@ -1000,6 +1000,9 @@
CompileTimeErrorCode.INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR:
status: noFix
since: 2.17
+CompileTimeErrorCode.INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR_TEAROFF:
+ status: noFix
+ since: 2.17
CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS:
status: noFix
CompileTimeErrorCode.INVALID_SUPER_FORMAL_PARAMETER_LOCATION:
diff --git a/pkg/analyzer/lib/src/diagnostic/diagnostic_code_values.g.dart b/pkg/analyzer/lib/src/diagnostic/diagnostic_code_values.g.dart
index ca1b3f2..1c6ce78 100644
--- a/pkg/analyzer/lib/src/diagnostic/diagnostic_code_values.g.dart
+++ b/pkg/analyzer/lib/src/diagnostic/diagnostic_code_values.g.dart
@@ -325,6 +325,7 @@
CompileTimeErrorCode.invalidOverride,
CompileTimeErrorCode.invalidOverrideSetter,
CompileTimeErrorCode.invalidReferenceToGenerativeEnumConstructor,
+ CompileTimeErrorCode.invalidReferenceToGenerativeEnumConstructorTearoff,
CompileTimeErrorCode.invalidReferenceToThis,
CompileTimeErrorCode.invalidSuperFormalParameterLocation,
CompileTimeErrorCode.invalidTypeArgumentInConstList,
diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart
index 4bf8cd5..d1e7f94 100644
--- a/pkg/analyzer/lib/src/error/codes.g.dart
+++ b/pkg/analyzer/lib/src/error/codes.g.dart
@@ -4031,13 +4031,25 @@
static const CompileTimeErrorWithoutArguments
invalidReferenceToGenerativeEnumConstructor = CompileTimeErrorWithoutArguments(
'INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR',
- "Generative enum constructors can only be used as targets of redirection.",
+ "Generative enum constructors can only be used to create an enum constant.",
correctionMessage: "Try using an enum value, or a factory constructor.",
hasPublishedDocs: true,
expectedTypes: [],
);
/// No parameters.
+ static const CompileTimeErrorWithoutArguments
+ invalidReferenceToGenerativeEnumConstructorTearoff =
+ CompileTimeErrorWithoutArguments(
+ 'INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR',
+ "Generative enum constructors can't be torn off.",
+ correctionMessage: "Try using an enum value, or a factory constructor.",
+ hasPublishedDocs: true,
+ uniqueName: 'INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR_TEAROFF',
+ expectedTypes: [],
+ );
+
+ /// No parameters.
static const CompileTimeErrorWithoutArguments invalidReferenceToThis =
CompileTimeErrorWithoutArguments(
'INVALID_REFERENCE_TO_THIS',
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index 1239af1..7e00155 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -3860,10 +3860,20 @@
constructorElement.isGenerative &&
constructorElement.enclosingElement is EnumElement) {
if (_currentLibrary.featureSet.isEnabled(Feature.enhanced_enums)) {
- diagnosticReporter.atNode(
- node,
- CompileTimeErrorCode.invalidReferenceToGenerativeEnumConstructor,
- );
+ if (node.parent case ConstructorReference(
+ :var parent,
+ ) when parent is! InstanceCreationExpression) {
+ diagnosticReporter.atNode(
+ node,
+ CompileTimeErrorCode
+ .invalidReferenceToGenerativeEnumConstructorTearoff,
+ );
+ } else {
+ diagnosticReporter.atNode(
+ node,
+ CompileTimeErrorCode.invalidReferenceToGenerativeEnumConstructor,
+ );
+ }
} else {
diagnosticReporter.atNode(node, CompileTimeErrorCode.instantiateEnum);
}
diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml
index f0cb70a..2ef9d31 100644
--- a/pkg/analyzer/messages.yaml
+++ b/pkg/analyzer/messages.yaml
@@ -9079,7 +9079,7 @@
hasPublishedDocs: true
INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR:
parameters: none
- problemMessage: Generative enum constructors can only be used as targets of redirection.
+ problemMessage: Generative enum constructors can only be used to create an enum constant.
correctionMessage: Try using an enum value, or a factory constructor.
hasPublishedDocs: true
documentation: |-
@@ -9134,6 +9134,12 @@
E f() => E.c(2);
```
+ INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR_TEAROFF:
+ sharedName: INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR
+ parameters: none
+ problemMessage: Generative enum constructors can't be torn off.
+ correctionMessage: Try using an enum value, or a factory constructor.
+ hasPublishedDocs: true
INVALID_REFERENCE_TO_THIS:
parameters: none
problemMessage: "Invalid reference to 'this' expression."
diff --git a/pkg/analyzer/test/src/diagnostics/invalid_reference_to_generative_enum_constructor_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_reference_to_generative_enum_constructor_test.dart
index f7851bd..5c4312e 100644
--- a/pkg/analyzer/test/src/diagnostics/invalid_reference_to_generative_enum_constructor_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/invalid_reference_to_generative_enum_constructor_test.dart
@@ -62,7 +62,8 @@
''',
[
error(
- CompileTimeErrorCode.invalidReferenceToGenerativeEnumConstructor,
+ CompileTimeErrorCode
+ .invalidReferenceToGenerativeEnumConstructorTearoff,
58,
7,
),
@@ -142,7 +143,8 @@
''',
[
error(
- CompileTimeErrorCode.invalidReferenceToGenerativeEnumConstructor,
+ CompileTimeErrorCode
+ .invalidReferenceToGenerativeEnumConstructorTearoff,
29,
5,
),