Scott Murphy Heiberg created GROOVY-12534:
---------------------------------------------

             Summary: SC: a property defined by an extension method of List is 
read from each element of the list
                 Key: GROOVY-12534
                 URL: https://issues.apache.org/jira/browse/GROOVY-12534
             Project: Groovy
          Issue Type: Bug
          Components: Static compilation
    Affects Versions: 6.0.0
            Reporter: Scott Murphy Heiberg


Under {{@CompileStatic}}, a property whose getter is an extension method of 
{{List}}, or of a class that implements {{List}}, is compiled as a property of 
the list's elements: the getter is called once for each element, with the 
element cast to the extension's receiver type. With this extension module:

{code:java}
package demo;
import java.util.List;
public class ListExtension {
    public static String getJoined(List<String> self) { return String.join(",", 
self); }
}
{code}

{code:groovy}
@groovy.transform.CompileStatic
class C {
    def test(List<String> words) { words.joined }
}
new C().test(['x', 'y']) // GroovyCastException: Cannot cast object 'x' with 
class 'java.lang.String' to class 'java.util.List'
{code}

{{words.getJoined()}} returns 'x,y' in every mode, and dynamic Groovy and 
{{@TypeChecked}} return 'x,y' for {{words.joined}} too. On an empty list, 
{{words.joined}} evaluates to {{[]}} instead of an empty string.

{{StaticCompilationVisitor.existsProperty}} marks the property expression 
spread-safe when the getter's declaring class implements {{List}} but does not 
declare the getter, which is meant for a property of the element type. The 
declaring class of an extension method is its receiver type, which never 
declares it. Extension getters declared for {{Collection}} or {{Iterable}}, 
such as {{getIndices}} and {{isSorted}}, are not affected. 
{{DefaultGroovyMethodsSTCTest.testPropertySemantics2}}, which 
{{StaticCompileDGMTest}} also runs, keeps the property forms of 
{{getSequence(List)}} and {{getString(List)}} from {{TestStringExtension}} 
commented out. Seen in 4.0.33, 5.0.8, 6.0.0 and master.

Expected: the property calls the extension getter on the list, as in dynamic 
Groovy and {{@TypeChecked}}.



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

Reply via email to