[
https://issues.apache.org/jira/browse/GROOVY-12104?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King updated GROOVY-12104:
-------------------------------
Description:
Inside a trait body, the qualifier syntax `T.this.<member>` (where T
is the enclosing trait) compiles successfully but produces invalid or
mis-typed bytecode that fails at runtime.
Minimal reproducer:
trait V {
static boolean defaultNullable() { false }
static seen() { V.this.defaultNullable() }
}
class Over implements V { static boolean defaultNullable() { true } }
Over.seen()
Expected: either return a value matching a defined semantic, or reject
at compile time as a syntax error (T.this has no coherent meaning in
trait code per GEP-22, where `this` is the implementing instance and
the trait is not an enclosing scope).
Actual:
* Groovy 4.0.32: java.lang.VerifyError "Bad type on operand stack"
(Type 'java/lang/Class' not assignable to 'groovy/lang/Closure'
at V$Trait$Helper.seen(Ljava/lang/Class;)Ljava/lang/Object; @1:
invokevirtual)
* Groovy 5.0.6 / 6.0.0-alpha-1: java.lang.ClassCastException:
Cannot cast java.lang.Class to V
* Bytecode disassembly: 2ab6 0032 c000 09ba 003f 0000 b0
(aload_0 of $static$self (Class) → invokevirtual expecting Closure)
The bug shape changed between 4.x and 5.x — the verifier-level
mismatch on 4.x was apparently fixed but the underlying transform
still produces wrong code (now ClassCastException at runtime).
Discovered during the GROOVY-11985 / GROOVY-12093 thread when
enumerating candidate alternatives Grails could use as workarounds
for trait-body static dispatch (Eric Milles' comment of 25-Jun-2026
asked whether `Validateable.this.defaultNullable()` would work).
Recommended fix: reject T.this.* in trait code at compile time.
For "force the trait's own implementation", `T.super.m(...)` is the
existing supported form. For normal dispatch, `this.m(...)` (which
follows row-1 override-visible dispatch).
> Trait body with T.this.* qualifier produces VerifyError (4.x) /
> ClassCastException (5.x/6.x) — should be a compile error
> ------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12104
> URL: https://issues.apache.org/jira/browse/GROOVY-12104
> Project: Groovy
> Issue Type: Improvement
> Reporter: Paul King
> Priority: Major
>
> Inside a trait body, the qualifier syntax `T.this.<member>` (where T
> is the enclosing trait) compiles successfully but produces invalid or
> mis-typed bytecode that fails at runtime.
> Minimal reproducer:
> trait V {
> static boolean defaultNullable() { false }
> static seen() { V.this.defaultNullable() }
> }
> class Over implements V { static boolean defaultNullable() { true } }
> Over.seen()
> Expected: either return a value matching a defined semantic, or reject
> at compile time as a syntax error (T.this has no coherent meaning in
> trait code per GEP-22, where `this` is the implementing instance and
> the trait is not an enclosing scope).
> Actual:
> * Groovy 4.0.32: java.lang.VerifyError "Bad type on operand stack"
> (Type 'java/lang/Class' not assignable to 'groovy/lang/Closure'
> at V$Trait$Helper.seen(Ljava/lang/Class;)Ljava/lang/Object; @1:
> invokevirtual)
> * Groovy 5.0.6 / 6.0.0-alpha-1: java.lang.ClassCastException:
> Cannot cast java.lang.Class to V
> * Bytecode disassembly: 2ab6 0032 c000 09ba 003f 0000 b0
> (aload_0 of $static$self (Class) → invokevirtual expecting Closure)
> The bug shape changed between 4.x and 5.x — the verifier-level
> mismatch on 4.x was apparently fixed but the underlying transform
> still produces wrong code (now ClassCastException at runtime).
> Discovered during the GROOVY-11985 / GROOVY-12093 thread when
> enumerating candidate alternatives Grails could use as workarounds
> for trait-body static dispatch (Eric Milles' comment of 25-Jun-2026
> asked whether `Validateable.this.defaultNullable()` would work).
> Recommended fix: reject T.this.* in trait code at compile time.
> For "force the trait's own implementation", `T.super.m(...)` is the
> existing supported form. For normal dispatch, `this.m(...)` (which
> follows row-1 override-visible dispatch).
--
This message was sent by Atlassian Jira
(v8.20.10#820010)