* [PATCH] c++: Implement CWG2635 - Constrained structured bindings
@ 2022-11-12 11:23 Jakub Jelinek
2022-11-13 11:50 ` [PATCH] c++, v2: " Jakub Jelinek
0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2022-11-12 11:23 UTC (permalink / raw)
To: Jason Merrill; +Cc: gcc-patches
Hi!
The following patch implements CWG2635.
So far tested on
GXX_TESTSUITE_STDS=98,11,14,17,20,2b make check-g++ RUNTESTFLAGS="dg.exp=decomp*"
ok for trunk if it passes full bootstrap/regtest and it is voted in?
2022-11-12 Jakub Jelinek <jakub@redhat.com>
* decl.cc (grokdeclarator): Implement
CWG2635 - Constrained structured bindings. Diagnose constrained
auto type.
* g++.dg/cpp2a/decomp5.C: New test.
--- gcc/cp/decl.cc.jj 2022-11-11 17:14:33.103869977 +0100
+++ gcc/cp/decl.cc 2022-11-12 12:13:52.217239729 +0100
@@ -12660,7 +12660,8 @@ grokdeclarator (const cp_declarator *dec
gcc_unreachable ();
}
if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
- || TYPE_IDENTIFIER (type) != auto_identifier)
+ || TYPE_IDENTIFIER (type) != auto_identifier
+ || PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
{
if (type != error_mark_node)
{
--- gcc/testsuite/g++.dg/cpp2a/decomp5.C.jj 2022-11-12 12:17:21.024392082 +0100
+++ gcc/testsuite/g++.dg/cpp2a/decomp5.C 2022-11-12 12:20:00.700214521 +0100
@@ -0,0 +1,20 @@
+// CWG2635 - Constrained structured bindings
+// { dg-do compile { target c++20 } }
+
+namespace std {
+ template<typename T> struct tuple_size;
+ template<int, typename> struct tuple_element;
+}
+
+struct A {
+ int i;
+ A(int x) : i(x) {}
+ template <int I> int& get() { return i; }
+};
+
+template<> struct std::tuple_size<A> { static const int value = 2; };
+template<int I> struct std::tuple_element<I,A> { using type = int; };
+
+template<class T> concept C = true;
+C auto [x, y] = A{1}; // { dg-error "structured binding declaration cannot have type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
+ // { dg-message "type must be cv-qualified 'auto' or reference to cv-qualified 'auto'" "" { target *-*-* } .-1 }
Jakub
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH] c++, v2: Implement CWG2635 - Constrained structured bindings
2022-11-12 11:23 [PATCH] c++: Implement CWG2635 - Constrained structured bindings Jakub Jelinek
@ 2022-11-13 11:50 ` Jakub Jelinek
2022-11-15 23:22 ` Jason Merrill
0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2022-11-13 11:50 UTC (permalink / raw)
To: Jason Merrill; +Cc: gcc-patches
On Sat, Nov 12, 2022 at 12:23:56PM +0100, Jakub Jelinek wrote:
> The following patch implements CWG2635.
>
> So far tested on
> GXX_TESTSUITE_STDS=98,11,14,17,20,2b make check-g++ RUNTESTFLAGS="dg.exp=decomp*"
> ok for trunk if it passes full bootstrap/regtest and it is voted in?
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 <jakub@redhat.com>
* decl.cc (grokdeclarator): Implement
CWG2635 - Constrained structured bindings. Diagnose constrained
auto type.
* g++.dg/cpp2a/decomp5.C: New test.
* g++.dg/cpp2a/concepts-placeholder7.C: Adjust expected diagnostics.
* g++.dg/cpp2a/concepts-placeholder8.C: Likewise.
--- gcc/cp/decl.cc.jj 2022-11-11 17:14:33.103869977 +0100
+++ gcc/cp/decl.cc 2022-11-12 12:13:52.217239729 +0100
@@ -12660,7 +12660,8 @@ grokdeclarator (const cp_declarator *dec
gcc_unreachable ();
}
if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
- || TYPE_IDENTIFIER (type) != auto_identifier)
+ || TYPE_IDENTIFIER (type) != auto_identifier
+ || PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
{
if (type != error_mark_node)
{
--- gcc/testsuite/g++.dg/cpp2a/decomp5.C.jj 2022-11-12 12:17:21.024392082 +0100
+++ gcc/testsuite/g++.dg/cpp2a/decomp5.C 2022-11-12 12:20:00.700214521 +0100
@@ -0,0 +1,20 @@
+// CWG2635 - Constrained structured bindings
+// { dg-do compile { target c++20 } }
+
+namespace std {
+ template<typename T> struct tuple_size;
+ template<int, typename> struct tuple_element;
+}
+
+struct A {
+ int i;
+ A(int x) : i(x) {}
+ template <int I> int& get() { return i; }
+};
+
+template<> struct std::tuple_size<A> { static const int value = 2; };
+template<int I> struct std::tuple_element<I,A> { using type = int; };
+
+template<class T> concept C = true;
+C auto [x, y] = A{1}; // { dg-error "structured binding declaration cannot have type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
+ // { dg-message "type must be cv-qualified 'auto' or reference to cv-qualified 'auto'" "" { target *-*-* } .-1 }
--- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C.jj 2021-04-06 23:49:08.288882716 +0200
+++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C 2022-11-13 12:17:38.570047510 +0100
@@ -7,16 +7,16 @@ template <class>
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 type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
+ C1 auto [c] = y; // { dg-error "structured binding declaration cannot have type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
}
template <class T>
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 type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
+ C1 auto [c] = y; // { dg-error "structured binding declaration cannot have type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
}
template void g<int>();
@@ -27,6 +27,6 @@ struct S { int a, b; } s;
template <class T>
void h() {
- const C2<T> auto& [a, b] = s;
+ const C2<T> auto& [a, b] = s; // { dg-error "structured binding declaration cannot have type 'const auto \\\[requires ::C2<<placeholder>, >\\\]'" }
}
template void h<int>();
--- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C.jj 2021-04-06 23:49:08.288882716 +0200
+++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C 2022-11-13 12:19:17.034684881 +0100
@@ -5,6 +5,6 @@ template <class T> 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 type 'const auto \\\[requires ::is_const<<placeholder>, >\\\]'" }
+ const is_const auto [c] = y; // { dg-error "structured binding declaration cannot have type 'const auto \\\[requires ::is_const<<placeholder>, >\\\]'" }
}
Jakub
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] c++, v2: Implement CWG2635 - Constrained structured bindings
2022-11-13 11:50 ` [PATCH] c++, v2: " Jakub Jelinek
@ 2022-11-15 23:22 ` Jason Merrill
2022-11-16 10:17 ` [PATCH] c++, v3: " Jakub Jelinek
0 siblings, 1 reply; 5+ messages in thread
From: Jason Merrill @ 2022-11-15 23:22 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On 11/13/22 01:50, Jakub Jelinek wrote:
> On Sat, Nov 12, 2022 at 12:23:56PM +0100, Jakub Jelinek wrote:
>> The following patch implements CWG2635.
>>
>> So far tested on
>> GXX_TESTSUITE_STDS=98,11,14,17,20,2b make check-g++ RUNTESTFLAGS="dg.exp=decomp*"
>> ok for trunk if it passes full bootstrap/regtest and it is voted in?
>
> 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 <jakub@redhat.com>
>
> * 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.
> * g++.dg/cpp2a/decomp5.C: New test.
> * g++.dg/cpp2a/concepts-placeholder7.C: Adjust expected diagnostics.
> * g++.dg/cpp2a/concepts-placeholder8.C: Likewise.
>
> --- gcc/cp/decl.cc.jj 2022-11-11 17:14:33.103869977 +0100
> +++ gcc/cp/decl.cc 2022-11-12 12:13:52.217239729 +0100
> @@ -12660,7 +12660,8 @@ grokdeclarator (const cp_declarator *dec
> gcc_unreachable ();
> }
> if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
> - || TYPE_IDENTIFIER (type) != auto_identifier)
> + || TYPE_IDENTIFIER (type) != auto_identifier
> + || PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
> {
> if (type != error_mark_node)
> {
> --- gcc/testsuite/g++.dg/cpp2a/decomp5.C.jj 2022-11-12 12:17:21.024392082 +0100
> +++ gcc/testsuite/g++.dg/cpp2a/decomp5.C 2022-11-12 12:20:00.700214521 +0100
> @@ -0,0 +1,20 @@
> +// CWG2635 - Constrained structured bindings
> +// { dg-do compile { target c++20 } }
> +
> +namespace std {
> + template<typename T> struct tuple_size;
> + template<int, typename> struct tuple_element;
> +}
> +
> +struct A {
> + int i;
> + A(int x) : i(x) {}
> + template <int I> int& get() { return i; }
> +};
> +
> +template<> struct std::tuple_size<A> { static const int value = 2; };
> +template<int I> struct std::tuple_element<I,A> { using type = int; };
> +
> +template<class T> concept C = true;
> +C auto [x, y] = A{1}; // { dg-error "structured binding declaration cannot have type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
> + // { dg-message "type must be cv-qualified 'auto' or reference to cv-qualified 'auto'" "" { target *-*-* } .-1 }
> --- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C.jj 2021-04-06 23:49:08.288882716 +0200
> +++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder7.C 2022-11-13 12:17:38.570047510 +0100
> @@ -7,16 +7,16 @@ template <class>
> 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 type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
> + C1 auto [c] = y; // { dg-error "structured binding declaration cannot have type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
> }
>
> template <class T>
> 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 type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
> + C1 auto [c] = y; // { dg-error "structured binding declaration cannot have type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
> }
> template void g<int>();
>
> @@ -27,6 +27,6 @@ struct S { int a, b; } s;
>
> template <class T>
> void h() {
> - const C2<T> auto& [a, b] = s;
> + const C2<T> auto& [a, b] = s; // { dg-error "structured binding declaration cannot have type 'const auto \\\[requires ::C2<<placeholder>, >\\\]'" }
> }
> template void h<int>();
> --- gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C.jj 2021-04-06 23:49:08.288882716 +0200
> +++ gcc/testsuite/g++.dg/cpp2a/concepts-placeholder8.C 2022-11-13 12:19:17.034684881 +0100
> @@ -5,6 +5,6 @@ template <class T> 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 type 'const auto \\\[requires ::is_const<<placeholder>, >\\\]'" }
> + const is_const auto [c] = y; // { dg-error "structured binding declaration cannot have type 'const auto \\\[requires ::is_const<<placeholder>, >\\\]'" }
> }
>
>
> Jakub
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH] c++, v3: Implement CWG2635 - Constrained structured bindings
2022-11-15 23:22 ` Jason Merrill
@ 2022-11-16 10:17 ` Jakub Jelinek
2022-11-16 13:12 ` Jason Merrill
0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2022-11-16 10:17 UTC (permalink / raw)
To: Jason Merrill; +Cc: gcc-patches
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 <jakub@redhat.com>
> >
> > * 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 <jakub@redhat.com>
* 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.
--- 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 "
+ "%<auto%> 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<typename T> struct tuple_size;
+ template<int, typename> struct tuple_element;
+}
+
+struct A {
+ int i;
+ A(int x) : i(x) {}
+ template <int I> int& get() { return i; }
+};
+
+template<> struct std::tuple_size<A> { static const int value = 2; };
+template<int I> struct std::tuple_element<I,A> { using type = int; };
+
+template<class T> concept C = true;
+C auto [x, y] = A{1}; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
--- 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<typename T> struct tuple_size;
+ template<int, typename> struct tuple_element;
+}
+
+struct A {
+ int i;
+ A(int x) : i(x) {}
+ template <int I> int& get() { return i; }
+};
+
+template<> struct std::tuple_size<A> { static const int value = 2; };
+template<int I> struct std::tuple_element<I,A> { using type = int; };
+
+template<class T> concept C = true;
+C auto [x, y] = A{1}; // { dg-warning "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
--- 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<typename T> struct tuple_size;
+ template<int, typename> struct tuple_element;
+}
+
+struct A {
+ int i;
+ A(int x) : i(x) {}
+ template <int I> int& get() { return i; }
+};
+
+template<> struct std::tuple_size<A> { static const int value = 2; };
+template<int I> struct std::tuple_element<I,A> { using type = int; };
+
+template<class T> concept C = true;
+C auto [x, y] = A{1}; // { dg-bogus "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
--- 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 <class>
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<<placeholder>, >\\\]'" }
+ C1 auto [c] = y; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
+ // { dg-error "constraints" "" { target *-*-* } .-1 }
}
template <class T>
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<<placeholder>, >\\\]'" }
+ C1 auto [c] = y; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
+ // { dg-error "constraints" "" { target *-*-* } .-1 }
}
template void g<int>();
@@ -27,6 +29,6 @@ struct S { int a, b; } s;
template <class T>
void h() {
- const C2<T> auto& [a, b] = s;
+ const C2<T> auto& [a, b] = s; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'const auto \\\[requires ::C2<<placeholder>, >\\\]'" }
}
template void h<int>();
--- 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 <class T> 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<<placeholder>, >\\\]'" }
+ // { 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<<placeholder>, >\\\]'" }
}
--- 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 <class T> concept C1 = sizeof(T) > sizeof(int[1]);
+
+template <class>
+void f() {
+ int x[] = {1,2};
+ int y[] = {3};
+ C1 auto [a,b] = x;
+ C1 auto [c] = y; // { dg-error "constraints" }
+}
+
+template <class T>
+void g() {
+ T x[] = {1,2};
+ T y[] = {3};
+ C1 auto [a,b] = x;
+ C1 auto [c] = y; // { dg-error "constraints" }
+}
+template void g<int>();
+
+
+template <class... Ts> concept C2 = sizeof...(Ts) > 1;
+
+struct S { int a, b; } s;
+
+template <class T>
+void h() {
+ const C2<T> auto& [a, b] = s;
+}
+template void h<int>();
--- 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 <class T> 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;
+}
Jakub
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] c++, v3: Implement CWG2635 - Constrained structured bindings
2022-11-16 10:17 ` [PATCH] c++, v3: " Jakub Jelinek
@ 2022-11-16 13:12 ` Jason Merrill
0 siblings, 0 replies; 5+ messages in thread
From: Jason Merrill @ 2022-11-16 13:12 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On 11/16/22 05:17, Jakub Jelinek wrote:
> 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 <jakub@redhat.com>
>>>
>>> * 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)?
OK.
> 2022-11-16 Jakub Jelinek <jakub@redhat.com>
>
> * 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.
>
> --- 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 "
> + "%<auto%> 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<typename T> struct tuple_size;
> + template<int, typename> struct tuple_element;
> +}
> +
> +struct A {
> + int i;
> + A(int x) : i(x) {}
> + template <int I> int& get() { return i; }
> +};
> +
> +template<> struct std::tuple_size<A> { static const int value = 2; };
> +template<int I> struct std::tuple_element<I,A> { using type = int; };
> +
> +template<class T> concept C = true;
> +C auto [x, y] = A{1}; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
> --- 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<typename T> struct tuple_size;
> + template<int, typename> struct tuple_element;
> +}
> +
> +struct A {
> + int i;
> + A(int x) : i(x) {}
> + template <int I> int& get() { return i; }
> +};
> +
> +template<> struct std::tuple_size<A> { static const int value = 2; };
> +template<int I> struct std::tuple_element<I,A> { using type = int; };
> +
> +template<class T> concept C = true;
> +C auto [x, y] = A{1}; // { dg-warning "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
> --- 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<typename T> struct tuple_size;
> + template<int, typename> struct tuple_element;
> +}
> +
> +struct A {
> + int i;
> + A(int x) : i(x) {}
> + template <int I> int& get() { return i; }
> +};
> +
> +template<> struct std::tuple_size<A> { static const int value = 2; };
> +template<int I> struct std::tuple_element<I,A> { using type = int; };
> +
> +template<class T> concept C = true;
> +C auto [x, y] = A{1}; // { dg-bogus "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C<<placeholder>, >\\\]'" }
> --- 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 <class>
> 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<<placeholder>, >\\\]'" }
> + C1 auto [c] = y; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
> + // { dg-error "constraints" "" { target *-*-* } .-1 }
> }
>
> template <class T>
> 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<<placeholder>, >\\\]'" }
> + C1 auto [c] = y; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'auto \\\[requires ::C1<<placeholder>, >\\\]'" }
> + // { dg-error "constraints" "" { target *-*-* } .-1 }
> }
> template void g<int>();
>
> @@ -27,6 +29,6 @@ struct S { int a, b; } s;
>
> template <class T>
> void h() {
> - const C2<T> auto& [a, b] = s;
> + const C2<T> auto& [a, b] = s; // { dg-error "structured binding declaration cannot have constrained 'auto' type 'const auto \\\[requires ::C2<<placeholder>, >\\\]'" }
> }
> template void h<int>();
> --- 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 <class T> 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<<placeholder>, >\\\]'" }
> + // { 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<<placeholder>, >\\\]'" }
> }
> --- 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 <class T> concept C1 = sizeof(T) > sizeof(int[1]);
> +
> +template <class>
> +void f() {
> + int x[] = {1,2};
> + int y[] = {3};
> + C1 auto [a,b] = x;
> + C1 auto [c] = y; // { dg-error "constraints" }
> +}
> +
> +template <class T>
> +void g() {
> + T x[] = {1,2};
> + T y[] = {3};
> + C1 auto [a,b] = x;
> + C1 auto [c] = y; // { dg-error "constraints" }
> +}
> +template void g<int>();
> +
> +
> +template <class... Ts> concept C2 = sizeof...(Ts) > 1;
> +
> +struct S { int a, b; } s;
> +
> +template <class T>
> +void h() {
> + const C2<T> auto& [a, b] = s;
> +}
> +template void h<int>();
> --- 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 <class T> 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;
> +}
>
> Jakub
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-11-16 13:12 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-12 11:23 [PATCH] c++: Implement CWG2635 - Constrained structured bindings Jakub Jelinek
2022-11-13 11:50 ` [PATCH] c++, v2: " Jakub Jelinek
2022-11-15 23:22 ` Jason Merrill
2022-11-16 10:17 ` [PATCH] c++, v3: " Jakub Jelinek
2022-11-16 13:12 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).