Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10096

STC: accepts incorrect program with flow typing

    XMLWordPrintableJSON

Details

    Description

      I have the following Groovy program.

      @groovy.transform.TypeChecked
      class Main {
        public static Bar buz(Foo bux) {
          (((true || bux instanceof Bar)) ?
            bux :
             new Bar())
        }
        public static void main(String[] args) {
          buz(new Foo()).bar()
        }
      }
      
      class Foo {
        public void foo() {}
      }
      
      class Bar extends Foo {
        public void bar() {}
      }
      
      

      Actual Behavior

      The program does not compile, and I get the following error.

      Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Foo@2e1792e7' with class 'Foo' to class 'Bar'
      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Foo@2e1792e7' with class 'Foo' to class 'Bar'
              at Main.buz(Main.groovy:6)
              at Main.main(Main.groovy:9)
              at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      
      

      Expected Behavior

      Compile-time error.

      Affected Version

      This programs have been tested with the compiler from the master (commit: e33e2849e247608c5e1826b76098d3a68d16bebf), 4.0.0-alpha-2, and 3.0.8.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              schaliasos Stefanos Chaliasos
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: