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

STC: for loop element type inference with multiple instanceof

    XMLWordPrintableJSON

Details

    Description

      Consider the following:

      @groovy.transform.TypeChecked
      void test(obj) {
        if (obj instanceof Collection) {
          if (obj instanceof Serializable) {
            for (item in obj) {
              println item
            }
          }
        }
      }
      

      The inferred type of "obj" is (Collection & Serializable). The loop element check fails with:

      java.lang.UnsupportedOperationException
      	at org.codehaus.groovy.transform.stc.UnionTypeClassNode.getComponentType(UnionTypeClassNode.java:232)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.inferLoopElementType(StaticTypeCheckingVisitor.java:2042)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitForLoop(StaticTypeCheckingVisitor.java:2008)
      	at org.codehaus.groovy.ast.stmt.ForStatement.visit(ForStatement.java:47)
      

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              emilles Eric Milles
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: