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

Static compilation requires casting inside instanceof check (additional cases)

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.12
    • Fix Version/s: 2.4.13
    • Component/s: Compiler
    • Labels:
      None
    • Environment:
      macOS JDK 1.8

      Description

      Cloned from GROOVY-8260 to handle these additional cases:

      import groovy.transform.CompileStatic
      
      @CompileStatic
      Date ternaryParam(Object input) {
          input instanceof Date ? input : null
      }
      
      @CompileStatic
      Date ternaryLocalVar(Object input) {
          Object copy = input
          copy instanceof Date ? copy : null
      }
      
      @CompileStatic
      Date ifThenElseParam(Object ifThenElseInput) {
          if (ifThenElseInput instanceof Date) {
              ifThenElseInput
          } else {
              null
          }
      }
      
      @CompileStatic
      Date ifThenElseLocalVar(Object input) {
          Date result
          if (input instanceof Date) {
              result = input
          } else {
              result = null
          }
          result
      }
      
      class FooBase {}
      class FooChild extends FooBase{}
      @CompileStatic
      FooChild ifThenElseLocalVar2(FooBase input) {
          FooChild result
          if (input instanceof FooChild) {
              result = input
          } else {
              result = null
          }
          result
      }
      
      def d = new Date()
      assert ternaryParam(42) == null
      assert ternaryParam('foo') == null
      assert ternaryParam(d) == d
      assert ternaryLocalVar(42) == null
      assert ternaryLocalVar('foo') == null
      assert ternaryLocalVar(d) == d
      assert ifThenElseParam(42) == null
      assert ifThenElseParam('foo') == null
      assert ifThenElseParam(d) == d
      assert ifThenElseLocalVar(42) == null
      assert ifThenElseLocalVar('foo') == null
      assert ifThenElseLocalVar(d) == d
      def fc = new FooChild()
      assert ifThenElseLocalVar2(fc) == fc
      assert ifThenElseLocalVar2(new FooBase()) == null
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                Schlogen James Kleeh
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: