public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: Push parms when late parsing default args
@ 2020-12-03  3:17 Jason Merrill
  2020-12-03 17:29 ` Rainer Orth
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2020-12-03  3:17 UTC (permalink / raw)
  To: gcc-patches

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.

Tested x86_64-pc-linux-gnu, applying to trunk.

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.
---
 gcc/cp/parser.c                       | 32 ++++++++++++++++++++++-----
 gcc/testsuite/g++.dg/parse/defarg17.C | 11 +++++++++
 2 files changed, 37 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/parse/defarg17.C

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);
+};
+

base-commit: 54f97a226a0d8b315aa1a0129df957a8bb3fdf65
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [pushed] c++: Push parms when late parsing default args
  2020-12-03  3:17 [pushed] c++: Push parms when late parsing default args Jason Merrill
@ 2020-12-03 17:29 ` Rainer Orth
  0 siblings, 0 replies; 2+ messages in thread
From: Rainer Orth @ 2020-12-03 17:29 UTC (permalink / raw)
  To: Jason Merrill via Gcc-patches

Hi Jason,

> 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.
>
> Tested x86_64-pc-linux-gnu, applying to trunk.
>
> 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.

this patch broke i386-pc-solaris2.11 and sparc-sun-solaris2.11 bootstrap
with gcc 8.1.0 in stage 1:

/vol/gcc/src/hg/master/local/gcc/cp/parser.c: In function 'void cp_parser_late_parsing_default_args(cp_parser*, tree)':
/vol/gcc/src/hg/master/local/gcc/cp/parser.c:30618:28: error: ambiguous overload for 'operator[]' (operand types are 'releasing_vec' and 'int')
       tree parmdecl = parms[i];
                            ^
/vol/gcc/src/hg/master/local/gcc/cp/parser.c:30618:28: note: candidate: 'operator[](releasing_vec::vec_t* {aka vec<tree_node*, va_gc>*}, int)' <built-in>
In file included from /vol/gcc/src/hg/master/local/gcc/cp/parser.c:25:
/vol/gcc/src/hg/master/local/gcc/cp/cp-tree.h:965:9: note: candidate: 'tree_node*& releasing_vec::operator[](unsigned int) const'
   tree& operator[] (unsigned i) const { return (*v)[i]; }
         ^~~~~~~~
In file included from /vol/gcc/src/hg/master/local/gcc/c-family/c-common.h:26,
                 from /vol/gcc/src/hg/master/local/gcc/cp/cp-tree.h:40,
                 from /vol/gcc/src/hg/master/local/gcc/cp/parser.c:25:
/vol/gcc/src/hg/master/local/gcc/cp/parser.c:30647:24: error: ambiguous overload for 'operator[]' (operand types are 'releasing_vec' and 'int')
       DECL_CHAIN (parms[i]) = parm;
                        ^
/vol/gcc/src/hg/master/local/gcc/tree.h:286:26: note: in definition of macro 'CONTAINS_STRUCT_CHECK'
 (contains_struct_check ((T), (STRUCT), __FILE__, __LINE__, __FUNCTION__))
                          ^
/vol/gcc/src/hg/master/local/gcc/tree.h:2424:27: note: in expansion of macro 'TREE_CHAIN'
 #define DECL_CHAIN(NODE) (TREE_CHAIN (DECL_MINIMAL_CHECK (NODE)))
[...]

	Rainer

-- 
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-12-03 17:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-03  3:17 [pushed] c++: Push parms when late parsing default args Jason Merrill
2020-12-03 17:29 ` Rainer Orth

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).