Scott Murphy Heiberg created GROOVY-12821:
---------------------------------------------

             Summary: SC: ClassCastException when a closure shared variable is 
assigned instances of two different subclasses
                 Key: GROOVY-12821
                 URL: https://issues.apache.org/jira/browse/GROOVY-12821
             Project: Groovy
          Issue Type: Bug
            Reporter: Scott Murphy Heiberg


On master (09e84fa), JDK 25.0.4:
{code:groovy}
@groovy.transform.CompileStatic
class Outer {
    static class Box {
        String id = 'box'
        String pm() { 'pm:' + id }
        static String t() { Sub s = new Sub1(); def c = { -> s.pm() }; s = new 
Sub2(); c() }
    }
    static class Sub extends Box { }
    static class Sub1 extends Sub { Sub1() { id = 'sub1' } }
    static class Sub2 extends Sub { Sub2() { id = 'sub2' } }
}
assert Outer.Box.t() == 'pm:sub2'
{code}
Dynamic Groovy and {{@TypeChecked}} return {{pm:sub2}}. Under 
{{@CompileStatic}} it compiles and throws ClassCastException at run time 
({{Outer$Sub2}} cannot be cast to {{Outer$Sub1}}): the shared variable is cast 
to the type of its first assignment.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to