groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: GROOVY-7691: Type checking error on generic property with covariant type in subclass
Date Sun, 13 May 2018 13:13:09 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 6560ce8e5 -> 67675780a


GROOVY-7691: Type checking error on generic property with covariant type in subclass


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/67675780
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/67675780
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/67675780

Branch: refs/heads/master
Commit: 67675780a258aa27c62ce29dcbda7683502e6a67
Parents: 6560ce8
Author: sunlan <sunlan@apache.org>
Authored: Sun May 13 21:12:59 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Sun May 13 21:12:59 2018 +0800

----------------------------------------------------------------------
 .../stc/StaticTypeCheckingVisitor.java          | 18 +++-
 src/test/groovy/bugs/Groovy7691Bug.groovy       | 97 ++++++++++++++++++++
 .../groovy/transform/stc/GenericsSTCTest.groovy | 23 -----
 3 files changed, 113 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/67675780/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
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 a1a1c7e..5638906 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1984,6 +1984,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
     protected ClassNode checkReturnType(final ReturnStatement statement) {
         Expression expression = statement.getExpression();
         ClassNode type = getType(expression);
+
         if (typeCheckingContext.getEnclosingClosure() != null) {
             return type;
         }
@@ -4496,8 +4497,21 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
             if (vexp == VariableExpression.SUPER_EXPRESSION) return makeSuper();
             final Variable variable = vexp.getAccessedVariable();
             if (variable instanceof FieldNode) {
-                checkOrMarkPrivateAccess(vexp, (FieldNode) variable, isLHSOfEnclosingAssignment(vexp));
-                return getType((FieldNode) variable);
+                FieldNode fieldNode = (FieldNode) variable;
+
+                checkOrMarkPrivateAccess(vexp, fieldNode, isLHSOfEnclosingAssignment(vexp));
+                ClassNode parameterizedType = GenericsUtils.findParameterizedType(fieldNode.getDeclaringClass(),
typeCheckingContext.getEnclosingClassNode());
+
+                if (null != parameterizedType) {
+                    ClassNode originalType = fieldNode.getOriginType();
+                    GenericsType gt = GenericsUtils.extractPlaceholders(parameterizedType).get(originalType.getUnresolvedName());
+
+                    if (null != gt) {
+                        return gt.getType().redirect();
+                    }
+                }
+
+                return getType(fieldNode);
             }
             if (variable != null && variable != vexp && variable instanceof
VariableExpression) {
                 return getType((Expression) variable);

http://git-wip-us.apache.org/repos/asf/groovy/blob/67675780/src/test/groovy/bugs/Groovy7691Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy7691Bug.groovy b/src/test/groovy/bugs/Groovy7691Bug.groovy
new file mode 100644
index 0000000..58588b0
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy7691Bug.groovy
@@ -0,0 +1,97 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+class Groovy7691Bug extends GroovyTestCase {
+//    @NotYetImplemented
+    void testCovariantGenericField() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            abstract class AbstractNumberWrapper<S extends Number> {
+                protected final S number;
+
+                AbstractNumberWrapper(S number) {
+                    this.number = number
+                }
+            }
+            @groovy.transform.CompileStatic
+            class LongWrapper<S extends Long> extends AbstractNumberWrapper<S>
{
+                LongWrapper(S longNumber) {
+                    super(longNumber)
+                }
+
+                S getValue() {
+                    return number;
+                }
+            }
+            assert new LongWrapper<Long>(42L).value == 42L
+        '''
+    }
+
+    void testCovariantGenericProperty() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            abstract class AbstractNumberWrapper<S extends Number> {
+                def S number;
+
+                AbstractNumberWrapper(S number) {
+                    this.number = number
+                }
+            }
+            @groovy.transform.CompileStatic
+            class LongWrapper<S extends Long> extends AbstractNumberWrapper<S>
{
+                LongWrapper(S longNumber) {
+                    super(longNumber)
+                }
+
+                S getValue() {
+                    return number;
+                }
+            }
+            assert new LongWrapper<Long>(42L).value == 42L
+        '''
+    }
+
+    // this test can pass even if GROOVY-7691 is not resolved, just for ensuring the common
case will pass test all the time!
+    void testCovariantGenericMethod() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            abstract class AbstractNumberWrapper<S extends Number> {
+                protected final S number;
+
+                AbstractNumberWrapper(S number) {
+                    this.number = number
+                }
+                
+                protected S getNumber() { return number; }
+            }
+            @groovy.transform.CompileStatic
+            class LongWrapper<S extends Long> extends AbstractNumberWrapper<S>
{
+                LongWrapper(S longNumber) {
+                    super(longNumber)
+                }
+
+                S getValue() {
+                    return getNumber();
+                }
+            }
+            assert new LongWrapper<Long>(42L).value == 42L
+        '''
+    }
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/67675780/src/test/groovy/transform/stc/GenericsSTCTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index ceae089..fcea9d0 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -1791,29 +1791,6 @@ assert result == 'ok'
         '''
     }
 
-    // GROOVY-7691
-    @NotYetImplemented
-    void testCovariantGenericField() {
-        assertScript '''
-            abstract class AbstractNumberWrapper<S extends Number> {
-                protected final S number;
-
-                AbstractNumberWrapper(S number) {
-                    this.number = number
-                }
-            }
-            class LongWrapper<S extends Long> extends AbstractNumberWrapper<S>
{
-                LongWrapper(S longNumber) {
-                    super(longNumber)
-                }
-
-                S getValue() {
-                    return number;
-                }
-            }
-            assert new LongWrapper<Long>(42L).value == 42L
-        '''
-    }
 
     //GROOVY-7804
     void testParameterlessClosureToGenericSAMTypeArgumentCoercion() {


Mime
View raw message