From: Jason Merrill Date: Wed, 5 Mar 2014 17:53:28 +0000 (-0500) Subject: re PR c++/60361 (unexpected 'use of parameter outside function body' error) X-Git-Tag: releases/gcc-4.9.0~566 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d264d62dc6d381510bea36469d50175fa6a39c2;p=thirdparty%2Fgcc.git re PR c++/60361 (unexpected 'use of parameter outside function body' error) PR c++/60361 * parser.c (cp_parser_template_id): Don't set up a CPP_TEMPLATE_ID if re-parsing might succeed. * semantics.c (finish_id_expression): Use of a parameter outside the function body is a parse error. From-SVN: r208351 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 379ac736ed17..9e7b9b77c9cd 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,11 @@ 2014-03-05 Jason Merrill + PR c++/60361 + * parser.c (cp_parser_template_id): Don't set up a CPP_TEMPLATE_ID + if re-parsing might succeed. + * semantics.c (finish_id_expression): Use of a parameter outside + the function body is a parse error. + * parser.c (cp_parser_mem_initializer): Set input_location properly for init-list warning. (cp_parser_postfix_open_square_expression): Likewise. diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 5b3e48951acf..b56870d62255 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -13466,7 +13466,12 @@ cp_parser_template_id (cp_parser *parser, the effort required to do the parse, nor will we issue duplicate error messages about problems during instantiation of the template. */ - if (start_of_id) + if (start_of_id + /* Don't do this if we had a parse error in a declarator; re-parsing + might succeed if a name changes meaning (60361). */ + && !(cp_parser_error_occurred (parser) + && cp_parser_parsing_tentatively (parser) + && parser->in_declarator_p)) { cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 787eab81a64f..4081e0ec1d57 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -3246,7 +3246,7 @@ finish_id_expression (tree id_expression, && DECL_CONTEXT (decl) == NULL_TREE && !cp_unevaluated_operand) { - error ("use of parameter %qD outside function body", decl); + *error_msg = "use of parameter outside function body"; return error_mark_node; } } diff --git a/gcc/testsuite/g++.dg/parse/ambig7.C b/gcc/testsuite/g++.dg/parse/ambig7.C new file mode 100644 index 000000000000..9a5b8791d243 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/ambig7.C @@ -0,0 +1,16 @@ +// PR c++/60361 + +struct Helper +{ + Helper(int a, void (*pfunc)()); +}; + +template void function(); + +const int A = 1; +const int B = 2; + +Helper testOk(A, function); +Helper testOk2(int(A), function); +Helper testOk3((int(A)), function); +Helper testFail(int(A), function); diff --git a/gcc/testsuite/g++.dg/parse/parameter-declaration-2.C b/gcc/testsuite/g++.dg/parse/parameter-declaration-2.C index 6116630433f3..3c983cc748c4 100644 --- a/gcc/testsuite/g++.dg/parse/parameter-declaration-2.C +++ b/gcc/testsuite/g++.dg/parse/parameter-declaration-2.C @@ -1,2 +1,2 @@ -void f (int i, int p[i]); // { dg-error "use of parameter .i. outside function body" } +void f (int i, int p[i]); // { dg-error "use of parameter.*outside function body" } // { dg-prune-output "array bound" } diff --git a/gcc/testsuite/g++.dg/parse/typename7.C b/gcc/testsuite/g++.dg/parse/typename7.C index 6ec76961013c..e49a1ec39352 100644 --- a/gcc/testsuite/g++.dg/parse/typename7.C +++ b/gcc/testsuite/g++.dg/parse/typename7.C @@ -7,10 +7,9 @@ struct A { - template void foo(int); // { dg-message "note" } - template void bar(T t) { // { dg-message "note" } + template void foo(int); + template void bar(T t) { this->foo(t); } // { dg-error "expected|parse error|no matching" } - // { dg-message "candidate" "candidate note" { target *-*-* } 12 } template void bad(T t) { foo(t); } // { dg-error "expected|parse error|no matching" } }; @@ -20,7 +19,6 @@ struct B { void bar(T t) { A().bar(t); } // { dg-error "expected|parse error|no matching" } - // { dg-message "candidate" "candidate note" { target *-*-* } 22 } void bad(T t) { B::bar(t); } // { dg-error "invalid|qualified-id|not a template" } };