public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/98122] [10/11 Regression] Accessing union member through pointer-to-member is not a constant expression
Date: Fri, 04 Dec 2020 12:09:23 +0000	[thread overview]
Message-ID: <bug-98122-4-oa82CGrPU2@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-98122-4@http.gcc.gnu.org/bugzilla/>

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
--- gcc/cp/constexpr.c.jj       2020-12-03 15:43:00.491620290 +0100
+++ gcc/cp/constexpr.c  2020-12-04 13:02:06.874418746 +0100
@@ -4679,7 +4679,8 @@ cxx_fold_indirect_ref_1 (location_t loc,
        }
     }
   /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
-  else if (TREE_CODE (optype) == RECORD_TYPE)
+  else if (TREE_CODE (optype) == RECORD_TYPE
+          || TREE_CODE (optype) == UNION_TYPE)
     {
       for (tree field = TYPE_FIELDS (optype);
           field; field = DECL_CHAIN (field))
would fix this, but wonder if for unions we don't need to take into account the
currently active member.

In:
union U { int a; char b; };

constexpr bool
foo ()
{
  U f { .b = 42 };
  constexpr auto m = &U::a;
  return (f.*m) == 42;
}

static_assert (foo (), "");
we even with the patch properly reject it:
pr98122-2.C:11:20: error: non-constant condition for static assertion
   11 | static_assert (foo (), "");
      |                ~~~~^~
pr98122-2.C:11:20:   in ‘constexpr’ expansion of ‘foo()’
pr98122-2.C:11:20: error: accessing ‘U::a’ member instead of initialized ‘U::b’
member in constant expression

But:
union U { int a; int b; };

constexpr bool
foo ()
{
  U f { .b = 42 };
  constexpr auto m = &U::b;
  return (f.*m) == 42;
}

static_assert (foo (), "");
is rejected too and I think it should be accepted.
So I guess for UNION_TYPEs we need to first try the active member and only then
fall back to what the code would do after the above patch.

  parent reply	other threads:[~2020-12-04 12:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-03 13:50 [Bug c++/98122] New: [regression] " m.cencora at gmail dot com
2020-12-03 18:09 ` [Bug c++/98122] [10/11 Regression] " mpolacek at gcc dot gnu.org
2020-12-03 18:10 ` mpolacek at gcc dot gnu.org
2020-12-04 12:09 ` jakub at gcc dot gnu.org [this message]
2020-12-04 13:23 ` jakub at gcc dot gnu.org
2020-12-05  0:32 ` cvs-commit at gcc dot gnu.org
2020-12-05  0:34 ` [Bug c++/98122] [10 " jakub at gcc dot gnu.org
2021-01-06  9:39 ` cvs-commit at gcc dot gnu.org
2021-01-06  9:44 ` jakub at gcc dot gnu.org

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=bug-98122-4-oa82CGrPU2@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).