* [PATCH] c++: Reject union std::initializer_list [PR102434]
@ 2022-01-29 1:21 Marek Polacek
2022-01-31 19:10 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2022-01-29 1:21 UTC (permalink / raw)
To: Jason Merrill, GCC Patches
Weird things are going to happen if you define your std::initializer_list
as a union. In this case, we crash in output_constructor_regular_field.
Let's not allow such a definition in the first place.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
PR c++/102434
gcc/cp/ChangeLog:
* class.cc (finish_struct): Don't allow union initializer_list.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/initlist128.C: New test.
---
gcc/cp/class.cc | 2 ++
gcc/testsuite/g++.dg/cpp0x/initlist128.C | 16 ++++++++++++++++
2 files changed, 18 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist128.C
diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc
index 5db3722ae46..696155714e8 100644
--- a/gcc/cp/class.cc
+++ b/gcc/cp/class.cc
@@ -7766,6 +7766,8 @@ finish_struct (tree t, tree attributes)
ok = true;
}
}
+ /* It also cannot be a union. */
+ ok &= NON_UNION_CLASS_TYPE_P (t);
if (!ok)
fatal_error (input_location, "definition of %qD does not match "
"%<#include <initializer_list>%>", TYPE_NAME (t));
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist128.C b/gcc/testsuite/g++.dg/cpp0x/initlist128.C
new file mode 100644
index 00000000000..22246860f4e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist128.C
@@ -0,0 +1,16 @@
+// PR c++/102434
+// { dg-do compile { target c++11 } }
+
+using size_t = decltype(sizeof 0);
+
+namespace std {
+ template<typename T> union initializer_list { // { dg-error "definition of .*std::initializer_list.* does not match" }
+ const T *ptr;
+ size_t n;
+ };
+}
+template<typename T>
+void Task() {}
+auto b = { &Task<int> };
+
+// { dg-prune-output "compilation terminated" }
base-commit: f6f2d6cfec1c2fe9570b98211be58329d8d7749b
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: Reject union std::initializer_list [PR102434]
2022-01-29 1:21 [PATCH] c++: Reject union std::initializer_list [PR102434] Marek Polacek
@ 2022-01-31 19:10 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-01-31 19:10 UTC (permalink / raw)
To: Marek Polacek, GCC Patches
On 1/28/22 20:21, Marek Polacek wrote:
> Weird things are going to happen if you define your std::initializer_list
> as a union. In this case, we crash in output_constructor_regular_field.
>
> Let's not allow such a definition in the first place.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
OK.
> PR c++/102434
>
> gcc/cp/ChangeLog:
>
> * class.cc (finish_struct): Don't allow union initializer_list.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/initlist128.C: New test.
> ---
> gcc/cp/class.cc | 2 ++
> gcc/testsuite/g++.dg/cpp0x/initlist128.C | 16 ++++++++++++++++
> 2 files changed, 18 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist128.C
>
> diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc
> index 5db3722ae46..696155714e8 100644
> --- a/gcc/cp/class.cc
> +++ b/gcc/cp/class.cc
> @@ -7766,6 +7766,8 @@ finish_struct (tree t, tree attributes)
> ok = true;
> }
> }
> + /* It also cannot be a union. */
> + ok &= NON_UNION_CLASS_TYPE_P (t);
> if (!ok)
> fatal_error (input_location, "definition of %qD does not match "
> "%<#include <initializer_list>%>", TYPE_NAME (t));
> diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist128.C b/gcc/testsuite/g++.dg/cpp0x/initlist128.C
> new file mode 100644
> index 00000000000..22246860f4e
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/initlist128.C
> @@ -0,0 +1,16 @@
> +// PR c++/102434
> +// { dg-do compile { target c++11 } }
> +
> +using size_t = decltype(sizeof 0);
> +
> +namespace std {
> + template<typename T> union initializer_list { // { dg-error "definition of .*std::initializer_list.* does not match" }
> + const T *ptr;
> + size_t n;
> + };
> +}
> +template<typename T>
> +void Task() {}
> +auto b = { &Task<int> };
> +
> +// { dg-prune-output "compilation terminated" }
>
> base-commit: f6f2d6cfec1c2fe9570b98211be58329d8d7749b
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-01-31 19:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-29 1:21 [PATCH] c++: Reject union std::initializer_list [PR102434] Marek Polacek
2022-01-31 19:10 ` 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).