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

Static type checker doesn't backtrack when solving generics

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 3.0.6
    • None
    • Static Type Checker
    • None

    Description

      When a method pipeline involves generics, the static type checker is greedy and tends to be overly specific, producing errors when the correct answer involves relaxing to a common bound.

      @CompileStatic
      Number returnANumber() {
        Number number = Optional.of(1234 as Integer) // should infer Optional<Number>
          orElse(42.0 as Double)
      }
      
      [Static type checking] - Cannot find matching method java.util.Optional#orElse(java.lang.Double). Please check if the declared type is correct and if the method exists.
      

      Explicitly inserting a type witness Optional.<Number>of works.

      Note: This might be a duplicate of GROOVY-8103, but I'm unable to tell precisely. 3.0.6 was released one week after it was marked resolved. GROOVY-8961 is marked fixed in 3.0.6, but I'm having the problem there.

      Attachments

        1. screenshot-1.png
          28 kB
          Eric Milles

        Activity

          People

            Unassigned Unassigned
            chrylis Christopher Smith
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: