public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r10-9314] c++: [[no_unique_address]] in empty base [PR98463]
@ 2021-01-29 16:00 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2021-01-29 16:00 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:2127d2c3ee23bbd03f02d88fd82403408696ee4a

commit r10-9314-g2127d2c3ee23bbd03f02d88fd82403408696ee4a
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jan 22 13:17:10 2021 -0500

    c++: [[no_unique_address]] in empty base [PR98463]
    
    In this testcase, cxx_eval_store_expression got confused trying to build up
    CONSTRUCTORs for initializing a subobject because the subobject is a member
    of an empty base.  In C++14 mode and below we don't build FIELD_DECLs for
    empty bases, so the CONSTRUCTOR skipped the empty base, and treated the
    member as a member of the derived class, which breaks.
    
    Fixed by recognizing this situation and giving up on trying to build a
    CONSTRUCTOR for the inner target at that point; since it doesn't have any
    data, we don't need to actually store anything.
    
    gcc/cp/ChangeLog:
    
            PR c++/98463
            * constexpr.c (get_or_insert_ctor_field): Add check.
            (cxx_eval_store_expression): Handle discontinuity of refs.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/98463
            * g++.dg/cpp2a/no_unique_address8.C: New test.

Diff:
---
 gcc/cp/constexpr.c                              | 16 +++++++++++++++-
 gcc/testsuite/g++.dg/cpp2a/no_unique_address8.C | 11 +++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index b28fde7b4b8..ea432cc61bd 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -3303,7 +3303,9 @@ get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
     }
   else
     {
-      gcc_assert (TREE_CODE (index) == FIELD_DECL);
+      gcc_assert (TREE_CODE (index) == FIELD_DECL
+		  && (same_type_ignoring_top_level_qualifiers_p
+		      (DECL_CONTEXT (index), TREE_TYPE (ctor))));
 
       /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
 	 Usually we meet initializers in that order, but it is
@@ -4901,6 +4903,18 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
       type = refs->pop();
       tree index = refs->pop();
 
+      if (TREE_CODE (index) == FIELD_DECL
+	  && !(same_type_ignoring_top_level_qualifiers_p
+	       (DECL_CONTEXT (index), TREE_TYPE (*valp))))
+	{
+	  /* INDEX isn't a member of *valp.  This can happen if it's a member
+	     of an empty base which isn't represented with a FIELD_DECL.  Stop
+	     trying to build a CONSTRUCTOR for the inner target; we'll notice
+	     this disconnect again below and just return init.  */
+	  gcc_assert (is_empty_class (DECL_CONTEXT (index)));
+	  break;
+	}
+
       if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
 	  && CONSTRUCTOR_ELT (*valp, 0)->index != index)
 	{
diff --git a/gcc/testsuite/g++.dg/cpp2a/no_unique_address8.C b/gcc/testsuite/g++.dg/cpp2a/no_unique_address8.C
new file mode 100644
index 00000000000..8b63ca37bab
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/no_unique_address8.C
@@ -0,0 +1,11 @@
+// PR c++/98463
+// { dg-do compile { target c++11 } }
+
+template <typename T> struct A { constexpr A () : a() {} [[no_unique_address]] T a; };
+template <unsigned long, typename...> struct B;
+template <unsigned long T, typename U, typename... V>
+struct B<T, U, V...> : B<1, V...>, A<U> {};
+template <unsigned long T, typename U> struct B<T, U> : A<U> {};
+template <typename... h> struct C : B<0, h...> {};
+struct D {};
+struct E { C<int, D> k; virtual ~E (); } a;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-01-29 16:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-29 16:00 [gcc r10-9314] c++: [[no_unique_address]] in empty base [PR98463] 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).