]> gcc.gnu.org Git - gcc.git/commitdiff
c++: diagnostic for template placeholder in parm [PR106793]
authorJason Merrill <jason@redhat.com>
Fri, 2 Sep 2022 12:45:02 +0000 (08:45 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 7 Sep 2022 17:37:10 +0000 (13:37 -0400)
Talking about the declarator form doesn't help when fixing that would get
you a different error about placeholders not being valid in a parameter.

This also adds a <> fixit, which isn't enough for most templates, but is a
start.

PR c++/106793

gcc/cp/ChangeLog:

* decl.cc (grokdeclarator): Improve placeholder diagnostics.
* parser.cc (cp_parser_type_id_1): Add fixit.

gcc/testsuite/ChangeLog:

* g++.dg/cpp23/auto-array2.C: Adjust.
* g++.dg/cpp1z/class-deduction113.C: New test.

gcc/cp/decl.cc
gcc/cp/parser.cc
gcc/testsuite/g++.dg/cpp1z/class-deduction113.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp23/auto-array2.C

index 6d20765f40c80ce527171cb9ab5c71b71616aa7a..4665a29a24d896b5da518892c04a4a8897b8c861 100644 (file)
@@ -12407,14 +12407,20 @@ grokdeclarator (const cp_declarator *declarator,
 
   if (cxx_dialect >= cxx17 && type && is_auto (type)
       && innermost_code != cdk_function
+      /* Placeholder in parm gets a better error below.  */
+      && !(decl_context == PARM || decl_context == CATCHPARM)
       && id_declarator && declarator != id_declarator)
     if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
-    {
-      error_at (typespec_loc, "template placeholder type %qT must be followed "
-               "by a simple declarator-id", type);
-      inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
-      type = error_mark_node;
-    }
+      {
+       auto_diagnostic_group g;
+       gcc_rich_location richloc (typespec_loc);
+       richloc.add_fixit_insert_after ("<>");
+       error_at (&richloc, "missing template argument list after %qE; "
+                 "for deduction, template placeholder must be followed "
+                 "by a simple declarator-id", tmpl);
+       inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
+       type = error_mark_node;
+      }
 
   staticp = 0;
   inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
@@ -12892,6 +12898,7 @@ grokdeclarator (const cp_declarator *declarator,
                  {
                    if (!funcdecl_p || !dguide_name_p (unqualified_id))
                      {
+                       auto_diagnostic_group g;
                        error_at (typespec_loc, "deduced class "
                                  "type %qD in function return type",
                                  DECL_NAME (tmpl));
@@ -13837,12 +13844,15 @@ grokdeclarator (const cp_declarator *declarator,
              else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
                {
                  auto_diagnostic_group g;
-                 error_at (typespec_loc,
-                           "class template placeholder %qE not permitted "
-                           "in this context", c);
+                 gcc_rich_location richloc (typespec_loc);
+                 richloc.add_fixit_insert_after ("<>");
+                 error_at (&richloc,
+                           "missing template argument list after %qE; template "
+                           "placeholder not permitted in parameter", c);
                  if (decl_context == PARM && cxx_dialect >= cxx20)
-                   inform (typespec_loc, "use %<auto%> for an "
+                   inform (typespec_loc, "or use %<auto%> for an "
                            "abbreviated function template");
+                 inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
                }
              else
                error_at (typespec_loc,
index 289c2142e4530ac4fe6e6c6374f0300f96b2e234..841ba6ed997bfef75bcccc8cbf12892d9676159d 100644 (file)
@@ -24397,8 +24397,11 @@ cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
                location_t loc = type_specifier_seq.locations[ds_type_spec];
                if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
                  {
-                   error_at (loc, "missing template arguments after %qT",
-                             auto_node);
+                   auto_diagnostic_group g;
+                   gcc_rich_location richloc (loc);
+                   richloc.add_fixit_insert_after ("<>");
+                   error_at (&richloc, "missing template arguments after %qE",
+                             tmpl);
                    inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
                            tmpl);
                  }
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction113.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction113.C
new file mode 100644 (file)
index 0000000..8f6908e
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/106793
+
+template <class T> struct A { A(T); };
+template <class T> void f(A *a); // { dg-error "placeholder.*parameter" "" { target c++17 } }
+// { dg-error "" "" { target c++14_down } .-1 }
index 06431685b30f218bedced34e44c5ec192f4547bc..3fc2eae3ceabb4746e8800eadf4dde23d38bf916 100644 (file)
@@ -5,7 +5,7 @@ template<class T> struct A { A(); };
 A<int> a[3];
 auto (*p)[3] = &a;
 A<int> (*p2)[3] = &a;
-A (*p3)[3] = &a; // { dg-error "template placeholder type" }
+A (*p3)[3] = &a; // { dg-error "template placeholder" }
 auto (&r)[3] = a;
 A<int> (&r2)[3] = a;
-A (&r3)[3] = a; // { dg-error "template placeholder type" }
+A (&r3)[3] = a; // { dg-error "template placeholder" }
This page took 0.140448 seconds and 5 git commands to generate.