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

CLONE - STC does not report type mismatches for coerced list expression

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Static Type Checker
    • Labels:
      None

      Description

      The following program compiles (with @CompileStatic), although it shouldn't.

      public class Main {
          public static void main(String[] args) {
            // Integer[] arr = new Integer[]{1, "str"}; // does not report error (see GROOVY-9985)
            Integer[] arr2 = [1, "fda"] as Integer[] // does not report error
          }
      }
      

      As a result, an exception is thrown at runtime

      Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'str' with class 'java.lang.String' to class 'java.lang.Integer'
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:172)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:282)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:243)
              at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
              at Main.main(test.groovy:4)
      

      Tested on https://github.com/apache/groovy/commit/f0eea862549529ef4e93fafe337f86dd4ac98751

       

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                theosot Thodoris Sotiropoulos
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: