public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix crash with broken deduction from {} [PR97895]
@ 2020-11-19 16:11 Marek Polacek
  2020-11-19 18:42 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2020-11-19 16:11 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

Unfortunately, the otherwise beautiful

  for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))

is not immune to an empty constructor, so we have to check
CONSTRUCTOR_ELTS first.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

gcc/cp/ChangeLog:

	PR c++/97895
	* pt.c (do_auto_deduction): Don't crash when the constructor has
	zero elements.

gcc/testsuite/ChangeLog:

	PR c++/97895
	* g++.dg/cpp0x/auto54.C: New test.
---
 gcc/cp/pt.c                         | 11 +++++++----
 gcc/testsuite/g++.dg/cpp0x/auto54.C | 10 ++++++++++
 2 files changed, 17 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/auto54.C

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 1babf833d32..a1b6631d691 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -29250,10 +29250,13 @@ do_auto_deduction (tree type, tree init, tree auto_node,
     return error_mark_node;
 
   if (BRACE_ENCLOSED_INITIALIZER_P (init))
-    /* We don't recurse here because we can't deduce from a nested
-       initializer_list.  */
-    for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))
-      elt.value = resolve_nondeduced_context (elt.value, complain);
+    {
+      /* We don't recurse here because we can't deduce from a nested
+	 initializer_list.  */
+      if (CONSTRUCTOR_ELTS (init))
+	for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))
+	  elt.value = resolve_nondeduced_context (elt.value, complain);
+    }
   else
     init = resolve_nondeduced_context (init, complain);
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/auto54.C b/gcc/testsuite/g++.dg/cpp0x/auto54.C
new file mode 100644
index 00000000000..0c1815a99bc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/auto54.C
@@ -0,0 +1,10 @@
+// PR c++/97895
+// { dg-do compile { target c++11 } }
+
+namespace std {
+  template<typename T> struct initializer_list {
+    const T *ptr;
+    decltype(sizeof 0) n;
+  };
+  auto a = {}; // { dg-error "unable to deduce" }
+}

base-commit: 25bb75f841c552cfd27a4344b7487efbe35b4481
-- 
2.28.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: Fix crash with broken deduction from {} [PR97895]
  2020-11-19 16:11 [PATCH] c++: Fix crash with broken deduction from {} [PR97895] Marek Polacek
@ 2020-11-19 18:42 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2020-11-19 18:42 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 11/19/20 11:11 AM, Marek Polacek wrote:
> Unfortunately, the otherwise beautiful
> 
>    for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))
> 
> is not immune to an empty constructor, so we have to check
> CONSTRUCTOR_ELTS first.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> gcc/cp/ChangeLog:
> 
> 	PR c++/97895
> 	* pt.c (do_auto_deduction): Don't crash when the constructor has
> 	zero elements.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c++/97895
> 	* g++.dg/cpp0x/auto54.C: New test.
> ---
>   gcc/cp/pt.c                         | 11 +++++++----
>   gcc/testsuite/g++.dg/cpp0x/auto54.C | 10 ++++++++++
>   2 files changed, 17 insertions(+), 4 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/auto54.C
> 
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index 1babf833d32..a1b6631d691 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -29250,10 +29250,13 @@ do_auto_deduction (tree type, tree init, tree auto_node,
>       return error_mark_node;
>   
>     if (BRACE_ENCLOSED_INITIALIZER_P (init))
> -    /* We don't recurse here because we can't deduce from a nested
> -       initializer_list.  */
> -    for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))
> -      elt.value = resolve_nondeduced_context (elt.value, complain);
> +    {
> +      /* We don't recurse here because we can't deduce from a nested
> +	 initializer_list.  */
> +      if (CONSTRUCTOR_ELTS (init))
> +	for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))
> +	  elt.value = resolve_nondeduced_context (elt.value, complain);
> +    }
>     else
>       init = resolve_nondeduced_context (init, complain);
>   
> diff --git a/gcc/testsuite/g++.dg/cpp0x/auto54.C b/gcc/testsuite/g++.dg/cpp0x/auto54.C
> new file mode 100644
> index 00000000000..0c1815a99bc
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/auto54.C
> @@ -0,0 +1,10 @@
> +// PR c++/97895
> +// { dg-do compile { target c++11 } }
> +
> +namespace std {
> +  template<typename T> struct initializer_list {
> +    const T *ptr;
> +    decltype(sizeof 0) n;
> +  };
> +  auto a = {}; // { dg-error "unable to deduce" }
> +}
> 
> base-commit: 25bb75f841c552cfd27a4344b7487efbe35b4481
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-19 18:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-19 16:11 [PATCH] c++: Fix crash with broken deduction from {} [PR97895] Marek Polacek
2020-11-19 18:42 ` 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).