[groovy] branch GROOVY_3_0_X updated: Revert "GROOVY-9821: STC: check "T" from "? extends T" for spread property (port to 3_0_X)"

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[groovy] branch GROOVY_3_0_X updated: Revert "GROOVY-9821: STC: check "T" from "? extends T" for spread property (port to 3_0_X)"

paulk
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 9cdcf78  Revert "GROOVY-9821: STC: check "T" from "? extends T" for spread property (port to 3_0_X)"
9cdcf78 is described below

commit 9cdcf78e3ee6c3f686b86d6bbbd5881ff040c268
Author: Paul King <[hidden email]>
AuthorDate: Fri Nov 20 22:09:55 2020 +1000

    Revert "GROOVY-9821: STC: check "T" from "? extends T" for spread property (port to 3_0_X)"
   
    This reverts commit 480c4b7421abfd43993fc6d1498c08f332189b05.
---
 .../transform/stc/StaticTypeCheckingSupport.java   |  2 +-
 .../transform/stc/StaticTypeCheckingVisitor.java   | 43 +++++++++++-----------
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 32 ----------------
 3 files changed, 23 insertions(+), 54 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index f890e36..a6dab3f 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1903,7 +1903,7 @@ public abstract class StaticTypeCheckingSupport {
         return newType;
     }
 
-    static ClassNode getCombinedBoundType(final GenericsType genericsType) {
+    private static ClassNode getCombinedBoundType(final GenericsType genericsType) {
         // TODO: this method should really return some kind of meta ClassNode
         // representing the combination of all bounds. The code here, just picks
         // something out to be able to proceed and is not actually correct
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 0b444fa..923346d 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -188,7 +188,6 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.getSetterName;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.isOrImplements;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.localVarX;
-import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.thisPropX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
 import static org.codehaus.groovy.ast.tools.GenericsUtils.toGenericTypesString;
@@ -246,7 +245,6 @@ import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDG
 import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findSetters;
 import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findTargetVariable;
 import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.fullyResolveType;
-import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.getCombinedBoundType;
 import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.getCorrectedClassNode;
 import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.getGenericsWithoutArray;
 import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.getOperationName;
@@ -899,10 +897,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     }
 
     private void validateResourceInARM(final BinaryExpression expression, final ClassNode lType) {
-        if (expression instanceof DeclarationExpression
-                && TryCatchStatement.isResource(expression)
-                && !isOrImplements(lType, AUTOCLOSEABLE_TYPE)) {
-            addError("Resource[" + lType.getName() + "] in ARM should be of type AutoCloseable", expression);
+        if (expression instanceof DeclarationExpression) {
+            if (TryCatchStatement.isResource(expression)) {
+                if (!lType.implementsInterface(AUTOCLOSEABLE_TYPE)) {
+                    addError("Resource[" + lType.getName() + "] in ARM should be of type AutoCloseable", expression);
+                }
+            }
         }
     }
 
@@ -1661,20 +1661,21 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     }
 
     private ClassNode getTypeForSpreadExpression(final ClassNode testClass, final ClassNode objectExpressionType, final PropertyExpression pexp) {
-        if (pexp.isSpreadSafe()) {
-            MethodCallExpression mce = callX(varX("_", testClass), "iterator");
-            mce.setImplicitThis(false);
-            mce.visit(this);
-            ClassNode iteratorType = getType(mce);
-            if (isOrImplements(iteratorType, Iterator_TYPE)) {
-                GenericsType[] gts = iteratorType.getGenericsTypes();
-                ClassNode itemType = (gts != null && gts.length == 1 ? getCombinedBoundType(gts[0]) : OBJECT_TYPE);
-
-                AtomicReference<ClassNode> propertyType = new AtomicReference<>();
-                if (existsProperty(propX(varX("{}", itemType), pexp.getProperty()), true, new PropertyLookupVisitor(propertyType))) {
-                    return extension.buildListType(propertyType.get());
-                }
-            }
+        if (!pexp.isSpreadSafe()) return null;
+        MethodCallExpression mce = callX(varX("_", testClass), "iterator", ArgumentListExpression.EMPTY_ARGUMENTS);
+        mce.setImplicitThis(false);
+        mce.visit(this);
+        ClassNode callType = getType(mce);
+        if (!implementsInterfaceOrIsSubclassOf(callType, Iterator_TYPE)) return null;
+        GenericsType[] types = callType.getGenericsTypes();
+        ClassNode contentType = OBJECT_TYPE;
+        if (types != null && types.length == 1) contentType = types[0].getType();
+        PropertyExpression subExp = new PropertyExpression(varX("{}", contentType), pexp.getPropertyAsString());
+        AtomicReference<ClassNode> result = new AtomicReference<>();
+        if (existsProperty(subExp, true, new PropertyLookupVisitor(result))) {
+            ClassNode intf = LIST_TYPE.getPlainNodeReference();
+            intf.setGenericsTypes(new GenericsType[]{new GenericsType(getWrapper(result.get()))});
+            return intf;
         }
         return null;
     }
@@ -2235,7 +2236,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
 
     private boolean looksLikeNamedArgConstructor(final ClassNode receiver, final ClassNode[] argumentTypes) {
         if (argumentTypes.length == 1 || argumentTypes.length == 2 && argumentTypes[0].equals(receiver.getOuterClass())) {
-            return isOrImplements(argumentTypes[argumentTypes.length - 1], MAP_TYPE);
+            return argumentTypes[argumentTypes.length - 1].implementsInterface(MAP_TYPE);
         }
         return false;
     }
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 6dbbb44..49b5f60 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -2161,38 +2161,6 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
             }
             null
         '''
-
-        // GROOVY-9821
-        config.with {
-            targetDirectory = File.createTempDir()
-            jointCompilationOptions = [memStub: true]
-        }
-        File parentDir = File.createTempDir()
-        try {
-            def a = new File(parentDir, 'Types.java')
-            a.write '''
-                interface A {
-                    java.util.Collection<? extends B> getBees();
-                }
-                interface B {
-                    Object getC();
-                }
-            '''
-            def b = new File(parentDir, 'Script.groovy')
-            b.write '''
-                def test(A a) {
-                    a.bees*.c
-                }
-            '''
-
-            def loader = new GroovyClassLoader(this.class.classLoader)
-            def cu = new JavaAwareCompilationUnit(config, loader)
-            cu.addSources(a, b)
-            cu.compile()
-        } finally {
-            parentDir.deleteDir()
-            config.targetDirectory.deleteDir()
-        }
     }
 
     // GROOVY-7804