groovy
  1. groovy
  2. GROOVY-4356

Static members should not be able to access generic types

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.8-beta-1
    • Fix Version/s: None
    • Component/s: class generator
    • Labels:
      None
    • Number of attachments :
      0

      Description

      The following code compiles, but it shouldn't because T is a non-static generic type, and difference instances of Outer will have different concrete representations of T. Hence it makes no sense that static inner classes should be able to use such generic type variables.

      class Outer<T> {
          T ofoo
          static class Inner {
              T ifoo
          }
      }
      

      Similarly, all the cases below should not compile as well:

      class Test1<T> {
          static T f1
      }
      
      class Test2<T> {
          static foo(T param1) {}
      }
      
      class Test3<T> {
          static foo() {
              T localVar1
          }
      }
      

        Activity

        Hide
        Roshan Dawrani added a comment -
        Tests that should pass
        class Groovy4365Bug extends gls.CompilableTestSupport {
        
            void testGenericTypeVariablesUsedInStaticSuccessV1() {
                shouldCompile """
                    class OuterS1<T> {
                        T ofoo
                        static class InnerS1<T> {
                            T ifoo
                        }
                    }
                """
            }
        
            void testGenericTypeVariablesUsedInStaticSuccessV2() {
                shouldCompile """
                    class TestS2 {
                        static <T> void foo(T param1){}
                    }
                """
            }
            
            void testGenericTypeVariablesUsedInStaticSuccessV3() {
                shouldCompile """
                    class TestS3 {
                        static <T> void foo() {
                            T localVar1
                        }
                    }
                """
            }
            
            void testGenericTypeVariablesUsedInStaticV1() {
                shouldNotCompile """
                    class Outer<T> {
                        T ofoo
                        static class Inner {
                            T ifoo
                        }
                    }
                """
            }
        
            void testGenericTypeVariablesUsedInStaticV2() {
                shouldNotCompile """
                    class Test1<T> {
                        static T f1
                    }
                """
            }
        
            void testGenericTypeVariablesUsedInStaticV3() {
                shouldNotCompile """
                    class Test2<T> {
                        static foo(T param1) {}
                    }
                """
            }
        
            void testGenericTypeVariablesUsedInStaticV4() {
                shouldNotCompile """
                    class Test3<T> {
                        static foo() {
                            T localVar1
                        }
                    }
                """
            }
        }
        
        Show
        Roshan Dawrani added a comment - Tests that should pass class Groovy4365Bug extends gls.CompilableTestSupport { void testGenericTypeVariablesUsedInStaticSuccessV1() { shouldCompile """ class OuterS1<T> { T ofoo static class InnerS1<T> { T ifoo } } """ } void testGenericTypeVariablesUsedInStaticSuccessV2() { shouldCompile """ class TestS2 { static <T> void foo(T param1){} } """ } void testGenericTypeVariablesUsedInStaticSuccessV3() { shouldCompile """ class TestS3 { static <T> void foo() { T localVar1 } } """ } void testGenericTypeVariablesUsedInStaticV1() { shouldNotCompile """ class Outer<T> { T ofoo static class Inner { T ifoo } } """ } void testGenericTypeVariablesUsedInStaticV2() { shouldNotCompile """ class Test1<T> { static T f1 } """ } void testGenericTypeVariablesUsedInStaticV3() { shouldNotCompile """ class Test2<T> { static foo(T param1) {} } """ } void testGenericTypeVariablesUsedInStaticV4() { shouldNotCompile """ class Test3<T> { static foo() { T localVar1 } } """ } }

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Roshan Dawrani
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: