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 2020/06/05 22:12:09 UTC

[groovy] branch GROOVY-7971 created (now 60aef38)

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

emilles pushed a change to branch GROOVY-7971
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 60aef38  GROOVY-7971: do not save instanceof types under logical or

This branch includes the following new commits:

     new 60aef38  GROOVY-7971: do not save instanceof types under logical or

The 1 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.



[groovy] 01/01: GROOVY-7971: do not save instanceof types under logical or

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

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

commit 60aef38f1c738c3562982fad4c3264dd56cc5903
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Jun 5 17:11:54 2020 -0500

    GROOVY-7971: do not save instanceof types under logical or
---
 build.gradle                                       |  3 ++-
 .../transform/stc/StaticTypeCheckingVisitor.java   |  7 +++++++
 .../groovy/transform/stc/MethodCallsSTCTest.groovy | 23 ++++++++++++++++++++--
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/build.gradle b/build.gradle
index 4bc522d..1527142 100644
--- a/build.gradle
+++ b/build.gradle
@@ -202,9 +202,10 @@ dependencies {
 
     testImplementation project(':groovy-ant')
     testImplementation project(':groovy-xml')
-    testImplementation project(':groovy-dateutil')
+    testImplementation project(':groovy-json')
     testImplementation project(':groovy-test')
     testImplementation project(':groovy-macro')
+    testImplementation project(':groovy-dateutil')
 }
 
 ext.generatedDirectory = "${buildDir}/generated/sources"
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 67e1d99..0b3166b 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -219,6 +219,7 @@ import static org.codehaus.groovy.syntax.Types.INTDIV_EQUAL;
 import static org.codehaus.groovy.syntax.Types.KEYWORD_IN;
 import static org.codehaus.groovy.syntax.Types.KEYWORD_INSTANCEOF;
 import static org.codehaus.groovy.syntax.Types.LEFT_SQUARE_BRACKET;
+import static org.codehaus.groovy.syntax.Types.LOGICAL_OR;
 import static org.codehaus.groovy.syntax.Types.MINUS_MINUS;
 import static org.codehaus.groovy.syntax.Types.MOD;
 import static org.codehaus.groovy.syntax.Types.MOD_EQUAL;
@@ -727,6 +728,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         typeCheckingContext.pushEnclosingBinaryExpression(expression);
         try {
             int op = expression.getOperation().getType();
+            if (op == LOGICAL_OR) {
+                typeCheckingContext.pushTemporaryTypeInfo();
+            }
             Expression leftExpression = expression.getLeftExpression();
             Expression rightExpression = expression.getRightExpression();
 
@@ -735,6 +739,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             ClassNode lType = null;
             if (setterInfo != null) {
                 if (ensureValidSetter(expression, leftExpression, rightExpression, setterInfo)) {
+                    if (op == LOGICAL_OR) typeCheckingContext.popTemporaryTypeInfo();
                     return;
                 }
             } else {
@@ -780,6 +785,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                 elvisOperatorExpression.visit(this);
                 resultType = getType(elvisOperatorExpression);
                 storeType(leftExpression, resultType);
+            } else if (op == LOGICAL_OR) {
+                typeCheckingContext.popTemporaryTypeInfo();
             }
 
             if (resultType == null) {
diff --git a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
index 4c5b818..dc6f1b2 100644
--- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -401,7 +401,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
                         foo(it)
                     }
                 }
-            ''', 'Reference to method is ambiguous'
+            ''', 'Cannot find matching method'
     }
 
     void testShouldFailWithMultiplePossibleMethods2() {
@@ -420,7 +420,26 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
                         foo(argument)
                     }
                 }
-            ''', 'Reference to method is ambiguous'
+            ''', 'Cannot find matching method'
+    }
+
+    // GROOVY-7971
+    void testShouldNotFailWithMultiplePossibleMethods() {
+        assertScript '''
+            import groovy.json.JsonOutput
+
+            def test(value) {
+                def out = new StringBuilder()
+                def isString = value.class == String
+                if (isString || value instanceof Map) {
+                    out.append(JsonOutput.toJson(value))
+                }
+                return out.toString()
+            }
+
+            def string = test('two')
+            assert string == '"two"'
+        '''
     }
 
     void testInstanceOfOnExplicitParameter() {