[
https://issues.apache.org/jira/browse/GROOVY-12577?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18134527#comment-18134527
]
ASF GitHub Bot commented on GROOVY-12577:
-----------------------------------------
codeconsole opened a new pull request, #3103:
URL: https://github.com/apache/groovy/pull/3103
Under `@CompileStatic`, a static method or static initializer that calls
`getName()`, `getSimpleName()` or `toString()` without a receiver now calls it
on the class object, as dynamic Groovy, an explicit `this.getSimpleName()` and
4.0 do. Before, the class failed verification:
```groovy
@CompileStatic
class C {
static String m() { getSimpleName() } // VerifyError: Bad local
variable type ... aload_0
}
```
The static invocation writer treated the method as one of an enclosing class
and loaded `Class.this`, local variable 0, which does not exist in a static
method. Methods of `Object`, such as `hashCode()`, already worked.
Tests: `MethodCallsSTCTest.testCallClassMethodFromStaticContext` (also run
statically compiled by `MethodCallsStaticCompilationTest`), which fails without
the change. The `groovy`, `gls`, `bugs` and `org.codehaus.groovy` packages pass
(15,497 tests), apart from `bugs.Groovy12363`, which also fails on master when
it runs after `bugs.Groovy12142` in the same JVM (GROOVY-12580 fixes it). The
Checkstyle and CodeNarc gates pass.
A regression in 5.0.0: 4.0.33 is correct, 5.0.8 and 6.0.0 are not.
> SC: a method of Class called without a receiver in a static method fails
> verification
> -------------------------------------------------------------------------------------
>
> Key: GROOVY-12577
> URL: https://issues.apache.org/jira/browse/GROOVY-12577
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> @groovy.transform.CompileStatic
> class C {
> static String m() { getSimpleName() }
> }
> C.m() // VerifyError: Bad local variable type ... C.m()Ljava/lang/String; @0:
> aload_0
> {code}
> In a static method or static initializer, {{this}} is the class object, and
> the type checker accepts a call to a method of {{Class}} without a receiver,
> like {{getName()}}, {{getSimpleName()}} or {{toString()}}. The static
> invocation writer treats the method as one of an enclosing class and loads
> {{Class.this}}, local variable 0, which does not exist in a static method.
> Methods of {{Object}}, such as {{hashCode()}}, work. 4.0.33 returns {{'C'}},
> as dynamic Groovy and an explicit {{this.getSimpleName()}} do.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)