[PR c++/84610,84642] recover from implicit template parms gracefully

Alexandre Oliva aoliva@redhat.com
Sat Mar 10 11:57:00 GMT 2018


If we get a parse error during an attempted fully implicit function
template parse, and need to skip to the end of the statement or block,
we may discard the function parms scope rather than the enclosing
injected implicit template parms scope.  If we rollback a tentative
parse and try something else, we'll no longer be in a function parms
scope, but rather in a template parms scope, but we may still attempt
to synthesize implicit template parms and then fail the assert that
checks we're in a function parms scope.

This patch introduces an alternative to
finish_fully_implicit_template_p, to be used during error recovery,
that floats the implicit template parm scope to the top so that it
gets discarded as we finish and discard the failed implicit template
data, while other scopes are retained as expected.  It also clears the
implicit template parser data as we finish the template, so that it
doesn't linger on referencing discarded or used scopes and parms.

Regstrapped on i686- and x86_64-linux-gnu.  Ok to install?

While debugging this, I first tried another patch, that avoids the same
ICEs.  I thought this one was a more complete solution, and it renders
the other unnecessary, but I still though it might be useful to disable
auto->implicit_parm while parsing declarators, so as to avoid useless
processing.

for gcc/cp/ChangeLog

	PR c++/84610
	PR c++/84642
	* parser.c (abort_fully_implicit_template_p): New.
	(cp_parser_skip_to_end_of_statement): Use it.
	(cp_parser_skip_to_end_of_block_or_statement): Likewise.
	(finish_fully_implicit_template_p): Clear
	implicit_template_parms and implicit_template_scope.

for  gcc/testsuite/ChangeLog

	PR c++/84610
	PR c++/84642
	* g++.dg/cpp0x/pr84610.C: New.
	* g++.dg/cpp0x/pr84642.C: New.
---
 gcc/cp/parser.c                      |   38 ++++++++++++++++++++++++++++++++--
 gcc/testsuite/g++.dg/cpp0x/pr84610.C |    3 +++
 gcc/testsuite/g++.dg/cpp0x/pr84642.C |    3 +++
 3 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr84610.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr84642.C

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index cdc623889732..e17507d7f6e0 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -2264,6 +2264,8 @@ static tree synthesize_implicit_template_parm
   (cp_parser *, tree);
 static tree finish_fully_implicit_template
   (cp_parser *, tree);
+static void abort_fully_implicit_template
+  (cp_parser *);
 
 /* Classes [gram.class] */
 
@@ -3585,7 +3587,7 @@ cp_parser_skip_to_end_of_statement (cp_parser* parser)
 
   /* Unwind generic function template scope if necessary.  */
   if (parser->fully_implicit_function_template_p)
-    finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
+    abort_fully_implicit_template (parser);
 
   while (true)
     {
@@ -3675,7 +3677,7 @@ cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
 
   /* Unwind generic function template scope if necessary.  */
   if (parser->fully_implicit_function_template_p)
-    finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
+    abort_fully_implicit_template (parser);
 
   while (nesting_depth >= 0)
     {
@@ -39261,11 +39263,43 @@ finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
   end_template_decl ();
 
   parser->fully_implicit_function_template_p = false;
+  parser->implicit_template_parms = 0;
+  parser->implicit_template_scope = 0;
   --parser->num_template_parameter_lists;
 
   return member_decl_opt;
 }
 
+/* Like finish_fully_implicit_template, but to be used in error
+   recovery, rearranging scopes so that we restore the state we had
+   before synthesize_implicit_template_parm inserted the implement
+   template parms scope.  */
+
+static void
+abort_fully_implicit_template (cp_parser *parser)
+{
+  cp_binding_level *return_to_scope = current_binding_level;
+
+  if (parser->implicit_template_scope
+      && return_to_scope != parser->implicit_template_scope)
+    {
+      cp_binding_level *child = return_to_scope;
+      for (cp_binding_level *scope = child->level_chain;
+	   scope != parser->implicit_template_scope;
+	   scope = child->level_chain)
+	child = scope;
+      child->level_chain = parser->implicit_template_scope->level_chain;
+      parser->implicit_template_scope->level_chain = return_to_scope;
+      current_binding_level = parser->implicit_template_scope;
+    }
+  else
+    return_to_scope = return_to_scope->level_chain;
+
+  finish_fully_implicit_template (parser, NULL);
+
+  gcc_assert (current_binding_level == return_to_scope);
+}
+
 /* Helper function for diagnostics that have complained about things
    being used with 'extern "C"' linkage.
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr84610.C b/gcc/testsuite/g++.dg/cpp0x/pr84610.C
new file mode 100644
index 000000000000..cc70748967bc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/pr84610.C
@@ -0,0 +1,3 @@
+// { dg-do compile { target c++11 } }
+
+a (int &__attribute__ ((aligned(auto x)) y)); // { dg-error "parameter declaration|before|expected constructor" }
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr84642.C b/gcc/testsuite/g++.dg/cpp0x/pr84642.C
new file mode 100644
index 000000000000..5c6895edb970
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/pr84642.C
@@ -0,0 +1,3 @@
+// { dg-do compile { target c++11 } }
+
+a(int(const &&__attribute__((b(auto;))))) // { dg-error "parameter declaration|with no type|before|expected constructor" }



Reset auto_is_implicit_function_template_parm_p while parsing declarators

for gcc/cp/ChangeLog

	PR c++/84610
	PR c++/84642
	* parser.c (cp_parser_parameter_declaration): Reset
	auto_is_implicit_function_template_parm_p while parsing
	declarators.

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 9f135cb4cda1..387d58814538 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -21520,6 +21520,10 @@ cp_parser_parameter_declaration (cp_parser *parser,
       return NULL;
     }
 
+  bool saved_auto_is_implicit_function_template_parm_p
+    = parser->auto_is_implicit_function_template_parm_p;
+  parser->auto_is_implicit_function_template_parm_p = false;
+
   /* Peek at the next token.  */
   token = cp_lexer_peek_token (parser->lexer);
 
@@ -21709,6 +21713,9 @@ cp_parser_parameter_declaration (cp_parser *parser,
 					decl_spec_token_start->location,
 					input_location);
 
+  parser->auto_is_implicit_function_template_parm_p
+    = saved_auto_is_implicit_function_template_parm_p;
+
   return make_parameter_declarator (&decl_specifiers,
 				    declarator,
 				    default_argument,


-- 
Alexandre Oliva, freedom fighter    http://FSFLA.org/~lxoliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/   FSF Latin America board member
Free Software Evangelist|Red Hat Brasil GNU Toolchain Engineer



More information about the Gcc-patches mailing list