Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7287

Problem With Trait Generics With Conflicting Names

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.0
    • Fix Version/s: 2.4.1
    • Component/s: None
    • Labels:
      None

      Description

      The attached traitgenerics.zip contains the following code:

      src/main/groovy/demo/SomeTrait.groovy
      package demo
      
      trait SomeTrait<T> {
      }
      
      src/main/groovy/demo/SomeOtherTrait.groovy
      package demo
      
      trait SomeOtherTrait {
          def <T> T someOtherMethod() {
          }
      }
      
      src/main/groovy/demo/TopClass.groovy
      package demo
      
      class TopClass<T> implements SomeTrait<T>, SomeOtherTrait {}
      
      src/main/groovy/demo/MiddleClass.groovy
      package demo
      
      class MiddleClass<T> extends TopClass<T> implements SomeTrait<T>, SomeOtherTrait {}
      
      src/main/groovy/demo/BottomClass.groovy
      package demo
      
      class BottomClass extends MiddleClass<String> implements SomeTrait<String>, SomeOtherTrait {}
      

      The code will not compile:

      $ ./gradlew clean cG
      :clean
      :compileJava UP-TO-DATE
      :compileGroovy
      startup failed:
      /Users/jeff/traitgenerics/src/main/groovy/demo/BottomClass.groovy: -1: The return type of java.lang.Object someOtherMethod() in demo.BottomClass is incompatible with java.lang.String in demo.MiddleClass
      . At [-1:-1]  @ line -1, column -1.
      1 error
      
      :compileGroovy FAILED
      
      FAILURE: Build failed with an exception.
      
      * What went wrong:
      Execution failed for task ':compileGroovy'.
      > Compilation failed; see the compiler error output for details.
      
      * Try:
      Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
      
      BUILD FAILED
      
      Total time: 4.054 secs
      

      If I change the type parameter name in SomeOtherTrait to anyting other than T, it compiles...

      src/main/groovy/demo/SomeOtherTrait.groovy
      package demo
      
      trait SomeOtherTrait {
          def <SOM> SOM someOtherMethod() {
          }
      }
      

        Attachments

        1. traitgenerics.zip
          50 kB
          Jeff Brown
        2. traitgenerics2.zip
          50 kB
          Jeff Brown

          Activity

            People

            • Assignee:
              blackdrag Jochen Theodorou
              Reporter:
              brownj Jeff Brown
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: