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 2020/12/24 04:15:19 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-9863: save type into synthetic property (backed by getter/setter)

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 2b2b6f6  GROOVY-9863: save type into synthetic property (backed by getter/setter)
2b2b6f6 is described below

commit 2b2b6f67bf3400c42c9248cfa5f5f5f5ea12d45a
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Dec 20 16:53:32 2020 -0600

    GROOVY-9863: save type into synthetic property (backed by getter/setter)
    
    VariableScopeVisitor#findClassMember makes PropertyNode for method-only
    properties but does not set any type information on it.
---
 .../classgen/asm/StatementMetaTypeChooser.java     | 15 +++++++-------
 .../transform/stc/StaticTypeCheckingVisitor.java   |  6 ++++--
 .../classgen/asm/sc/BugsStaticCompileTest.groovy   | 23 ++++++++++++++++++++--
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/StatementMetaTypeChooser.java b/src/main/java/org/codehaus/groovy/classgen/asm/StatementMetaTypeChooser.java
index f140d32..99611b5 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/StatementMetaTypeChooser.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/StatementMetaTypeChooser.java
@@ -45,17 +45,16 @@ public class StatementMetaTypeChooser implements TypeChooser {
         if (type != null) return type;
 
         if (exp instanceof VariableExpression) {
-            VariableExpression ve = (VariableExpression) exp;
-            if (ve.isClosureSharedVariable()) return ve.getType();
-            if (ve.isSuperExpression()) return current.getSuperClass();
+            VariableExpression vexp = (VariableExpression) exp;
+            if (vexp.isClosureSharedVariable()) return vexp.getType();
+            if (vexp.isSuperExpression()) return current.getSuperClass();
 
-            type = ve.getOriginType();
-        } else if (exp instanceof Variable) {
-            Variable v = (Variable) exp;
-            type = v.getOriginType();
+            Variable var = vexp.getAccessedVariable();
+            if (var == null) var = vexp;
+            type = var.getOriginType();
         } else {
             type = exp.getType();
         }
-        return type.redirect();
+        return type;
     }
 }
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 afe5910..6804381 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4261,8 +4261,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             if (accessedVariable != exp && accessedVariable instanceof VariableExpression) {
                 storeType((VariableExpression) accessedVariable, cn);
             }
-            if (accessedVariable instanceof Parameter) {
-                ((Parameter) accessedVariable).putNodeMetaData(INFERRED_TYPE, cn);
+            if (accessedVariable instanceof Parameter
+                    || (accessedVariable instanceof PropertyNode
+                        && ((PropertyNode) accessedVariable).getField().isSynthetic())) {
+                ((ASTNode) accessedVariable).putNodeMetaData(INFERRED_TYPE, cn);
             }
             if (var.isClosureSharedVariable() && cn != null) {
                 List<ClassNode> assignedTypes = typeCheckingContext.closureSharedVariablesAssignmentTypes.computeIfAbsent(var, k -> new LinkedList<ClassNode>());
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
index ce29a78..8d484f5 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
@@ -23,9 +23,10 @@ import groovy.transform.stc.BugsSTCTest
 /**
  * Unit tests for static type checking : bugs.
  */
-class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilationTestSupport {
+final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilationTestSupport {
 
-    void testGroovy5498PropertyAccess() {
+    // GROOVY-5498
+    void testPropertyAccess() {
         assertScript '''
             class Test {
 
@@ -117,6 +118,24 @@ class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilationTest
         '''
     }
 
+    // GROOVY-9863
+    void testPlusShouldNotThrowGroovyBugError() {
+        assertScript '''
+            import static org.junit.Assert.assertEquals
+
+            class C {
+                double getSomeValue() {
+                    0.0d
+                }
+                double test() {
+                    1.0d + someValue
+                }
+            }
+
+            assertEquals(1.0d, new C().test(), 0.00000001d)
+        '''
+    }
+
     // GROOVY-
     void testPowerShouldNotThrowVerifyError() {
         assertScript '''int squarePlusOne(int num) {