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/02/24 21:05:15 UTC

[groovy] branch GROOVY-9953 created (now 2181d34)

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

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


      at 2181d34  GROOVY-9953: lookup instanceof type for variable on right of binary expr

This branch includes the following new commits:

     new 2181d34  GROOVY-9953: lookup instanceof type for variable on right of binary expr

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-9953: lookup instanceof type for variable on right of binary expr

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

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

commit 2181d3411e0f244b8cb0a8d1edc1675174300f42
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Feb 24 15:05:00 2021 -0600

    GROOVY-9953: lookup instanceof type for variable on right of binary expr
---
 .../groovy/transform/stc/StaticTypeCheckingVisitor.java |  8 +++-----
 .../groovy/transform/stc/TypeInferenceSTCTest.groovy    | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 5 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 e6fd34f..0a77404 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -763,11 +763,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             }
 
             if (lType == null) lType = getType(leftExpression);
-            ClassNode rType = (isNullConstant(rightExpression) && !isPrimitiveType(lType)
-                    // primitive types should be ignored as they will result in another failure
-                    ? UNKNOWN_PARAMETER_TYPE
-                    : getType(rightExpression)
-            );
+            ClassNode rType = isNullConstant(rightExpression) && !isPrimitiveType(lType)
+                    ? UNKNOWN_PARAMETER_TYPE // null to primitive type is handled elsewhere
+                    : getInferredTypeFromTempInfo(rightExpression, getType(rightExpression));
 
             BinaryExpression reversedBinaryExpression = binX(rightExpression, expression.getOperation(), leftExpression);
             ClassNode resultType = (op == KEYWORD_IN || op == COMPARE_NOT_IN)
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index 3190f6a..b14427d 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -424,6 +424,23 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         ''', 'No such property: y for class: A'
     }
 
+    // GROOVY-9953
+    void testInstanceOfPropagatesToLocalVariable() {
+        assertScript '''
+            class A {
+            }
+            A test(Object x) {
+                if (x instanceof A) {
+                    def y = x
+                    return y
+                } else {
+                    new A()
+                }
+            }
+            new A().with { assert test(it) === it }
+        '''
+    }
+
     void testShouldNotFailWithWith() {
         assertScript '''
             class A {