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

Static compiler does not coerce Groovy truth from closures

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.0.12
    • 5.0.0-alpha-1
    • Static compilation
    • None

    Description

      The static compiler will accept a closure returning a non-boolean value as fulfilling a Predicate, but it does not coerce the return value using Groovy truth.

      @CompileStatic
      class Bug {
          static void main(String[] args) {
              println new AtomicReference<Object>(null).stream()
                  .filter( { it.get() } as Predicate<AtomicReference<?>>)
            // or .filter(AtomicReference::get as Predicate<AtomicReference<?>>)
                  .findAny()
          }
      }
      

      Expected result: Optional.empty
      Actual result: NullPointerException on unboxing the null return value

      Using an explicit as boolean inside the lambda works, but this doesn't work with method references. Coercion should work even without as Predicate if the compiler sees the functional return type is boolean/Boolean.

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: