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

Conversion from int to byte does not work in case of ternary operator

    XMLWordPrintableJSON

    Details

      Description

      I have the following Groovy program.

      @groovy.transform.CompileStatic
      public class Main {
        static byte x = 1;
      
        public static byte foo() {
          1
        }
      
        public static void main(String[] args) {
          x = (true) ? 0 : foo() // this does not work
      
          // the following works
          if (true) {
            x = 0
          } else {
            x = foo()
          }
      
          byte y = (true) ? 0 : foo() // this doesn't work as well
        }
      }
      
      

      Actual Behavior

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

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Main.groovy: 11: [Static type checking] - Possible loss of precision from int to byte
       @ line 11, column 9.
             x = (true) ? 0 : foo() 
                 ^
      
      Main.groovy: 20: [Static type checking] - Possible loss of precision from int to byte
       @ line 20, column 14.
             byte y = (true) ? 0 : foo() 
                      ^
      
      2 errors
      
      
      

      Expected Behavior

      Compile successfully.

      Affected Version

      This programs fails when compiled with the compiler from the master (commit: a8023b1b0d1d79800677ce2b4665ee3fb14688f4), 4.0.0-alpha-2, and 3.0.8.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              schaliasos Stefanos Chaliasos
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: