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

commit e38d63aa4988745416f721ecaa7de2bd33f8fddc
Author: Eric Milles <[email protected]>
AuthorDate: Wed Sep 4 12:55:35 2024 -0500

    GROOVY-11450: STC: nested conditional assignments
---
 .../transform/stc/StaticTypeCheckingVisitor.java    | 13 +++++--------
 .../groovy/transform/stc/STCAssignmentTest.groovy   | 21 +++++++++++++++++++++
 2 files changed, 26 insertions(+), 8 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 a2e261d0f6..4fd3527978 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4014,15 +4014,8 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
             restoreTypeBeforeConditional();
 
             ifElse.getElseBlock().visit(this);
-
-            // GROOVY-9786: if chaining: "if (...) x=?; else if (...) x=?;"
-            Map<VariableExpression, ClassNode> updates =
-                ifElse.getElseBlock().getNodeMetaData("assignments");
-            if (updates != null) {
-                updates.forEach(this::recordAssignment);
-            }
         } finally {
-            ifElse.putNodeMetaData("assignments", 
popAssignmentTracking(oldTracker));
+            popAssignmentTracking(oldTracker);
         }
 
         if (!typeCheckingContext.enclosingBlocks.isEmpty()) {
@@ -4201,6 +4194,10 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
             });
         });
         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 257927f18a..852de8bef6 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -658,6 +658,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'

Reply via email to