You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/01/17 23:55:52 UTC

[groovy] branch master updated: minor edits

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 2922545  minor edits
2922545 is described below

commit 292254523545a1ab04e85f31ff4f82de417b0ac7
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jan 17 17:24:47 2021 -0600

    minor edits
---
 ...StaticTypesMethodReferenceExpressionWriter.java |  2 +-
 .../transform/stc/StaticTypeCheckingSupport.java   |  8 ++--
 .../transform/stc/StaticTypeCheckingVisitor.java   | 54 +++++++++-------------
 src/test/groovy/transform/stc/BugsSTCTest.groovy   | 33 +++++++++----
 .../classgen/asm/sc/BugsStaticCompileTest.groovy   | 12 ++---
 5 files changed, 56 insertions(+), 53 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
index 6fead88..a1151d5 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
@@ -166,7 +166,7 @@ public class StaticTypesMethodReferenceExpressionWriter extends MethodReferenceE
                     + Arrays.stream(parametersWithExactType)
                             .map(e -> e.getType().getText())
                             .collect(Collectors.joining(","))
-                    + ")] in the type[" + typeOrTargetRefType.getName() + "]", methodReferenceExpression);
+                    + ")] in the type[" + typeOrTargetRefType.getText() + "]", methodReferenceExpression);
         } else if (parametersWithExactType.length > 0 && isTypeReferingInstanceMethod(typeOrTargetRef, methodRefMethod)) {
             ClassNode firstParameterType = parametersWithExactType[0].getType();
             if (!isAssignableTo(firstParameterType, typeOrTargetRefType)) {
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 2b404d8..854c306 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1369,16 +1369,16 @@ public abstract class StaticTypeCheckingSupport {
         if (isUsingUncheckedGenerics(receiver)) {
             return true;
         }
-        if (CLASS_Type.equals(receiver)
+        boolean isExtensionMethod = candidateMethod instanceof ExtensionMethodNode;
+        if (!isExtensionMethod
                 && receiver.isUsingGenerics()
-                && !candidateMethod.getDeclaringClass().equals(receiver)
-                && !(candidateMethod instanceof ExtensionMethodNode)) {
+                && receiver.equals(CLASS_Type)
+                && !candidateMethod.getDeclaringClass().equals(receiver)) {
             return typeCheckMethodsWithGenerics(receiver.getGenericsTypes()[0].getType(), argumentTypes, candidateMethod);
         }
         // both candidate method and receiver have generic information so a check is possible
         GenericsType[] genericsTypes = candidateMethod.getGenericsTypes();
         boolean methodUsesGenerics = (genericsTypes != null && genericsTypes.length > 0);
-        boolean isExtensionMethod = candidateMethod instanceof ExtensionMethodNode;
         if (isExtensionMethod && methodUsesGenerics) {
             ClassNode[] dgmArgs = new ClassNode[argumentTypes.length + 1];
             dgmArgs[0] = receiver;
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 ddaf453..7d2f5af 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2676,7 +2676,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         }
         ArgumentListExpression newArgs = args(expressions);
 
-        for (int i = 0, expressionsSize = expressions.size(); i < expressionsSize; i++) {
+        int nExpressions = expressions.size();
+        for (int i = 0; i < nExpressions; i += 1) {
             Expression expression = expressions.get(i);
             if (visitClosures && expression instanceof ClosureExpression
                     || !visitClosures && !(expression instanceof ClosureExpression)) {
@@ -2695,7 +2696,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                 expression.removeNodeMetaData(DELEGATION_METADATA);
             }
         }
-        if (expressions.size() > 0 && expressions.get(0) instanceof MapExpression && params.length > 0) {
+        if (nExpressions > 0 && expressions.get(0) instanceof MapExpression && params.length > 0) {
             checkNamedParamsAnnotation(params[0], (MapExpression) expressions.get(0));
         }
     }
@@ -3962,19 +3963,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         return instanceOfExpression;
     }
 
-    private boolean notReturningBlock(final Statement block) {
-        if (!(block instanceof BlockStatement)) {
-            return true;
-        }
-        BlockStatement bs = (BlockStatement) block;
-        if (bs.getStatements().size() == 0) {
-            return true;
-        }
-        Statement last = DefaultGroovyMethods.last(bs.getStatements());
-        if (!(last instanceof ReturnStatement)) {
-            return true;
-        }
-        return false;
+    private static boolean notReturningBlock(final Statement statement) {
+        return statement.isEmpty() || !(statement instanceof BlockStatement)
+            || !(DefaultGroovyMethods.last(((BlockStatement) statement).getStatements()) instanceof ReturnStatement);
     }
 
     @Override
@@ -4193,6 +4184,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     }
 
     protected void storeType(final Expression exp, ClassNode cn) {
+        if (cn == UNKNOWN_PARAMETER_TYPE) {
+            // this can happen for example when "null" is used in an assignment or a method parameter.
+            // In that case, instead of storing the virtual type, we must "reset" type information
+            // by determining the declaration type of the expression
+            cn = getOriginalDeclarationType(exp);
+        }
         if (cn != null && isPrimitiveType(cn)) {
             if (exp instanceof VariableExpression && ((VariableExpression) exp).isClosureSharedVariable()) {
                 cn = getWrapper(cn);
@@ -4202,13 +4199,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                 cn = getWrapper(cn);
             }
         }
-        if (cn == UNKNOWN_PARAMETER_TYPE) {
-            // this can happen for example when "null" is used in an assignment or a method parameter.
-            // In that case, instead of storing the virtual type, we must "reset" type information
-            // by determining the declaration type of the expression
-            storeType(exp, getOriginalDeclarationType(exp));
-            return;
-        }
+
         ClassNode oldValue = (ClassNode) exp.putNodeMetaData(INFERRED_TYPE, cn);
         if (oldValue != null) {
             // this may happen when a variable declaration type is wider than the subsequent assignment values
@@ -4225,6 +4216,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                 exp.putNodeMetaData(DECLARATION_INFERRED_TYPE, cn == null ? null : lowestUpperBound(oldValue, cn));
             }
         }
+
         if (exp instanceof VariableExpression) {
             VariableExpression var = (VariableExpression) exp;
             Variable accessedVariable = var.getAccessedVariable();
@@ -4413,7 +4405,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
 
         // extract the generics from the return type
         Map<GenericsTypeName, GenericsType> connections = new HashMap<>();
-        extractGenericsConnections(connections, getWrapper(getInferredReturnType(closureExpression)), abstractMethod.getReturnType());
+        extractGenericsConnections(connections, wrapTypeIfNecessary(getInferredReturnType(closureExpression)), abstractMethod.getReturnType());
 
         // next we get the block parameter types and set the generics
         // information just like before
@@ -4563,10 +4555,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     }
 
     /**
-     * This method returns the list of methods named against the supplied parameter that
-     * are defined on the specified receiver, but it will also add "non existing" methods
-     * that will be generated afterwards by the compiler, for example if a method is using
-     * default values and that the specified class node isn't compiled yet.
+     * Returns methods defined for the specified receiver and adds "non-existing"
+     * methods that will be generated afterwards by the compiler; for example if
+     * a method is using default values and the class node isn't compiled yet.
      *
      * @param receiver the receiver where to find methods
      * @param name     the name of the methods to return
@@ -4658,11 +4649,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
 
                 if ("call".equals(name) && isFunctionalInterface(receiver)) {
                     MethodNode sam = findSAM(receiver);
-                    MethodNode callMethodNode = new MethodNode("call", sam.getModifiers(), sam.getReturnType(), sam.getParameters(), sam.getExceptions(), sam.getCode());
-                    callMethodNode.setDeclaringClass(sam.getDeclaringClass());
-                    callMethodNode.setSourcePosition(sam);
-
-                    methods.addAll(Collections.singletonList(callMethodNode));
+                    MethodNode callMethod = new MethodNode("call", sam.getModifiers(), sam.getReturnType(), sam.getParameters(), sam.getExceptions(), sam.getCode());
+                    callMethod.setDeclaringClass(sam.getDeclaringClass());
+                    callMethod.setSourcePosition(sam);
+                    methods.add(callMethod);
                 }
             }
             // TODO: investigate the trait exclusion a bit further, needed otherwise
diff --git a/src/test/groovy/transform/stc/BugsSTCTest.groovy b/src/test/groovy/transform/stc/BugsSTCTest.groovy
index fda9633..184fea8 100644
--- a/src/test/groovy/transform/stc/BugsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/BugsSTCTest.groovy
@@ -22,6 +22,7 @@ package groovy.transform.stc
  * Unit tests for static type checking : bug fixes.
  */
 class BugsSTCTest extends StaticTypeCheckingTestCase {
+
     // GROOVY-5456
     void testShouldNotAllowDivOnUntypedVariable() {
         shouldFailWithMessages '''
@@ -352,7 +353,7 @@ class BugsSTCTest extends StaticTypeCheckingTestCase {
         execute()'''
     }
 
-    // GROOVY-5874-part-1
+    // GROOVY-5874 (pt.1)
     void testClosureSharedVariableInBinExp() {
         shouldFailWithMessages '''
             def sum = 0
@@ -610,7 +611,7 @@ Printer
     }
 
     // GROOVY-6970
-    void testShouldBeAbleToChooseBetweenTwoEquivalentInterfaceMethods() {
+    void testShouldBeAbleToChooseBetweenTwoEquivalentInterfaceMethods1() {
         assertScript '''
             interface A { void m() }
             interface B { void m() }
@@ -628,7 +629,8 @@ Printer
             new D(new CImpl())
         '''
     }
-    void testShouldBeAbleToChooseBetweenTwoEquivalentInterfaceMethodsVariant() {
+
+    void testShouldBeAbleToChooseBetweenTwoEquivalentInterfaceMethods2() {
         assertScript '''
             interface A { void m() }
             interface B { void m() }
@@ -645,7 +647,7 @@ Printer
         '''
     }
 
-    void testShouldBeAbleToChooseBetweenTwoEquivalentInterfaceMethodsVariant2() {
+    void testShouldBeAbleToChooseBetweenTwoEquivalentInterfaceMethods3() {
         assertScript '''
             interface A { void m() }
             interface B { void m() }
@@ -663,7 +665,8 @@ Printer
         '''
     }
 
-    void testAmbiguousMethodResolutionGroovy6849() {
+    // GROOVY-6849
+    void testAmbiguousMethodResolution() {
         assertScript '''
             interface ObservableList<E> extends List<E> {
                 public boolean addAll(E... elements)
@@ -674,13 +677,27 @@ Printer
         '''
     }
 
-    void testAmbiguousMethodResolutionGroovy7710NoArgsOverloaded() {
+    // GROOVY-7160
+    void _FIXME_testAmbiguousMethodResolutionObjectVsEnums() {
+        assertScript '''
+            import static java.nio.file.AccessMode.*
+            def test() {
+                // more than 5 to match of(E first, E[] rest) variant
+                EnumSet.of(READ, WRITE, EXECUTE, READ, WRITE, EXECUTE)
+            }
+            assert test() == [READ, WRITE, EXECUTE].toSet()
+        '''
+    }
+
+    // GROOVY-7710
+    void testAmbiguousMethodResolutionNoArgsOverload() {
         shouldFailWithMessages '''
             Arrays.sort()
         ''', 'Reference to method is ambiguous. Cannot choose between '
     }
 
-    void testAmbiguousMethodResolutionGroovy7711NoArgsCovariantOverride() {
+    // GROOVY-7711
+    void testAmbiguousMethodResolutionNoArgsCovariantOverride() {
         assertScript '''
             class A {}
             class B {
@@ -773,7 +790,7 @@ Printer
         '''
     }
 
-    // GROOVY-8255 and GROOVY-8382
+    // GROOVY-8255, GROOVY-8382
     void testTargetTypingEmptyCollectionLiterals() {
         assertScript '''
             class Foo {
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
index 8d484f5..fa5b0c6 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
@@ -1488,15 +1488,11 @@ println someInt
     void testGenericsArrayPlaceholder() {
         assertScript '''
             import static java.nio.file.AccessMode.*
-
-            class Dummy {
-                static main() {
-                    // more than 5 to match `of(E first, E[] rest)` variant
-                    EnumSet.of(READ, WRITE, EXECUTE, READ, WRITE, EXECUTE)
-                }
+            def test() {
+                // more than 5 to match of(E first, E[] rest) variant
+                EnumSet.of(READ, WRITE, EXECUTE, READ, WRITE, EXECUTE)
             }
-
-            assert Dummy.main() == [READ, WRITE, EXECUTE].toSet()
+            assert test() == [READ, WRITE, EXECUTE].toSet()
         '''
     }