* [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