Add integer_cast for checking conversions don't change the value

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Affects Version/s: 22
    • Component/s: hotspot

      We currently use checked_cast to check that various (often otherwise implicit) conversions "don't lose information". This is in part in preparation for enabling `-Wconversion` (JDK-8135181).

      checked_cast performs a round-trip check to verify the value is unchanged. But that round-trip test doesn't work or is unsafe in some cases. For example:

      checked_cast<uint>(SIZE_MAX) => error as expected
      checked_cast<int>(SIZE_MAX) => no error?

      The problem is that SIZE_MAX successfully round-trips because
      static_cast<size_t>(-1) => SIZE_MAX
      due to sign extension when promoting the type.

      Another case where it doesn't work is casting an integer to an enum. If the integer doesn't fit in the enum type, that's UB. (C++20 changes some cases.)

            Assignee:
            Kim Barrett
            Reporter:
            Kim Barrett
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: