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

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


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new ec279b2b44 GROOVY-11007: STC: property as variable expression keeps 
temporary types
ec279b2b44 is described below

commit ec279b2b44e562ecc1b0b02a575aaf80ba8ab0ff
Author: Eric Milles <[email protected]>
AuthorDate: Mon Apr 10 16:19:27 2023 -0500

    GROOVY-11007: STC: property as variable expression keeps temporary types
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  9 +++++----
 .../transform/stc/TypeInferenceSTCTest.groovy      | 22 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 4 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 5cd1dbc611..4b6861c4aa 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -692,11 +692,12 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
                 Object val = pexp.getNodeMetaData(key);
                 if (val != null) vexp.putNodeMetaData(key, val);
             }
-            vexp.removeNodeMetaData(INFERRED_TYPE);
-            if (!asBoolean(getTemporaryTypesForExpression(vexp))) {
-                ClassNode type = pexp.getNodeMetaData(INFERRED_TYPE);
-                storeType(vexp, 
Optional.ofNullable(type).orElseGet(pexp::getType));
+            ClassNode type = pexp.getNodeMetaData(INFERRED_TYPE);
+            if (vexp.isClosureSharedVariable()) {
+                type = wrapTypeIfNecessary(type);
             }
+            if (type == null) type = OBJECT_TYPE;
+            vexp.putNodeMetaData(INFERRED_TYPE, type); // GROOVY-11007
             String receiver = vexp.getNodeMetaData(IMPLICIT_RECEIVER);
             Boolean dynamic = pexp.getNodeMetaData(DYNAMIC_RESOLUTION);
             // GROOVY-7701, GROOVY-7996: correct false assumption made by 
VariableScopeVisitor
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy 
b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index af7526fcb9..d3cbbc5c93 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -260,6 +260,28 @@ class TypeInferenceSTCTest extends 
StaticTypeCheckingTestCase {
         'Cannot return value of type Foo for method returning Bar'
     }
 
+    // GROOVY-11007
+    void testInstanceOf11() {
+        assertScript '''
+            interface I {
+                CharSequence getCharSequence()
+            }
+
+            void accept(CharSequence cs) {   }
+
+            void test(I i) {
+                i.with {
+                    if (charSequence instanceof String) {
+                        charSequence.toUpperCase()
+                        accept(charSequence)
+                    }
+                }
+            }
+
+            test({ -> 'works' } as I)
+        '''
+    }
+
     void testNestedInstanceOf1() {
         assertScript '''
             Object o

Reply via email to