This patch fixes two issues with variable templates:
1) -Wunused was warning about a variable template specialization when
leaving the template specialization scope, and
2) The diagnostic referring to the specialization wasn't showing the
template arguments.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 51aed4b97f9a6dd8bc7954a4691f7790d6bc41c5
Author: Jason Merrill <ja...@redhat.com>
Date: Mon Sep 22 10:33:57 2014 -0400
* decl.c (poplevel): Don't warn about unused vars in template scope.
* error.c (dump_decl): Handle variable templates.
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index fe5a4af..12a9f43 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -624,6 +624,7 @@ poplevel (int keep, int reverse, int functionbody)
/* Before we remove the declarations first check for unused variables. */
if ((warn_unused_variable || warn_unused_but_set_variable)
+ && current_binding_level->kind != sk_template_parms
&& !processing_template_decl)
for (tree d = getdecls (); d; d = TREE_CHAIN (d))
{
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index 86fd405..a03bfe1 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -1044,6 +1044,18 @@ dump_decl (cxx_pretty_printer *pp, tree t, int flags)
case FIELD_DECL:
case PARM_DECL:
dump_simple_decl (pp, t, TREE_TYPE (t), flags);
+
+ /* Handle variable template specializations. */
+ if (TREE_CODE (t) == VAR_DECL
+ && DECL_LANG_SPECIFIC (t)
+ && DECL_TEMPLATE_INFO (t)
+ && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
+ {
+ pp_cxx_begin_template_argument_list (pp);
+ tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
+ dump_template_argument_list (pp, args, flags);
+ pp_cxx_end_template_argument_list (pp);
+ }
break;
case RESULT_DECL:
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ12.C b/gcc/testsuite/g++.dg/cpp1y/var-templ12.C
new file mode 100644
index 0000000..49ea588
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/var-templ12.C
@@ -0,0 +1,10 @@
+// { dg-do compile { target c++14 } }
+// { dg-options "-Wall" }
+
+template <class T> T x;
+template <> int x<int> = 0;
+
+int main()
+{
+ return x<int>;
+}
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ13.C b/gcc/testsuite/g++.dg/cpp1y/var-templ13.C
new file mode 100644
index 0000000..e398d22
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/var-templ13.C
@@ -0,0 +1,5 @@
+// { dg-do compile { target c++14 } }
+
+template <class T> T x;
+template <> int x<int> = 0;
+template <> int x<int> = 0; // { dg-error "x<int>" }