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

SC: ClassCastException for comparison to single-character string literal in closure

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.13
    • 4.0.0-alpha-1, 2.5.14, 3.0.6
    • None
    • None

    Description

      This started failing with Groovy 2.5.13:

      class Node {
        String name, text
      }
      class Root implements Iterable<Node> {
        @Override
        Iterator<Node> iterator() {
          return [
            new Node(name: 'term', text: 'foo'),
            new Node(name: 'dash', text: '-'  ),
            new Node(name: 'term', text: 'bar')
          ].iterator()
        }
      }
      
      @groovy.transform.CompileStatic
      void test() {
        Root root = new Root()
      
        root[0].with {
          assert name == 'term'
          assert text == 'foo'
        }
        root[1].with {
          assert name == 'dash'
          assert text == '-' // GroovyCastException: Cannot cast object 'script@b91d8c4' with class 'script' to class 'bugs.Node'
        }
      }
      
      test()
      

      Adding a cast to String in front of "-" fixes and @CompileDynamic also fixes. I'm guessing @TypeChecked would not have the issue. I noticed some changes in BinaryExpressionTransformer#tryOptimizeCharComparison that may be relevant.

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 20m
                20m