[ https://issues.apache.org/jira/browse/GROOVY-11457?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Paul King closed GROOVY-11457. ------------------------------ > STC does not properly handle try/catch in flow typing leading to type unsafety > ------------------------------------------------------------------------------ > > Key: GROOVY-11457 > URL: https://issues.apache.org/jira/browse/GROOVY-11457 > Project: Groovy > Issue Type: Bug > Components: Static Type Checker > Reporter: Thodoris Sotiropoulos > Assignee: Eric Milles > Priority: Minor > Fix For: 5.0.0-alpha-10 > > > I have the following incorrect program > {code:java} > class Main { > public static void main(String[] args) { > var p1 = (java.lang.Appendable) null; > if (false) { > } else { > p1 = 51 > }; > //if (false) {} else {p1 = (java.lang.Appendable) null;} > try {} catch(Exception e) { p1 = (java.lang.Appendable) null; } > final java.lang.Appendable p2 = p1 > } > }{code} > h3. Actual behavior > The compiler accepts the program, but I get a ClassCastException > {code:java} > Exception in thread "main" > org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast > object '51' with class 'java.lang.Integer' to class 'java.lang.Appendable' > at > org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:416) > at > org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:329) > at > org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:248) > at > org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:336) > at Main.main(test.groovy:11) > {code} > h3. Expected behavior > The code should have been rejected with > {code:java} > org.codehaus.groovy.control.MultipleCompilationErrorsException: startup > failed: > test.groovy: 11: [Static type checking] - Cannot assign value of type > java.lang.Object to variable of type java.lang.Appendable > @ line 11, column 37. > final java.lang.Appendable p2 = p1 > ^1 error {code} > h3. > Notes > When replacing try/catch with another if/else like so: > {code:java} > class Main { > public static void main(String[] args) { > var p1 = (java.lang.Appendable) null; > if (false) { > } else { > p1 = 51 > }; > if (false) {} else {p1 = (java.lang.Appendable) null;} > //try {} catch(Exception e) { p1 = (java.lang.Appendable) null; } > final java.lang.Appendable p2 = p1 > } > } {code} > the compiler rejected the code as expected. -- This message was sent by Atlassian Jira (v8.20.10#820010)