From patchwork Wed Apr 8 06:01:46 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 132787 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id C0C394BA2E0E for ; Wed, 8 Apr 2026 06:02:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0C394BA2E0E 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=G274MscH 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.133.124]) by sourceware.org (Postfix) with ESMTP id E15CA4BA5436 for ; Wed, 8 Apr 2026 06:01:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E15CA4BA5436 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 E15CA4BA5436 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775628114; cv=none; b=uLzXKROAAlMRcy0VsolaUMS5ShDPdVwiLp5dC9QCUcsNyaH+AcJl3oFr2Cu3+/pgwTMV+Y+RgPMAGzEZzuPSAKbSDPsVSys25eua7YYgm66CxkgxAS6paG0lmhA3CfFH4R6fIqbMEVYa4BNNpMuT44n5Ccu8vj6aZ2ANnWdVdXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775628114; c=relaxed/simple; bh=g3txNN/720PEVzv5tQo+85LMiPX2ittVw3obwbRsL58=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=GEyN2jtc1Kpv61G8HoKiu/eJkBsZNzU1Qb7R7s9odBIacoFFqcGRnjgpD0okmhlxyG9JDo49kAjaSmKlDF/HagZmIrx/xQtq8SoT9yjOwrQvfBZr603RpSOBxliCSkck9BYfkhUzAOmHCLvp2zG8GoQIdTFG8nir3IoI3vhdF5c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E15CA4BA5436 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1775628113; 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; bh=qhPbibcusCTG+U3zptFWNUlV13SaznLhNnQt/mztF1w=; b=G274MscHLr7hLDKCsX+ufdFrBakV4fOT228P7qWx3vDZwyiGmDfqvEedw+RoRBrJL1mAmD 2JtMa+gN2r4jW4PFP7JIoJ6bHmWcYvHLAAlap1CHnuSklUTIwB61TaXx9+sP6lCGr/G5bL kXkYK3a0CVlg1GIK4kK8aPMUnU1Ynr8= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-512-8oIMNZSMM06KehX6vGZcEA-1; Wed, 08 Apr 2026 02:01:51 -0400 X-MC-Unique: 8oIMNZSMM06KehX6vGZcEA-1 X-Mimecast-MFC-AGG-ID: 8oIMNZSMM06KehX6vGZcEA_1775628111 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D4D0A1800464 for ; Wed, 8 Apr 2026 06:01:50 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.44.48.95]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 36BF019560B1; Wed, 8 Apr 2026 06:01:50 +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 63861l5b630458 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 8 Apr 2026 08:01:47 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.18.1/8.18.1/Submit) id 63861lBD630457; Wed, 8 Apr 2026 08:01:47 +0200 Date: Wed, 8 Apr 2026 08:01:46 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Implement CWG3149 - Rvalues in destructuring expansion statements Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 51fTVSQfMRoXbtpD9UkDpEF0sul3wW8ALkml0qAKUo8_1775628111 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.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_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham 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 Hi! The following patch implements CWG3149. If expansion_init is not an lvalue in destructiring expansion stmt, init is wrapped in a static_cast to decltype(u_i)&&. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2026-04-08 Jakub Jelinek * pt.cc (finish_expansion_stmt): Implement CWG3149 - Rvalues in destructuring expansion statements. For esk_destructuring, if expansion_init is not lvalue_p, wrap init into a static_cast to decltype of destruct_decls[i]. * g++.dg/cpp26/expansion-stmt39.C: New test. * g++.dg/reflect/cwg3149.C: New test. Jakub --- gcc/cp/pt.cc.jj 2026-04-06 23:10:37.311524244 +0200 +++ gcc/cp/pt.cc 2026-04-07 09:27:33.149372737 +0200 @@ -33313,6 +33313,7 @@ finish_expansion_stmt (tree expansion_st location_t loc = DECL_SOURCE_LOCATION (range_decl); tree begin = NULL_TREE, begin_minus_begin_type = NULL_TREE; auto_vec destruct_decls; + bool is_lvalue = false; if (BRACE_ENCLOSED_INITIALIZER_P (expansion_init)) { /* Enumerating expansion statements. */ @@ -33375,6 +33376,7 @@ finish_expansion_stmt (tree expansion_st if (sz < 0) return; n = sz; + is_lvalue = lvalue_p (expansion_init); tree auto_node = make_auto (); tree decomp_type = cp_build_reference_type (auto_node, true); decomp_type = do_auto_deduction (decomp_type, expansion_init, auto_node); @@ -33501,6 +33503,18 @@ finish_expansion_stmt (tree expansion_st break; case esk_destructuring: init = convert_from_reference (destruct_decls[i]); + if (!is_lvalue) + { + tree ctype; + if (DECL_HAS_VALUE_EXPR_P (destruct_decls[i])) + ctype = unlowered_expr_type (destruct_decls[i]); + else + ctype = lookup_decomp_type (destruct_decls[i]); + ctype = cp_build_reference_type (ctype, /*rval=*/true); + init = build_static_cast (loc, ctype, init, + tf_warning_or_error); + init = convert_from_reference (init); + } break; default: gcc_unreachable (); --- gcc/testsuite/g++.dg/cpp26/expansion-stmt39.C.jj 2026-04-07 09:49:42.588856656 +0200 +++ gcc/testsuite/g++.dg/cpp26/expansion-stmt39.C 2026-04-07 09:54:56.389542680 +0200 @@ -0,0 +1,16 @@ +// CWG3149 - Rvalues in destructuring expansion statements +// { dg-do compile { target c++26 } } + +#include +#include + +std::tuple foo (); + +void +bar () +{ + template for (auto &&elem : foo ()) + static_assert (std::is_same_v , long long> + ? std::is_same_v + : std::is_same_v ); +} --- gcc/testsuite/g++.dg/reflect/cwg3149.C.jj 2026-04-07 09:40:10.554543795 +0200 +++ gcc/testsuite/g++.dg/reflect/cwg3149.C 2026-04-07 09:39:07.591610057 +0200 @@ -0,0 +1,17 @@ +// CWG3149 - Rvalues in destructuring expansion statements +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +#include +#include + +std::tuple foo (); + +void +bar () +{ + template for (auto &&elem : foo ()) + static_assert (remove_reference (type_of (^^elem)) == ^^long long + ? type_of (^^elem) == ^^long long & + : type_of (^^elem) == ^^int &&); +}