[
https://issues.apache.org/jira/browse/GROOVY-12473?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18130677#comment-18130677
]
ASF GitHub Bot commented on GROOVY-12473:
-----------------------------------------
paulk-asert commented on PR #3008:
URL: https://github.com/apache/groovy/pull/3008#issuecomment-6356955487
AI suggestion, I haven't checked myself yet:
> The fix works for the reported case and `def x = Float; x.NaN`, but the
restructuring regresses a raw-`Class` receiver, which is the
`ClosuresStaticCompileTest.testClassClosureDelegateProperty` failure on every
JDK job:
>
> Foo.with { assert name == 'Foo' } // PR gives 'java.lang.Class'
>
> The delegate's property owner is raw `Class<T>` (no concrete type
argument). The new `isClassType(receiverType)` check sends it down the
class-receiver branch, and `classX(receiverType)` then becomes the literal
`Class.class`, so `getName()` runs on that. Substituting a class literal is
only valid when the type argument is concrete.
>
> The underlying bug looks narrower than the rewrite:
`makeGetPropertyWithGetter` only rejects an instance getter when `receiver
instanceof ClassExpression`, so a `Class`-valued receiver that isn't a literal
(cast, variable, `def`) lets the instance `isNaN()` beat the static field
`NaN`. The cast case additionally isn't flagged as a class receiver because the
checker records `Float` as the property owner with no inferred type.
>
> A ~20-line alternative on master: pass the existing `isClassReceiver` flag
into `makeGetPropertyWithGetter` and reject instance getters for it, the same
way as for a class literal, and in `getPropertyOwnerType` also set the flag
when the receiver's declared type is `Class<X>` with `X` the recorded owner:
>
> ```diff
> -
> SC: static compilation in some situation prefers boolean getter over static
> field and produces cast exception
> -------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12473
> URL: https://issues.apache.org/jira/browse/GROOVY-12473
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.5.23, 3.0.25, 5.0.3, 4.0.33, 5.1.3, 6.0.0
> Reporter: Björn Kautler
> Assignee: Eric Milles
> Priority: Major
>
> Given this snippet:
> {code:groovy}
> @groovy.transform.CompileStatic
> def foo() {
> (Float as Class<Float>).NaN
> }
> foo().toString()
> {code}
> It fails at runtime with a class-cast exception, because static compilation
> decides it would be a good idea to interpret the {{NaN}} not as the static
> field, but as {{isNaN()}} call, casting the {{Class<FLoat>}} to {{Float}}
> which fails of course.
> (This might seem like forged example, of course you would usually do
> {{Float.NaN}} which works. Nevertheless it should work and not produce cast
> exception, and if you use {{Float.NaN}} in a Spock assertion with a
> SC-optimized Spock, you get exactly that cast exception)
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)