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

!instanceof treated incorrectly in type checker

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.0.0-rc-1
    • Static Type Checker
    • None

    Description

      Currently in:
      https://github.com/apache/groovy/blob/183b8fbf0248a2dceffbba684e50c3c2c060e46c/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java#L703

      The `instanceof` and `!instanceof` operators are treated the same when in fact the exact opposite is required.

      Consider the following code where the `instanceof` has been accidentally replaced with `!instanceof`.

      [42, 'bar'].each { doit(it) }
      
      @groovy.transform.TypeChecked
      doit(arg) {
        if (arg !instanceof String) {
          println arg.toUpperCase() // MME: Integer.toUpperCase()
        } else {
          // println arg.toUpperCase() // [STC] [Static type checking] - Cannot find Object#toUpperCase()
          println 'is a number'
        }
      }
      

      We'd now expect the `toUpperCase` call to fail compilation but it succeeds and then fails at runtime.

      Bonus points if the commented line in the else clause can be made to work.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              paulk Paul King
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: