[ 
https://issues.apache.org/jira/browse/GROOVY-12190?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King resolved GROOVY-12190.
--------------------------------
    Fix Version/s: 4.0.33
                   5.0.8
                   6.0.0-beta-1
       Resolution: Fixed

> @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
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 4.0.33, 5.0.8, 6.0.0-beta-1
>
>
> 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)

Reply via email to