[PATCH] PR c++/47208

Dodji Seketeli dodji@redhat.com
Thu Feb 17 19:49:00 GMT 2011


Hello,

Consider this short example invalid example:

 constexpr auto list = { };
 static const int l = list.size();

It lacks a '#include <initializer_list>' and so it makes G++ crash.

As 'list' cannot be initialized the type of its DECL is set to
error_mark_node. Then at some point finish_id_expression passes that
DECL to decl_constant_var_p that chokes because DECL is erroneous and
breaks one of its invariants.

The patch below just avoids passing an erroneous DECL to
decl_constant_var_p.

The other hunk of the patch is because we get an additional diagnostic
that says.

  unable to deduce ‘<type error>’ from ‘<brace-enclosed initializer list>() 

I find the <type error> there being less than helpful to the user so I
thought maybe we could avoid displaying that message if the type we are
issuing the diagnostic about is error_mark_node. We then just get the
diagnostic message:

  "deducing from brace-enclosed initializer list requires #include <initializer_list>"

Which I believe is enough to understand what is going on.

Tested on x86_64-unknown-linux-gnu against trunk.

-- 
		Dodji

From 5cda69a0ef9c8b5f053b5929eb808c5769d49d2f Mon Sep 17 00:00:00 2001
From: Dodji Seketeli <dodji@redhat.com>
Date: Thu, 17 Feb 2011 13:07:28 +0100
Subject: [PATCH] PR c++/47208

gcc/cp/

	PR c++/47208
	* pt.c (do_auto_deduction): Do not mention error_mark_node in
	diagnostics.
	* semantics.c (finish_id_expression): Do not pass erroneous decl
	to decl_constant_var_p.

gcc/testsuite/

	PR c++/47208
	* g++.dg/cpp0x/auto21.C: New test.
---
 gcc/cp/pt.c                         |    6 +++++-
 gcc/cp/semantics.c                  |    3 ++-
 gcc/testsuite/g++.dg/cpp0x/auto21.C |    5 +++++
 3 files changed, 12 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/auto21.C

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 02b8d15..4990636 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -18926,7 +18926,11 @@ do_auto_deduction (tree type, tree init, tree auto_node)
 			       DEDUCE_CALL, LOOKUP_NORMAL);
   if (val > 0)
     {
-      error ("unable to deduce %qT from %qE", type, init);
+      if (type && type != error_mark_node)
+	/* If type is error_mark_node a diagnostic must have been
+	   emitted by now.  Also, having a mention to '<type error>'
+	   in the diagnostic is not really useful to the user.  */
+	error ("unable to deduce %qT from %qE", type, init);
       return error_mark_node;
     }
 
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index daa7280..1ad707b 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -3148,7 +3148,8 @@ finish_id_expression (tree id_expression,
       /* Only certain kinds of names are allowed in constant
 	 expression.  Enumerators and template parameters have already
 	 been handled above.  */
-      if (integral_constant_expression_p
+      if (! error_operand_p (decl)
+	  && integral_constant_expression_p
 	  && ! decl_constant_var_p (decl)
 	  && ! builtin_valid_in_constant_expr_p (decl))
 	{
diff --git a/gcc/testsuite/g++.dg/cpp0x/auto21.C b/gcc/testsuite/g++.dg/cpp0x/auto21.C
new file mode 100644
index 0000000..1cbcac5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/auto21.C
@@ -0,0 +1,5 @@
+// Origin PR c++/47208
+// { dg-options "-std=c++0x" }
+
+constexpr auto list = { }; // { dg-error "deducing from brace-enclosed initializer list requires #include <initializer_list>" }
+static const int l = list.size();
-- 
1.7.3.4



More information about the Gcc-patches mailing list