[ 
https://issues.apache.org/jira/browse/GROOVY-12821?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18153547#comment-18153547
 ] 

ASF GitHub Bot commented on GROOVY-12821:
-----------------------------------------

blackdrag commented on PR #3332:
URL: https://github.com/apache/groovy/pull/3332#issuecomment-7317672234

   But the problem is actually that `Sub1` or `Sub2` is taken as the receiver 
class for `pm()`, where it should be `Box`, even without flow typing. The type 
most general possible should be used for the bytecode level method call. If 
Sub1 and Sub2 each defined pm and not Sub or Box, then this should actually 
fail compilation.  flow typing breaks with async code execution. So any usage 
in a closure cannot (initially) depend on the flow type. And any write in a 
closure "poisons" the flow type and makes it degrade to declarion type only - 
at least that is my opionion.




> 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
>            Priority: Major
>
> 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