From patchwork Wed Sep 2 12:02:25 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 142703 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2C77B4BA79B1 for ; Wed, 2 Sep 2026 12:03:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C77B4BA79B1 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=JQMgymoW X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id EAA544BA2E21 for ; Wed, 2 Sep 2026 12:02:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EAA544BA2E21 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EAA544BA2E21 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788350559; cv=none; b=MugGFF80JuqFD2HnQt0HOMMzWtUDQoE/U7z6aQgS1qadNyuBnbxVbC3U5BOUqhcwEM1dvZvKth3Xtkg09tVnxUXNF6s/Z79r+Ga5hth2CoSdk5O5XabY0ZbvKvzQ/NHJDLJpjqnF/FcZ/wZ/beey/QNpSkvScCdkq2JmY2H9+b4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788350559; c=relaxed/simple; bh=FD8TntNRrW8y5iXkTcsE8XJ+YybVVb8a7yS7ECzbmKE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=PLSN7oqb176EqCiLsEp3PeH7+jDDKkemcE2zEYjCdIC2FeMH+yhuT8O4CaziLvJkMHQvC7qUatY2LYwZEvsYhv856oYkmPr3fXT84jXOLhZUeIl9JquX5SvlEpxSIFuoYjRpkI1CFuruDZbdj4277wygUb8OSC8PkJYUjt8NYh4= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=JQMgymoW DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EAA544BA2E21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1788350558; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=8zb8b2LdPO6+iUv91xHZUEmFmr94nOdd/zDrnwM9Fsw=; b=JQMgymoWPtDcCIwyV7aSifUr2DvgWI3SbQjGS3SmPuYqljIQgOHquoR55f75Fjz+5z+2cp PrcwWP+q5K4uDv/7XpXBI+k9ooOr25LyZuyXAco65CDVvzux6VOfcqr4Z8cnt8MmJ+FTnf nZYtBGM6FY7a5IxCnC+DRWVzwo0M+70= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-687-tOXLEC-5ML2uavQMfWLjJw-1; Wed, 02 Sept 2026 08:02:36 -0400 X-MC-Unique: tOXLEC-5ML2uavQMfWLjJw-1 X-Mimecast-MFC-AGG-ID: tOXLEC-5ML2uavQMfWLjJw_1788350555 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 835C719540D0 for ; Wed, 2 Sep 2026 12:02:35 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.44.33.31]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E75A9195604A; Wed, 2 Sep 2026 12:02:34 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.18.1/8.18.1) with ESMTPS id 682C2UnY003382 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 2 Sep 2026 14:02:30 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.18.1/8.18.1/Submit) id 682C2R3w003379; Wed, 2 Sep 2026 14:02:27 +0200 Date: Wed, 2 Sep 2026 14:02:25 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v2: Fix up constexpr structured bindings used as condition [PR127109] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 3zl8SpUGtIT7qyu8YHRddWJj1kgP8AK3zT-LenosmAk_1788350555 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SBL_CSS, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org On Tue, Sep 01, 2026 at 06:52:52PM -0400, Jason Merrill wrote: > But now you have a TARGET_EXPR outside of any CLEANUP_POINT_EXPR. Since > you're effectively declaring a non-temporary variable, better to do that > directly rather than with a TARGET_EXPR, like the get_temp_regvar change I > made for the cleanup guards. Ok, that works too, though I had to tweak it a little bit. As described on the DECL_DECOMP_BASE macro, the current behavior was NULL_TREE -> this is a base var VAR_DECL -> this is a structured binding and the VAR_DECL is the base var corresponding to it INTEGER_CST -> this is a base var of sb used as condition before cp_finish_decomp (0 for if/while/for, 1 for switch) TARGET_EXPR -> base of sb used as condition after cp_finish_decomp Now, when we don't have a TARGET_EXPR, storing there just the plain get_temp_regvar result wouldn't work, it wouldn't be distinguishable from non-base var. So, I went with NON_LVALUE_EXPR around it. Lightly tested so far, ok for trunk if it passes full bootstrap/regtest? 2026-09-02 Jakub Jelinek PR c++/127109 * decl.cc (cp_finish_decomp): Add [dcl.struct.bind]/7 reference to comment. Use get_temp_regvar and pushdecl instead of get_internal_target_expr and add_stmt. Set DECL_DECOMP_BASE to NON_LVALUE_EXPR around the temp regvar. * cp-tree.h (DECL_DECOMP_BASE): Adjust macro comment. * semantics.cc (maybe_convert_cond, switch_finish_cond): Check for NON_LVALUE_EXPR rather than TARGET_EXPR and extract its operand rather than TARGET_EXPR_SLOT. * g++.dg/cpp26/decomp32.C: New test. Jakub --- a/gcc/cp/decl.cc 2026-09-02 10:05:24.578779537 +0200 +++ b/gcc/cp/decl.cc 2026-09-02 13:41:20.670427577 +0200 @@ -11105,7 +11105,10 @@ cp_finish_decomp (tree decl, cp_decomp * /* For structured bindings used in conditions we need to evaluate the conversion of decl (aka e in the standard) to bool or integral/enumeral type (the latter for switch conditions) - before the get methods. */ + before the get methods, as [dcl.struct.bind]/7 requires that: + "The initialization of e and any conversion of e considered as + a decision variable is sequenced before the initialization of + any r_i." */ tree cond = convert_from_reference (decl); if (integer_onep (DECL_DECOMP_BASE (decl))) /* switch condition. */ @@ -11116,12 +11119,14 @@ cp_finish_decomp (tree decl, cp_decomp * cond = contextual_conv_bool (cond, tf_warning_or_error); if (cond && !error_operand_p (cond)) { - /* Wrap that value into a TARGET_EXPR, emit it right - away and save for later uses in the cp_parse_condition - or its instantiation. */ - cond = get_internal_target_expr (cond); - add_stmt (cond); - DECL_DECOMP_BASE (decl) = cond; + cond = get_temp_regvar (TREE_TYPE (cond), cond); + pushdecl (cond); + /* Set DECL_DECOMP_BASE to cond VAR_DECL wrapped in + NON_LVALUE_EXPR, such that it is considered to be + the condition of a structured binding rather than + structured binding's base variable. */ + DECL_DECOMP_BASE (decl) + = build1 (NON_LVALUE_EXPR, TREE_TYPE (cond), cond); } } int save_read = DECL_READ_P (decl); --- a/gcc/cp/cp-tree.h 2026-09-02 10:05:24.575950822 +0200 +++ b/gcc/cp/cp-tree.h 2026-09-02 12:58:54.936847764 +0200 @@ -4845,7 +4845,7 @@ get_vec_init_expr (tree t) /* The underlying artificial VAR_DECL for structured binding. On the artificial base VAR_DECL this can be NULL, or integer_{zero,one}_node for structured binding used in if/while/for resp. switch conditions, - or a TARGET_EXPR with the condition value after cp_finish_decomp in + or a NON_LVALUE_EXPR with the condition value after cp_finish_decomp in those cases. */ #define DECL_DECOMP_BASE(NODE) \ (LANG_DECL_DECOMP_CHECK (NODE)->base) --- a/gcc/cp/semantics.cc 2026-09-02 10:05:24.579779524 +0200 +++ b/gcc/cp/semantics.cc 2026-09-02 13:50:41.510182195 +0200 @@ -1139,12 +1139,12 @@ maybe_convert_cond (tree cond) /* For structured binding used in condition, the conversion needs to be evaluated before the individual variables are initialized in the std::tuple_{size,element} case. cp_finish_decomp saved the conversion - result in a TARGET_EXPR, pick it up from there. */ + result in a NON_LVALUE_EXPR, pick it up from there. */ if (DECL_DECOMPOSITION_P (cond) && DECL_DECOMP_IS_BASE (cond) && DECL_DECOMP_BASE (cond) - && TREE_CODE (DECL_DECOMP_BASE (cond)) == TARGET_EXPR) - cond = TARGET_EXPR_SLOT (DECL_DECOMP_BASE (cond)); + && TREE_CODE (DECL_DECOMP_BASE (cond)) == NON_LVALUE_EXPR) + cond = TREE_OPERAND (DECL_DECOMP_BASE (cond), 0); if (warn_sequence_point && !processing_template_decl) verify_sequence_points (cond); @@ -1932,12 +1932,12 @@ finish_switch_cond (tree cond, tree swit /* For structured binding used in condition, the conversion needs to be evaluated before the individual variables are initialized in the std::tuple_{size,element} case. cp_finish_decomp saved the - conversion result in a TARGET_EXPR, pick it up from there. */ + conversion result in a NON_LVALUE_EXPR, pick it up from there. */ if (DECL_DECOMPOSITION_P (cond) && DECL_DECOMP_IS_BASE (cond) && DECL_DECOMP_BASE (cond) - && TREE_CODE (DECL_DECOMP_BASE (cond)) == TARGET_EXPR) - cond = TARGET_EXPR_SLOT (DECL_DECOMP_BASE (cond)); + && TREE_CODE (DECL_DECOMP_BASE (cond)) == NON_LVALUE_EXPR) + cond = TREE_OPERAND (DECL_DECOMP_BASE (cond), 0); cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true); if (cond == NULL_TREE) { --- a/gcc/testsuite/g++.dg/cpp26/decomp32.C 2026-09-02 12:51:11.198952436 +0200 +++ b/gcc/testsuite/g++.dg/cpp26/decomp32.C 2026-09-02 12:51:11.198952436 +0200 @@ -0,0 +1,33 @@ +// PR c++/127109 +// { dg-do compile { target c++14 } } +// { dg-options "" } + +namespace std { + using size_t = decltype (sizeof 0); + template struct tuple_size; + template struct tuple_element; +} + +struct A { + int a, b; + bool c; + constexpr explicit operator bool () const { return c; } + template + constexpr const int &get () const { return I == 0 ? a : b; } +}; + +template <> +struct std::tuple_size { static constexpr int value = 2; }; +template +struct std::tuple_element { using type = const int; }; + +constexpr int +foo (int v) +{ + if (auto [a, b] = A { v, v * 2, v != 0 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } } + return a + b; + return -1; +} + +static_assert (foo (1) == 3); +static_assert (foo (0) == -1);