Thodoris Sotiropoulos created GROOVY-11477: ----------------------------------------------
Summary: assignments in closures may lead to CCE when flow typing is enabled Key: GROOVY-11477 URL: https://issues.apache.org/jira/browse/GROOVY-11477 Project: Groovy Issue Type: Bug Components: Static Type Checker Reporter: Thodoris Sotiropoulos This is related to 11471. I have the following program {code:java} class Test { public static void main(String[] args) { def x = 1 Closure m = null if (true) { m = {x = ""} } x = 1 m() Integer y = x y.intValue() } } {code} h3. Actual behavior The code compiles, but I get {code:java} 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:10) {code} h3. Expected behavior The code should have been rejected. h3. Notes Notably, in this slightly modified version of the above program, the type checkers behaves as expected. {code:java} class Test { public static void main(String[] args) { def x = 1 Closure m = null if (true) { m = {x = ""} } x = 1 m() x.intValue() } } {code} {code:java} test.groovy: 10: [Static type checking] - The closure shared variable "x" has been assigned with various types and the method intValue() does not exist in the lowest upper bound of those types: (java.io.Serializable & java.lang.Comparable). In general, this style of variable reuse is a bad practice because the compiler cannot determine safely what is the type of the variable at the moment of the call in a multi-threaded context. @ line 10, column 5. x.intValue() ^1 error {code} -- This message was sent by Atlassian Jira (v8.20.10#820010)