On Tue, Oct 24, 2023 at 04:46:02PM -0400, Jason Merrill wrote: > On 10/24/23 12:18, Marek Polacek wrote: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > -- >8 -- > > Here we issue a bogus error: invalid operands of types 'unsigned char:2' > > and 'int' to binary 'operator!=' when casting a bit-field of scoped enum > > type to bool. > > > > In build_static_cast_1, perform_direct_initialization_if_possible returns > > NULL_TREE, because the invented declaration T t(e) fails, which is > > correct. So we go down to ocp_convert, which has code to deal with this > > case: > > /* We can't implicitly convert a scoped enum to bool, so convert > > to the underlying type first. */ > > if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC)) > > e = build_nop (ENUM_UNDERLYING_TYPE (intype), e); > > but the SCOPED_ENUM_P is false since intype is <unnamed-unsigned:2>. > > This could be fixed by using unlowered_expr_type. But then > > c_common_truthvalue_conversion/CASE_CONVERT has a similar problem, and > > unlowered_expr_type is a C++-only function. > > > > Rather than adding a dummy unlowered_expr_type to C, I think we should > > follow [expr.static.cast]p3: "the lvalue-to-rvalue conversion is applied > > to the bit-field and the resulting prvalue is used as the operand of the > > static_cast." There are no prvalue bit-fields, so the l-to-r conversion > > will get us an expression whose type is the enum. (I thought we didn't > > need decay_conversion because that does a whole lot more but using it > > would make sense to me too.) > > It's possible that we might want some of that more, particularly > mark_rvalue_use; decay_conversion seems like the right answer. OK with that > change.
Makes total sense, thank you. (I'd tested the version with decay_conversion and it worked fine.) > rvalue() would also make sense, though that seems to be missing a call to > unlowered_expr_type at the moment. In fact, after "otherwise, it's the > lvalue-to-rvalue conversion" in decay_conv should probably just be a call to > rvalue, with missing bits added to the latter function. Sounds good; I hope I'll get to it next week. I'm not going to make it part of this patch so that I can backport this one to 13 and leave the cleanup for trunk only. Marek