aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2020-12-02 17:11:48 -0500
committerJason Merrill <jason@redhat.com>2020-12-02 22:15:54 -0500
commitc03a78d8f8cac3019e7bc67b6ae39f4edc61cf2c (patch)
treea91b22d64976926d88b622aa66919ff41cff528e /gcc
parent54f97a226a0d8b315aa1a0129df957a8bb3fdf65 (diff)
downloadgcc-c03a78d8f8cac3019e7bc67b6ae39f4edc61cf2c.zip
gcc-c03a78d8f8cac3019e7bc67b6ae39f4edc61cf2c.tar.gz
gcc-c03a78d8f8cac3019e7bc67b6ae39f4edc61cf2c.tar.bz2
c++: Push parms when late parsing default args
In this testcase we weren't catching the error in A::f because the parameter 'I' wasn't in scope, so the default argument for 'b' found the global typedef I. Fixed by pushing the parms before parsing. This is a bit complicated because pushdecl clears DECL_CHAIN; do_push_parm_decls deals with this by nreversing first, but that doesn't work here because we only want to push them one at a time; if we pushed all of them before parsing, we'd wrongly reject A::g. gcc/cp/ChangeLog: * parser.c (cp_parser_primary_expression): Distinguish parms from vars in error. (cp_parser_late_parsing_default_args): Pushdecl parms as we go. gcc/testsuite/ChangeLog: * g++.dg/parse/defarg17.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/parser.c32
-rw-r--r--gcc/testsuite/g++.dg/parse/defarg17.C11
2 files changed, 37 insertions, 6 deletions
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index a8e86cf..ef4d73d 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -5814,8 +5814,11 @@ cp_parser_primary_expression (cp_parser *parser,
if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
&& local_variable_p (decl))
{
- error_at (id_expression.get_location (),
- "local variable %qD may not appear in this context",
+ const char *msg
+ = (TREE_CODE (decl) == PARM_DECL
+ ? _("parameter %qD may not appear in this context")
+ : _("local variable %qD may not appear in this context"));
+ error_at (id_expression.get_location (), msg,
decl.get_value ());
return error_mark_node;
}
@@ -30551,7 +30554,6 @@ static void
cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
{
unsigned char saved_local_variables_forbidden_p;
- tree parm, parmdecl;
/* While we're parsing the default args, we might (due to the
statement expression extension) encounter more classes. We want
@@ -30568,11 +30570,18 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
begin_scope (sk_function_parms, fn);
- for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
- parmdecl = DECL_ARGUMENTS (fn);
+ /* Gather the PARM_DECLs into a vec so we can keep track of them when
+ pushdecl clears DECL_CHAIN. */
+ releasing_vec parms;
+ for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
+ parmdecl = DECL_CHAIN (parmdecl))
+ vec_safe_push (parms, parmdecl);
+
+ tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
+ for (int i = 0;
parm && parm != void_list_node;
parm = TREE_CHAIN (parm),
- parmdecl = DECL_CHAIN (parmdecl))
+ ++i)
{
tree default_arg = TREE_PURPOSE (parm);
tree parsed_arg;
@@ -30580,6 +30589,9 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
tree copy;
unsigned ix;
+ tree parmdecl = parms[i];
+ pushdecl (parmdecl);
+
if (!default_arg)
continue;
@@ -30602,6 +30614,14 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
pop_bindings_and_leave_scope ();
+ /* Restore DECL_CHAINs after clobbering by pushdecl. */
+ parm = NULL_TREE;
+ for (int i = parms->length () - 1; i >= 0; --i)
+ {
+ DECL_CHAIN (parms[i]) = parm;
+ parm = parms[i];
+ }
+
pop_defarg_context ();
/* Make sure no default arg is missing. */
diff --git a/gcc/testsuite/g++.dg/parse/defarg17.C b/gcc/testsuite/g++.dg/parse/defarg17.C
new file mode 100644
index 0000000..c39a819
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/defarg17.C
@@ -0,0 +1,11 @@
+typedef int I;
+
+int f(float I = 0.0, int b = I(2)); // { dg-error "parameter" }
+int g(int b = I(2), float I = 0.0);
+
+struct A
+{
+ int f(float I = 0.0, int b = I(2)); // { dg-error "parameter" }
+ int g(int b = I(2), float I = 0.0);
+};
+