[{"id":3679636,"web_url":"http://patchwork.ozlabs.org/comment/3679636/","msgid":"<986cbaf3-8958-e0dd-4e3b-9fe0425a730f@idea>","list_archive_url":null,"date":"2026-04-21T00:25:53","subject":"Re: [PATCH RFC] c++: constexpr union with no active member [PR124910]","submitter":{"id":78319,"url":"http://patchwork.ozlabs.org/api/people/78319/","name":"Patrick Palka","email":"ppalka@redhat.com"},"content":"On Mon, 20 Apr 2026, Jason Merrill wrote:\n\n> Tested x86_64-pc-linux-gnu.  Since this isn't a regression I'm inclined to\n> leave it for 17.\n> \n> -- 8< --\n> \n> Patrick pointed out that while r16-8767 made a union constant after\n> destroying its active member, we still weren't treating a union that never\n> had an active member as constant; the difference is the\n> CONSTRUCTOR_NO_CLEARING flag, and what that means to\n> reduced_constant_expression_p.\n> \n> It seems to me that since P2686 [expr.const] says whether a prvalue\n> expression is a constant expression depends on the constituent values, and\n> [intro.object] says that only the active member is a constituent value of a\n> union, so a union with no active member has no constituent values and so is\n> vacuously constant, like an object of empty type.  P2686 as a whole is not a\n> DR, but the draft was previously unclear, and CWG2658 also clarified that\n> copying a union is equivalent to copying the active member *if any*.\n> \n> I was somewhat surprised that none of the existing tests needed to be\n> changed.\n\nNice.  FWIW that logic was added in r10-7313 (by me apparently) just so\nthat we reject cpp1y/constexpr-union4.C.  Makes sense to leave it for\nGCC 17, I don't even think we have an existing PR about it.\n\n> \n> \tPR c++/124910\n> \tDR 2658\n> \n> gcc/cp/ChangeLog:\n> \n> \t* constexpr.cc (reduced_constant_expression_p): Allow a union\n> \twith no active member.\n> \n> gcc/testsuite/ChangeLog:\n> \n> \t* g++.dg/cpp2a/constexpr-union12.C: New test.\n> ---\n>  gcc/cp/constexpr.cc                            | 7 ++++---\n>  gcc/testsuite/g++.dg/cpp2a/constexpr-union12.C | 8 ++++++++\n>  2 files changed, 12 insertions(+), 3 deletions(-)\n>  create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-union12.C\n> \n> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc\n> index a593b66dd41..1926b64cbb7 100644\n> --- a/gcc/cp/constexpr.cc\n> +++ b/gcc/cp/constexpr.cc\n> @@ -4747,9 +4747,10 @@ reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)\n>  \t\t   && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)\n>  \t    {\n>  \t      if (CONSTRUCTOR_NELTS (t) == 0)\n> -\t\t/* An initialized union has a constructor element.  */\n> -\t\treturn false;\n> -\t      /* And it only initializes one member.  */\n> +\t\t/* A union with no active member has no constituent values, so\n> +\t\t   all constituent values are constant.  */\n> +\t\tgoto ok;\n> +\t      /* A union can have at most one active member.  */\n>  \t      field = NULL_TREE;\n>  \t    }\n>  \t  else\n> diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-union12.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-union12.C\n> new file mode 100644\n> index 00000000000..42ddfaaf775\n> --- /dev/null\n> +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-union12.C\n> @@ -0,0 +1,8 @@\n> +// PR c++/124910\n> +// { dg-do compile { target c++20 } }\n> +\n> +union U {\n> +  int n;\n> +  constexpr U() { }\n> +};\n> +constexpr U u; // { dg-bogus \"\" } U() is not a constant expression\n> \n> base-commit: 8e6b7c97232489557e376bdc134ad69aaeb35794\n> -- \n> 2.53.0\n> \n>","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=Pb5rJZUD;\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=Pb5rJZUD","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.133.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 4g037j692rz1yD8\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 21 Apr 2026 10:26:40 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 3DC434D108C5\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 21 Apr 2026 00:26:38 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.133.124])\n by sourceware.org (Postfix) with ESMTP id 47C864A98F0B\n for <gcc-patches@gcc.gnu.org>; Tue, 21 Apr 2026 00:26:00 +0000 (GMT)","from mail-qv1-f71.google.com (mail-qv1-f71.google.com\n [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n us-mta-608-RnBgvqT-NnWjOFUNp-u6_g-1; Mon, 20 Apr 2026 20:25:56 -0400","by mail-qv1-f71.google.com with SMTP id\n 6a1803df08f44-895375da74bso6320776d6.0\n for <gcc-patches@gcc.gnu.org>; Mon, 20 Apr 2026 17:25:56 -0700 (PDT)","from [2600:4040:aa66:bf00:9e8e:99ff:fed1:71f]\n ([2600:4040:aa66:bf00:9e8e:99ff:fed1:71f])\n by smtp.gmail.com with ESMTPSA id\n 6a1803df08f44-8b02ac78513sm90600536d6.19.2026.04.20.17.25.54\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Mon, 20 Apr 2026 17:25:54 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 3DC434D108C5","OpenDKIM Filter v2.11.0 sourceware.org 47C864A98F0B"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 47C864A98F0B","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 47C864A98F0B","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776731160; cv=none;\n b=ny+BdBq3A25dp3NnCf7oDxiQCeDkEDFPpaEs37G3QMs9wDxgpM21nb5XMlwVdqvC9+57CuU73AtZFAYBgeQwE9vBbgMmIlWVOLZtOaOJQpqc0sl8DhFkWgIvdJwr807bmNkLyNRkAeZ6O96hNd+sj/4XEWfS4ei7OS+SKNJt4ms=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776731160; c=relaxed/simple;\n bh=efDa2eSOWlI0h9kCz+iZ/4NgPF2rGkCkg+K7XKbdKjA=;\n h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version;\n b=qtG8mgOCmuClJvngGQTLDKssXvynhS1aP0+ffrs7ndAdkJwUWM5hpVgclBnrXqSqOAaqliP3us/Uoak5DNW4IVhWZyGfcp6k+7ILpq2UVpH00Ij3ZrxMFXcS1bcbyTmTmB7agYtZisqNG3ZPGPIf8hfabIbNMMa1SZGL4KMX5iA=","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=1776731159;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n in-reply-to:in-reply-to:references:references;\n bh=zgc0FeoY5HZnvSrRxy+kjsoVmEAs/U2Xt9MMRt/ztig=;\n b=Pb5rJZUDmSzeUDu87URiawezOkylANhZHFY57eGiC7tY9iQ4FKt4oYzEZlg1tnD5dZyTFJ\n AczWu8hLFjG2h89YPmFT2o16dghw3VYTNKo4jVgfhCOBQKLB+fK1tz1Qlnv41MD58/FFqJ\n dfgMtFxa8M7TJpUKdPmJ4Rj8yGTt62w=","X-MC-Unique":"RnBgvqT-NnWjOFUNp-u6_g-1","X-Mimecast-MFC-AGG-ID":"RnBgvqT-NnWjOFUNp-u6_g_1776731156","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1776731156; x=1777335956;\n h=mime-version:references:message-id:in-reply-to:subject:cc:to:date\n :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id\n :reply-to;\n bh=zgc0FeoY5HZnvSrRxy+kjsoVmEAs/U2Xt9MMRt/ztig=;\n b=DMrF2pj/2/RRMGmnaAfTaGwiBoUBoU/wq9yLFPpz1n3EUjdloY3I7EYlgC0nKrIxJj\n kc+xWngL6y3MlQbCljXda/J6a0OOhFL6FWItP9udJepGKVAJjTC/dD5fN8LKxdaCG4qD\n JYyRTzzfIAomCxdA8T9iZm1/2pJ2GqFyZxbbrMgxxdDMCDaxkamQiojFAazwOWFJiCrx\n uT5CO8+hNyQSDlHaY35jLt1fbVo+FBg1r1sEGw86fBZjdM+2iGVHnHGpJh7pj7V1WKCC\n ewdjIdyX/5ZmQYIVIOwgxaYY5IpowH+v39kVj2si/5IORh5YAIWcHZBUDqCcYl0TItcA\n a0qQ==","X-Gm-Message-State":"AOJu0Yy0+/qV2M93YPqXAfeUyMAfB1Rht+HMvdZw+k5YrL3XNkQEGK4l\n alBcr1fy64jHDp/8eHZ2mdWYbVmitsTS891LzG68myew1muS+yE2H82Z/s02ocol8Px8PwhgswZ\n k07DfA6R9iBxnoYSVHXBdInCaELzrc+23wVgeaSxw7FkPmL4Gp1R/ktuHDhc=","X-Gm-Gg":"AeBDietiksfjCvPsFVIvzWjsOoaPVomzD/Iu4+9WDQIPlqIqAu3n6YPpenDsOkFVq5z\n Lb5Q/bKz8bc+b61xVW+RgfNkpWATsivJpcyuozC//MnewRlpk0yOH6GG821lhm1RT/hKLg3luio\n ALYMDjtk0DyxR6RedXy9Obza199Q0DQLDTx1udL2C2hp4uq0oIGwb+AYVZlWY4aFtG7Bb9+RSez\n v3v9c1rk/zfaT8Vqf0O1WM156cv3QL+Bpo+DONj5/2dl7WgChHCh2LsPgSsi7RchWK0MrUabUgx\n fDVj3vK5hDuDPcX2sZL1PcoBbOil50TWyvB938bmNVP/CRBE/YEHo8MlfeLt8dCEVS3gObNhtuI\n 7v3SIyu6kld+hxyjyUqg9joNF4klXNF29PY/YuzvwNdV8YgJlLgXrq3IFhKCdFCJL","X-Received":["by 2002:a05:6214:300c:b0:8ac:b4d5:50f0 with SMTP id\n 6a1803df08f44-8b028162b50mr174481636d6.7.1776731156011;\n Mon, 20 Apr 2026 17:25:56 -0700 (PDT)","by 2002:a05:6214:300c:b0:8ac:b4d5:50f0 with SMTP id\n 6a1803df08f44-8b028162b50mr174481266d6.7.1776731155457;\n Mon, 20 Apr 2026 17:25:55 -0700 (PDT)"],"From":"Patrick Palka <ppalka@redhat.com>","X-Google-Original-From":"Patrick Palka <patrick@idea>","Date":"Mon, 20 Apr 2026 20:25:53 -0400 (EDT)","To":"Jason Merrill <jason@redhat.com>","cc":"gcc-patches@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>","Subject":"Re: [PATCH RFC] c++: constexpr union with no active member [PR124910]","In-Reply-To":"<20260420215359.1773044-1-jason@redhat.com>","Message-ID":"<986cbaf3-8958-e0dd-4e3b-9fe0425a730f@idea>","References":"<20260420215359.1773044-1-jason@redhat.com>","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"lY2OTG6keEmw3elCTr8Nji_XBwX_ZmlMki3-fdmNw_U_1776731156","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=US-ASCII","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>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}}]