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

Compiler fails to infer type if given a concrete type instead of a generified interface

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.0-beta-2
    • 2.3.1
    • Static Type Checker
    • None

    Description

      The following test case fails:

      import groovy.transform.ASTTest
      import static org.codehaus.groovy.transform.stc.StaticTypesMarker.*
      import static org.codehaus.groovy.ast.ClassHelper.*
      
      interface Converter<F, T> {
      T convertC(F from)
      }
        
      class Holder<T> {
         T thing
        
         Holder(T thing) {
          this.thing = thing
         }
        
         def <R> Holder<R> convertH(Converter<? super T, ? extends R> func1) {
            new Holder(func1.convertC(thing))
         }
      }
      
      class IntToFloatConverter implements Converter<Integer,Float> {
          public Float convertC(Integer from) { from.floatValue() } 
      }
       
      @groovy.transform.TypeChecked
      void foo() {
      @ASTTest(phase=INSTRUCTION_SELECTION,value={
          def holderType = node.getNodeMetaData(INFERRED_TYPE)
          assert holderType.genericsTypes[0].type == Float_TYPE
      })
      def h1 = new Holder<Integer>(2).convertH(new IntToFloatConverter())
      }
      foo()
      

      The important part is that when inference is done, it should map the placeholders from Converter<F,T> to the concrete implementations of IntToFloatConverter, that is to say Integer and Double.

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            melix Cédric Champeau
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: