* [PATCH] c++: constexpr union member access folding [PR114709]
@ 2024-04-22 22:18 Patrick Palka
2024-04-23 2:58 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2024-04-22 22:18 UTC (permalink / raw)
To: gcc-patches; +Cc: jason, Patrick Palka
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
for trunk/13/12?
-- >8 --
The object/offset canonicalization performed in cxx_fold_indirect_ref
is undesirable for union member accesses because it loses information
about the member being accessed which we may later need to diagnose an
inactive-member access. So this patch restricts the canonicalization
accordingly.
PR c++/114709
gcc/cp/ChangeLog:
* constexpr.cc (cxx_fold_indirect_ref): Restrict object/offset
canonicalization to RECORD_TYPE member accesses.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/constexpr-union8.C: New test.
---
gcc/cp/constexpr.cc | 1 +
gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C | 8 ++++++++
2 files changed, 9 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index fcc847d85df..941a478e889 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -5797,6 +5797,7 @@ cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
more folding opportunities. */
auto canonicalize_obj_off = [] (tree& obj, tree& off) {
while (TREE_CODE (obj) == COMPONENT_REF
+ && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
&& (tree_int_cst_sign_bit (off) || integer_zerop (off)))
{
tree field = TREE_OPERAND (obj, 1);
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C
new file mode 100644
index 00000000000..34c264944b6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C
@@ -0,0 +1,8 @@
+// PR c++/114709
+// { dg-do compile { target c++11 } }
+
+struct T1 { int a, b; };
+struct T2 { int c; double d; };
+union U { T1 t1; T2 t2; };
+
+constexpr int v = U{{1,2}}.t2.*&T2::c; // { dg-error "accessing 'U::t2'" }
--
2.45.0.rc0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: constexpr union member access folding [PR114709]
2024-04-22 22:18 [PATCH] c++: constexpr union member access folding [PR114709] Patrick Palka
@ 2024-04-23 2:58 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2024-04-23 2:58 UTC (permalink / raw)
To: Patrick Palka, gcc-patches
On 4/22/24 15:18, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk/13/12?
OK with a rationale comment.
> -- >8 --
>
> The object/offset canonicalization performed in cxx_fold_indirect_ref
> is undesirable for union member accesses because it loses information
> about the member being accessed which we may later need to diagnose an
> inactive-member access. So this patch restricts the canonicalization
> accordingly.
>
> PR c++/114709
>
> gcc/cp/ChangeLog:
>
> * constexpr.cc (cxx_fold_indirect_ref): Restrict object/offset
> canonicalization to RECORD_TYPE member accesses.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/constexpr-union8.C: New test.
> ---
> gcc/cp/constexpr.cc | 1 +
> gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C | 8 ++++++++
> 2 files changed, 9 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C
>
> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
> index fcc847d85df..941a478e889 100644
> --- a/gcc/cp/constexpr.cc
> +++ b/gcc/cp/constexpr.cc
> @@ -5797,6 +5797,7 @@ cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
> more folding opportunities. */
> auto canonicalize_obj_off = [] (tree& obj, tree& off) {
> while (TREE_CODE (obj) == COMPONENT_REF
> + && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
> && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
> {
> tree field = TREE_OPERAND (obj, 1);
> diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C
> new file mode 100644
> index 00000000000..34c264944b6
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-union8.C
> @@ -0,0 +1,8 @@
> +// PR c++/114709
> +// { dg-do compile { target c++11 } }
> +
> +struct T1 { int a, b; };
> +struct T2 { int c; double d; };
> +union U { T1 t1; T2 t2; };
> +
> +constexpr int v = U{{1,2}}.t2.*&T2::c; // { dg-error "accessing 'U::t2'" }
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-04-23 2:58 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-22 22:18 [PATCH] c++: constexpr union member access folding [PR114709] Patrick Palka
2024-04-23 2:58 ` Jason Merrill
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).