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 2023/01/24 19:19:25 UTC

[groovy] branch master updated: GROOVY-6137, GROOVY-7473, GROOVY-10909: SC: `x in this` is implicit safe

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 22db973494 GROOVY-6137, GROOVY-7473, GROOVY-10909: SC: `x in this` is implicit safe
22db973494 is described below

commit 22db9734948fdb19acbcdcd8b4dcf370e4ae2446
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Jan 24 12:42:32 2023 -0600

    GROOVY-6137, GROOVY-7473, GROOVY-10909: SC: `x in this` is implicit safe
---
 .../sc/transformers/BinaryExpressionTransformer.java |  5 ++++-
 .../transform/stc/STCnAryExpressionTest.groovy       | 20 ++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/sc/transformers/BinaryExpressionTransformer.java b/src/main/java/org/codehaus/groovy/transform/sc/transformers/BinaryExpressionTransformer.java
index cd7911c58e..cae77487d8 100644
--- a/src/main/java/org/codehaus/groovy/transform/sc/transformers/BinaryExpressionTransformer.java
+++ b/src/main/java/org/codehaus/groovy/transform/sc/transformers/BinaryExpressionTransformer.java
@@ -56,6 +56,7 @@ import java.util.List;
 import java.util.function.Consumer;
 
 import static org.apache.groovy.ast.tools.ExpressionUtils.isNullConstant;
+import static org.apache.groovy.ast.tools.ExpressionUtils.isThisOrSuper;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
@@ -242,8 +243,10 @@ public class BinaryExpressionTransformer {
                 || rightExpression instanceof MapExpression
                 || rightExpression instanceof RangeExpression
                 || rightExpression instanceof ClassExpression
-                || rightExpression instanceof ConstantExpression
+                ||(rightExpression instanceof ConstantExpression
                             && !isNullConstant(rightExpression))
+                // rightExpression instanceof VariableExpression
+                || isThisOrSuper(rightExpression))//GROOVY-10909
             return staticCompilationTransformer.transform(call);
 
         // GROOVY-6137, GROOVY-7473: null safety and one-time evaluation
diff --git a/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy b/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy
index f67192b670..ad2e05ed0f 100644
--- a/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy
+++ b/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy
@@ -150,6 +150,26 @@ class STCnAryExpressionTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-6137, GROOVY-7473, GROOVY-10909
+    void testInOperatorImplicitNullSafetyChecks() {
+        assertScript '''
+            class C {
+                int i = 0
+                int getA() { i++ }
+                boolean isCase(val) { true }
+                boolean isNotCase(val) { false }
+
+                void test() {
+                    assert !(a !in this)
+                    assert i == 1
+                    assert a in this
+                    assert i == 2
+                }
+            }
+            new C().test()
+        '''
+    }
+
     // GROOVY-7473
     void testInOperatorShouldEvaluateOperandsOnce() {
         assertScript '''