Mattias Reichel created GROOVY-12789:
----------------------------------------

             Summary: An instance method of a class is checked as an override 
of a static method of its interfaces, which since GROOVY-12111 breaks classes 
that implement a trait
                 Key: GROOVY-12789
                 URL: https://issues.apache.org/jira/browse/GROOVY-12789
             Project: Groovy
          Issue Type: Bug
          Components: Compiler
    Affects Versions: 6.0.0
         Environment: OpenJDK 21.0.12, Linux
            Reporter: Mattias Reichel


Since GROOVY-12111, a trait's public static methods are also static methods of 
the generated trait interface. A class that implements the trait and declares 
an instance method with the same name and parameters, but another return type, 
no longer compiles. A property is enough, because its getter is such a method.

This compiles on Groovy 5.1.3 and fails on 6.0.0, both when the trait is in the 
same compilation and when it is precompiled.

h3. Steps to reproduce

{code:title=T.groovy}
trait T {
    static Long getCount() { 0L }
}
{code}

{code:title=C.groovy}
class C implements T {
    Integer count
}
{code}

{noformat}
mkdir out
java -cp groovy-6.0.0.jar org.codehaus.groovy.tools.FileSystemCompiler -d out 
T.groovy C.groovy
{noformat}

h3. Expected

Compilation succeeds, as on Groovy 5.1.3. javac accepts the same shape in Java 
(see below).

h3. Actual

{noformat}
C.groovy: 2: The return type of java.lang.Integer getCount() in C is 
incompatible with java.lang.Long in T
{noformat}

h3. Notes

The same error comes with an explicit {{Integer getCount()}} method instead of 
the property, and with a method that isn't a getter: {{static Long size()}} in 
the trait and {{Integer size()}} in the class. When the return type is 
assignable, as with a trait {{TNum}} that has {{static Number getCount()}} 
instead, and the same class, 6.0.0 reports {{Cannot override method 
java.lang.Number getCount() in TNum with disparate static modifier}} instead. 
{{Long count}} compiles. A {{@Virtual}} trait static isn't promoted, so with it 
the class compiles too.

*The check is older than 6.0.* The same class against a Java interface fails on 
4.0.33, 5.1.3 and 6.0.0:

{code:title=J.java}
public interface J {
    static Long getCount() { return 0L; }
}
{code}

{code:title=CJ.groovy}
class CJ implements J {
    Integer count
}
{code}

{noformat}
CJ.groovy: 2: The return type of java.lang.Integer getCount() in CJ is 
incompatible with java.lang.Long in J
{noformat}

javac compiles the Java equivalent, and {{new JC().getCount()}} and 
{{J.getCount()}} return 1 and 0:

{code:title=JC.java}
public class JC implements J {
    public Integer getCount() { return 1; }
}
{code}

JLS ยง8.4.8 says: "A class does not inherit private or static methods from its 
superinterface types." So an instance method can't override a static method of 
an interface, and there is nothing to check. Since GROOVY-12111, every public 
trait static reaches this check.

For a static method of a superclass, javac reports an error ({{getCount() in 
JSub cannot override getCount() in JSup}}), and so does Groovy, so that part of 
the check is right.

*Where it happens.* {{Verifier.addCovariantMethods}} passes every method of 
each interface, static ones included, to {{storeMissingCovariantMethods}} 
([Verifier.java:1812|https://github.com/apache/groovy/blob/GROOVY_6_0_0/src/main/java/org/codehaus/groovy/classgen/Verifier.java#L1812]
 at {{GROOVY_6_0_0}}, unchanged on {{GROOVY_6_0_X}} and {{master}}). 
{{getCovariantImplementation}} then throws one of the two errors (lines 1882 
and 1901) without looking at whether the interface method is static.

I tried leaving out the static methods of the interface in that loop:

{code:java}
-            List<MethodNode> ifaceMethods = anInterface.getMethods();
+            // a class does not inherit the static methods of its interfaces 
(JLS 8.4.8)
+            List<MethodNode> ifaceMethods = 
anInterface.getMethods().stream().filter(m -> 
!m.isStatic()).collect(java.util.stream.Collectors.toList());
{code}

With the patched {{Verifier}} on the classpath before groovy-6.0.0.jar, all the 
classes above compile: against a trait in the same compilation, against a 
precompiled trait and against a Java interface. The class extending a 
superclass with the static method still fails, as before. At run time, {{new 
C(count: 1).count}} is 1, {{T.getCount()}} is 0, and {{C.count}} throws 
{{MissingPropertyException}}, as on 5.1.3, where the class's own getter also 
hides the trait static. I haven't run Groovy's test suite with the change.

Found while moving Apache Grails to Groovy 6. GORM's {{GormEntity}} trait, 
which every domain class implements, has {{static Long getCount()}} and 
{{static List<D> getAll()}}. So a domain class with a property {{Integer 
count}} or {{String all}} compiles with Groovy 5 and fails with 6.0.0. 
{{@Virtual}} on those statics would avoid the error, but then 
{{GormEntity.getCount()}} no longer works, and Java compiled together with the 
domain classes can't call {{Book.getCount()}}, because the joint-compilation 
stub leaves a {{@Virtual}} static out.



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

Reply via email to