From: Marek Polacek <polacek@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: error with bit-fields and scoped enums [PR111895]
Date: Tue, 24 Oct 2023 16:56:12 -0400 [thread overview]
Message-ID: <ZTgvbM3YEmwxbb5D@redhat.com> (raw)
In-Reply-To: <de9f6c25-7bdf-493a-96ab-8b32c4f13ba4@redhat.com>
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
prev parent reply other threads:[~2023-10-24 20:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-24 16:18 Marek Polacek
2023-10-24 20:46 ` Jason Merrill
2023-10-24 20:56 ` Marek Polacek [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZTgvbM3YEmwxbb5D@redhat.com \
--to=polacek@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).