]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -Wno-abbreviated-auto-in-template-arg [PR120917]
authorJason Merrill <jason@redhat.com>
Wed, 2 Jul 2025 09:15:01 +0000 (05:15 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 7 Jul 2025 14:36:31 +0000 (10:36 -0400)
In r14-1659 I added a missing error for a Concepts TS feature that we were
failing to diagnose, but this PR requests a way to disable that error for
code written thinking it was valid.  Which seems reasonable, since it
doesn't require any work beyond that and is a plausible extension by itself.

While looking at this, I also noticed we were still not giving the
diagnostic in a few cases, and fixing that affected a few of our old
concepts testcases.

PR c++/120917

gcc/ChangeLog:

* doc/invoke.texi: Add -Wno-abbreviated-auto-in-template-arg.

gcc/c-family/ChangeLog:

* c.opt: Add -Wno-abbreviated-auto-in-template-arg.
* c.opt.urls: Regenerate.

gcc/cp/ChangeLog:

* parser.cc (cp_parser_simple_type_specifier): Attach
auto in targ in parameter to -Wabbreviated-auto-in-template-arg.
(cp_parser_placeholder_type_specifier): Diagnose constrained auto in
template arg.

gcc/testsuite/ChangeLog:

* g++.dg/concepts/auto7a.C: Add diagnostic.
* g++.dg/concepts/auto7b.C: New test.
* g++.dg/concepts/auto7c.C: New test.
* g++.dg/cpp1y/pr85076.C: Expect 'auto' error.
* g++.dg/concepts/pr67249.C: Likewise.
* g++.dg/cpp1y/lambda-generic-variadic.C: Likewise.
* g++.dg/cpp2a/concepts-pr67210.C: Likewise.
* g++.dg/concepts/pr67249a.C: New test.
* g++.dg/cpp1y/lambda-generic-variadic-a.C: New test.
* g++.dg/cpp2a/concepts-pr67210a.C: New test.

14 files changed:
gcc/c-family/c.opt
gcc/c-family/c.opt.urls
gcc/cp/parser.cc
gcc/doc/invoke.texi
gcc/testsuite/g++.dg/concepts/auto7a.C
gcc/testsuite/g++.dg/concepts/auto7b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/concepts/auto7c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/concepts/pr67249.C
gcc/testsuite/g++.dg/concepts/pr67249a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic-a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic.C
gcc/testsuite/g++.dg/cpp1y/pr85076.C
gcc/testsuite/g++.dg/cpp2a/concepts-pr67210.C
gcc/testsuite/g++.dg/cpp2a/concepts-pr67210a.C [new file with mode: 0644]

index 8af466d1ed1f34384d9a0b3206a724d58a081a5b..6a55e7118d120bed3804ac5e307e09c26bc872b6 100644 (file)
@@ -397,6 +397,10 @@ Wassign-intercept
 ObjC ObjC++ Var(warn_assign_intercept) Warning
 Warn whenever an Objective-C assignment is being intercepted by the garbage collector.
 
+Wabbreviated-auto-in-template-arg
+C++ ObjC++ Warning Var(warn_abbev_auto_targ) Init(1)
+Diagnose a placeholder type in a template argument in a function parameter type.
+
 Wbad-function-cast
 C ObjC Var(warn_bad_function_cast) Warning
 Warn about casting functions to incompatible types.
index 65d1221c4ad83921f236e4ee549f8f8a98c36c24..401e6e741383944cd66be6444b3c6d91588e4754 100644 (file)
@@ -139,6 +139,9 @@ UrlSuffix(gcc/Warning-Options.html#index-Warray-parameter)
 Wassign-intercept
 UrlSuffix(gcc/Objective-C-and-Objective-C_002b_002b-Dialect-Options.html#index-Wassign-intercept)
 
+Wabbreviated-auto-in-template-arg
+UrlSuffix(gcc/C_002b_002b-Dialect-Options.html#index-Wabbreviated-auto-in-template-arg)
+
 Wbad-function-cast
 UrlSuffix(gcc/Warning-Options.html#index-Wbad-function-cast)
 
index 44a78324c6e66dc9b032868dace083b2d6bf977a..239e6f9a5565fad0f1d0dae37b0ad63e32d34edb 100644 (file)
@@ -21021,9 +21021,6 @@ cp_parser_simple_type_specifier (cp_parser* parser,
                         "only available with "
                         "%<-std=c++14%> or %<-std=gnu++14%>");
            }
-         else if (parser->in_template_argument_list_p)
-           error_at (token->location,
-                    "use of %<auto%> in template argument");
          else if (!flag_concepts)
            pedwarn (token->location, OPT_Wc__20_extensions,
                     "use of %<auto%> in parameter declaration "
@@ -21033,6 +21030,11 @@ cp_parser_simple_type_specifier (cp_parser* parser,
                     "use of %<auto%> in parameter declaration "
                     "only available with "
                     "%<-std=c++14%> or %<-std=gnu++14%>");
+
+         if (parser->in_template_argument_list_p)
+           permerror_opt (token->location,
+                          OPT_Wabbreviated_auto_in_template_arg,
+                          "use of %<auto%> in template argument");
        }
       else
        type = make_auto ();
@@ -21479,6 +21481,10 @@ cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
          error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
          return error_mark_node;
        }
+      if (parser->in_template_argument_list_p)
+       permerror_opt (placeholder->location,
+                      OPT_Wabbreviated_auto_in_template_arg,
+                      "use of %<auto%> in template argument");
       tree parm = build_constrained_parameter (con, proto, args);
       return synthesize_implicit_template_parm (parser, parm);
     }
index 7640e7d88671ed823ac6771d116e8d248ff6b249..74f5ee26042dc419152fddd66066bc0430d2ffe0 100644 (file)
@@ -3816,6 +3816,23 @@ Warn when a type with an ABI tag is used in a context that does not
 have that ABI tag.  See @ref{C++ Attributes} for more information
 about ABI tags.
 
+@opindex Wabbreviated-auto-in-template-arg
+@opindex Wno-abbreviated-auto-in-template-arg
+@item -Wno-abbreviated-auto-in-template-arg
+Disable the error for an @code{auto} placeholder type used within a
+template argument list to declare a C++20 abbreviated function
+template, e.g.
+
+@smallexample
+void f(S<auto>);
+@end smallexample
+
+This feature was proposed in the Concepts TS, but was not adopted into
+C++20; in the standard, a placeholder in a parameter declaration must
+appear as a decl-specifier.  The error can also be reduced to a
+warning by @option{-fpermissive} or
+@option{-Wno-error=abbreviated-auto-in-template-arg}.
+
 @opindex Wcomma-subscript
 @opindex Wno-comma-subscript
 @item -Wcomma-subscript @r{(C++ and Objective-C++ only)}
@@ -6443,6 +6460,7 @@ only by this flag, but it also downgrades some C and C++ diagnostics
 that have their own flag:
 
 @gccoptlist{
+-Wabbreviated-auto-in-template-arg @r{(C++ and Objective-C++ only)}
 -Wdeclaration-missing-parameter-type @r{(C and Objective-C only)}
 -Wimplicit-function-declaration @r{(C and Objective-C only)}
 -Wimplicit-int @r{(C and Objective-C only)}
index 88868f45d1c1ad53680b153f4bba3fd0275e17ce..f36038d2fb463e05c2a6849b873693ea42260009 100644 (file)
@@ -2,6 +2,7 @@
 
 template <class T> struct A { };
 void f(A<auto> a) { }          // { dg-error "auto. in template argument" }
+// { dg-message "in parameter declaration" "" { target c++17_down } .-1 }
 int main()
 {
   f(A<int>());
diff --git a/gcc/testsuite/g++.dg/concepts/auto7b.C b/gcc/testsuite/g++.dg/concepts/auto7b.C
new file mode 100644 (file)
index 0000000..874192c
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/120917
+// { dg-do compile { target c++14 } }
+// { dg-additional-options "-fconcepts -Wno-abbreviated-auto-in-template-arg" }
+
+template <class T> struct A { };
+void f(A<auto> a) { }
+int main()
+{
+  f(A<int>());
+}
diff --git a/gcc/testsuite/g++.dg/concepts/auto7c.C b/gcc/testsuite/g++.dg/concepts/auto7c.C
new file mode 100644 (file)
index 0000000..5b16027
--- /dev/null
@@ -0,0 +1,12 @@
+// { dg-do compile { target c++17 } }
+// { dg-additional-options -fconcepts }
+
+template <class T>
+concept True = true;
+
+template <class T> struct A { };
+void f(A<True auto> a) { } // { dg-error "use of .auto. in template argument" }
+int main()
+{
+  f(A<int>());
+}
index e0f8d5ade01e0055634e6b429dcc73cad9dfa29c..e97183d1b211c3059f5b1ba356ce0322767353e8 100644 (file)
@@ -4,4 +4,4 @@
 template<class T> concept C1 = true;
 template<class A, class B> struct Pair {};
 // We used to test "Pair<auto, C1 >".
-void f(Pair<C1 auto, C1 auto>);
+void f(Pair<C1 auto, C1 auto>);        // { dg-error "auto" }
diff --git a/gcc/testsuite/g++.dg/concepts/pr67249a.C b/gcc/testsuite/g++.dg/concepts/pr67249a.C
new file mode 100644 (file)
index 0000000..cb5d90e
--- /dev/null
@@ -0,0 +1,7 @@
+// { dg-do compile { target c++17 } }
+// { dg-options "-fconcepts -Wno-abbreviated-auto-in-template-arg" }
+
+template<class T> concept C1 = true;
+template<class A, class B> struct Pair {};
+// We used to test "Pair<auto, C1 >".
+void f(Pair<C1 auto, C1 auto>);
diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic-a.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic-a.C
new file mode 100644 (file)
index 0000000..557ecb9
--- /dev/null
@@ -0,0 +1,15 @@
+// Basic generic lambda test
+// { dg-do run { target c++14 } }
+// { dg-additional-options -Wno-abbreviated-auto-in-template-arg }
+
+template <typename T, typename U> struct pair {};
+template <typename... T> struct tuple {};
+
+int main()
+{
+  auto a = [] (auto, pair<auto,auto> v) { return sizeof (v); };
+  auto b = [] (auto, pair<pair<auto,auto>,auto>... v) { return sizeof... (v); };
+
+  a(1, pair<int, float>());
+  b(2, pair<pair<short,char>, double>(), pair<pair<float,long>, int>());
+}
index 6d2d250f393c4155ee73a93c928961573462f857..971f58fea0b2cc66f64e33e6893c080b4bfce75c 100644 (file)
@@ -6,8 +6,8 @@ template <typename... T> struct tuple {};
 
 int main()
 {
-  auto a = [] (auto, pair<auto,auto> v) { return sizeof (v); };
-  auto b = [] (auto, pair<pair<auto,auto>,auto>... v) { return sizeof... (v); };
+  auto a = [] (auto, pair<auto,auto> v) { return sizeof (v); }; // { dg-error "auto" }
+  auto b = [] (auto, pair<pair<auto,auto>,auto>... v) { return sizeof... (v); }; // { dg-error "auto" }
 
   a(1, pair<int, float>());
   b(2, pair<pair<short,char>, double>(), pair<pair<float,long>, int>());
index 6d54dea6c01899f12a177ffb494f8de0bfbdfda6..b68143b23f931885b64a7533a35be086d1dd64c4 100644 (file)
@@ -3,4 +3,4 @@
 
 template<typename> struct A*;  // { dg-error "expected unqualified-id before" }
 
-auto a = [](A<auto>) {};       // { dg-error "is not a template|has incomplete type" }
+auto a = [](A<auto>) {};       // { dg-error "is not a template|has incomplete type|auto" }
index a31750eb495b47f719a81e42d18dea0d99fbf273..baddc1cd465ff34c78728b8e4fd1c1da96c664ee 100644 (file)
@@ -7,4 +7,4 @@ concept C = true;
 template <class T>
 struct A {};
 
-void f(A<C<int> auto >) {} 
+void f(A<C<int> auto >) {}     // { dg-error "auto" }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr67210a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-pr67210a.C
new file mode 100644 (file)
index 0000000..1d63a84
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/67210
+// { dg-do compile { target c++20 } }
+// { dg-additional-options -Wno-abbreviated-auto-in-template-arg }
+
+template <class T, class U>
+concept C = true;
+
+template <class T>
+struct A {};
+
+void f(A<C<int> auto >) {}