[ 
https://issues.apache.org/jira/browse/GROOVY-12654?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18137335#comment-18137335
 ] 

ASF GitHub Bot commented on GROOVY-12654:
-----------------------------------------

codeconsole opened a new pull request, #3175:
URL: https://github.com/apache/groovy/pull/3175

   Under `@CompileStatic`, an instance method of `java.lang.Class` called 
without a receiver in a static method or static initializer loaded local slot 0 
as the receiver:
   
   ```groovy
   @groovy.transform.CompileStatic
   class C {
       static Object sm() { getName() }        // VerifyError: Bad local 
variable type
       static Object sp(Class t) { getName() } // returns t's name instead of 
'C'
   }
   ```
   
   Dynamic and `@TypeChecked` code call it on the class, as did 4.0.33; 5.0.8 
and 6.0.0 fail. Packed closures (`groovy.target.closure.pack`, 
`@PackedClosures`) hit the same failure: a closure in a static method is 
hoisted into a static method, so `{ -> toString() }()` there failed 
verification too. That is the failure in 
`ClosuresStaticCompileTest.testClosureObjectMethodPrecedence` under the flag.
   
   `StaticInvocationWriter.writeDirectMethodCall` now uses the enclosing class 
as the receiver when the call is in a static method, outside a closure or 
lambda class, and the method is a member of `Class`. This matches how 
`AsmClassGenerator` loads `this` in a static method. The receiver of an 
outer-class method inside `this(...)`/`super(...)` arguments is unchanged.
   
   ## Tests
   
   `MethodCallsStaticCompilationTest.testStaticContext3` (static methods, a 
static initializer, a `Class` parameter, a static nested class) and 
`PackedClosuresTransformTest.compileStaticPackedClosureInStaticMethodCallsClassMethodsOnTheClass`
 fail without the change. The packing test classes, 
`org.codehaus.groovy.classgen.asm.sc.*` and `groovy.transform.stc.*` pass, 
without and with `-Pgroovy.target.closure.pack=true` (with the flag, three 
failures remain that also fail on master and have their own issues). The 
Checkstyle and CodeNarc gates pass.
   




> SC: calling an instance method of Class without a receiver in a static method 
> fails verification or uses the wrong receiver
> ---------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12654
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12654
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 5.0.8, 7.0.0-alpha-1, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code:groovy}
> @groovy.transform.CompileStatic
> class C {
>     static Object sm() { getName() }
>     static Object st() { toString() }
>     static Object sp(Class type) { getName() }
> }
> println C.sm(); println C.st(); println C.sp(String)
> {code}
> *Actual:* {{java.lang.VerifyError: Bad local variable type}} 
> ({{C.sm()Ljava/lang/Object; @0: aload_0}}). With only {{sp}}, it prints 
> {{java.lang.String}}: the method is called on the parameter. A static field 
> initializer such as {{static String p = getName()}} fails the same way in the 
> static initializer. With closure packing ({{groovy.target.closure.pack=true}} 
> or {{@PackedClosures}}), a closure in a static method is hoisted into a 
> static method, so {{static m() ... toString() inside a closure}} fails 
> verification the same way.
> *Expected:* {{C}}, {{class C}}, {{C}}, as in dynamic and {{@TypeChecked}} 
> code, and as in 4.0.33.
> Cause: for an implicit-this call to a method declared outside the enclosing 
> class, {{StaticInvocationWriter.writeDirectMethodCall}} builds the receiver 
> {{Type.this}}, which in a static method loads local slot 0 instead of the 
> class.
> Verified: 4.0.33 prints the expected values; 5.0.8, 6.0.0 and master fail.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to