Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7125400

unsigned short shift-assignment should warn about loss of precision

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Duplicate
    • P4
    • None
    • 7
    • tools
    • x86
    • solaris_10

    Description

      FULL PRODUCT VERSION :
      javac 1.7.0_01


      ADDITIONAL OS VERSION INFORMATION :
      SunOS <host> 5.10 Generic_141415-07 i86pc i386 i86pc


      A DESCRIPTION OF THE PROBLEM :
      The following code will happily compile:

                  i >>>= 4;
                  s >>>= 4;

      One would expect the compiler to warn you about loss-of-precision like it does if you change the code to be:

                  i = i >>> 4;
                  s = s >>> 4;

      since, otherwise, the shift will not be unsigned. (The MSB gets a zero in integer space but this is truncated away when the value is silently converted back to a short.)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Simply compile the code supplied.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect to see a loss-of-precision warning when doing the shift-assignment as well:

      Foo.java:9: error: possible loss of precision in shift-assignment
                  s >>>= 4;
                        ^
        required: short
        found: int
      1 error
      ACTUAL -
      Code compiles without warning

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Foo
      {
          public static void main(String[] args)
          {
              int i = 0xdead;
              short s = (short)i;
              do {
                  i >>>= 4;
                  s >>>= 4;
                  System.err.println(
                      String.format("0x%x", (short)i) + " " +
                      String.format("0x%x", s) + " " +
                      ((short)i==s)
                  );
              } while (i != 0);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Don't use ">>>=" with shorts.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: