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

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

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5.13
    • Fix Version/s: 4.0.0-alpha-1, 2.5.14, 3.0.6
    • Component/s: None
    • Labels:
      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

            • Assignee:
              emilles Eric Milles
              Reporter:
              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