[PR c++/84610,84642] recover from implicit template parms gracefully
authorAlexandre Oliva <aoliva@redhat.com>
Wed, 21 Mar 2018 22:08:19 +0000 (22:08 +0000)
committerAlexandre Oliva <aoliva@gcc.gnu.org>
Wed, 21 Mar 2018 22:08:19 +0000 (22:08 +0000)
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.

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.

From-SVN: r258748

gcc/cp/ChangeLog
gcc/cp/parser.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/pr84610.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/pr84642.C [new file with mode: 0644]

index aa8ba26959b9195811bd0cf473403a5759b3b51c..233b57e2620005db621eb1b50881b925c00a5963 100644 (file)
@@ -1,3 +1,13 @@
+2018-03-21  Alexandre Oliva <aoliva@redhat.com>
+
+       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.
+
 2018-03-21  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/84972
index 4e3e1dccda0effa112627460d5cd78384c7d96df..6dcfae125b7bff0c34c31baa85dfbb41f697623a 100644 (file)
@@ -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)
     {
@@ -39273,11 +39275,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.
 
index 93dc7a28c681ec33ce8f59eefa68e86594153d7c..45705c0eacbae94879d7f4356f494393f00432e6 100644 (file)
@@ -1,3 +1,10 @@
+2018-03-21  Alexandre Oliva <aoliva@redhat.com>
+
+       PR c++/84610
+       PR c++/84642
+       * g++.dg/cpp0x/pr84610.C: New.
+       * g++.dg/cpp0x/pr84642.C: New.
+
 2018-03-21  Jakub Jelinek  <jakub@redhat.com>
 
        PR c/84999
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr84610.C b/gcc/testsuite/g++.dg/cpp0x/pr84610.C
new file mode 100644 (file)
index 0000000..cc70748
--- /dev/null
@@ -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 (file)
index 0000000..5c6895e
--- /dev/null
@@ -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" }