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

STC loses generic specialization on inheritance

    XMLWordPrintableJSON

Details

    Description

      When a base class contains a type parameter <N> and a method returns N, the STC believes that the return type is the superclass bound of N and not the actual type of N. Overriding the method in the subclass with the specific type does not fix the problem.

      package bug.groovy
      
      import groovy.transform.CompileStatic
      
      @CompileStatic
      abstract class BugRepro<N extends Number> {
        protected N process(N input) { input }
      }
      
      @CompileStatic
      class IntegerRepro extends BugRepro<Integer> {
        static void consume(Optional<Integer> input) {}
      
        // The compilation fails with or without this override
        @Override
        protected Integer process(Integer input) { super.process(input) }
      
        void run() {
          consume(Optional.of(42).map(this::process))
        }
      }
      

      Error:

      Groovy:[Static type checking] - Cannot call bug.groovy.IntegerRepro#consume(java.util.Optional<java.lang.Integer>) with arguments [java.util.Optional<java.lang.Number>]
      

      Expected: The compiler recognizes that N at the call site is Integer.

      This bug appears to have been introduced in 4.0.15; the code compiles correctly in 4.0.14. The bug is still present in 4.0.21.

      Attachments

        Activity

          People

            emilles Eric Milles
            chrylis Christopher Smith
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: