From patchwork Wed Nov 16 10:17:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 20871 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp60557wru; Wed, 16 Nov 2022 02:18:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf4LCZh+LxpplYj4UGIkwhkC8EvHwKyexSV/INdUuUi2OpYOrjjJup8vhGqqf3P3X8VvN0TF X-Received: by 2002:a17:906:c048:b0:7ae:d116:fabb with SMTP id bm8-20020a170906c04800b007aed116fabbmr15727488ejb.317.1668593913690; Wed, 16 Nov 2022 02:18:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668593913; cv=none; d=google.com; s=arc-20160816; b=jaSRaM6ap4Ay/qMs/cXPWeZEsyYe5z02xlbo94QZGe1qrPd6LRfMfUEmVzzwzeQgqF 833LJQF6fWmXFsMb3/MtJKBzqQerWVddkBlmRCP2/aR1FmbuNdF+NU/ChpQZy2f16KC8 tRUvUcRwbspQ8/ONz34qg5A901ZAzIxTlZWvtiNxQ9sc3fgLWn4w1IJ4JQoHSPbrFhiB eAq/oXQIN8K7Xt7BrgtB4RtH+WuzIgsVu+nFBd2J08H77KAZIAUVRnKafujO9l9Fqsr6 bLZ+vMlso/8G0rxxx/iGOprEguQ7f1+gtF9cRpuGwT0IjnDLWyX7pOQCzUv1ZDU2JSfJ Vr5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=IRGy5AjLEy86qA+cSGoMrnrDZBgYozTalOxpAkpm/cc=; b=oq2NVIJ6Y/DAd4c553YSx+UyMQMEKrtNALp5JwIC0An3iHj3BmOqTvuFim07mq4dzl MAJlbc7xprJCCZKt5fGsCdOe6ExWqgJbr1JzUnIOA+IvsiuTwxJuhXZfnaAeypkJFwsl trzT5UajgH7jHOePFkWULJhJW+r32+SF2JBB9qo2dfuYwf8JgyAoDIOf07AQCVIhF3D3 mUxQcCHR/cQSqbG2yThsHa66JKxgy5adL7Y+70WrQJNCyGdnZLM20+4FbI+OL2ywBFAN pV5VdvqPM6U6Jnryd/h0skJhcI2SuT7ftWbArL1eO5R/MDOLleY0pwsHIiTkWJkV4aB+ +fIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=llk0ZVsP; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y8-20020a056402270800b0046751bddcf0si12776212edd.425.2022.11.16.02.18.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Nov 2022 02:18:33 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=llk0ZVsP; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7D1523959C4E for ; Wed, 16 Nov 2022 10:18:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7D1523959C4E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668593912; bh=IRGy5AjLEy86qA+cSGoMrnrDZBgYozTalOxpAkpm/cc=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=llk0ZVsPzELXQEroxw+r1u/UyOYL9XLg6eV0CA5JW/cOA4tec5/XSH09V6J3GzCHz cPcSJS9j5hubumZW6XVZ7KJQ3zAdYoMdVEiuzNh0W9jcF01mX2T5uSVPtXal07DlaS HTvt6szdc8VgIWzQw23IrFwqMxJGrVoy4LMFjiEo= 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 ESMTPS id A3A3B3954C63 for ; Wed, 16 Nov 2022 10:17:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A3A3B3954C63 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-183-1V5lIGRdOE2iPclHGlDygg-1; Wed, 16 Nov 2022 05:17:45 -0500 X-MC-Unique: 1V5lIGRdOE2iPclHGlDygg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0516D1C06EF1 for ; Wed, 16 Nov 2022 10:17:45 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B945F35429; Wed, 16 Nov 2022 10:17:44 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2AGAHdpo2460903 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 16 Nov 2022 11:17:40 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2AGAHcWC2460902; Wed, 16 Nov 2022 11:17:38 +0100 Date: Wed, 16 Nov 2022 11:17:38 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v3: Implement CWG2635 - Constrained structured bindings Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 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, SPF_HELO_NONE, SPF_NONE, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749647531628714227?= X-GMAIL-MSGID: =?utf-8?q?1749647531628714227?= On Tue, Nov 15, 2022 at 06:22:36PM -0500, Jason Merrill wrote: > > Here is another version of the patch that passed bootstrap/regtest, the only > > change are tweaks to 2 further testcases. > > > > 2022-11-13 Jakub Jelinek > > > > * decl.cc (grokdeclarator): Implement > > CWG2635 - Constrained structured bindings. Diagnose constrained > > auto type. > > Let's make the constrained auto case a pedwarn instead of an error. So like this (i.e. just pedwarn but don't change the type and keep doing what we were before)? 2022-11-16 Jakub Jelinek * decl.cc (grokdeclarator): Implement CWG2635 - Constrained structured bindings. Emit a pedwarn on constrained auto type. Add auto_diagnostic_group for error_at and inform for non-auto type on structured bindings declaration. * g++.dg/cpp2a/decomp5.C: New test. * g++.dg/cpp2a/decomp6.C: New test. * g++.dg/cpp2a/decomp7.C: New test. * g++.dg/cpp2a/concepts-placeholder7.C: Adjust expected diagnostics. * g++.dg/cpp2a/concepts-placeholder8.C: Likewise. * g++.dg/cpp2a/concepts-placeholder9.C: New test. * g++.dg/cpp2a/concepts-placeholder10.C: New test. Jakub --- gcc/cp/decl.cc.jj 2022-11-16 07:29:11.603716748 +0100 +++ gcc/cp/decl.cc 2022-11-16 11:07:05.169822071 +0100 @@ -12664,6 +12664,7 @@ grokdeclarator (const cp_declarator *dec { if (type != error_mark_node) { + auto_diagnostic_group d; error_at (loc, "structured binding declaration cannot have " "type %qT", type); inform (loc, @@ -12673,6 +12674,10 @@ grokdeclarator (const cp_declarator *dec type = build_qualified_type (make_auto (), type_quals); declspecs->type = type; } + else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)) + pedwarn (loc, OPT_Wpedantic, + "structured binding declaration cannot have constrained " + "% type %qT", type); inlinep = 0; typedef_p = 0; constexpr_p = 0; --- gcc/testsuite/g++.dg/cpp2a/decomp5.C.jj 2022-11-16 10:39:20.469335601 +0100 +++ gcc/testsuite/g++.dg/cpp2a/decomp5.C 2022-11-16 10:46:02.272896156 +0100 @@ -0,0 +1,19 @@ +// CWG2635 - Constrained structured bindings +// { dg-do compile { target c++20 } } + +namespace std { + template struct tuple_size; + template struct tuple_element; +} + +struct A { + int i; + A(int x) : i(x) {} + template int& get() { return i; } +}; + +template<> struct std::tuple_size { static const int value = 2; }; +template struct std::tuple_element { using type = int; }; + +template concept C = true; +C auto [x, y] = A{1}; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<, >\\\]'" } --- gcc/testsuite/g++.dg/cpp2a/decomp6.C.jj 2022-11-16 10:46:44.868319518 +0100 +++ gcc/testsuite/g++.dg/cpp2a/decomp6.C 2022-11-16 10:47:07.485013348 +0100 @@ -0,0 +1,20 @@ +// CWG2635 - Constrained structured bindings +// { dg-do compile { target c++20 } } +// { dg-options "-pedantic" } + +namespace std { + template struct tuple_size; + template struct tuple_element; +} + +struct A { + int i; + A(int x) : i(x) {} + template int& get() { return i; } +}; + +template<> struct std::tuple_size { static const int value = 2; }; +template struct std::tuple_element { using type = int; }; + +template concept C = true; +C auto [x, y] = A{1}; // { dg-warning "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<, >\\\]'" } --- gcc/testsuite/g++.dg/cpp2a/decomp7.C.jj 2022-11-16 10:47:14.578917309 +0100 +++ gcc/testsuite/g++.dg/cpp2a/decomp7.C 2022-11-16 10:47:32.061680633 +0100 @@ -0,0 +1,20 @@ +// CWG2635 - Constrained structured bindings +// { dg-do compile { target c++20 } } +// { dg-options "-Wno-pedantic" } + +namespace std { + template struct tuple_size; + template struct tuple_element; +} + +struct A { + int i; + A(int x) : i(x) {} + template int& get() { return i; } +}; + +template<> struct std::tuple_size { static const int value = 2; }; +template struct std::tuple_element { using type = int; }; + +template concept C = true; +C auto [x, y] = A{1}; // { dg-bogus "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<, >\\\]'" } --- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C.jj 2022-11-13 17:53:46.037681901 +0100 +++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C 2022-11-16 11:09:56.092513219 +0100 @@ -7,16 +7,18 @@ template void f() { int x[] = {1,2}; int y[] = {3}; - C1 auto [a,b] = x; - C1 auto [c] = y; // { dg-error "constraints" } + C1 auto [a,b] = x; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C1<, >\\\]'" } + C1 auto [c] = y; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C1<, >\\\]'" } + // { dg-error "constraints" "" { target *-*-* } .-1 } } template void g() { T x[] = {1,2}; T y[] = {3}; - C1 auto [a,b] = x; - C1 auto [c] = y; // { dg-error "constraints" } + C1 auto [a,b] = x; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C1<, >\\\]'" } + C1 auto [c] = y; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C1<, >\\\]'" } + // { dg-error "constraints" "" { target *-*-* } .-1 } } template void g(); @@ -27,6 +29,6 @@ struct S { int a, b; } s; template void h() { - const C2 auto& [a, b] = s; + const C2 auto& [a, b] = s; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'const auto \\\[requires ::C2<, >\\\]'" } } template void h(); --- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C.jj 2022-11-13 17:53:46.037681901 +0100 +++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C 2022-11-16 11:10:25.432116956 +0100 @@ -5,6 +5,7 @@ template concept is_const = __ void f() { int x[] = {1,2}; const int y[] = {3}; - const is_const auto [a,b] = x; // { dg-error "constraints" } - const is_const auto [c] = y; + const is_const auto [a,b] = x; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'const auto \\\[requires ::is_const<, >\\\]'" } + // { dg-error "constraints" "" { target *-*-* } .-1 } + const is_const auto [c] = y; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'const auto \\\[requires ::is_const<, >\\\]'" } } --- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder9.C.jj 2022-11-16 10:49:54.610750873 +0100 +++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder9.C 2022-11-16 11:12:09.234715037 +0100 @@ -0,0 +1,33 @@ +// PR c++/99899 +// { dg-do compile { target c++20 } } +// { dg-options "-Wno-pedantic" } + +template concept C1 = sizeof(T) > sizeof(int[1]); + +template +void f() { + int x[] = {1,2}; + int y[] = {3}; + C1 auto [a,b] = x; + C1 auto [c] = y; // { dg-error "constraints" } +} + +template +void g() { + T x[] = {1,2}; + T y[] = {3}; + C1 auto [a,b] = x; + C1 auto [c] = y; // { dg-error "constraints" } +} +template void g(); + + +template concept C2 = sizeof...(Ts) > 1; + +struct S { int a, b; } s; + +template +void h() { + const C2 auto& [a, b] = s; +} +template void h(); --- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder10.C.jj 2022-11-16 10:49:54.610750873 +0100 +++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder10.C 2022-11-16 10:50:16.757451058 +0100 @@ -0,0 +1,11 @@ +// { dg-do compile { target c++20 } } +// { dg-options "-Wno-pedantic" } + +template concept is_const = __is_same(T, const T); + +void f() { + int x[] = {1,2}; + const int y[] = {3}; + const is_const auto [a,b] = x; // { dg-error "constraints" } + const is_const auto [c] = y; +}