[{"id":3679499,"web_url":"http://patchwork.ozlabs.org/comment/3679499/","msgid":"<0283d9bc-ad1f-4f65-e95e-e403833d2782@idea>","list_archive_url":null,"date":"2026-04-20T17:39:53","subject":"Re: [pushed] c++: std::optional reset and constexpr [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, applying to trunk.\n> \n> -- 8< --\n> \n> Constant evaluation didn't recognize that destroying _M_value made it no\n> longer the active member of the anonymous union, so we were treating the\n> result as containing an out-of-lifetime value.  Instead we should treat the\n> union as no longer having an active member.\n> \n> \tPR c++/124910\n> \n> gcc/cp/ChangeLog:\n> \n> \t* constexpr.cc (cxx_eval_store_expression): Ending the\n> \tlifetime of the active member means no active member.\n> \n> gcc/testsuite/ChangeLog:\n> \n> \t* g++.dg/cpp2a/constexpr-union11.C: New test.\n> ---\n>  gcc/cp/constexpr.cc                           | 10 +++++++\n>  .../g++.dg/cpp2a/constexpr-union11.C          | 29 +++++++++++++++++++\n>  2 files changed, 39 insertions(+)\n>  create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-union11.C\n> \n> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc\n> index 5b7d385fa97..a593b66dd41 100644\n> --- a/gcc/cp/constexpr.cc\n> +++ b/gcc/cp/constexpr.cc\n> @@ -8207,6 +8207,16 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,\n>  \t  no_zero_init = true;\n>  \t}\n>  \n> +      /* Ending the lifetime of the active union member means the union no\n> +\t longer has an active member.  */\n> +      if (code == UNION_TYPE && refs->is_empty ()\n> +\t  && TREE_CLOBBER_P (init)\n> +\t  && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)\n> +\t{\n> +\t  vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);\n> +\t  return void_node;\n> +\t}\n> +\n>        ctors.safe_push (valp);\n>        vec_safe_push (indexes, index);\n>  \n> diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-union11.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-union11.C\n> new file mode 100644\n> index 00000000000..56919715f55\n> --- /dev/null\n> +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-union11.C\n> @@ -0,0 +1,29 @@\n> +// PR c++/124910\n> +// { dg-do compile { target c++20 } }\n> +\n> +struct nullopt_t { } nullopt;\n> +using _Stored_type = int;\n> +struct _Empty { };\n> +struct _Optional_payload_base {\n> +  union {\n> +    int _M_value = 1;\n> +    _Empty _M_empty;\n> +  };\n> +  constexpr void _M_reset() {\n> +    _M_value.~_Stored_type();\n> +    _M_empty = {};\n\nI think we should strengthen this testcase by removing the _M_empty\nassignment/activation?\n\nIt's curious that\n\n    union U {\n      int n;\n      constexpr U() { }\n    };\n    constexpr U u; // error: U() is not a constant expression\n\nis still rejected even though the roundabout\n\n    union U {\n      int n;\n      constexpr U() { n = 42; using type = int; n.~type(); }\n    };\n    constexpr U u;\n\nis now accepted.\n\n\n> +  }\n> +};\n> +struct _Optional_payload : _Optional_payload_base {};\n> +struct _Optional_base {\n> +  _Optional_payload _M_payload;\n> +};\n> +struct optional : _Optional_base {\n> +  constexpr void operator=(nullopt_t) { _M_payload._M_reset(); }\n> +};\n> +constexpr auto f() {\n> +  optional result;\n> +  result = nullopt;\n> +  return result;\n> +}\n> +constexpr auto v = f();\n> \n> base-commit: b40bb34c3f5e9942d083efdee393c6e2d25e5981\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=X8h6Q/5a;\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=X8h6Q/5a","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 4fzt7H5wBFz1yD4\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 21 Apr 2026 03:40:42 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id C61444B358A9\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 20 Apr 2026 17:40:39 +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 CE20F4BA2E29\n for <gcc-patches@gcc.gnu.org>; Mon, 20 Apr 2026 17:39:59 +0000 (GMT)","from mail-qk1-f197.google.com (mail-qk1-f197.google.com\n [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n us-mta-670-iS_G5lKrPqCI43qVFf04Sg-1; Mon, 20 Apr 2026 13:39:56 -0400","by mail-qk1-f197.google.com with SMTP id\n af79cd13be357-8cfe29579d5so46160985a.3\n for <gcc-patches@gcc.gnu.org>; Mon, 20 Apr 2026 10:39: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 af79cd13be357-8ebce6ef86dsm187607685a.30.2026.04.20.10.39.54\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Mon, 20 Apr 2026 10:39:54 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org C61444B358A9","OpenDKIM Filter v2.11.0 sourceware.org CE20F4BA2E29"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org CE20F4BA2E29","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org CE20F4BA2E29","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776706799; cv=none;\n b=ZupZtQEvqo809fGWBoDOHrQ9y695KATc6bSxTXCwm2Gg+rzhkLk/St0BLQWfkYRAP7DZM/h8AwJcTTcGOsXNmo2PRwFTyD5XV8n5uGvmKdrnxjD9N0qeIhDdwZgcu/frKcbMV6dlFMoxSScFVyKZ2EVkIHRqVuRHbI3GhP7TRvY=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776706799; c=relaxed/simple;\n bh=5IMtx27iOlwuefXUkn+iuUucbdqangy+5uMF4bCwPKw=;\n h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version;\n b=NBjmxN7oIdyW0mFRX2ssIeXWYWBlOWAfBjZg/tUNN5D3+kIPyAqYdZ6cYZeypRNt3vtMeGPgtETBv/1P5RA/Zh6pKniOF4NAFJE73+MkOj7dDQbU8zpOOpg4d7HvtvFOyi1DguJqZst/OCR8yMkILNRSKpUldOpYLxGB002dKBo=","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=1776706799;\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=WVshJarZyu9KztVOXbMAMZJdZ0kRA9kevEE6PzGKhEQ=;\n b=X8h6Q/5abuLYtD1HZ2sjokflWgMiMSBAWyDhGWi9eIw9zSicmYsL8ZnE0eIDnEImoSGBUk\n bf1PSsAuDYGTkr7w1uonF1xQM8bkYc7iGnqg7bRObldGeh3eyPVWEpDffZsTNKAIVEoQ1B\n TnPMyxEF8SwM1KWGVgasISX9zoLqXfE=","X-MC-Unique":"iS_G5lKrPqCI43qVFf04Sg-1","X-Mimecast-MFC-AGG-ID":"iS_G5lKrPqCI43qVFf04Sg_1776706796","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1776706796; x=1777311596;\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=WVshJarZyu9KztVOXbMAMZJdZ0kRA9kevEE6PzGKhEQ=;\n b=LGE0T0x6q3yCX+e5ZvI3VZxgwNWXc80IqBeyuw/YWckn9iUh5dUJ935jmlCVoMVuCW\n IxC8FpeQTH9VyTbWkjhHqmnlzzbVRwztwDIqa85pum+uj+ycOiaICpO8i9gzNDFoE9jo\n r0ZRmrfqFVGOkpnLkNGo8NBjgtAEpuqjTQUndsWTiDiThviJ1sH2zWGguDWg5xeNAkjm\n qj0d8DW+2Do0aWbZTsRj+yz4XaxkuMN4lk7yoIF971ok1DZ/5ZD2AyrXC5zvVoa2Qlkj\n H9szlph1ZMpyHgbbzdDfIdefqTSRHuOiIIqzYKv5yMkhm9NBEOqhcpUewztdthjBKGKc\n xsVg==","X-Gm-Message-State":"AOJu0YxSkG8k6w7ExJkSMic8RBh9SI+4zbb3dOnd3CwkaP8XA2bCU+TT\n 5ePJWapYJDFmtD8Y35mQjpX3B9ysuyi/OcZzONlpgYEYwTIip+WUOE9pAxO0CglMcPnx/DRO61h\n MJtmM6+Yzg56jcnusB/3x1x116F1lNlfWBqtEL1aRuBZHd6W94FCf2TaEA0k=","X-Gm-Gg":"AeBDieshrej48/pVDpVJVQR3MGztKdk6dgHkRAV7h0lc+kRD40px8SMF3fXGbGsfIcO\n FsjS56pkCSMHqkPhnf0PLh2FhbQ/BaFnqMLLSkv4t1Kg8Eo4JkqChK0Rnz8D65HaqPjw1MMyeFk\n WgdWCkM/p+Mxv4KDsJ1ZKXIQNk2KRJfglSAkgEn7o1KV067rwxt2M7bvakidFuHYKHlJcDJX9to\n 9NLzY0I1DuDkv3KwVkcjix/I3GE+mAWyVOGmGoYVh8XiD1f/6tP+dnwhkq/V1X5NV951W7+V6PB\n F0vMlT0cKBoP+0ws7F+mQ5/tXiYwgM0k8KB8ZjfVWoysixl42e/9hTnUuAk6dSUI7r2ewB26pdO\n he268CFNDvnvKhI5AbAM1sgRc1W9Bvb8N39xVn46p7dA9aycmfYh1PeHRgtFlOM0Q","X-Received":["by 2002:a05:620a:404a:b0:8d7:a2b3:3aaf with SMTP id\n af79cd13be357-8e78f24f73amr1349114685a.1.1776706795581;\n Mon, 20 Apr 2026 10:39:55 -0700 (PDT)","by 2002:a05:620a:404a:b0:8d7:a2b3:3aaf with SMTP id\n af79cd13be357-8e78f24f73amr1349112185a.1.1776706795014;\n Mon, 20 Apr 2026 10:39:55 -0700 (PDT)"],"From":"Patrick Palka <ppalka@redhat.com>","X-Google-Original-From":"Patrick Palka <patrick@idea>","Date":"Mon, 20 Apr 2026 13:39:53 -0400 (EDT)","To":"Jason Merrill <jason@redhat.com>","cc":"gcc-patches@gcc.gnu.org","Subject":"Re: [pushed] c++: std::optional reset and constexpr [PR124910]","In-Reply-To":"<20260420164552.1598895-1-jason@redhat.com>","Message-ID":"<0283d9bc-ad1f-4f65-e95e-e403833d2782@idea>","References":"<20260420164552.1598895-1-jason@redhat.com>","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"UsYcdXq3OogiV3cOnc9I2nxo1M4p_iLqXhJQRCW-J-o_1776706796","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"}}]