Scott Murphy Heiberg created GROOVY-12512:
---------------------------------------------
Summary: SC: instance method of the enclosing class called from an
anonymous class in a static context compiles, then fails with VerifyError
Key: GROOVY-12512
URL: https://issues.apache.org/jira/browse/GROOVY-12512
Project: Groovy
Issue Type: Bug
Components: Static compilation, Static Type Checker
Affects Versions: 6.0.0
Reporter: Scott Murphy Heiberg
An anonymous class created in a static method has no enclosing instance, so it
cannot call an instance method of the enclosing class. {{@CompileStatic}}
accepts the call and generates code that fails verification:
{code:groovy}
@groovy.transform.CompileStatic
class Box {
String pm() { 'pm' }
static Object test() {
def result
new Runnable() {
void run() { result = pm() }
}.run()
result
}
}
Box.test()
{code}
Result:
* {{@CompileStatic}}: {{java.lang.VerifyError: Bad type on operand stack}} (in
{{Box$1.run}})
* dynamic Groovy and {{@TypeChecked}}: {{groovy.lang.MissingMethodException}}:
"No signature of method: pm for class: Box$1 is applicable for argument types:
()"
* javac: "non-static method pm() cannot be referenced from a static context"
The same happens for a private method, for an anonymous class in a static
initializer or a static field initializer, and for an anonymous class in a
static method of a static nested class (all {{VerifyError}}). With a closure
inside the anonymous class, or an anonymous class created inside a closure in a
static method, {{@CompileStatic}} fails at run time with a
{{ClassCastException}} ({{Box$1 cannot be cast to Box}}, or {{java.lang.Class
cannot be cast to Box}}).
A reference to an instance field in the same place is already rejected
("Apparent variable 'id' was found in a static scope but doesn't refer to a
local variable, static field or class"), and the same call in a lambda gets
"Non-static method Box#pm cannot be called from static context".
Seen in 4.0.33, 5.0.8, 6.0.0 and master.
Expected: a compile error under {{@TypeChecked}} and {{@CompileStatic}}, such
as "Non-static method Box#pm cannot be called from static context".
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)