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/20 22:53:50 UTC

[groovy] branch GROOVY-9863 created (now 0964f40)

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

emilles pushed a change to branch GROOVY-9863
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 0964f40  GROOVY-9863: save type into synthetic property (backed by getter/setter)

This branch includes the following new commits:

     new 0964f40  GROOVY-9863: save type into synthetic property (backed by getter/setter)

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



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

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 0964f40ac9220a7b23e0d0023d7bb94d9571ea25
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 d31d597..a3c3be1 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4232,8 +4232,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) {