public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-5695] c++: Push parms when late parsing default args
@ 2020-12-03  3:17 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2020-12-03  3:17 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:c03a78d8f8cac3019e7bc67b6ae39f4edc61cf2c

commit r11-5695-gc03a78d8f8cac3019e7bc67b6ae39f4edc61cf2c
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Dec 2 17:11:48 2020 -0500

    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.

Diff:
---
 gcc/cp/parser.c                       | 32 ++++++++++++++++++++++++++------
 gcc/testsuite/g++.dg/parse/defarg17.C | 11 +++++++++++
 2 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index a8e86cf250d..ef4d73d6161 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 00000000000..c39a819c723
--- /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);
+};
+


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-12-03  3:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-03  3:17 [gcc r11-5695] c++: Push parms when late parsing default args Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).