{"id":2232367,"url":"http://patchwork.ozlabs.org/api/patches/2232367/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/afiFkPUdHhkIz_Jx@tucnak/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<afiFkPUdHhkIz_Jx@tucnak>","list_archive_url":null,"date":"2026-05-04T11:40:00","name":"[RFC,WIP] c++: Implement C++26 P3074R7 trivial unions","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"720facb76bd4bed80fb57c06bb86af110ff49e17","submitter":{"id":671,"url":"http://patchwork.ozlabs.org/api/people/671/?format=json","name":"Jakub Jelinek","email":"jakub@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/afiFkPUdHhkIz_Jx@tucnak/mbox/","series":[{"id":502650,"url":"http://patchwork.ozlabs.org/api/series/502650/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=502650","date":"2026-05-04T11:40:00","name":"[RFC,WIP] c++: Implement C++26 P3074R7 trivial unions","version":1,"mbox":"http://patchwork.ozlabs.org/series/502650/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2232367/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2232367/checks/","tags":{},"related":[],"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=TdL5FsO3;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=TdL5FsO3","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","server2.sourceware.org;\n arc=none smtp.remote-ip=170.10.129.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g8KTR0lzSz1y04\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 04 May 2026 21:40:41 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id ABCD34BAE7C4\n\tfor <incoming@patchwork.ozlabs.org>; Mon,  4 May 2026 11:40:39 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n by sourceware.org (Postfix) with ESMTP id 12A804BAD151\n for <gcc-patches@gcc.gnu.org>; Mon,  4 May 2026 11:40:07 +0000 (GMT)","from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-79-PhA8v71sOMWuWMgJ2W4gXg-1; Mon,\n 04 May 2026 07:40:05 -0400","from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n (No client certificate requested)\n by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id 7B2C119560AA; Mon,  4 May 2026 11:40:04 +0000 (UTC)","from tucnak.zalov.cz (unknown [10.44.32.15])\n by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with\n ESMTPS\n id E96341800480; Mon,  4 May 2026 11:40:03 +0000 (UTC)","from tucnak.zalov.cz (localhost [127.0.0.1])\n by tucnak.zalov.cz (8.18.1/8.18.1) with ESMTPS id 644Be1gH2260075\n (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT);\n Mon, 4 May 2026 13:40:01 +0200","(from jakub@localhost)\n by tucnak.zalov.cz (8.18.1/8.18.1/Submit) id 644Be04O2260074;\n Mon, 4 May 2026 13:40:00 +0200"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org ABCD34BAE7C4","OpenDKIM Filter v2.11.0 sourceware.org 12A804BAD151"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 12A804BAD151","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 12A804BAD151","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777894807; cv=none;\n b=YZ/JJUH/dLLu7KjimsA+qh6oNitK0pggMjYWIgFp8bz5kkr/b9xmF1p1DzFDL6yJ+nSzS350AjDJf0561spL6retbfBSUDNWU1E/uvnzdZMIaCRgLpHQezRzuCUN4Ipeqqe0A8iyZdZKf4CeH48qIv+W5zpDJRqmAIqoU6uudRc=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777894807; c=relaxed/simple;\n bh=8851zQLgApjTdLr/hEef0AgrnhWJA8q36XcmKIhR6sw=;\n h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version;\n b=IABkKXDjELvpWo6pM9440D9cxTBkuNKEpdJ+jcm+o6FjMPHT28l6cj4j31dNIyaIagI/72QK7sRUfby0rPNJld3gmyO9kEvsjLrbUVEQCaUOD8GNaU+mcHGqB8jAqfxgwb3Z+RvrHVha0M/o4u4oavOrAr3whpenfeOOmWZpkT0=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1777894806;\n h=from:from:reply-to:reply-to:subject:subject:date:date:\n message-id:message-id:to:to:cc:cc:mime-version:mime-version:\n content-type:content-type:\n content-transfer-encoding:content-transfer-encoding;\n bh=5c5B9/oK1TABj8tHtaxqS2XXSxqIaGxVjQ+NoXjL0tc=;\n b=TdL5FsO33cbhAhYhJzqqVWrATqjhHgHvPaeCc4aCrG0dx6jUQpffSEnDukpb7gPTXvjJvq\n w1rfs+ziHvXaOEd5sva4//VQ3tPVUoce6Az98L4pt6gMDIjVFqS1OkFj8o+ChOCyLjI6bT\n VPRfMuhlMnJxOJBgpMH+kD8ak+KbUgM=","X-MC-Unique":"PhA8v71sOMWuWMgJ2W4gXg-1","X-Mimecast-MFC-AGG-ID":"PhA8v71sOMWuWMgJ2W4gXg_1777894804","Date":"Mon, 4 May 2026 13:40:00 +0200","From":"Jakub Jelinek <jakub@redhat.com>","To":"Jason Merrill <jason@redhat.com>,\n Tomasz =?utf-8?b?S2FtacWEc2tp?= <tkaminsk@redhat.com>","Cc":"gcc-patches@gcc.gnu.org, Barry Revzin <barry.revzin@gmail.com>","Subject":"[RFC WIP PATCH] c++: Implement C++26 P3074R7 trivial unions","Message-ID":"<afiFkPUdHhkIz_Jx@tucnak>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.4.1 on 10.30.177.93","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"PsWHC6cFUGLHG3KEmQ8leJMjVt6xUteuOmxT2VthG8Q_1777894804","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Reply-To":"Jakub Jelinek <jakub@redhat.com>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"Hi!\n\nSo, I've started working on the P3074R7 trivial unions paper except for\nthat [class.default.ctor]/4 long sentence that P3726R2 removed again.\nI hope I'm missing something, but the more I look into this the more\nproblems I see.\n\nThe paper contains:\n\"which wasn’t particularly surprising since this change is entirely about making\nexisting ill-formed code valid\" but that doesn't seem to be the case\nto me, see below.\n\nOne small nit first, I don't understand e.g. the\nhttps://eel.is/c++draft/class.default.ctor#2.2 addition of\n\"X is a non-union class and\", because the bullet only talks about\nnon-variant non-static data members, aren't all non-static data members\nin a union already variant members and so the addition doesn't change\nanything?\n\nMore importantly, defaulted dtor would be previously deleted if it had some\nvariant subobject with deleted or inaccessible or non-trivial dtor, my\nunderstanding is that the paper wants to intentionally limit that to variant\nmembers with default initializer, but\nhttps://eel.is/c++draft/class.mem#class.dtor-7.2.2\ndoesn't seem to do that.  For one, it doesn't apply to variant members in\nanonymous unions because it only talks about union classes, so I believe\nstruct A { A (int); ~A () = delete; int a; };\nstruct B { union { A a = 42; int b; }; };\nB::~B () per the new rules isn't deleted.  And for unions, guess whether\nthe subobject with default initializer and deleted dtor is now covered by\nhttps://eel.is/c++draft/class.mem#class.dtor-7.2.1 because in that case\nthe ctor due to https://eel.is/c++draft/class.default.ctor#2.4 will be\ndeleted, but that checks for accessibility of the subobject dtor in the\ndefault ctor, not in the dtor.\n\nMost importantly, the https://eel.is/c++draft/class.mem#class.dtor-7.2.1\nnew rule doesn't seem to match the\n\"entirely about making existing ill-formed code valid\" comment, I believe\nunion C { int a = 42; long b; } u;\nused to be valid in C++11 to C++23, but in my understanding is now invalid.\nThere is a variant member, so https://eel.is/c++draft/class.default.ctor#3.2\nsays that C::C () is non-trivial, and then\nhttps://eel.is/c++draft/class.mem#class.dtor-7.2.1 says that if the default\nctor is non-trivial, defaulted C::~C () is defined as deleted.\nWas that intentional?\n\nThe patch below drops the default_init_uninitialized_part check in\nall variant members because already C++11 had there the non-variant case,\nthe second hunk doesn't implement the paper exactly as written (see the\nmore importantly point above), as it doesn't differentiate between the\nsfk_destructor case due to dtor_from_ctor (that would look like what\nit writes right now) but if !dtor_from_ctor with the current wording it\nwould need to limit itself just to the toplevel unions (not anonymous\nunions) and somehow check if the corresponding subobj dtor is non-trivial\nwithout checking if it is deleted or doesn't exist (how?).\n\nThe synthesized_method_walk hunk I believe implements\nhttps://eel.is/c++draft/class.mem#class.dtor-7.2.1 as written with the\nabove mentioned implications.\n\nRegarding P3726R2, guess we want some type generic builtin\n(__builtin_start_lifetime ?) which will be used in std::start_lifetime\ntemplate and at constant evaluation time will set/switch active union\nmember (what should happen if some other union member is active and there\nis non-trivial dtor?).  Plus some tweaks on what is valid constant\nexpression for C++26 in (possibly multi-dimensional) arrays inside of\nunions.\n\n\n\tJakub","diff":"--- gcc/c-family/c-cppbuiltin.cc.jj\t2026-04-22 15:03:36.485207851 +0200\n+++ gcc/c-family/c-cppbuiltin.cc\t2026-05-04 11:13:26.546729223 +0200\n@@ -1122,6 +1122,7 @@ c_cpp_builtins (cpp_reader *pfile)\n \t    cpp_define (pfile, \"__cpp_impl_reflection=202603L\");\n \t  else\n \t    cpp_warn (pfile, \"__cpp_impl_reflection\");\n+\t  cpp_define (pfile, \"__cpp_trivial_union=202603L\");\n \t}\n       if (flag_concepts && cxx_dialect > cxx14)\n \tcpp_define (pfile, \"__cpp_concepts=202002L\");\n--- gcc/cp/method.cc.jj\t2026-05-04 09:08:58.427717395 +0200\n+++ gcc/cp/method.cc\t2026-05-04 12:33:01.324557261 +0200\n@@ -2771,6 +2771,7 @@ walk_field_subobs (tree fields, special_\n \n \t  bad = false;\n \t  if (CP_TYPE_CONST_P (mem_type)\n+\t      && TREE_CODE (ctx) != UNION_TYPE\n \t      && default_init_uninitialized_part (mem_type))\n \t    {\n \t      if (diag)\n@@ -2847,6 +2848,15 @@ walk_field_subobs (tree fields, special_\n       else\n \targtype = NULL_TREE;\n \n+      if (cxx_dialect >= cxx26 && TREE_CODE (ctx) == UNION_TYPE)\n+\t{\n+\t  if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)\n+\t    continue;\n+\n+\t  if (sfk == sfk_destructor && DECL_INITIAL (field) == NULL_TREE)\n+\t    continue;\n+\t}\n+\n       rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);\n \n       process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,\n@@ -2985,6 +2995,35 @@ synthesized_method_walk (tree ctype, spe\n       /* The synthesized method will call base dtors, but check complete\n \t here to avoid having to deal with VTT.  */\n       fnname = complete_dtor_identifier;\n+\n+      if (TREE_CODE (ctype) == UNION_TYPE\n+\t  && cxx_dialect >= cxx26\n+\t  && deleted_p)\n+\t{\n+\t  /* [class.dtor]/(7.2.1):\n+\t     A defaulted destructor for a class X is defined as deleted if  X\n+\t     is a union and\n+\t     -- overload resolution to select a constructor to\n+\t\tdefault-initialize an object of type X either fails or selects\n+\t\ta constructor that is either deleted or not trivial.  */\n+\t  tree ctor = locate_ctor (ctype);\n+\t  if (ctor == NULL_TREE)\n+\t    {\n+\t      *deleted_p = true;\n+\t      if (diag)\n+\t\tinform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),\n+\t\t        \"default constructor of %qT unusable\", ctype);\n+\t      return;\n+\t    }\n+\t  else if (!trivial_fn_p (ctor))\n+\t    {\n+\t      *deleted_p = true;\n+\t      if (diag)\n+\t\tinform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),\n+\t\t\t\"default constructor of %qT is not trivial\", ctype);\n+\t      return;\n+\t    }\n+\t}\n     }\n   else if (SFK_ASSIGN_P (sfk))\n     fnname = assign_op_identifier;\n--- gcc/testsuite/g++.dg/DRs/dr2581-1.C.jj\t2026-03-27 10:17:15.320311386 +0100\n+++ gcc/testsuite/g++.dg/DRs/dr2581-1.C\t2026-05-04 11:15:02.107141015 +0200\n@@ -94,7 +94,7 @@\n #undef __cpp_template_parameters\n #undef __cpp_template_template_args\t// { dg-warning \"undefining '__cpp_template_template_args'\" \"\" { target c++20 } }\n #undef __cpp_threadsafe_static_init\t// { dg-warning \"undefining '__cpp_threadsafe_static_init'\" \"\" { target c++20 } }\n-#undef __cpp_trivial_union\n+#undef __cpp_trivial_union\t\t// { dg-warning \"undefining '__cpp_trivial_union'\" \"\" { target c++26 } }\n #undef __cpp_unicode_characters\t\t// { dg-warning \"undefining '__cpp_unicode_characters'\" \"\" { target c++20 } }\n #undef __cpp_unicode_literals\t\t// { dg-warning \"undefining '__cpp_unicode_literals'\" \"\" { target c++20 } }\n #undef __cpp_user_defined_literals\t// { dg-warning \"undefining '__cpp_user_defined_literals'\" \"\" { target c++20 } }\n--- gcc/testsuite/g++.dg/DRs/dr2581-2.C.jj\t2026-04-22 15:03:36.485585053 +0200\n+++ gcc/testsuite/g++.dg/DRs/dr2581-2.C\t2026-05-04 11:15:29.961678074 +0200\n@@ -95,7 +95,7 @@\n #define __cpp_template_parameters 202502L\n #define __cpp_template_template_args 201611L\t// { dg-error \"'__cpp_template_template_args' redefined\" \"\" { target c++20 } }\n #define __cpp_threadsafe_static_init 200806L\t// { dg-error \"'__cpp_threadsafe_static_init' redefined\" \"\" { target c++20 } }\n-#define __cpp_trivial_union 202502L\n+#define __cpp_trivial_union 202603L\t\t// { dg-error \"'__cpp_trivial_union' redefined\" \"\" { target c++26 } }\n #define __cpp_unicode_characters 200704L\t// { dg-error \"'__cpp_unicode_characters' redefined\" \"\" { target c++17 } }\n #define __cpp_unicode_literals 200710L\t\t// { dg-error \"'__cpp_unicode_literals' redefined\" \"\" { target c++20 } }\n #define __cpp_user_defined_literals 200809L\t// { dg-error \"'__cpp_user_defined_literals' redefined\" \"\" { target c++20 } }\n--- gcc/testsuite/g++.dg/cpp26/feat-cxx26.C.jj\t2026-03-27 10:17:15.654305935 +0100\n+++ gcc/testsuite/g++.dg/cpp26/feat-cxx26.C\t2026-05-04 11:14:08.680028970 +0200\n@@ -652,3 +652,9 @@\n #elif __cpp_expansion_statements != 202506\n #  error \"__cpp_expansion_statements != 202506\"\n #endif\n+\n+#ifndef __cpp_trivial_union\n+#  error \"__cpp_trivial_union\"\n+#elif __cpp_trivial_union != 202603\n+#  error \"__cpp_trivial_union != 202603\"\n+#endif\n--- gcc/testsuite/g++.dg/cpp26/trivial-union1.C.jj\t2026-05-04 11:17:47.404393790 +0200\n+++ gcc/testsuite/g++.dg/cpp26/trivial-union1.C\t2026-05-04 12:43:42.852056550 +0200\n@@ -0,0 +1,37 @@\n+// P3074R7 - trivial unions (was std::uninitialized<T>)\n+// P3726R2 - Adjustments to Union Lifetime Rules\n+// { dg-do compile { target c++11 } }\n+\n+#include <type_traits>\n+\n+// These two were incorrectly deleted, except for C++26\n+// where the dtor is deleted.\n+union A { int a; const int b; };\n+static_assert (std::is_default_constructible <A>::value, \"\");\n+static_assert (std::is_trivially_default_constructible <A>::value, \"\");\n+struct B { int a; union { int b; const int c; }; };\n+static_assert (std::is_default_constructible <B>::value, \"\");\n+static_assert (std::is_trivially_default_constructible <B>::value, \"\");\n+// C::C() is incorrectly not deleted in C++11 to 23, but in C++26 it should\n+// not be deleted.\n+union C { const int a = 42; const long b; ~C (); };\n+#if __cpp_trivial_union >= 202502L\n+static_assert (std::is_default_constructible <C>::value, \"\");\n+static_assert (!std::is_trivially_default_constructible <C>::value, \"\");\n+#endif\n+struct D { D () = delete; D (int); ~D () = default; };\n+union E { D a = 42; D b; ~E (); };\n+static_assert (std::is_default_constructible <E>::value, \"\");\n+struct F { int a; union { D b = 42; D c; }; };\n+static_assert (std::is_default_constructible <F>::value, \"\");\n+struct G { G (); ~G (); };\n+struct H { H (int); ~H (); int h; protected: H () = default; };\n+union I { int a; const int b; ~I (); };\n+static_assert (std::is_default_constructible <I>::value, \"\");\n+static_assert (!std::is_trivially_default_constructible <I>::value, \"\");\n+union J { D a; int b; };\n+#if __cpp_trivial_union >= 202502L\n+static_assert (std::is_default_constructible <J>::value, \"\");\n+#else\n+static_assert (!std::is_default_constructible <J>::value, \"\");\n+#endif\n","prefixes":["RFC","WIP"]}