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

static type checking fails in for-each loops

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.0, 2.0.1
    • 2.0.2
    • Static Type Checker
    • None

    Description

      I have some code that fail to compile with type checking. The example is not contrived, it comes from a real-life project that uses neo4j. The Node_ class is called Node, and I changed it here so that we exclude the possibility that Groovy somehow treats it as its Node class (which it doesn't anyways). The following code:

      package test
      
      import groovy.transform.TypeChecked
      
      @TypeChecked
      class Test {
      
          public void traverse() {
              println new Node_().class.name
              for (/*Object*/Node_ node : new MyTraverser().nodes()) {
                  println node.class.name
              }
          }
      }
      
      class Node_ {}
      
      interface Traverser {
      
          Iterable<Node_> nodes();
      }
      
      class MyTraverser implements Traverser {
      
          @Override
          Iterable<Node_> nodes() {
              []
          }
      }
      

      does not compile with the following error:

      $ groovyc Test.groovy 
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Test.groovy: 12: [Static type checking] - Cannot loop with element of type test.Node -> test.Node with collection of type java.util.List <java.lang.Object> -> java.util.List <E extends java.lang.Object -> java.lang.Object>
       @ line 12, column 9.
                 for (/*Object*/Node node : new MyTraverser().nodes()) {
      

      It compiles fine when the iteration uses Object instead of Node_, and when there is no type checking.

      Attachments

        Activity

          People

            melix Cédric Champeau
            wujek.srujek wujek srujek
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: