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/11/30 17:17:38 UTC

(groovy) branch GROOVY_3_0_X updated: GROOVY-10356, GROOVY-10623: STC: inferred variable type following `null`

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

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 53f48ee432 GROOVY-10356, GROOVY-10623: STC: inferred variable type following `null`
53f48ee432 is described below

commit 53f48ee432ea979d0447637d232173159f3980e4
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Nov 30 11:08:43 2023 -0600

    GROOVY-10356, GROOVY-10623: STC: inferred variable type following `null`
    
    3_0_X backport
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  7 +++--
 .../groovy/transform/stc/ClosuresSTCTest.groovy    |  1 -
 .../groovy/transform/stc/STCAssignmentTest.groovy  | 35 +++++++++++++++-------
 3 files changed, 29 insertions(+), 14 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 11a110a173..e302bb94fb 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4442,11 +4442,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
+        if (cn == UNKNOWN_PARAMETER_TYPE) { // null for assignment or parameter
+            // instead of storing an "unknown" type, reset the type information
             // by determining the declaration type of the expression
             cn = getOriginalDeclarationType(exp);
+            // GROOVY-10356, GROOVY-10623 : "def"
+            if (cn == DYNAMIC_TYPE) return;
         }
         if (cn != null && isPrimitiveType(cn)) {
             if (exp instanceof VariableExpression && ((VariableExpression) exp).isClosureSharedVariable()) {
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index 050a979165..93a9f69102 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -373,7 +373,6 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-10356
-    @NotYetImplemented
     void testClosureSharedVariable4() {
         assertScript '''
             interface A {
diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
index 462fa9535c..64539ed353 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -1009,18 +1009,22 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         'Inconvertible types: cannot cast java.lang.String[] to java.util.Set[]'
     }
 
-    // GROOVY-5535
+    // GROOVY-5535, GROOVY-10623
     void testAssignToNullInsideIf() {
-        assertScript '''
-            Date test() {
-                Date x = new Date()
-                if (true) {
-                    x = null
+        ['Date', 'def', 'var'].each {
+            assertScript """
+                Date test() {
+                    $it x = new Date()
+                    if (true) {
+                        x = null
+                        Date y = x
+                    }
+                    Date z = x
+                    return x
                 }
-                x
-            }
-            assert test() == null
-        '''
+                assert test() == null
+            """
+        }
     }
 
     // GROOVY-10294
@@ -1050,6 +1054,17 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-10623
+    void testAssignToNullAfterInit() {
+        assertScript '''
+            class C {
+            }
+            def x = new C()
+            x = null
+            C c = x
+        '''
+    }
+
     // GROOVY-5798
     void testShouldNotThrowConversionError() {
         assertScript '''