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 2021/12/12 22:21:16 UTC

[groovy] branch master updated: GROOVY-5502 (pt.1): STC: ignore `null` initializer for references

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 996592f  GROOVY-5502 (pt.1): STC: ignore `null` initializer for references
996592f is described below

commit 996592fe4b1dfc55a6c6cf963d62a4ef19e3e870
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Dec 12 16:14:05 2021 -0600

    GROOVY-5502 (pt.1): STC: ignore `null` initializer for references
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  3 +-
 .../groovy/transform/stc/STCAssignmentTest.groovy  | 33 ++++++++++++++++++----
 2 files changed, 30 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 3d7debd..b93193e 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -841,7 +841,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                 }
             }
 
-            boolean isEmptyDeclaration = (expression instanceof DeclarationExpression && rightExpression instanceof EmptyExpression);
+            boolean isEmptyDeclaration = (expression instanceof DeclarationExpression
+                    && (rightExpression instanceof EmptyExpression || rType == UNKNOWN_PARAMETER_TYPE));
             if (!isEmptyDeclaration && isAssignment(op)) {
                 if (rightExpression instanceof ConstructorCallExpression)
                     inferDiamondType((ConstructorCallExpression) rightExpression, lType);
diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
index 390b3da..499af6e 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -663,13 +663,13 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
     void testIfWithCommonInterface() {
         assertScript '''
             interface I {
-                def foo()
+                def m()
             }
             class A implements I {
-                def foo() { 'A' }
+                def m() { 'A' }
             }
             class B implements I {
-                def foo() { 'B' }
+                def m() { 'B' }
             }
 
             def x = new A()
@@ -677,13 +677,36 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
             if (y) {
                 x = new B()
             }
-            assert x.foo() == 'B'
+            assert x.m() == 'B'
         '''
     }
 
+    // GROOVY-5502
+    void testIfElseWithCommonSuperclass() {
+        for (val in ['null', 'new A()', 'new B()', 'new C()'/*TODO:, 'new Object()'*/]) {
+            assertScript """
+                class A {
+                    def m() { 'A' }
+                }
+                class B extends A {
+                }
+                class C extends A {
+                }
+
+                def var = $val
+                if (true) {
+                    var = new B()
+                } else {
+                    var = new C()
+                }
+                assert var.m() == 'A' // Cannot find matching method Object#m()
+            """
+        }
+    }
+
     // GROOVY-9786
     void testIfElseIfWithCommonInterface() {
-        ['I', 'def', 'var', 'Object'].each {
+        for (it in ['I', 'def', 'var', 'Object']) {
             assertScript """
                 interface I {
                     def foo()