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 2024/09/04 18:02:48 UTC

(groovy) branch master updated: GROOVY-11450: STC: nested conditional assignments

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 35478b7e20 GROOVY-11450: STC: nested conditional assignments
35478b7e20 is described below

commit 35478b7e2096f3a75ebd452ee675fbd8ee40781b
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Sep 4 12:55:35 2024 -0500

    GROOVY-11450: STC: nested conditional assignments
---
 .../transform/stc/StaticTypeCheckingVisitor.java    | 11 +++++------
 .../groovy/transform/stc/STCAssignmentTest.groovy   | 21 +++++++++++++++++++++
 2 files changed, 26 insertions(+), 6 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 c8ceefcc31..7abf95d5cf 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4178,13 +4178,8 @@ trying: for (ClassNode[] signature : signatures) {
             if (elsePath.isEmpty() && !GeneralUtils.maybeFallsThrough(thenPath)) {
                 tti.forEach(this::putNotInstanceOfTypeInfo);
             }
-            // GROOVY-9786: if chaining: "if (...) x=?; else if (...) x=?;"
-            Map<VariableExpression, ClassNode> updates = elsePath.getNodeMetaData("assignments");
-            if (updates != null) {
-                updates.forEach(this::recordAssignment);
-            }
         } finally {
-            ifElse.putNodeMetaData("assignments", popAssignmentTracking(oldTracker));
+            popAssignmentTracking(oldTracker);
         }
     }
 
@@ -4288,6 +4283,10 @@ trying: for (ClassNode[] signature : signatures) {
             });
         });
         typeCheckingContext.ifElseForWhileAssignmentTracker = oldTracker;
+        // GROOVY-9786, GROOVY-11450: nested conditional assignments
+        if (oldTracker != null) {
+            assignments.forEach(this::recordAssignment);
+        }
         return assignments;
     }
 
diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
index 8df0dec4ae..fd7c636481 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -559,6 +559,27 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         }
     }
 
+    // GROOVY-11450
+    void testIfElseIfInNestedBlock() {
+        shouldFailWithMessages '''
+            class C {
+                def m() { }
+            }
+
+            def x
+            x = new C()
+            if (false) {
+                x = new C()
+            } else {
+                if (true) {
+                    x = 1
+                }
+            }
+            x.m() // x should be LUB(C,int)
+        ''',
+        'Cannot find matching method java.lang.Object#m()'
+    }
+
     void testForLoopWithAssignment() {
         shouldFailWithMessages '''
             def x = '123'