public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: Fix late-parsed default arg context
@ 2020-12-03  3:15 Jason Merrill
  2020-12-03  9:29 ` [PATCH] c++: consteval-defarg1.C test variant for templates Jakub Jelinek
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Merrill @ 2020-12-03  3:15 UTC (permalink / raw)
  To: gcc-patches; +Cc: jakub

Jakub noticed that we weren't recognizing a default argument for a consteval
member function as being in immediate function context because there was no
function parameter scope to look at.

Note that this patch doesn't actually push the parameters into the scope,
that happens in a separate commit.

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

gcc/cp/ChangeLog:

	* name-lookup.c (begin_scope): Set immediate_fn_ctx_p.
	* parser.c (cp_parser_late_parsing_default_args): Push
	sk_function_parms scope.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/consteval-defarg1.C: New test.
---
 gcc/cp/name-lookup.c                           |  7 ++++++-
 gcc/cp/parser.c                                |  4 ++++
 gcc/testsuite/g++.dg/cpp2a/consteval-defarg1.C | 11 +++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/consteval-defarg1.C

diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index 837c0ea89af..c87d151b441 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -3291,12 +3291,17 @@ begin_scope (scope_kind kind, tree entity)
     case sk_cond:
     case sk_class:
     case sk_scoped_enum:
-    case sk_function_parms:
     case sk_transaction:
     case sk_omp:
       scope->keep = keep_next_level_flag;
       break;
 
+    case sk_function_parms:
+      scope->keep = keep_next_level_flag;
+      if (entity)
+	scope->immediate_fn_ctx_p = DECL_IMMEDIATE_FUNCTION_P (entity);
+      break;
+
     case sk_namespace:
       NAMESPACE_LEVEL (entity) = scope;
       break;
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 52743b03be2..a8e86cf250d 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -30566,6 +30566,8 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
 
   push_defarg_context (fn);
 
+  begin_scope (sk_function_parms, fn);
+
   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
 	 parmdecl = DECL_ARGUMENTS (fn);
        parm && parm != void_list_node;
@@ -30598,6 +30600,8 @@ cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
 	TREE_PURPOSE (copy) = parsed_arg;
     }
 
+  pop_bindings_and_leave_scope ();
+
   pop_defarg_context ();
 
   /* Make sure no default arg is missing.  */
diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval-defarg1.C b/gcc/testsuite/g++.dg/cpp2a/consteval-defarg1.C
new file mode 100644
index 00000000000..826ee254c20
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/consteval-defarg1.C
@@ -0,0 +1,11 @@
+// Test that late-parsed default args have the same consteval semantics.
+// { dg-do compile { target c++20 } }
+
+consteval bool foo (bool x) { if (x) throw 1; return false; }
+consteval bool bar (bool x = foo (true)) { return true; }
+struct S
+{
+  consteval static bool baz (bool x = foo (true)) { return true; }
+};
+constexpr bool a = bar (true);
+constexpr bool b = S::baz (true);

base-commit: 2847d7d28ea79e2f93049fad16f931b6705c9fff
-- 
2.27.0


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

* [PATCH] c++: consteval-defarg1.C test variant for templates
  2020-12-03  3:15 [pushed] c++: Fix late-parsed default arg context Jason Merrill
@ 2020-12-03  9:29 ` Jakub Jelinek
  2020-12-03 14:04   ` Jason Merrill
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Jelinek @ 2020-12-03  9:29 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

On Wed, Dec 02, 2020 at 10:15:25PM -0500, Jason Merrill wrote:
> Jakub noticed that we weren't recognizing a default argument for a consteval
> member function as being in immediate function context because there was no
> function parameter scope to look at.
> 
> Note that this patch doesn't actually push the parameters into the scope,
> that happens in a separate commit.

Shouldn't we also be testing how it behaves in templates?

The following testcase is an attempt to test both non-dependent and
dependent consteval calls in both function and class templates, and with
your committed patch it now passes.

Ok for trunk?

2020-12-03  Jakub Jelinek  <jakub@redhat.com>

	* g++.dg/cpp2a/consteval-defarg2.C: New test.

--- gcc/testsuite/g++.dg/cpp2a/consteval-defarg2.C.jj	2020-12-03 10:26:16.340256056 +0100
+++ gcc/testsuite/g++.dg/cpp2a/consteval-defarg2.C	2020-12-03 10:19:33.215853790 +0100
@@ -0,0 +1,29 @@
+// Test that late-parsed default args have the same consteval semantics.
+// { dg-do compile { target c++20 } }
+
+template <int N>
+consteval bool foo (bool x) { if (x) throw N; return false; }
+consteval bool qux (bool x) { if (x) throw 1; return false; }
+template <int N>
+consteval bool bar (bool x = foo<N> (true)) { return true; }
+template <int N>
+consteval bool corge (bool x = qux (true)) { return true; }
+template <int N>
+struct S
+{
+  consteval static bool baz (bool x = foo<N> (true)) { return true; }
+  consteval static bool garply (bool x = qux (true)) { return true; }
+};
+struct T
+{
+  template <int N>
+  consteval static bool baz (bool x = foo<N> (true)) { return true; }
+  template <int N>
+  consteval static bool garply (bool x = qux (true)) { return true; }
+};
+constexpr bool a = bar<0> (true);
+constexpr bool b = corge<0> (true);
+constexpr bool c = S<0>::baz (true);
+constexpr bool d = S<0>::garply (true);
+constexpr bool e = T::baz<0> (true);
+constexpr bool f = T::garply<0> (true);


	Jakub


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

* Re: [PATCH] c++: consteval-defarg1.C test variant for templates
  2020-12-03  9:29 ` [PATCH] c++: consteval-defarg1.C test variant for templates Jakub Jelinek
@ 2020-12-03 14:04   ` Jason Merrill
  0 siblings, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2020-12-03 14:04 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 12/3/20 4:29 AM, Jakub Jelinek wrote:
> On Wed, Dec 02, 2020 at 10:15:25PM -0500, Jason Merrill wrote:
>> Jakub noticed that we weren't recognizing a default argument for a consteval
>> member function as being in immediate function context because there was no
>> function parameter scope to look at.
>>
>> Note that this patch doesn't actually push the parameters into the scope,
>> that happens in a separate commit.
> 
> Shouldn't we also be testing how it behaves in templates?
> 
> The following testcase is an attempt to test both non-dependent and
> dependent consteval calls in both function and class templates, and with
> your committed patch it now passes.
> 
> Ok for trunk?

OK, thanks.

> 2020-12-03  Jakub Jelinek  <jakub@redhat.com>
> 
> 	* g++.dg/cpp2a/consteval-defarg2.C: New test.
> 
> --- gcc/testsuite/g++.dg/cpp2a/consteval-defarg2.C.jj	2020-12-03 10:26:16.340256056 +0100
> +++ gcc/testsuite/g++.dg/cpp2a/consteval-defarg2.C	2020-12-03 10:19:33.215853790 +0100
> @@ -0,0 +1,29 @@
> +// Test that late-parsed default args have the same consteval semantics.
> +// { dg-do compile { target c++20 } }
> +
> +template <int N>
> +consteval bool foo (bool x) { if (x) throw N; return false; }
> +consteval bool qux (bool x) { if (x) throw 1; return false; }
> +template <int N>
> +consteval bool bar (bool x = foo<N> (true)) { return true; }
> +template <int N>
> +consteval bool corge (bool x = qux (true)) { return true; }
> +template <int N>
> +struct S
> +{
> +  consteval static bool baz (bool x = foo<N> (true)) { return true; }
> +  consteval static bool garply (bool x = qux (true)) { return true; }
> +};
> +struct T
> +{
> +  template <int N>
> +  consteval static bool baz (bool x = foo<N> (true)) { return true; }
> +  template <int N>
> +  consteval static bool garply (bool x = qux (true)) { return true; }
> +};
> +constexpr bool a = bar<0> (true);
> +constexpr bool b = corge<0> (true);
> +constexpr bool c = S<0>::baz (true);
> +constexpr bool d = S<0>::garply (true);
> +constexpr bool e = T::baz<0> (true);
> +constexpr bool f = T::garply<0> (true);
> 
> 
> 	Jakub
> 


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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-03  3:15 [pushed] c++: Fix late-parsed default arg context Jason Merrill
2020-12-03  9:29 ` [PATCH] c++: consteval-defarg1.C test variant for templates Jakub Jelinek
2020-12-03 14:04   ` 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).