You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/12/24 00:12:27 UTC

[groovy] branch GROOVY_3_0_X updated (98964b8 -> 7f43c0a)

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

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


    from 98964b8  Tweak test
     new 6984ec4  add missing pops and other minor edits
     new 30f0b25  minor edits
     new 900e010  GROOVY-9351: Make `NumberRange` serializable to align with `IntRange` (#1128)
     new 7f43c0a  GROOVY-9327: handle STC for AIC in non-STC class but STC method

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/main/java/groovy/lang/NumberRange.java         |   4 +-
 .../transform/sc/StaticCompilationVisitor.java     |  32 +++---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 115 ++++++++++++---------
 .../bugs/{Groovy8686.groovy => Groovy9327.groovy}  |  34 +++---
 .../stc/AnonymousInnerClassSTCTest.groovy          |   5 +-
 5 files changed, 108 insertions(+), 82 deletions(-)
 copy src/test/groovy/bugs/{Groovy8686.groovy => Groovy9327.groovy} (61%)


[groovy] 04/04: GROOVY-9327: handle STC for AIC in non-STC class but STC method

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 7f43c0a36cf3aed446c1f4644bafa7d16b28b33c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Dec 23 13:35:55 2019 -0600

    GROOVY-9327: handle STC for AIC in non-STC class but STC method
    
    (cherry picked from commit 44893abc65656fa45f468e970da2964fd181a0ef)
---
 .../transform/sc/StaticCompilationVisitor.java     |  6 +++
 .../transform/stc/StaticTypeCheckingVisitor.java   | 16 +++++-
 src/test/groovy/bugs/Groovy9327.groovy             | 60 ++++++++++++++++++++++
 .../stc/AnonymousInnerClassSTCTest.groovy          |  5 +-
 4 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java b/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
index 7a08905..e4f28ec 100644
--- a/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
@@ -410,6 +410,12 @@ public class StaticCompilationVisitor extends StaticTypeCheckingVisitor {
     public void visitConstructorCallExpression(final ConstructorCallExpression call) {
         super.visitConstructorCallExpression(call);
 
+        if (call.isUsingAnonymousInnerClass() && call.getType().getNodeMetaData(StaticTypeCheckingVisitor.class) != null) {
+            ClassNode anonType = call.getType();
+            anonType.putNodeMetaData(STATIC_COMPILE_NODE, anonType.getEnclosingMethod().getNodeMetaData(STATIC_COMPILE_NODE));
+            anonType.putNodeMetaData(WriterControllerFactory.class, anonType.getOuterClass().getNodeMetaData(WriterControllerFactory.class));
+        }
+
         MethodNode target = call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
         if (target == null && call.getLineNumber() > 0) {
             addError("Target constructor for constructor call expression hasn't been set", call);
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 2bacc9d..1b06c8c 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -435,7 +435,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     }
 
     protected boolean shouldSkipClassNode(final ClassNode node) {
-        return isSkipMode(node);
+        return Boolean.TRUE.equals(node.getNodeMetaData(StaticTypeCheckingVisitor.class)) || isSkipMode(node);
     }
 
     public boolean isSkipMode(final AnnotatedNode node) {
@@ -2212,6 +2212,20 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             if (node != null) storeTargetMethod(call, node);
         }
 
+        // GROOVY-9327: check for AIC in STC method with non-STC enclosing class
+        if (call.isUsingAnonymousInnerClass()) {
+            Set<MethodNode> methods = typeCheckingContext.methodsToBeVisited;
+            if (!methods.isEmpty()) { // indicates specific methods have STC
+                typeCheckingContext.methodsToBeVisited = Collections.emptySet();
+
+                ClassNode anonType = call.getType();
+                visitClass(anonType); // visit anon. inner class inline with method
+                anonType.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
+
+                typeCheckingContext.methodsToBeVisited = methods;
+            }
+        }
+
         extension.afterMethodCall(call);
     }
 
diff --git a/src/test/groovy/bugs/Groovy9327.groovy b/src/test/groovy/bugs/Groovy9327.groovy
new file mode 100644
index 0000000..7112f3f
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9327.groovy
@@ -0,0 +1,60 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.shouldFail
+
+@CompileStatic
+final class Groovy9327 {
+
+    @Test
+    void testCheckedAIC() {
+        def err = shouldFail '''
+        @groovy.transform.TypeChecked
+        void test() {
+            def runner = new Runnable() {
+                @Override
+                void run() {
+                    unknownReference
+                }
+            }
+        }
+        '''
+        assert err.message.contains('The variable [unknownReference] is undeclared.')
+    }
+
+    @Test
+    void testCompiledAIC() {
+        def err = shouldFail '''
+        @groovy.transform.CompileStatic
+        void test() {
+            def runner = new Runnable() {
+                @Override
+                void run() {
+                    unknownReference
+                }
+            }
+        }
+        '''
+        assert err.message.contains('The variable [unknownReference] is undeclared.')
+    }
+}
diff --git a/src/test/groovy/transform/stc/AnonymousInnerClassSTCTest.groovy b/src/test/groovy/transform/stc/AnonymousInnerClassSTCTest.groovy
index 28a1703..5ad7019 100644
--- a/src/test/groovy/transform/stc/AnonymousInnerClassSTCTest.groovy
+++ b/src/test/groovy/transform/stc/AnonymousInnerClassSTCTest.groovy
@@ -122,9 +122,10 @@ class AnonymousInnerClassSTCTest extends StaticTypeCheckingTestCase {
                   }
                 }
                 s.size()
-            }'''
+            }
+        '''
     }
-    
+
     void testAICInAICInStaticMethod() {
         assertScript '''
             class A {


[groovy] 01/04: add missing pops and other minor edits

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 6984ec4bfd861a36404e77b0e8fd161f03271bf7
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Dec 23 12:02:05 2019 -0600

    add missing pops and other minor edits
    
    (cherry picked from commit 6ee13a0aeca344c092f8028cba7c5b0d125c8774)
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 88 +++++++++++-----------
 1 file changed, 45 insertions(+), 43 deletions(-)

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 659597a..30c389b 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -394,55 +394,50 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     @Override
     public void visitClass(final ClassNode node) {
         if (shouldSkipClassNode(node)) return;
-        if (extension.beforeVisitClass(node)) {
-            extension.afterVisitClass(node);
-            return;
-        }
-        Object type = node.getNodeMetaData(INFERRED_TYPE);
-        if (type != null) {
-            // transformation has already been run on this class node
-            // so we'll use a silent collector in order not to duplicate errors
-            typeCheckingContext.pushErrorCollector();
-        }
-        typeCheckingContext.pushEnclosingClassNode(node);
-        Set<MethodNode> oldVisitedMethod = typeCheckingContext.alreadyVisitedMethods;
-        typeCheckingContext.alreadyVisitedMethods = new LinkedHashSet<>();
-        super.visitClass(node);
-        Iterator<InnerClassNode> innerClasses = node.getInnerClasses();
-        while (innerClasses.hasNext()) {
-            InnerClassNode innerClassNode = innerClasses.next();
-            visitClass(innerClassNode);
-        }
-        typeCheckingContext.alreadyVisitedMethods = oldVisitedMethod;
-        node.putNodeMetaData(INFERRED_TYPE, node);
-        // mark all methods as visited. We can't do this in visitMethod because the type checker
-        // works in a two pass sequence and we don't want to skip the second pass
-        for (MethodNode methodNode : node.getMethods()) {
-            methodNode.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
-        }
-        for (ConstructorNode constructorNode : node.getDeclaredConstructors()) {
-            constructorNode.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
+        if (!extension.beforeVisitClass(node)) {
+            Object type = node.getNodeMetaData(INFERRED_TYPE);
+            if (type != null) {
+                // transformation has already been run on this class node
+                // so use a silent collector in order not to duplicate errors
+                typeCheckingContext.pushErrorCollector();
+            }
+            typeCheckingContext.pushEnclosingClassNode(node);
+            Set<MethodNode> oldSet = typeCheckingContext.alreadyVisitedMethods;
+            typeCheckingContext.alreadyVisitedMethods = new LinkedHashSet<>();
+
+            super.visitClass(node);
+            node.getInnerClasses().forEachRemaining(this::visitClass);
+
+            typeCheckingContext.alreadyVisitedMethods = oldSet;
+            typeCheckingContext.popEnclosingClassNode();
+            if (type != null) {
+                typeCheckingContext.popErrorCollector();
+            }
+
+            node.putNodeMetaData(INFERRED_TYPE, node);
+            // mark all methods as visited. We can't do this in visitMethod because the type checker
+            // works in a two pass sequence and we don't want to skip the second pass
+            node.getMethods().forEach(n -> n.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE));
+            node.getDeclaredConstructors().forEach(n -> n.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE));
         }
         extension.afterVisitClass(node);
     }
 
-    protected boolean shouldSkipClassNode(final ClassNode node) {
-        return isSkipMode(node);
-    }
-
     /**
-     * Returns the list of type checking annotations class nodes. Subclasses may override this method
-     * in order to provide additional classes which must be looked up when checking if a method or
-     * a class node should be skipped.
+     * Returns array of type checking annotations. Subclasses may override this
+     * method in order to provide additional types which must be looked up when
+     * checking if a method or a class node should be skipped.
      * <p>
      * The default implementation returns {@link TypeChecked}.
-     *
-     * @return array of class nodes
      */
     protected ClassNode[] getTypeCheckingAnnotations() {
         return TYPECHECKING_ANNOTATIONS;
     }
 
+    protected boolean shouldSkipClassNode(final ClassNode node) {
+        return isSkipMode(node);
+    }
+
     public boolean isSkipMode(final AnnotatedNode node) {
         if (node == null) return false;
         for (ClassNode tca : getTypeCheckingAnnotations()) {
@@ -465,19 +460,25 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         if (node instanceof MethodNode) {
             return isSkipMode(node.getDeclaringClass());
         }
-        if (isSkippedInnerClass(node)) return true;
-        return false;
+        return isSkippedInnerClass(node);
     }
 
     /**
-     * Test if a node is an inner class node, and if it is, then checks if the enclosing method is skipped.
+     * Tests if a node is an inner class node, and if it is, then checks if the enclosing method is skipped.
      *
      * @return true if the inner class node should be skipped
      */
     protected boolean isSkippedInnerClass(final AnnotatedNode node) {
-        if (!(node instanceof InnerClassNode)) return false;
-        MethodNode enclosingMethod = ((InnerClassNode) node).getEnclosingMethod();
-        return enclosingMethod != null && isSkipMode(enclosingMethod);
+        if (node instanceof ClassNode) {
+            ClassNode type = (ClassNode) node;
+            if (type.getOuterClass() != null) {
+                MethodNode enclosingMethod = type.getEnclosingMethod();
+                if (enclosingMethod != null && isSkipMode(enclosingMethod)) {
+                    return true;
+                }
+            }
+        }
+        return false;
     }
 
     @Override
@@ -2210,6 +2211,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             }
             if (node != null) storeTargetMethod(call, node);
         }
+
         extension.afterMethodCall(call);
     }
 


[groovy] 03/04: GROOVY-9351: Make `NumberRange` serializable to align with `IntRange` (#1128)

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 900e01002e3af7612b4bb2983ce6090b5f3cabd6
Author: Daniel.Sun <su...@apache.org>
AuthorDate: Tue Dec 24 08:03:41 2019 +0800

    GROOVY-9351: Make `NumberRange` serializable to align with `IntRange` (#1128)
    
    (cherry picked from commit a9838e239a3682dd58dc03fa3ea115b1c31df6ca)
---
 src/main/java/groovy/lang/NumberRange.java | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/main/java/groovy/lang/NumberRange.java b/src/main/java/groovy/lang/NumberRange.java
index 92d6552..3e1c6e3 100644
--- a/src/main/java/groovy/lang/NumberRange.java
+++ b/src/main/java/groovy/lang/NumberRange.java
@@ -21,6 +21,7 @@ package groovy.lang;
 import org.codehaus.groovy.runtime.InvokerHelper;
 import org.codehaus.groovy.runtime.IteratorClosureAdapter;
 
+import java.io.Serializable;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.AbstractList;
@@ -48,8 +49,9 @@ import static org.codehaus.groovy.runtime.dgmimpl.NumberNumberPlus.plus;
  *
  * @since 2.5.0
  */
-public class NumberRange extends AbstractList<Comparable> implements Range<Comparable> {
+public class NumberRange extends AbstractList<Comparable> implements Range<Comparable>, Serializable {
 
+    private static final long serialVersionUID = 5107424833653948484L;
     /**
      * The first value in the range.
      */


[groovy] 02/04: minor edits

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 30f0b25cd171f268b4139a9b09827dfee7059522
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Dec 23 13:32:22 2019 -0600

    minor edits
    
    (cherry picked from commit 4eb3529256ab510f5d741fd50eba9412a3b73cc7)
---
 .../transform/sc/StaticCompilationVisitor.java     | 26 +++++++++-------------
 .../transform/stc/StaticTypeCheckingVisitor.java   | 13 +++++------
 2 files changed, 16 insertions(+), 23 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java b/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
index 3dec6b8..7a08905 100644
--- a/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
@@ -175,15 +175,14 @@ public class StaticCompilationVisitor extends StaticTypeCheckingVisitor {
 
         ClassNode previousClassNode = classNode; classNode = node;
 
-        for (Iterator<InnerClassNode> innerClasses = classNode.getInnerClasses(); innerClasses.hasNext(); ) {
-            InnerClassNode innerClassNode = innerClasses.next();
+        classNode.getInnerClasses().forEachRemaining(innerClassNode -> {
             boolean innerStaticCompile = !(skip || isSkippedInnerClass(innerClassNode));
             innerClassNode.putNodeMetaData(STATIC_COMPILE_NODE, Boolean.valueOf(innerStaticCompile));
             innerClassNode.putNodeMetaData(WriterControllerFactory.class, node.getNodeMetaData(WriterControllerFactory.class));
             if (innerStaticCompile && !anyMethodSkip(innerClassNode)) {
                 innerClassNode.putNodeMetaData(MopWriter.Factory.class, StaticCompilationMopWriter.FACTORY);
             }
-        }
+        });
         super.visitClass(node);
         addPrivateFieldAndMethodAccessors(node);
         if (isStaticallyCompiled(node)) {
@@ -414,19 +413,16 @@ public class StaticCompilationVisitor extends StaticTypeCheckingVisitor {
         MethodNode target = call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
         if (target == null && call.getLineNumber() > 0) {
             addError("Target constructor for constructor call expression hasn't been set", call);
-        } else {
-            if (target==null) {
-                // try to find a target
-                ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(call.getArguments());
-                List<Expression> expressions = argumentListExpression.getExpressions();
-                ClassNode[] args = new ClassNode[expressions.size()];
-                for (int i = 0, n = args.length; i < n; i += 1) {
-                    args[i] = typeChooser.resolveType(expressions.get(i), classNode);
-                }
-                MethodNode constructor = findMethodOrFail(call, call.isSuperCall() ? classNode.getSuperClass() : classNode, "<init>", args);
-                call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, constructor);
-                target = constructor;
+        } else if (target == null) {
+            // try to find a target
+            ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(call.getArguments());
+            List<Expression> expressions = argumentListExpression.getExpressions();
+            ClassNode[] args = new ClassNode[expressions.size()];
+            for (int i = 0, n = args.length; i < n; i += 1) {
+                args[i] = typeChooser.resolveType(expressions.get(i), classNode);
             }
+            target = findMethodOrFail(call, call.isSuperCall() ? classNode.getSuperClass() : classNode, "<init>", args);
+            call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
         }
         if (target != null) {
             memorizeInitialExpressions(target);
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 30c389b..2bacc9d 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2215,14 +2215,11 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         extension.afterMethodCall(call);
     }
 
-    private boolean looksLikeNamedArgConstructor(final ClassNode receiver, final ClassNode[] args) {
-        return (args.length == 1 || args.length == 2 && isInnerConstructor(receiver, args[0]))
-                && implementsInterfaceOrIsSubclassOf(args[args.length - 1], MAP_TYPE);
-    }
-
-    private boolean isInnerConstructor(final ClassNode receiver, final ClassNode parent) {
-        return receiver.isRedirectNode() && receiver.redirect() instanceof InnerClassNode &&
-                receiver.redirect().getOuterClass().equals(parent);
+    private boolean looksLikeNamedArgConstructor(final ClassNode receiver, final ClassNode[] argumentTypes) {
+        if (argumentTypes.length == 1 || argumentTypes.length == 2 && argumentTypes[0].equals(receiver.getOuterClass())) {
+            return argumentTypes[argumentTypes.length - 1].implementsInterface(MAP_TYPE);
+        }
+        return false;
     }
 
     protected MethodNode typeCheckMapConstructor(final ConstructorCallExpression call, final ClassNode receiver, final Expression arguments) {