public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/gccgo] c++: Fix value-init crash in template [PR93676]
@ 2020-04-04 21:28 Ian Lance Taylor
  0 siblings, 0 replies; only message in thread
From: Ian Lance Taylor @ 2020-04-04 21:28 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:38e1002657828150b2cda9f80c1f752184286e80

commit 38e1002657828150b2cda9f80c1f752184286e80
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed Feb 19 15:59:33 2020 -0500

    c++: Fix value-init crash in template [PR93676]
    
    Since <https://gcc.gnu.org/ml/gcc-patches/2015-02/msg00556.html> we
    attempt to value-initialize in build_vec_init even when there's no
    initializer but the type has a constexpr default constructor.  But
    build_value_init doesn't work in templates, and build_vec_init
    creates a lot of garbage that would not be used anyway, so don't
    call it in a template.
    
            PR c++/93676 - value-init crash in template.
            * init.c (build_new_1): Don't call build_vec_init in a template.
    
            * g++.dg/cpp0x/nsdmi-template19.C: New test.

Diff:
---
 gcc/cp/ChangeLog                              |  5 +++++
 gcc/cp/init.c                                 |  8 ++++++--
 gcc/testsuite/ChangeLog                       |  5 +++++
 gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C | 13 +++++++++++++
 4 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 5ba298fe5e9..d34730c6ec1 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,8 @@
+2020-02-26  Marek Polacek  <polacek@redhat.com>
+
+	PR c++/93676 - value-init crash in template.
+	* init.c (build_new_1): Don't call build_vec_init in a template.
+
 2020-02-26  Marek Polacek  <polacek@redhat.com>
 
 	PR c++/93862 - ICE with static_cast when converting from int[].
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index d480660445e..61ed3aa7e93 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -3511,13 +3511,17 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
 	  explicit_value_init_p = true;
 	}
 
-      if (processing_template_decl && explicit_value_init_p)
+      if (processing_template_decl)
 	{
+	  /* Avoid an ICE when converting to a base in build_simple_base_path.
+	     We'll throw this all away anyway, and build_new will create
+	     a NEW_EXPR.  */
+	  tree t = fold_convert (build_pointer_type (elt_type), data_addr);
 	  /* build_value_init doesn't work in templates, and we don't need
 	     the initializer anyway since we're going to throw it away and
 	     rebuild it at instantiation time, so just build up a single
 	     constructor call to get any appropriate diagnostics.  */
-	  init_expr = cp_build_fold_indirect_ref (data_addr);
+	  init_expr = cp_build_fold_indirect_ref (t);
 	  if (type_build_ctor_call (elt_type))
 	    init_expr = build_special_member_call (init_expr,
 						   complete_ctor_identifier,
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 08146db0ced..572989b2c7a 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2020-02-26  Marek Polacek  <polacek@redhat.com>
+
+	PR c++/93676 - value-init crash in template.
+	* g++.dg/cpp0x/nsdmi-template19.C: New test.
+
 2020-02-26  Marek Polacek  <polacek@redhat.com>
 
 	PR c++/93862 - ICE with static_cast when converting from int[].
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C
new file mode 100644
index 00000000000..f3e2cb87fd6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C
@@ -0,0 +1,13 @@
+// PR c++/93676 - value-init crash in template.
+// { dg-do compile { target c++11 } }
+
+struct P {
+  int x = 0;
+};
+
+template<class T>
+struct S {
+  S() { new P[2][2]; }
+};
+
+S<int> s;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-04-04 21:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-04 21:28 [gcc/devel/gccgo] c++: Fix value-init crash in template [PR93676] Ian Lance Taylor

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).