From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id F099B3852C5C; Fri, 18 Nov 2022 08:05:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F099B3852C5C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668758741; bh=eV8ib8zfr0tiJMYId/7JL75wuZYf10INuWVQg3OjbCk=; h=From:To:Subject:Date:From; b=NHwnjucxFTjIvve3LqMjcpL4rJ8lhhp9g5rlYGeNsFyZJdAVu/WSCVbqhUZrNdQ8n 8XkO/woalkN5kVD0qFt2VJ3pPHCF8qnFXu+jlbrUZCrt81FlEM2YlpqCDDDmbWnZQB w/tLj9xfY6320Hl918nVBps2BcCNmZ9deyIPN+Nw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4138] c++: Implement CWG2635 - Constrained structured bindings X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: c5df8392c5848c0462558f41cdf6eab31db301cf X-Git-Newrev: e5049dfbe294abc56953d4f0b412000cd8a1f39e Message-Id: <20221118080541.F099B3852C5C@sourceware.org> Date: Fri, 18 Nov 2022 08:05:41 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e5049dfbe294abc56953d4f0b412000cd8a1f39e commit r13-4138-ge5049dfbe294abc56953d4f0b412000cd8a1f39e Author: Jakub Jelinek Date: Fri Nov 18 09:04:16 2022 +0100 c++: Implement CWG2635 - Constrained structured bindings The following patch implements CWG2635. 2022-11-18 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. Diff: --- gcc/cp/decl.cc | 5 ++++ .../g++.dg/cpp2a/concepts-placeholder10.C | 11 ++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C | 12 ++++---- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C | 5 ++-- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder9.C | 33 ++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/decomp5.C | 19 +++++++++++++ gcc/testsuite/g++.dg/cpp2a/decomp6.C | 20 +++++++++++++ gcc/testsuite/g++.dg/cpp2a/decomp7.C | 20 +++++++++++++ 8 files changed, 118 insertions(+), 7 deletions(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 9a7b1a6c381..341b4bba992 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -12664,6 +12664,7 @@ grokdeclarator (const cp_declarator *declarator, { 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 *declarator, 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; diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder10.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder10.C new file mode 100644 index 00000000000..b831531b113 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder10.C @@ -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; +} diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C index 93219ff4f05..7ba6ab942b5 100644 --- a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C @@ -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(); diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C index e78659203ba..d887961227f 100644 --- a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C @@ -5,6 +5,7 @@ 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; + 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<, >\\\]'" } } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder9.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder9.C new file mode 100644 index 00000000000..96732f39756 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder9.C @@ -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(); diff --git a/gcc/testsuite/g++.dg/cpp2a/decomp5.C b/gcc/testsuite/g++.dg/cpp2a/decomp5.C new file mode 100644 index 00000000000..9ea31c6b87b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/decomp5.C @@ -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<, >\\\]'" } diff --git a/gcc/testsuite/g++.dg/cpp2a/decomp6.C b/gcc/testsuite/g++.dg/cpp2a/decomp6.C new file mode 100644 index 00000000000..e6cd034ca35 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/decomp6.C @@ -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<, >\\\]'" } diff --git a/gcc/testsuite/g++.dg/cpp2a/decomp7.C b/gcc/testsuite/g++.dg/cpp2a/decomp7.C new file mode 100644 index 00000000000..581797e2d44 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/decomp7.C @@ -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<, >\\\]'" }