[
https://issues.apache.org/jira/browse/GROOVY-11457?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Eric Milles resolved GROOVY-11457.
----------------------------------
Fix Version/s: 5.0.0-alpha-10
Resolution: Fixed
https://github.com/apache/groovy/commit/6e6f304643a68881dec973e62baf5add7f8cad50
> 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)