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

flow typing on loops can introduce type unsafety

    XMLWordPrintableJSON

Details

    Description

      I have the following program

      class Test {
        public static void main(String[] args) {
          def x = 1
          while (true) {
            Integer y = x
            x = ""
            
          }
        }
      }

      Actual behavior

      The code compiles, but I get a CCE at runtime:

      Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '' with class 'java.lang.String' to class 'java.lang.Integer'
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:177)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:294)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:248)
              at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:336)
              at Test.main(test.groovy:5)
       

      Expected behavior

      The code should have been rejected.

      Attachments

        Activity

          People

            emilles Eric Milles
            theosot Thodoris Sotiropoulos
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: