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

flow typing can lead to type unsafe results when having nested ifs

Agile BoardAttach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    Description

      I have the following program

      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();
        }
      } 

      Actual behavior

      The program compiles, but I receive the following error at runtime:

      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) 

      Expected behavior

      The code should have been rejected with

      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
       

       

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            emilles Eric Milles
            theosot Thodoris Sotiropoulos
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment