public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-1783] c++: defaulted ctor with DMI in union [PR94823]
Date: Thu, 21 Jul 2022 21:21:22 +0000 (GMT)	[thread overview]
Message-ID: <20220721212122.413043835699@sourceware.org> (raw)

https://gcc.gnu.org/g:df118d7ba138cacb17203d4a1b5f27730347cc77

commit r13-1783-gdf118d7ba138cacb17203d4a1b5f27730347cc77
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jul 20 18:15:31 2022 -0400

    c++: defaulted ctor with DMI in union [PR94823]
    
    CWG2084 clarifies that a variant member with a non-trivial constructor does
    not make the union's defaulted default constructor deleted if another
    variant member has a default member initializer.
    
            DR 2084
            PR c++/94823
    
    gcc/cp/ChangeLog:
    
            * method.cc (walk_field_subobs): Fix DMI in union case.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/nsdmi-union7.C: New test.

Diff:
---
 gcc/cp/method.cc                          | 35 +++++++++++++++++++++++++++----
 gcc/testsuite/g++.dg/cpp0x/nsdmi-union7.C | 13 ++++++++++++
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
index f2050f6e970..573ef016f82 100644
--- a/gcc/cp/method.cc
+++ b/gcc/cp/method.cc
@@ -2315,8 +2315,19 @@ walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
 		   bool diag, int flags, tsubst_flags_t complain,
 		   bool dtor_from_ctor)
 {
-  tree field;
-  for (field = fields; field; field = DECL_CHAIN (field))
+  if (!fields)
+    return;
+
+  tree ctx = DECL_CONTEXT (fields);
+
+  /* CWG2084: A defaulted default ctor for a union with a DMI only initializes
+     that member, so don't check other members.  */
+  enum { unknown, no, yes }
+  only_dmi_mem = (sfk == sfk_constructor && TREE_CODE (ctx) == UNION_TYPE
+		  ? unknown : no);
+
+ again:
+  for (tree field = fields; field; field = DECL_CHAIN (field))
     {
       tree mem_type, argtype, rval;
 
@@ -2331,9 +2342,18 @@ walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
 	 asking if this is deleted, don't even look up the function; we don't
 	 want an error about a deleted function we aren't actually calling.  */
       if (sfk == sfk_destructor && deleted_p == NULL
-	  && TREE_CODE (DECL_CONTEXT (field)) == UNION_TYPE)
+	  && TREE_CODE (ctx) == UNION_TYPE)
 	break;
 
+      if (only_dmi_mem != no)
+	{
+	  if (DECL_INITIAL (field))
+	    only_dmi_mem = yes;
+	  else
+	    /* Don't check this until we know there's no DMI.  */
+	    continue;
+	}
+
       mem_type = strip_array_types (TREE_TYPE (field));
       if (SFK_ASSIGN_P (sfk))
 	{
@@ -2416,7 +2436,7 @@ walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
 	  if (constexpr_p
 	      && cxx_dialect < cxx20
 	      && !CLASS_TYPE_P (mem_type)
-	      && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
+	      && TREE_CODE (ctx) != UNION_TYPE)
 	    {
 	      *constexpr_p = false;
 	      if (diag)
@@ -2465,6 +2485,13 @@ walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
       process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
 			constexpr_p, diag, field, dtor_from_ctor);
     }
+
+  /* We didn't find a DMI in this union, now check all the members.  */
+  if (only_dmi_mem == unknown)
+    {
+      only_dmi_mem = no;
+      goto again;
+    }
 }
 
 /* Base walker helper for synthesized_method_walk.  Inspect a direct
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-union7.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-union7.C
new file mode 100644
index 00000000000..c840ddf5fbc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-union7.C
@@ -0,0 +1,13 @@
+// PR c++/94823
+// { dg-do compile { target c++11 } }
+
+struct A{
+  A(){}
+};
+union C{
+  A a;
+  int b = 0;
+};
+int main(){
+  C c;
+}


                 reply	other threads:[~2022-07-21 21:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220721212122.413043835699@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@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).