Skip to content

Commit ded5f0b

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Dot shorthands: Report error for using enum constructor.
Added an additional error check for `CompileTimeErrorCode.INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR`. Refactored the helper slightly to allow dot shorthand constructor invocations to be able to use it. Added a unit test. Bug: #59835 Change-Id: I4eee2a3464913cdba0e213aa47a29faad2141937 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428222 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 2565d07 commit ded5f0b

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
628628
@override
629629
void visitConstructorReference(covariant ConstructorReferenceImpl node) {
630630
_typeArgumentsVerifier.checkConstructorReference(node);
631-
_checkForInvalidGenerativeConstructorReference(node.constructorName);
631+
_checkForInvalidGenerativeConstructorReference(
632+
node.constructorName,
633+
node.constructorName.element,
634+
);
632635
}
633636

634637
@override
@@ -650,11 +653,14 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
650653
void visitDotShorthandConstructorInvocation(
651654
DotShorthandConstructorInvocation node,
652655
) {
653-
if (node.isConst) {
654-
_checkForConstWithNonConst(
655-
node,
656-
node.constructorName.element as ConstructorElement?,
657-
node.constKeyword,
656+
var constructorElement = node.constructorName.element;
657+
if (constructorElement is ConstructorElement?) {
658+
if (node.isConst) {
659+
_checkForConstWithNonConst(node, constructorElement, node.constKeyword);
660+
}
661+
_checkForInvalidGenerativeConstructorReference(
662+
node.constructorName,
663+
constructorElement,
658664
);
659665
}
660666
_requiredParametersVerifier.visitDotShorthandConstructorInvocation(node);
@@ -1096,7 +1102,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
10961102
DartType type = namedType.typeOrThrow;
10971103
if (type is InterfaceType) {
10981104
_checkForConstOrNewWithAbstractClass(node, namedType, type);
1099-
_checkForInvalidGenerativeConstructorReference(constructorName);
1105+
_checkForInvalidGenerativeConstructorReference(
1106+
constructorName,
1107+
constructorName.element,
1108+
);
11001109
_checkForConstOrNewWithMixin(node, namedType, type);
11011110
_requiredParametersVerifier.visitInstanceCreationExpression(node);
11021111
_constArgumentsVerifier.visitInstanceCreationExpression(node);
@@ -3823,8 +3832,12 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
38233832
}
38243833
}
38253834

3826-
void _checkForInvalidGenerativeConstructorReference(ConstructorName node) {
3827-
var constructorElement = node.element;
3835+
/// Verify that we're not using an enum constructor anywhere other than to
3836+
/// create an enum constant or as a target of constructor redirection.
3837+
void _checkForInvalidGenerativeConstructorReference(
3838+
AstNode node,
3839+
ConstructorElement? constructorElement,
3840+
) {
38283841
if (constructorElement != null &&
38293842
constructorElement.isGenerative &&
38303843
constructorElement.enclosingElement is EnumElement) {
@@ -3834,7 +3847,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
38343847
CompileTimeErrorCode.INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR,
38353848
);
38363849
} else {
3837-
errorReporter.atNode(node.type, CompileTimeErrorCode.INSTANTIATE_ENUM);
3850+
errorReporter.atNode(node, CompileTimeErrorCode.INSTANTIATE_ENUM);
38383851
}
38393852
}
38403853
}
@@ -5029,7 +5042,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
50295042
arguments: [constructorStrName, redirectedClass.name3!],
50305043
);
50315044
}
5032-
_checkForInvalidGenerativeConstructorReference(redirectedConstructor);
5045+
_checkForInvalidGenerativeConstructorReference(
5046+
redirectedConstructor,
5047+
redirectedElement,
5048+
);
50335049
}
50345050

50355051
/// Check whether the redirecting constructor, [element], is const, and

pkg/analyzer/test/src/dart/resolution/dot_shorthand_constructor_invocation_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,30 @@ DotShorthandConstructorInvocation
342342
''');
343343
}
344344

345+
test_enum_constructor() async {
346+
await assertErrorsInCode(
347+
r'''
348+
enum E {
349+
v.named();
350+
351+
const E.named();
352+
}
353+
354+
void f() {
355+
E e = .named();
356+
print(e);
357+
}
358+
''',
359+
[
360+
error(
361+
CompileTimeErrorCode.INVALID_REFERENCE_TO_GENERATIVE_ENUM_CONSTRUCTOR,
362+
65,
363+
5,
364+
),
365+
],
366+
);
367+
}
368+
345369
test_equality() async {
346370
await assertNoErrorsInCode(r'''
347371
class C {

0 commit comments

Comments
 (0)