Commit 7446b353 by Alexandre Oliva Committed by Alexandre Oliva

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

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
parent aa1c9429
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> 2018-03-21 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/84972 PR c++/84972
......
...@@ -2264,6 +2264,8 @@ static tree synthesize_implicit_template_parm ...@@ -2264,6 +2264,8 @@ static tree synthesize_implicit_template_parm
(cp_parser *, tree); (cp_parser *, tree);
static tree finish_fully_implicit_template static tree finish_fully_implicit_template
(cp_parser *, tree); (cp_parser *, tree);
static void abort_fully_implicit_template
(cp_parser *);
/* Classes [gram.class] */ /* Classes [gram.class] */
...@@ -3585,7 +3587,7 @@ cp_parser_skip_to_end_of_statement (cp_parser* parser) ...@@ -3585,7 +3587,7 @@ cp_parser_skip_to_end_of_statement (cp_parser* parser)
/* Unwind generic function template scope if necessary. */ /* Unwind generic function template scope if necessary. */
if (parser->fully_implicit_function_template_p) if (parser->fully_implicit_function_template_p)
finish_fully_implicit_template (parser, /*member_decl_opt=*/0); abort_fully_implicit_template (parser);
while (true) while (true)
{ {
...@@ -3675,7 +3677,7 @@ cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) ...@@ -3675,7 +3677,7 @@ cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
/* Unwind generic function template scope if necessary. */ /* Unwind generic function template scope if necessary. */
if (parser->fully_implicit_function_template_p) 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) while (nesting_depth >= 0)
{ {
...@@ -39273,11 +39275,43 @@ finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt) ...@@ -39273,11 +39275,43 @@ finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
end_template_decl (); end_template_decl ();
parser->fully_implicit_function_template_p = false; parser->fully_implicit_function_template_p = false;
parser->implicit_template_parms = 0;
parser->implicit_template_scope = 0;
--parser->num_template_parameter_lists; --parser->num_template_parameter_lists;
return member_decl_opt; 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 /* Helper function for diagnostics that have complained about things
being used with 'extern "C"' linkage. being used with 'extern "C"' linkage.
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> 2018-03-21 Jakub Jelinek <jakub@redhat.com>
PR c/84999 PR c/84999
......
// { dg-do compile { target c++11 } }
a (int &__attribute__ ((aligned(auto x)) y)); // { dg-error "parameter declaration|before|expected constructor" }
// { dg-do compile { target c++11 } }
a(int(const &&__attribute__((b(auto;))))) // { dg-error "parameter declaration|with no type|before|expected constructor" }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment