From patchwork Sat Mar 2 00:58:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1907012 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gB8lSMF/; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tmmn06HVGz23fC for ; Sat, 2 Mar 2024 11:58:59 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CEFE9385840F for ; Sat, 2 Mar 2024 00:58:56 +0000 (GMT) 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 ESMTPS id D63FC3858D39 for ; Sat, 2 Mar 2024 00:58:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D63FC3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 D63FC3858D39 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=1709341116; cv=none; b=DSg0dPH6PVWHiRcDFvA+5zXxulGAJdQ1REiUdVjK2hRrjrbYzWFBKZ63PPGaHIdKuz558CImxevM2d1YA5Ye/Pe0cLzjtIcq63/zvVRQ4PZSYsTh5B92K1ltBfmUyHwpA+UPznySpMSTcFzrQVrcl0a/pPMDoYuiZqB+cAoi40U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709341116; c=relaxed/simple; bh=6BbzRWBrhA+3ObwoEiWa+KWofmrGr9cgp9N/qHW4FZI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=h7gd1kvuCieHwM6u5f3oI96IKZkiRk9n7te/64ngQw699Wpg6Qfn7+tNQmXLRVzj982Wmo+burGhjoJFBtWRcQPo8hS2cc6Njxxwz3adNYAa4f3udC0kNJWaa6KGu9PjV5yDb85NN4FNHI8GYZsTWS4dVIu0cEOZY66QVQ64Jvc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709341114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yppjgTAL4CFwPN9Vhi93Y6dox35/N0N6A++BVaa/8cg=; b=gB8lSMF/EiFhIpoNvMKjq+CBLd+e208xI1pH2l05Lq7ojGm53A/eWeSpRcqJO3kAH/Cb7D nrWbbJ6d7uJLc2yZD1DThDqqwoIXjLuG4k3PVQMLIBBmiOr2QZgdhvxk5wnopB/KP3i8Os 77lQIaFtV6QBe4fO5Jgy8xzxbkfmEOI= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-398-kTIxlFzePHuYdB9yITJpOw-1; Fri, 01 Mar 2024 19:58:33 -0500 X-MC-Unique: kTIxlFzePHuYdB9yITJpOw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id D02BC1C05190 for ; Sat, 2 Mar 2024 00:58:32 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id ADF80492BCA; Sat, 2 Mar 2024 00:58:32 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: lambda capturing structured bindings [PR85889] Date: Fri, 1 Mar 2024 19:58:24 -0500 Message-ID: <20240302005824.924155-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for 15? (Or even trunk?) -- >8 -- clarifies that it's OK to capture structured bindings. [expr.prim.lambda.capture]/4 says "The identifier in a simple-capture shall denote a local entity" and [basic.pre]/3: "An entity is a [...] structured binding". It doesn't appear that this was made a DR, so, strictly speaking, we should have a -Wc++20-extensions warning, like clang++. PR c++/85889 gcc/cp/ChangeLog: * lambda.cc (add_capture): Add a pedwarn for capturing structured bindings. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/decomp3.C: Use -Wno-c++20-extensions. * g++.dg/cpp1z/decomp60.C: New test. --- gcc/cp/lambda.cc | 9 +++++++++ gcc/testsuite/g++.dg/cpp1z/decomp60.C | 12 ++++++++++++ gcc/testsuite/g++.dg/cpp2a/decomp3.C | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/decomp60.C base-commit: 574fd1f17f100c7c355ad26bc525ab5a3386bb2d diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index 4b1f9391fee..470f9d2c4f1 100644 --- a/gcc/cp/lambda.cc +++ b/gcc/cp/lambda.cc @@ -607,6 +607,15 @@ add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p, TCTX_CAPTURE_BY_COPY, type)) return error_mark_node; } + + if (cxx_dialect < cxx20) + { + tree stripped_init = tree_strip_any_location_wrapper (initializer); + if (DECL_DECOMPOSITION_P (stripped_init) + && pedwarn (input_location, OPT_Wc__20_extensions, + "captured structured bindings are a C++20 extension")) + inform (DECL_SOURCE_LOCATION (stripped_init), "declared here"); + } } /* Add __ to the beginning of the field name so that user code diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp60.C b/gcc/testsuite/g++.dg/cpp1z/decomp60.C new file mode 100644 index 00000000000..b6117f3af58 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp60.C @@ -0,0 +1,12 @@ +// PR c++/85889 +// { dg-do compile { target c++17 } } +// { dg-options "" } + +struct X { int i, j; }; +void f() { + X x{}; + auto [i, j] = x; + [&i]() { }; // { dg-warning "captured structured bindings" "" { target c++17_only } } + [i]() { }; // { dg-warning "captured structured bindings" "" { target c++17_only } } +} + diff --git a/gcc/testsuite/g++.dg/cpp2a/decomp3.C b/gcc/testsuite/g++.dg/cpp2a/decomp3.C index 5e77973d41f..25ab8319975 100644 --- a/gcc/testsuite/g++.dg/cpp2a/decomp3.C +++ b/gcc/testsuite/g++.dg/cpp2a/decomp3.C @@ -1,6 +1,6 @@ // P1381R1 // { dg-do compile { target c++11 } } -// { dg-options "" } +// { dg-options "-Wno-c++20-extensions" } struct Foo { int a : 1; int b; };