public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression
@ 2020-12-03 13:50 m.cencora at gmail dot com
  2020-12-03 18:09 ` [Bug c++/98122] [10/11 Regression] " mpolacek at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: m.cencora at gmail dot com @ 2020-12-03 13:50 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98122
           Summary: [regression] Accessing union member through
                    pointer-to-member is not a constant expression
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m.cencora at gmail dot com
  Target Milestone: ---

Following code does not compile on gcc-10 and newer in all std C++14-20 modes.

This used to work in gcc-9 and earlier.

union foo
{
    int a;
};

constexpr bool test()
{
    foo f{ .a = 42 };

    constexpr auto memPtr = &foo::a;

    return (f.*memPtr) == 42;
}

// error: accessing value of 'f' through a 'int' glvalue in a constant
expression
//     return (f.*memPtr) == 42;
static_assert(test(), "");

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/98122] [10/11 Regression] Accessing union member through pointer-to-member is not a constant expression
  2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression m.cencora at gmail dot com
@ 2020-12-03 18:09 ` mpolacek at gcc dot gnu.org
  2020-12-03 18:10 ` mpolacek at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-12-03 18:09 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-12-03
     Ever confirmed|0                           |1
                 CC|                            |mpolacek at gcc dot gnu.org
           Keywords|                            |rejects-valid
            Summary|[regression] Accessing      |[10/11 Regression]
                   |union member through        |Accessing union member
                   |pointer-to-member is not a  |through pointer-to-member
                   |constant expression         |is not a constant
                   |                            |expression
   Target Milestone|---                         |10.3

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Started with r276563.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/98122] [10/11 Regression] Accessing union member through pointer-to-member is not a constant expression
  2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression 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
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-12-03 18:10 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/98122] [10/11 Regression] Accessing union member through pointer-to-member is not a constant expression
  2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression 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
  2020-12-04 13:23 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-12-04 12:09 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/98122] [10/11 Regression] Accessing union member through pointer-to-member is not a constant expression
  2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression m.cencora at gmail dot com
                   ` (2 preceding siblings ...)
  2020-12-04 12:09 ` jakub at gcc dot gnu.org
@ 2020-12-04 13:23 ` jakub at gcc dot gnu.org
  2020-12-05  0:32 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-12-04 13:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 49680
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49680&action=edit
gcc11-pr98122.patch

Untested fix.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/98122] [10/11 Regression] Accessing union member through pointer-to-member is not a constant expression
  2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression m.cencora at gmail dot com
                   ` (3 preceding siblings ...)
  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
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-12-05  0:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:43e84ce7d62be121445e17cc0ee009a81fb285d7

commit r11-5755-g43e84ce7d62be121445e17cc0ee009a81fb285d7
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Sat Dec 5 01:30:08 2020 +0100

    c++: Fix constexpr access to union member through pointer-to-member
[PR98122]

    We currently incorrectly reject the first testcase, because
    cxx_fold_indirect_ref_1 doesn't attempt to handle UNION_TYPEs.
    As the second testcase shows, it isn't that easy, because I believe we need
    to take into account the active member and prefer that active member over
    other members, because if we pick a non-active one, we might reject valid
    programs.

    2020-12-05  Jakub Jelinek  <jakub@redhat.com>

            PR c++/98122
            * constexpr.c (cxx_union_active_member): New function.
            (cxx_fold_indirect_ref_1): Add ctx argument, pass it through to
            recursive call.  Handle UNION_TYPE.
            (cxx_fold_indirect_ref): Add ctx argument, pass it to recursive
calls
            and cxx_fold_indirect_ref_1.
            (cxx_eval_indirect_ref): Adjust cxx_fold_indirect_ref calls.

            * g++.dg/cpp1y/constexpr-98122.C: New test.
            * g++.dg/cpp2a/constexpr-98122.C: New test.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/98122] [10 Regression] Accessing union member through pointer-to-member is not a constant expression
  2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression m.cencora at gmail dot com
                   ` (4 preceding siblings ...)
  2020-12-05  0:32 ` cvs-commit at gcc dot gnu.org
@ 2020-12-05  0:34 ` 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
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-12-05  0:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[10/11 Regression]          |[10 Regression] Accessing
                   |Accessing union member      |union member through
                   |through pointer-to-member   |pointer-to-member is not a
                   |is not a constant           |constant expression
                   |expression                  |

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed on the trunk so far.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/98122] [10 Regression] Accessing union member through pointer-to-member is not a constant expression
  2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression m.cencora at gmail dot com
                   ` (5 preceding siblings ...)
  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
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-01-06  9:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:0c3467bd82d8df0294bbe2f141404d2a588bcbd9

commit r10-9216-g0c3467bd82d8df0294bbe2f141404d2a588bcbd9
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Sat Dec 5 01:30:08 2020 +0100

    c++: Fix constexpr access to union member through pointer-to-member
[PR98122]

    We currently incorrectly reject the first testcase, because
    cxx_fold_indirect_ref_1 doesn't attempt to handle UNION_TYPEs.
    As the second testcase shows, it isn't that easy, because I believe we need
    to take into account the active member and prefer that active member over
    other members, because if we pick a non-active one, we might reject valid
    programs.

    2020-12-05  Jakub Jelinek  <jakub@redhat.com>

            PR c++/98122
            * constexpr.c (cxx_union_active_member): New function.
            (cxx_fold_indirect_ref_1): Add ctx argument, pass it through to
            recursive call.  Handle UNION_TYPE.
            (cxx_fold_indirect_ref): Add ctx argument, pass it to recursive
calls
            and cxx_fold_indirect_ref_1.
            (cxx_eval_indirect_ref): Adjust cxx_fold_indirect_ref calls.

            * g++.dg/cpp1y/constexpr-98122.C: New test.
            * g++.dg/cpp2a/constexpr-98122.C: New test.

    (cherry picked from commit 43e84ce7d62be121445e17cc0ee009a81fb285d7)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/98122] [10 Regression] Accessing union member through pointer-to-member is not a constant expression
  2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression m.cencora at gmail dot com
                   ` (6 preceding siblings ...)
  2021-01-06  9:39 ` cvs-commit at gcc dot gnu.org
@ 2021-01-06  9:44 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-06  9:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 10.3+ too.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-01-06  9:44 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-03 13:50 [Bug c++/98122] New: [regression] Accessing union member through pointer-to-member is not a constant expression 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
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

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).