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

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

daniellansun commented on code in PR #3058:
URL: https://github.com/apache/groovy/pull/3058#discussion_r4430099011


##########
src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java:
##########
@@ -376,8 +378,12 @@ public void visitField(final FieldNode node) {
                 ClassNode declaringClass = node.getDeclaringClass();
                 if (declaringClass != null) {
                     if 
(StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(declaringClass, 
ClassHelper.LIST_TYPE)) {
-                        boolean spread = 
declaringClass.getDeclaredField(node.getName()) != node;
-                        pexp.setSpreadSafe(spread);
+                        boolean declaredField = 
declaringClass.getDeclaredField(node.getName()) == node;
+                        // GROOVY-12534: the virtual field for an extension 
setter is not an element
+                        // property. A declared field, or a real one-argument 
setter, is unchanged.
+                        if (declaredField || 
declaresUnarySetter(declaringClass, node.getName())) {
+                            pexp.setSpreadSafe(!declaredField);

Review Comment:
   When `declaredField` is true, the List (or subclass) actually owns that 
field, so the property targets the list receiver itself, not each element. 
`setSpreadSafe(false)` keeps that non-spread behavior. That matches the 
previous logic for a real declared field (`getDeclaredField(...) == node` 
already meant spread was false). GROOVY-12534 only changes the extension-method 
virtual-field path: those used to force `setSpreadSafe(true)`, which made 
`list.someExtProp` compile as element-wise access. Now we leave spread alone 
unless it is a declared field or a real unary setter.





> 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
>            Assignee: Daniel Sun
>            Priority: Major
>
> 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