Compiler warns about implicit cast from long to int in shift operation

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • None
    • Affects Version/s: 23
    • Component/s: tools
    • None
    • generic
    • generic

      Consider this input:

      public class Shift {
          void m() {
              int a = 1 << 1L; // no warning
              a <<= 1L; // implicit cast from long to int in compound assignment is possibly lossy
              long b = 1 << 1L; // no warning
              b <<= 1L; // no warning
          }
      }

      Inconsistency #1: The first shift does not generate a warning, while the second one does, even though the shift amount the warning complains about is the same (1L).

      Inconsistency #2: The second shift generates an "implicit cast from long to int" warning, the fourth shift does not. But the shift amount is always cast to (something even smaller than) an int, even when the target being shifted is a long. So if there's any kind of "implicit cast" relating to the shift amount, it happens in both cases.

      But probably there should not be any warning. The shift operation doesn't "care" whether the shift amount is expressed as an int or a long, it only cares about the lower 5 or 6 bits of the value. The JLS doesn't specify that the shift amount must have type int, it just talks about the bits of the value to use. Any integral type will do.

            Assignee:
            Unassigned
            Reporter:
            Archie Cobbs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: