Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Duplicate
-
3.0.17
-
None
-
None
Description
Under static compilation, inside an "if" condition that checks if either of two variables are an instance of a class, both variables are inferred to be that class. If one of them is not, accessing it results in a ClassCastException.
import groovy.transform.CompileStatic @CompileStatic class InstanceofTypeInference { static abstract class BaseClass { abstract String getMyString() } static class ClassA extends BaseClass { String getMyString() { return "Class A String" } } static class ClassB extends BaseClass { String getMyString() { return "Class B String" } } void doStuff(BaseClass foo, BaseClass bar) { if((foo instanceof ClassA) || (bar instanceof ClassA)) { println foo.myString println bar.myString // cannot cast ClassB to ClassA } } static void main(args) { def example = new InstanceofTypeInference() example.doStuff(new ClassA(), new ClassB()) } }
Running this example fails with a ClassCastException.
Attachments
Issue Links
- duplicates
-
GROOVY-7971 @CS flow typing incorrectly casting to map at runtime
- In Progress
- is related to
-
GROOVY-8965 instanceof with || inserts wrong cast
- Closed