You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2016/08/29 00:37:59 UTC

groovy git commit: GROOVY-4356: Static members should not be able to access class-level generic types (closes #393)

Repository: groovy
Updated Branches:
  refs/heads/master 0480d3995 -> 18415d959


GROOVY-4356: Static members should not be able to access class-level generic types (closes #393)


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

Branch: refs/heads/master
Commit: 18415d959f3ca8cf43a9f17b44751147848a24ae
Parents: 0480d39
Author: paulk <pa...@asert.com.au>
Authored: Wed Aug 24 17:28:27 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Mon Aug 29 10:35:18 2016 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/control/ResolveVisitor.java |  13 ++-
 src/test/groovy/bugs/Groovy4356Bug.groovy       | 101 +++++++++++++++++++
 .../groovy/transform/stc/GenericsSTCTest.groovy |   2 +-
 .../transform/traitx/Groovy7846Bug.groovy       |  38 ++++---
 4 files changed, 131 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/18415d95/src/main/org/codehaus/groovy/control/ResolveVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/org/codehaus/groovy/control/ResolveVisitor.java
index f967355..af199ef 100644
--- a/src/main/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/org/codehaus/groovy/control/ResolveVisitor.java
@@ -180,7 +180,9 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
         VariableScope oldScope = currentScope;
         currentScope = node.getVariableScope();
         Map<String, GenericsType> oldPNames = genericParameterNames;
-        genericParameterNames = new HashMap<String, GenericsType>(genericParameterNames);
+        genericParameterNames = node.isStatic()
+                ? new HashMap<String, GenericsType>()
+                : new HashMap<String, GenericsType>(genericParameterNames);
 
         resolveGenericsHeader(node.getGenericsTypes());
 
@@ -199,8 +201,8 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
         MethodNode oldCurrentMethod = currentMethod;
         currentMethod = node;
         super.visitConstructorOrMethod(node, isConstructor);
-        currentMethod = oldCurrentMethod;
 
+        currentMethod = oldCurrentMethod;
         genericParameterNames = oldPNames;
         currentScope = oldScope;
     }
@@ -214,10 +216,17 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
     }
 
     public void visitProperty(PropertyNode node) {
+        Map<String, GenericsType> oldPNames = genericParameterNames;
+        if (node.isStatic()) {
+            genericParameterNames = new HashMap<String, GenericsType>();
+        }
+
         ClassNode t = node.getType();
         resolveOrFail(t, node);
         super.visitProperty(node);
         fieldTypesChecked.add(node.getField());
+
+        genericParameterNames = oldPNames;
     }
 
     private boolean resolveToInner (ClassNode type) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/18415d95/src/test/groovy/bugs/Groovy4356Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy4356Bug.groovy b/src/test/groovy/bugs/Groovy4356Bug.groovy
new file mode 100644
index 0000000..e5a1998
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy4356Bug.groovy
@@ -0,0 +1,101 @@
+/*
+ *  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
+
+import gls.CompilableTestSupport
+
+class Groovy4356Bug extends CompilableTestSupport {
+
+    void testDeclareGenericPropertyInNestedClassWithGenericTypeAtNestedLevel() {
+        shouldCompile """
+            class OuterS1<T1> {
+                T1 ofoo
+                static class InnerS1<T1> {
+                    T1 ifoo
+                }
+            }
+        """
+    }
+
+    void testDeclareGenericParameterInStaticMethodWithMethodLevelGenericType() {
+        shouldCompile """
+            class TestS2 {
+                static <T2> void foo(T2 param1){}
+            }
+        """
+    }
+
+    void testDeclareGenericLocalVarInStaticMethodWithMethodLevelGenericType() {
+        shouldCompile """
+            class TestS3 {
+                static <T3> void foo() {
+                    T3 localVar1
+                }
+            }
+        """
+    }
+
+    void testDeclareGenericPropertyOfInnerClassWithGenericTypeAtOuterLevel() {
+        shouldCompile """
+            class Outer<T4> {
+                T4 ofoo
+                class Inner {
+                    T4 ifoo
+                }
+            }
+        """
+    }
+
+    void testDeclareGenericPropertyOfNestedClassWithGenericTypeAtOuterLevel() {
+        shouldNotCompile """
+            class Outer<T5> {
+                T5 ofoo
+                static class Inner {
+                    T5 ifoo
+                }
+            }
+        """
+    }
+
+    void testDeclareGenericStaticPropertyOfClassWithGenericType() {
+        shouldNotCompile """
+            class Test1<T6> {
+                static T6 f1
+            }
+        """
+    }
+
+    void testDeclareGenericParameterInStaticMethodOfClassWithGenericType() {
+        shouldNotCompile """
+            class Test2<T7> {
+                static foo(T7 param1) {}
+            }
+        """
+    }
+
+    void testDeclareGenericLocalVarInStaticMethodOfClassWithGenericType() {
+        shouldNotCompile """
+            class Test3<T8> {
+                static foo() {
+                    T8 localVar1
+                }
+            }
+        """
+    }
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/18415d95/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 611d11c..9655e05 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -1308,7 +1308,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
             class Done extends Base<Done> { }
             class Next<H, T extends Base<T>> extends Base<Next<H, T>> {
                 H head; T tail
-                static Next<H, T> next(H h, T t) { new Next<H, T>(head:h, tail:t) }
+                static <H, T extends Base<T>> Next<H, T> next(H h, T t) { new Next<H, T>(head:h, tail:t) }
                 String toString() { "Next($head, ${tail.toString()})" }
             }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/18415d95/src/test/org/codehaus/groovy/transform/traitx/Groovy7846Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/traitx/Groovy7846Bug.groovy b/src/test/org/codehaus/groovy/transform/traitx/Groovy7846Bug.groovy
index 5512235..e309ebb 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/Groovy7846Bug.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/Groovy7846Bug.groovy
@@ -25,27 +25,25 @@ class Groovy7846Bug extends GroovyTestCase {
     void testTraitsShouldAllowGenerifiedReturnTypesInStaticMethods() {
         def cl = new GroovyClassLoader()
         cl.parseClass('''
-
-class Foo {
-    static <T> T withClient(@DelegatesTo(Foo) Closure<T> callable ) {
-        callable.call()
-    }
-}
-trait TraitWithStaticMethod<D>  {
-
-    static Collection<D> asCollection(D type) {
-        return [type]
-    }
-
-    static <T> T withClient(@DelegatesTo(Foo) Closure<T> callable ) {
-        callable.call()
-    }
-}
-''')
+            class Foo {
+                static <T> T withClient(@DelegatesTo(Foo) Closure<T> callable ) {
+                    callable.call()
+                }
+            }
+
+            trait TraitWithStaticMethod<D>  {
+                Collection<D> asCollection(D type) {
+                    return [type]
+                }
+
+                static <T> T withClient(@DelegatesTo(Foo) Closure<T> callable ) {
+                    callable.call()
+                }
+            }
+        ''')
         Class cls = cl.parseClass('''
-class Bar implements TraitWithStaticMethod<Bar> {}
-
-''')
+            class Bar implements TraitWithStaticMethod<Bar> {}
+        ''')
 
         assert new ClassNode(cls).methods
         assert cls.withClient { true }