[
https://issues.apache.org/jira/browse/GROOVY-12534?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18135927#comment-18135927
]
ASF GitHub Bot commented on GROOVY-12534:
-----------------------------------------
blackdrag commented on code in PR #3058:
URL: https://github.com/apache/groovy/pull/3058#discussion_r4433079437
##########
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:
I think we should test something like this
```Groovy
class Person {
String name
}
class People extends ArrayList<Person> {
String name = 'the collection'
}
@groovy.transform.CompileStatic
def test() {
def people = new People()
people.add(new Person(name: 'Alice'))
people.add(new Person(name: 'Bob'))
assert people.name == 'the collection'
assert people*.name == ['Alice', 'Bob']
}
```
and the same with extension methods instead of fields.
> 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)