On 7/9/26 2:13 PM, Joyee Cheung wrote:

On 9 Jul 2026, at 18:53, Jason Merrill <[email protected]> wrote:

How about implementing this rule in general, rather than special-casing bool?


Hi,

Thanks for the review - if I understand correctly, you are asking about handling
the non-bool types in ocp_convert as well? For fixed underlying types, the enum
type itself is already given the precision and signedness of that underlying 
type
in copy_type_enum. So in the case of non-bool types, the existing
convert_to_integer_maybe_fold below the boolean branch was already sufficient to
implement [expr.static.cast]/8:

if (code == BOOLEAN_TYPE)
{
/* boolean handling */
}

converted = convert_to_integer_maybe_fold (type, e, dofold);

The static_asserts in the new Wconversion-enum-fixed.C do exercise this for
non-bool underlying types, the equivalence tests pass for them even
without the cvt.cc change. The changes in this patch just dropped the
-Wconversion warning for those types (since the conversion is well-defined),
and verified the warning behavior change in the test.

The bug was only that for a bool-backed enum, convert_to_integer_maybe_fold
would truncate the operand to the enum's 1-bit precision, keeping only
the low bit and contradicting [conv.bool], so this patch handles the
bool-backed enum case specially by routing it to the truth value conversion
(and its diagnostics) in the existing boolean-handling branch instead of
letting it fall through to convert_to_integer_maybe_fold below.

-      if (code == BOOLEAN_TYPE)
+      if (code == BOOLEAN_TYPE || bool_enum_p)
Yes, I agree that the existing code got the right result for non-bool underlying types, but it seems simpler to follow the letter of the standard with e.g.

+         if (ENUM_FIXED_UNDERLYING_TYPE_P (type))
+           e = ocp_convert (ENUM_UNDERLYING_TYPE (type), e, convtype,
+                            flags, complain);

right after the comment quoting that passage; then none of the other changes are needed.

Jason

Reply via email to