https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96496

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Jason Merrill <[email protected]>:

https://gcc.gnu.org/g:ca3810f001338bb4d15da43160e017d453f234e8

commit r17-2358-gca3810f001338bb4d15da43160e017d453f234e8
Author: Joyee Cheung <[email protected]>
Date:   Mon Jul 13 11:35:47 2026 -0400

    c++: Fix conversion to enum with fixed bool underlying type [PR96496]

    For an enumeration with a fixed underlying type, [expr.static.cast]/8
    requires the operand to be converted to the underlying type first, and
    then to the enumeration type.

    Previously, ocp_convert would convert the operand directly to the
    enumeration type via convert_to_integer_maybe_fold.  For non-bool
    underlying types this gives the required value, but for an underlying
    type of bool it truncates the operand to the enum's 1-bit precision,
    keeping only the low bit and contradicting [conv.bool]: e.g.
    converting 2 yielded the enum value corresponding to false.

    This patch fixes it by following the specification and converting
    operands with a fixed underlying type to that type first, then to the
    enumeration type.  For bool this routes the operand through the
    existing boolean-handling code with its truth value conversion, so it
    now goes through -Wint-in-bool-context diagnostics just like a cast to
    bool.  The conversion now no longer emits -Wconversion if the operand
    is out of range since the value is converted first through well-defined
    conversions, consistent with explicit casts to the underlying type
    itself.  This patch also includes tests for the changed warning
    behavior.

            PR c++/96496

    gcc/cp/ChangeLog:

            * cvt.cc (ocp_convert): For an enumeration type with a fixed
            underlying type, convert the operand to that type first, as
            specified by [expr.static.cast]/8.  Update the comment quoting
            [expr.static.cast]/8.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/enum-bool-conv.C: New test.
            * g++.dg/warn/Wconversion-enum-fixed.C: New test.
            * g++.dg/warn/Wint-in-bool-context-bool-enum.C: New test.

    Signed-off-by: Joyee Cheung <[email protected]>

Reply via email to