Thodoris Sotiropoulos created GROOVY-11450:
----------------------------------------------

             Summary: flow typing can lead to type unsafe results when having 
nested ifs
                 Key: GROOVY-11450
                 URL: https://issues.apache.org/jira/browse/GROOVY-11450
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Thodoris Sotiropoulos


I have the following program
{code:java}
class A {
  Object clone() { return null; }
}
class Main {
  public static void main(Object y) {
    def x
    x = new A()
    if (false ){
      x = new A()
    } else {
      if (true) {
        x = 1
      }
    }
    x.clone();
  }
} {code}
h3. Actual behavior

The program compiles, but I receive the following error at runtime:
{code:java}
Exception in thread "main" java.lang.ClassCastException: class 
java.lang.Integer cannot be cast to class A (java.lang.Integer is in module 
java.base of loader 'bootstrap'; A is in unnamed module of loader 'app')
    at Main.main(test.groovy:16) {code}
h3. Expected behavior

The code should have been rejected with
{code:java}
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
test.groovy: 12: Cannot access method: clone() of class: java.lang.Object @ 
line 12, column 5.
       x.clone();
       ^1 error
 {code}
 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to