[
https://issues.apache.org/jira/browse/GROOVY-12190?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18099073#comment-18099073
]
ASF GitHub Bot commented on GROOVY-12190:
-----------------------------------------
Copilot commented on code in PR #2735:
URL: https://github.com/apache/groovy/pull/2735#discussion_r3649246451
##########
src/main/java/org/codehaus/groovy/transform/StaticTypesTransformation.java:
##########
@@ -73,6 +81,45 @@ public void visit(ASTNode[] nodes, SourceUnit source) {
}
if (visitor != null) {
visitor.performSecondPass();
+ resolveEnumConstantsInSwitchCases(node, source);
+ }
+ }
+
+ /**
+ * GROOVY-8444, GROOVY-11614: replaces "CONST" expressions resolved by the
+ * type checker in enum switch case labels with "EnumType.CONST"
expressions;
+ * without static compilation they would otherwise be dynamic property
lookups
+ * on the enclosing class, failing at runtime.
+ */
Review Comment:
The Javadoc for this helper mentions GROOVY-8444 and GROOVY-11614 only, but
this change also implements GROOVY-12190 and is now invoked by the
`@TypeChecked` transformation. Updating the comment makes it easier to track
why this AST rewrite exists and when it applies.
##########
src/main/java/org/codehaus/groovy/transform/StaticTypesTransformation.java:
##########
@@ -73,6 +81,45 @@ public void visit(ASTNode[] nodes, SourceUnit source) {
}
if (visitor != null) {
visitor.performSecondPass();
+ resolveEnumConstantsInSwitchCases(node, source);
+ }
+ }
+
+ /**
+ * GROOVY-8444, GROOVY-11614: replaces "CONST" expressions resolved by the
+ * type checker in enum switch case labels with "EnumType.CONST"
expressions;
+ * without static compilation they would otherwise be dynamic property
lookups
+ * on the enclosing class, failing at runtime.
+ */
+ private static void resolveEnumConstantsInSwitchCases(final AnnotatedNode
node, final SourceUnit source) {
+ ClassCodeExpressionTransformer transformer = new
ClassCodeExpressionTransformer() {
+ @Override
+ protected SourceUnit getSourceUnit() {
+ return source;
+ }
+
+ @Override
+ public Expression transform(final Expression expression) {
+ if (expression instanceof ClosureExpression) { // switch
expressions are desugared to closures
+ expression.visit(this);
+ return expression;
+ }
+ if (expression instanceof VariableExpression) {
+ ClassNode enumType =
expression.getNodeMetaData(StaticTypesMarker.SWITCH_CONDITION_EXPRESSION_TYPE);
+ if (enumType != null) {
+ PropertyExpression pe = propX(classX(enumType),
expression.getText());
+ pe.getProperty().setSourcePosition(expression);
+ return pe;
Review Comment:
When rewriting the case-label VariableExpression to a PropertyExpression,
the new node should preserve source positions (for accurate stack traces/debug
info) and should mirror the existing static-compilation rewrite by tagging the
inferred type. `ClassCodeExpressionTransformer#setSourcePosition` exists for
this purpose.
> @TypeChecked switch on enum: unqualified case constants pass type checking
> but throw MissingPropertyException at runtime
> ------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12190
> URL: https://issues.apache.org/jira/browse/GROOVY-12190
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.32, 6.0.0-alpha-2, 5.0.7
> Reporter: Paul King
> Priority: Major
>
> Since the follow-up refactor for GROOVY-11614 (4.0.28, 5.0.0), unqualified
> enum
> constants in {{switch}} case labels under {{@TypeChecked}} (without static
> compilation) pass type checking but throw {{MissingPropertyException}} at
> runtime. This worked correctly from Groovy 3.0.0-RC-1, where GROOVY-8444 first
> shipped, through 4.0.27.
> h3. Reproducer
> {code:groovy}
> enum Color { RED, GREEN, BLUE }
> @groovy.transform.TypeChecked
> def m(Color c) {
> switch (c) {
> case GREEN: return 'matched'
> default: return 'default'
> }
> }
> assert m(Color.GREEN) == 'matched'
> {code}
> *Expected* (and actual on 3.0.0-RC-1 through 4.0.27): the assert passes.
> *Actual* on 4.0.28+, 5.x and master:
> {noformat}
> groovy.lang.MissingPropertyException: No such property: GREEN for class: repro
> at repro.m(repro.groovy:6)
> at repro.run(repro.groovy:10)
> {noformat}
> The same failure occurs with switch expressions ({{case GREEN -> 'matched'}})
> —
> same root cause, so both forms are covered by this issue. With
> {{@CompileStatic}} both forms work correctly.
> h3. Timeline
> * *2.x*: unqualified enum case constants were never supported; type-checked
> code failed fast with a clean compile-time error:
> {{[Static type checking] - The variable [GREEN] is undeclared.}}
> * *3.0.0-RC-1 → 4.0.27*: works under both {{@TypeChecked}} and
> {{@CompileStatic}} (GROOVY-8444, never backported to 2.x). Switch
> _expressions_ with unqualified constants were broken under both modes until
> GROOVY-11614.
> * *4.0.28+ / 5.0.0+*: GROOVY-11614 fixed switch expressions for
> {{@CompileStatic}}, but its follow-up refactor regressed {{@TypeChecked}} for
> both switch forms to compile-then-runtime-failure — worse than both earlier
> eras, since it fails neither at compile time nor safely.
> h3. Root cause
> {{EnumTypeCheckingExtension#handleUnresolvedVariableExpression}} resolves the
> bare constant during type checking (so no compile error is reported), and the
> follow-up commit for GROOVY-11614 (81098ecb52, "SC: apply enum-case
> transformation after STC visitation") moved the {{CONST}} →
> {{EnumType.CONST}} rewrite from {{CompilationUnit}} into
> {{VariableExpressionTransformer}}, which only runs under static compilation.
> So in TC-only mode the type checker vouches for the reference, but the
> generated bytecode still performs a dynamic property lookup on the enclosing
> class, which fails at runtime. {{Groovy8444.groovy}} only exercises
> {{@CompileStatic}}, so no test caught the regression.
> h3. Possible fixes
> * Apply the enum-case rewrite after STC visitation for type-checked (non-SC)
> code as well, restoring the 3.x/early-4.x behaviour (originally implemented as
> a {{CompilationUnit}} phase operation keyed off STC metadata); or
> * Have {{EnumTypeCheckingExtension}} decline to resolve the constant outside
> static compilation, so TC code at least fails at compile time (matching the
> 2.x behaviour) rather than at runtime.
> Either way, {{Groovy8444.groovy}} should gain {{@TypeChecked}} variants of the
> switch statement and switch expression tests.
> h3. Workarounds
> Qualify the constant ({{case Color.GREEN:}}), add
> {{import static Color.*}}, or use {{@CompileStatic}}.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)