From: Nathaniel Shead Date: Mon, 4 Mar 2024 11:59:56 +0000 (+1100) Subject: c++: Improve fixits for incorrect explicit instantiations X-Git-Tag: basepoints/gcc-16~6775 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7f719612515a86d1d2a36e24b02ade3f0904e10;p=thirdparty%2Fgcc.git c++: Improve fixits for incorrect explicit instantiations When forgetting the '<>' on an explicit specialisation, the suggested fixit hint suggests to add 'template <>', but naively applying will cause nonsense results like 'template template <> struct S {};'. Instead check if we're currently parsing an explicit instantiation, and if so inform about the issue (an instantiation cannot have a class body) and suggest a fixit of simply '<>' to create a specialisation instead. gcc/cp/ChangeLog: * parser.cc (cp_parser_class_head): Clarify error message for explicit instantiations. gcc/testsuite/ChangeLog: * g++.dg/template/explicit-instantiation9.C: New test. Signed-off-by: Nathaniel Shead --- diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index f625b0a310c..82f3903838e 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -27721,11 +27721,20 @@ cp_parser_class_head (cp_parser* parser, class_head_start_location, get_finish (type_start_token->location)); rich_location richloc (line_table, reported_loc); - richloc.add_fixit_insert_before (class_head_start_location, - "template <> "); - error_at (&richloc, - "an explicit specialization must be preceded by" - " %