diff mbox

Small C++ PATCH to add context to error messages about invalid template arguments

Message ID 4CC717AF.1030509@redhat.com
State New
Headers show

Commit Message

Jason Merrill Oct. 26, 2010, 6:02 p.m. UTC
While looking at another issue, I was confused by messages talking about 
one template instantiation as context for an error about an invalid 
template argument which was actually an argument for a different 
template in the definition of the instantiation described in the error 
message.  This patch causes us to push a template instantiation context 
level before converting template arguments so that the printed context 
includes the template we're trying to provide arguments for.

Tested x86_64-pc-linux-gnu, applied to trunk.
diff mbox

Patch

commit 07d43402f88ca5c539d670657cbe0b7b18b43f36
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Oct 26 00:47:47 2010 -0400

    	* pt.c (lookup_template_class): push_tinst_level around call to
    	coerce_template_parms.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 88ebd47..f1a9d17 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -6600,13 +6600,17 @@  lookup_template_class (tree d1,
 	  arglist = bound_args;
 	}
       else
-	arglist
-	  = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
-				   INNERMOST_TEMPLATE_ARGS (arglist),
-				   gen_tmpl,
-				   complain,
-				   /*require_all_args=*/true,
-				   /*use_default_args=*/true);
+	{
+	  push_tinst_level (templ);
+	  arglist
+	    = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
+				     INNERMOST_TEMPLATE_ARGS (arglist),
+				     gen_tmpl,
+				     complain,
+				     /*require_all_args=*/true,
+				     /*use_default_args=*/true);
+	  pop_tinst_level ();
+	}
 
       if (arglist == error_mark_node)
 	/* We were unable to bind the arguments.  */
diff --git a/gcc/testsuite/g++.dg/template/arg8.C b/gcc/testsuite/g++.dg/template/arg8.C
new file mode 100644
index 0000000..5b3a31c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/arg8.C
@@ -0,0 +1,12 @@ 
+// Test for a message indicating what template we're trying to convert
+// arguments for.  We can't actually test for it directly because it
+// doesn't have an associated line number, but we can test for the
+// "instantiated from here" message that follows.
+
+template <int I>
+struct A { };
+
+int i;
+A<i> a;				// { dg-message "instantiated from here" }
+// { dg-error "not a valid template argument" "" { target *-*-* } 10 }
+// { dg-error "invalid type in declaration" "" { target *-*-* } 10 }