C++ PATCH to improve diagnostic for non-type template parameters

Marek Polacek polacek@redhat.com
Fri May 10 23:10:00 GMT 2019


When we have 

  template<auto>
  struct S { };

then in

   S<int()> s;

"int()" is resolved to a type-id, as per [temp.arg]/2, causing this program to
fail to compile.  This can be rather confusing so I think we want to improve the
diagnostic a bit. 

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

2019-05-10  Marek Polacek  <polacek@redhat.com>

	* pt.c (convert_template_argument): Add a diagnostic for the
	[temp.arg]/2 ambiguity case.

	* g++.dg/cpp2a/nontype-class17.C: New test.

diff --git gcc/cp/pt.c gcc/cp/pt.c
index 08da94ae0c9..b38e65d7f7e 100644
--- gcc/cp/pt.c
+++ gcc/cp/pt.c
@@ -7961,10 +7961,22 @@ convert_template_argument (tree parm,
 		     "parameter list for %qD",
 		     i + 1, in_decl);
 	      if (is_type)
-		inform (input_location,
-			"  expected a constant of type %qT, got %qT",
-			TREE_TYPE (parm),
-			(DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
+		{
+		  /* The template argument is a type, but we're expecting
+		     an expression.  */
+		  inform (input_location,
+			  "  expected a constant of type %qT, got %qT",
+			  TREE_TYPE (parm),
+			  (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
+		  /* [temp.arg]/2: "In a template-argument, an ambiguity
+		     between a type-id and an expression is resolved to a
+		     type-id, regardless of the form of the corresponding
+		     template-parameter."  So give the user a clue.  */
+		  if (TREE_CODE (arg) == FUNCTION_TYPE)
+		    inform (input_location, "  template argument for "
+			    "non-type template parameter is treated as "
+			    "function type");
+		}
 	      else if (requires_tmpl_type)
 		inform (input_location,
 			"  expected a class template, got %qE", orig_arg);
diff --git gcc/testsuite/g++.dg/cpp2a/nontype-class17.C gcc/testsuite/g++.dg/cpp2a/nontype-class17.C
new file mode 100644
index 00000000000..ca5f68e1611
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp2a/nontype-class17.C
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++2a } }
+
+template<auto>
+struct S { };
+
+struct R { };
+
+void
+g (void)
+{
+  S<R()> s; // { dg-error "mismatch" }
+// { dg-message "treated as function" "note" { target *-*-* } .-1 }
+  S<R{}> s2;
+  S<int()> s3; // { dg-error "mismatch" }
+// { dg-message "treated as function" "note" { target *-*-* } .-1 }
+  S<int{}> s4;
+}



More information about the Gcc-patches mailing list