public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: ICE with noexcept and local specialization, again [PR114349]
@ 2024-03-21 21:01 Marek Polacek
  2024-03-21 21:27 ` Jason Merrill
  0 siblings, 1 reply; 5+ messages in thread
From: Marek Polacek @ 2024-03-21 21:01 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill, Patrick Palka

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
we're dealing with a noexcept-spec there, not a noexcept-expr, so
setting cp_noexcept_operand et al is incorrect.  Back to the drawing
board then.

To fix noexcept84.C, we should probably avoid doing push_to_top_level
in certain cases.  Patrick suggested checking:

  const bool push_to_top = current_function_decl != fn;

which works, but I'm not sure I follow the logic there.  I also came
up with

  const bool push_to_top = !decl_function_context (fn);

which also works.  But ultimately I went with !DECL_TEMPLATE_INSTANTIATED;
if DECL_TEMPLATE_INSTANTIATED is set, we've already pushed to top level
if it was necessary in instantiate_body.

This also fixes c++/114349, introduced by r14-9339.

	PR c++/114349

gcc/cp/ChangeLog:

	* pt.cc (maybe_instantiate_noexcept): Don't push_to_top_level if
	fn has already been instantiated.  Don't save/restore
	cp_unevaluated_operand, c_inhibit_evaluation_warnings, and
	cp_noexcept_operand around the tsubst_expr call.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/noexcept85.C: New test.
	* g++.dg/cpp0x/noexcept86.C: New test.
---
 gcc/cp/pt.cc                            | 17 ++++++-------
 gcc/testsuite/g++.dg/cpp0x/noexcept85.C | 33 +++++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/noexcept86.C | 25 +++++++++++++++++++
 3 files changed, 66 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept85.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept86.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 8cf0d5b7a8d..b7ebd93ac7d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -26855,7 +26855,12 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	}
       else if (push_tinst_level (fn))
 	{
-	  push_to_top_level ();
+	  /* If we've already instantiated FN, there's no need to push to
+	     top level (as instantiate_body already pushed to top level if
+	     needed).  */
+	  const bool push_to_top = !DECL_TEMPLATE_INSTANTIATED (fn);
+	  if (push_to_top)
+	    push_to_top_level ();
 	  push_access_scope (fn);
 	  push_deferring_access_checks (dk_no_deferred);
 	  input_location = DECL_SOURCE_LOCATION (fn);
@@ -26878,17 +26883,10 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	  if (orig_fn)
 	    ++processing_template_decl;
 
-	  ++cp_unevaluated_operand;
-	  ++c_inhibit_evaluation_warnings;
-	  ++cp_noexcept_operand;
 	  /* Do deferred instantiation of the noexcept-specifier.  */
 	  noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex),
 			      DEFERRED_NOEXCEPT_ARGS (noex),
 			      tf_warning_or_error, fn);
-	  --cp_unevaluated_operand;
-	  --c_inhibit_evaluation_warnings;
-	  --cp_noexcept_operand;
-
 	  /* Build up the noexcept-specification.  */
 	  spec = build_noexcept_spec (noex, tf_warning_or_error);
 
@@ -26898,7 +26896,8 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	  pop_deferring_access_checks ();
 	  pop_access_scope (fn);
 	  pop_tinst_level ();
-	  pop_from_top_level ();
+	  if (push_to_top)
+	    pop_from_top_level ();
 	}
       else
 	spec = noexcept_false_spec;
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept85.C b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
new file mode 100644
index 00000000000..b415bb46bc9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
@@ -0,0 +1,33 @@
+// PR c++/114349
+// { dg-do compile { target c++11 } }
+
+using A = struct {};
+template <template <typename> class, typename, typename>
+using B = A;
+template <typename T>
+using C = typename T::D;
+struct E {
+  using D = B<C, int, A>;
+};
+template <class> constexpr bool foo (A) { return false; }
+template <class T> struct F {
+  using G = T;
+  using H = E;
+  F(const F &);
+  void operator=(F) noexcept(foo <G> (H::D{}));
+};
+template <typename, typename, typename>
+using I = F<int>;
+template <typename K, typename V, typename H = K>
+using J = I<K, V, H>;
+struct K {
+  typedef J<long, char> L;
+  L k;
+  K();
+};
+struct M {
+  bool bar () const;
+  K::L m;
+};
+K n;
+bool M::bar () const { n.k = m; return true; }
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept86.C b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
new file mode 100644
index 00000000000..2d040c090f5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
@@ -0,0 +1,25 @@
+// PR c++/114349
+// { dg-do compile { target c++14 } }
+
+struct B
+{
+  int i;
+};
+
+template <bool BA>
+void
+goo ()
+{
+  constexpr bool is_yes = BA;
+  struct C
+  {
+    static auto g(B b) noexcept(is_yes) { }
+  };
+  C::g({});
+}
+
+void
+x ()
+{
+  goo<false>();
+}

base-commit: 48d49200510198cafcab55601cd8e5f8eb541f01
-- 
2.44.0


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

* Re: [PATCH] c++: ICE with noexcept and local specialization, again [PR114349]
  2024-03-21 21:01 [PATCH] c++: ICE with noexcept and local specialization, again [PR114349] Marek Polacek
@ 2024-03-21 21:27 ` Jason Merrill
  2024-03-22 21:30   ` [PATCH v2] " Marek Polacek
  0 siblings, 1 reply; 5+ messages in thread
From: Jason Merrill @ 2024-03-21 21:27 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches, Patrick Palka

On 3/21/24 17:01, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> -- >8 --
> Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
> we're dealing with a noexcept-spec there, not a noexcept-expr, so
> setting cp_noexcept_operand et al is incorrect.  Back to the drawing
> board then.
> 
> To fix noexcept84.C, we should probably avoid doing push_to_top_level
> in certain cases.  Patrick suggested checking:
> 
>    const bool push_to_top = current_function_decl != fn;
> 
> which works, but I'm not sure I follow the logic there.  I also came
> up with
> 
>    const bool push_to_top = !decl_function_context (fn);
> 
> which also works.  But ultimately I went with !DECL_TEMPLATE_INSTANTIATED;
> if DECL_TEMPLATE_INSTANTIATED is set, we've already pushed to top level
> if it was necessary in instantiate_body.

This sort of thing is what maybe_push_to_top_level is for, does that 
also work?

> This also fixes c++/114349, introduced by r14-9339.
> 
> 	PR c++/114349
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (maybe_instantiate_noexcept): Don't push_to_top_level if
> 	fn has already been instantiated.  Don't save/restore
> 	cp_unevaluated_operand, c_inhibit_evaluation_warnings, and
> 	cp_noexcept_operand around the tsubst_expr call.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/noexcept85.C: New test.
> 	* g++.dg/cpp0x/noexcept86.C: New test.
> ---
>   gcc/cp/pt.cc                            | 17 ++++++-------
>   gcc/testsuite/g++.dg/cpp0x/noexcept85.C | 33 +++++++++++++++++++++++++
>   gcc/testsuite/g++.dg/cpp0x/noexcept86.C | 25 +++++++++++++++++++
>   3 files changed, 66 insertions(+), 9 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept85.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept86.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 8cf0d5b7a8d..b7ebd93ac7d 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -26855,7 +26855,12 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
>   	}
>         else if (push_tinst_level (fn))
>   	{
> -	  push_to_top_level ();
> +	  /* If we've already instantiated FN, there's no need to push to
> +	     top level (as instantiate_body already pushed to top level if
> +	     needed).  */
> +	  const bool push_to_top = !DECL_TEMPLATE_INSTANTIATED (fn);
> +	  if (push_to_top)
> +	    push_to_top_level ();
>   	  push_access_scope (fn);
>   	  push_deferring_access_checks (dk_no_deferred);
>   	  input_location = DECL_SOURCE_LOCATION (fn);
> @@ -26878,17 +26883,10 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
>   	  if (orig_fn)
>   	    ++processing_template_decl;
>   
> -	  ++cp_unevaluated_operand;
> -	  ++c_inhibit_evaluation_warnings;
> -	  ++cp_noexcept_operand;
>   	  /* Do deferred instantiation of the noexcept-specifier.  */
>   	  noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex),
>   			      DEFERRED_NOEXCEPT_ARGS (noex),
>   			      tf_warning_or_error, fn);
> -	  --cp_unevaluated_operand;
> -	  --c_inhibit_evaluation_warnings;
> -	  --cp_noexcept_operand;
> -
>   	  /* Build up the noexcept-specification.  */
>   	  spec = build_noexcept_spec (noex, tf_warning_or_error);
>   
> @@ -26898,7 +26896,8 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
>   	  pop_deferring_access_checks ();
>   	  pop_access_scope (fn);
>   	  pop_tinst_level ();
> -	  pop_from_top_level ();
> +	  if (push_to_top)
> +	    pop_from_top_level ();
>   	}
>         else
>   	spec = noexcept_false_spec;
> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept85.C b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
> new file mode 100644
> index 00000000000..b415bb46bc9
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
> @@ -0,0 +1,33 @@
> +// PR c++/114349
> +// { dg-do compile { target c++11 } }
> +
> +using A = struct {};
> +template <template <typename> class, typename, typename>
> +using B = A;
> +template <typename T>
> +using C = typename T::D;
> +struct E {
> +  using D = B<C, int, A>;
> +};
> +template <class> constexpr bool foo (A) { return false; }
> +template <class T> struct F {
> +  using G = T;
> +  using H = E;
> +  F(const F &);
> +  void operator=(F) noexcept(foo <G> (H::D{}));
> +};
> +template <typename, typename, typename>
> +using I = F<int>;
> +template <typename K, typename V, typename H = K>
> +using J = I<K, V, H>;
> +struct K {
> +  typedef J<long, char> L;
> +  L k;
> +  K();
> +};
> +struct M {
> +  bool bar () const;
> +  K::L m;
> +};
> +K n;
> +bool M::bar () const { n.k = m; return true; }
> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept86.C b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
> new file mode 100644
> index 00000000000..2d040c090f5
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
> @@ -0,0 +1,25 @@
> +// PR c++/114349
> +// { dg-do compile { target c++14 } }
> +
> +struct B
> +{
> +  int i;
> +};
> +
> +template <bool BA>
> +void
> +goo ()
> +{
> +  constexpr bool is_yes = BA;
> +  struct C
> +  {
> +    static auto g(B b) noexcept(is_yes) { }
> +  };
> +  C::g({});
> +}
> +
> +void
> +x ()
> +{
> +  goo<false>();
> +}
> 
> base-commit: 48d49200510198cafcab55601cd8e5f8eb541f01


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

* [PATCH v2] c++: ICE with noexcept and local specialization, again [PR114349]
  2024-03-21 21:27 ` Jason Merrill
@ 2024-03-22 21:30   ` Marek Polacek
  2024-03-25 19:40     ` Jason Merrill
  0 siblings, 1 reply; 5+ messages in thread
From: Marek Polacek @ 2024-03-22 21:30 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches, Patrick Palka

On Thu, Mar 21, 2024 at 05:27:37PM -0400, Jason Merrill wrote:
> On 3/21/24 17:01, Marek Polacek wrote:
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > -- >8 --
> > Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
> > we're dealing with a noexcept-spec there, not a noexcept-expr, so
> > setting cp_noexcept_operand et al is incorrect.  Back to the drawing
> > board then.
> > 
> > To fix noexcept84.C, we should probably avoid doing push_to_top_level
> > in certain cases.  Patrick suggested checking:
> > 
> >    const bool push_to_top = current_function_decl != fn;
> > 
> > which works, but I'm not sure I follow the logic there.  I also came
> > up with
> > 
> >    const bool push_to_top = !decl_function_context (fn);
> > 
> > which also works.  But ultimately I went with !DECL_TEMPLATE_INSTANTIATED;
> > if DECL_TEMPLATE_INSTANTIATED is set, we've already pushed to top level
> > if it was necessary in instantiate_body.
> 
> This sort of thing is what maybe_push_to_top_level is for, does that also
> work?

Sadly -- and I should have mentioned that -- no.  maybe_push_to_top_level asks:

  bool push_to_top
    = !(current_function_decl
   && !LAMBDA_FUNCTION_P (d)
   && decl_function_context (d) == current_function_decl);

here both d and current_function_decl are test()::S::S(), and
decl_function_context (d) is test().  (current_function_decl was
set to test()::S::S() by an earlier push_access_scope call.)

But I want it to work, and I think using maybe_ would be a way nicer
fix.  So what if we don't push to top level if decl_function_context
is non-null?  I had to add the LAMBDA_TYPE_P check though: it looks
that we always have to push to top level for lambdas, but sometimes
we get a lambda's TYPE_DECL, and LAMBDA_FUNCTION_P doesn't catch
that.  An example is lambda-nested4.C.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
we're dealing with a noexcept-spec there, not a noexcept-expr, so
setting cp_noexcept_operand et al is incorrect.  Back to the drawing
board then.

To fix noexcept84.C, we should probably avoid doing push_to_top_level
in certain cases.  maybe_push_to_top_level didn't work here as-is, so
I changed it to not push to top level if decl_function_context is
non-null, when we are not dealing with a lambda.

This also fixes c++/114349, introduced by r14-9339.

	PR c++/114349

gcc/cp/ChangeLog:

	* name-lookup.cc (maybe_push_to_top_level): For a non-lambda,
	don't push to top level if decl_function_context is non-null.
	* pt.cc (maybe_instantiate_noexcept): Use maybe_push_to_top_level.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/noexcept85.C: New test.
	* g++.dg/cpp0x/noexcept86.C: New test.
---
 gcc/cp/name-lookup.cc                   | 12 ++++++---
 gcc/cp/pt.cc                            | 11 ++-------
 gcc/testsuite/g++.dg/cpp0x/noexcept85.C | 33 +++++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/noexcept86.C | 25 +++++++++++++++++++
 4 files changed, 68 insertions(+), 13 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept85.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept86.C

diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index dce4caf8981..4b2b27bdd0d 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -8664,10 +8664,14 @@ maybe_push_to_top_level (tree d)
 {
   /* Push if D isn't function-local, or is a lambda function, for which name
      resolution is already done.  */
-  bool push_to_top
-    = !(current_function_decl
-	&& !LAMBDA_FUNCTION_P (d)
-	&& decl_function_context (d) == current_function_decl);
+  const bool push_to_top
+    = (LAMBDA_FUNCTION_P (d)
+       || (TREE_CODE (d) == TYPE_DECL
+	   && TREE_TYPE (d)
+	   && LAMBDA_TYPE_P (TREE_TYPE (d)))
+       || !current_function_decl
+       || (!decl_function_context (d)
+	   && decl_function_context (d) != current_function_decl));
 
   if (push_to_top)
     push_to_top_level ();
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 8cf0d5b7a8d..7b00a8615d2 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -26855,7 +26855,7 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	}
       else if (push_tinst_level (fn))
 	{
-	  push_to_top_level ();
+	  const bool push_to_top = maybe_push_to_top_level (fn);
 	  push_access_scope (fn);
 	  push_deferring_access_checks (dk_no_deferred);
 	  input_location = DECL_SOURCE_LOCATION (fn);
@@ -26878,17 +26878,10 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	  if (orig_fn)
 	    ++processing_template_decl;
 
-	  ++cp_unevaluated_operand;
-	  ++c_inhibit_evaluation_warnings;
-	  ++cp_noexcept_operand;
 	  /* Do deferred instantiation of the noexcept-specifier.  */
 	  noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex),
 			      DEFERRED_NOEXCEPT_ARGS (noex),
 			      tf_warning_or_error, fn);
-	  --cp_unevaluated_operand;
-	  --c_inhibit_evaluation_warnings;
-	  --cp_noexcept_operand;
-
 	  /* Build up the noexcept-specification.  */
 	  spec = build_noexcept_spec (noex, tf_warning_or_error);
 
@@ -26898,7 +26891,7 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	  pop_deferring_access_checks ();
 	  pop_access_scope (fn);
 	  pop_tinst_level ();
-	  pop_from_top_level ();
+	  maybe_pop_from_top_level (push_to_top);
 	}
       else
 	spec = noexcept_false_spec;
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept85.C b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
new file mode 100644
index 00000000000..b415bb46bc9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
@@ -0,0 +1,33 @@
+// PR c++/114349
+// { dg-do compile { target c++11 } }
+
+using A = struct {};
+template <template <typename> class, typename, typename>
+using B = A;
+template <typename T>
+using C = typename T::D;
+struct E {
+  using D = B<C, int, A>;
+};
+template <class> constexpr bool foo (A) { return false; }
+template <class T> struct F {
+  using G = T;
+  using H = E;
+  F(const F &);
+  void operator=(F) noexcept(foo <G> (H::D{}));
+};
+template <typename, typename, typename>
+using I = F<int>;
+template <typename K, typename V, typename H = K>
+using J = I<K, V, H>;
+struct K {
+  typedef J<long, char> L;
+  L k;
+  K();
+};
+struct M {
+  bool bar () const;
+  K::L m;
+};
+K n;
+bool M::bar () const { n.k = m; return true; }
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept86.C b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
new file mode 100644
index 00000000000..2d040c090f5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
@@ -0,0 +1,25 @@
+// PR c++/114349
+// { dg-do compile { target c++14 } }
+
+struct B
+{
+  int i;
+};
+
+template <bool BA>
+void
+goo ()
+{
+  constexpr bool is_yes = BA;
+  struct C
+  {
+    static auto g(B b) noexcept(is_yes) { }
+  };
+  C::g({});
+}
+
+void
+x ()
+{
+  goo<false>();
+}

base-commit: 65b7d1862e11784a0ce67ab758e06dd8aa65b181
-- 
2.44.0


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

* Re: [PATCH v2] c++: ICE with noexcept and local specialization, again [PR114349]
  2024-03-22 21:30   ` [PATCH v2] " Marek Polacek
@ 2024-03-25 19:40     ` Jason Merrill
  2024-03-25 22:17       ` [PATCH v3] " Marek Polacek
  0 siblings, 1 reply; 5+ messages in thread
From: Jason Merrill @ 2024-03-25 19:40 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches, Patrick Palka

On 3/22/24 17:30, Marek Polacek wrote:
> On Thu, Mar 21, 2024 at 05:27:37PM -0400, Jason Merrill wrote:
>> On 3/21/24 17:01, Marek Polacek wrote:
>>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>>>
>>> -- >8 --
>>> Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
>>> we're dealing with a noexcept-spec there, not a noexcept-expr, so
>>> setting cp_noexcept_operand et al is incorrect.  Back to the drawing
>>> board then.
>>>
>>> To fix noexcept84.C, we should probably avoid doing push_to_top_level
>>> in certain cases.  Patrick suggested checking:
>>>
>>>     const bool push_to_top = current_function_decl != fn;
>>>
>>> which works, but I'm not sure I follow the logic there.  I also came
>>> up with
>>>
>>>     const bool push_to_top = !decl_function_context (fn);
>>>
>>> which also works.  But ultimately I went with !DECL_TEMPLATE_INSTANTIATED;
>>> if DECL_TEMPLATE_INSTANTIATED is set, we've already pushed to top level
>>> if it was necessary in instantiate_body.
>>
>> This sort of thing is what maybe_push_to_top_level is for, does that also
>> work?
> 
> Sadly -- and I should have mentioned that -- no.  maybe_push_to_top_level asks:
> 
>    bool push_to_top
>      = !(current_function_decl
>     && !LAMBDA_FUNCTION_P (d)
>     && decl_function_context (d) == current_function_decl);
> 
> here both d and current_function_decl are test()::S::S(), and
> decl_function_context (d) is test().  (current_function_decl was
> set to test()::S::S() by an earlier push_access_scope call.)
> 
> But I want it to work, and I think using maybe_ would be a way nicer
> fix.  So what if we don't push to top level if decl_function_context
> is non-null?  I had to add the LAMBDA_TYPE_P check though: it looks
> that we always have to push to top level for lambdas, but sometimes
> we get a lambda's TYPE_DECL, and LAMBDA_FUNCTION_P doesn't catch
> that.  An example is lambda-nested4.C.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> -- >8 --
> Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
> we're dealing with a noexcept-spec there, not a noexcept-expr, so
> setting cp_noexcept_operand et al is incorrect.  Back to the drawing
> board then.
> 
> To fix noexcept84.C, we should probably avoid doing push_to_top_level
> in certain cases.  maybe_push_to_top_level didn't work here as-is, so
> I changed it to not push to top level if decl_function_context is
> non-null, when we are not dealing with a lambda.
> 
> This also fixes c++/114349, introduced by r14-9339.
> 
> 	PR c++/114349
> 
> gcc/cp/ChangeLog:
> 
> 	* name-lookup.cc (maybe_push_to_top_level): For a non-lambda,
> 	don't push to top level if decl_function_context is non-null.
> 	* pt.cc (maybe_instantiate_noexcept): Use maybe_push_to_top_level.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/noexcept85.C: New test.
> 	* g++.dg/cpp0x/noexcept86.C: New test.
> ---
>   gcc/cp/name-lookup.cc                   | 12 ++++++---
>   gcc/cp/pt.cc                            | 11 ++-------
>   gcc/testsuite/g++.dg/cpp0x/noexcept85.C | 33 +++++++++++++++++++++++++
>   gcc/testsuite/g++.dg/cpp0x/noexcept86.C | 25 +++++++++++++++++++
>   4 files changed, 68 insertions(+), 13 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept85.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept86.C
> 
> diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
> index dce4caf8981..4b2b27bdd0d 100644
> --- a/gcc/cp/name-lookup.cc
> +++ b/gcc/cp/name-lookup.cc
> @@ -8664,10 +8664,14 @@ maybe_push_to_top_level (tree d)
>   {
>     /* Push if D isn't function-local, or is a lambda function, for which name
>        resolution is already done.  */
> -  bool push_to_top
> -    = !(current_function_decl
> -	&& !LAMBDA_FUNCTION_P (d)
> -	&& decl_function_context (d) == current_function_decl);
> +  const bool push_to_top
> +    = (LAMBDA_FUNCTION_P (d)
> +       || (TREE_CODE (d) == TYPE_DECL
> +	   && TREE_TYPE (d)
> +	   && LAMBDA_TYPE_P (TREE_TYPE (d)))
> +       || !current_function_decl
> +       || (!decl_function_context (d)
> +	   && decl_function_context (d) != current_function_decl));

This line seems unnecessary; the case it excludes is when 
decl_function_context and current_function_decl are both null, but if 
current_function_decl is null we already succeeded.

OK with this line removed.

>     if (push_to_top)
>       push_to_top_level ();
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 8cf0d5b7a8d..7b00a8615d2 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -26855,7 +26855,7 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
>   	}
>         else if (push_tinst_level (fn))
>   	{
> -	  push_to_top_level ();
> +	  const bool push_to_top = maybe_push_to_top_level (fn);
>   	  push_access_scope (fn);
>   	  push_deferring_access_checks (dk_no_deferred);
>   	  input_location = DECL_SOURCE_LOCATION (fn);
> @@ -26878,17 +26878,10 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
>   	  if (orig_fn)
>   	    ++processing_template_decl;
>   
> -	  ++cp_unevaluated_operand;
> -	  ++c_inhibit_evaluation_warnings;
> -	  ++cp_noexcept_operand;
>   	  /* Do deferred instantiation of the noexcept-specifier.  */
>   	  noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex),
>   			      DEFERRED_NOEXCEPT_ARGS (noex),
>   			      tf_warning_or_error, fn);
> -	  --cp_unevaluated_operand;
> -	  --c_inhibit_evaluation_warnings;
> -	  --cp_noexcept_operand;
> -
>   	  /* Build up the noexcept-specification.  */
>   	  spec = build_noexcept_spec (noex, tf_warning_or_error);
>   
> @@ -26898,7 +26891,7 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
>   	  pop_deferring_access_checks ();
>   	  pop_access_scope (fn);
>   	  pop_tinst_level ();
> -	  pop_from_top_level ();
> +	  maybe_pop_from_top_level (push_to_top);
>   	}
>         else
>   	spec = noexcept_false_spec;
> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept85.C b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
> new file mode 100644
> index 00000000000..b415bb46bc9
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
> @@ -0,0 +1,33 @@
> +// PR c++/114349
> +// { dg-do compile { target c++11 } }
> +
> +using A = struct {};
> +template <template <typename> class, typename, typename>
> +using B = A;
> +template <typename T>
> +using C = typename T::D;
> +struct E {
> +  using D = B<C, int, A>;
> +};
> +template <class> constexpr bool foo (A) { return false; }
> +template <class T> struct F {
> +  using G = T;
> +  using H = E;
> +  F(const F &);
> +  void operator=(F) noexcept(foo <G> (H::D{}));
> +};
> +template <typename, typename, typename>
> +using I = F<int>;
> +template <typename K, typename V, typename H = K>
> +using J = I<K, V, H>;
> +struct K {
> +  typedef J<long, char> L;
> +  L k;
> +  K();
> +};
> +struct M {
> +  bool bar () const;
> +  K::L m;
> +};
> +K n;
> +bool M::bar () const { n.k = m; return true; }
> diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept86.C b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
> new file mode 100644
> index 00000000000..2d040c090f5
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
> @@ -0,0 +1,25 @@
> +// PR c++/114349
> +// { dg-do compile { target c++14 } }
> +
> +struct B
> +{
> +  int i;
> +};
> +
> +template <bool BA>
> +void
> +goo ()
> +{
> +  constexpr bool is_yes = BA;
> +  struct C
> +  {
> +    static auto g(B b) noexcept(is_yes) { }
> +  };
> +  C::g({});
> +}
> +
> +void
> +x ()
> +{
> +  goo<false>();
> +}
> 
> base-commit: 65b7d1862e11784a0ce67ab758e06dd8aa65b181


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

* [PATCH v3] c++: ICE with noexcept and local specialization, again [PR114349]
  2024-03-25 19:40     ` Jason Merrill
@ 2024-03-25 22:17       ` Marek Polacek
  0 siblings, 0 replies; 5+ messages in thread
From: Marek Polacek @ 2024-03-25 22:17 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches, Patrick Palka

On Mon, Mar 25, 2024 at 03:40:10PM -0400, Jason Merrill wrote:
> On 3/22/24 17:30, Marek Polacek wrote:
> > On Thu, Mar 21, 2024 at 05:27:37PM -0400, Jason Merrill wrote:
> > > On 3/21/24 17:01, Marek Polacek wrote:
> > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > > > 
> > > > -- >8 --
> > > > Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
> > > > we're dealing with a noexcept-spec there, not a noexcept-expr, so
> > > > setting cp_noexcept_operand et al is incorrect.  Back to the drawing
> > > > board then.
> > > > 
> > > > To fix noexcept84.C, we should probably avoid doing push_to_top_level
> > > > in certain cases.  Patrick suggested checking:
> > > > 
> > > >     const bool push_to_top = current_function_decl != fn;
> > > > 
> > > > which works, but I'm not sure I follow the logic there.  I also came
> > > > up with
> > > > 
> > > >     const bool push_to_top = !decl_function_context (fn);
> > > > 
> > > > which also works.  But ultimately I went with !DECL_TEMPLATE_INSTANTIATED;
> > > > if DECL_TEMPLATE_INSTANTIATED is set, we've already pushed to top level
> > > > if it was necessary in instantiate_body.
> > > 
> > > This sort of thing is what maybe_push_to_top_level is for, does that also
> > > work?
> > 
> > Sadly -- and I should have mentioned that -- no.  maybe_push_to_top_level asks:
> > 
> >    bool push_to_top
> >      = !(current_function_decl
> >     && !LAMBDA_FUNCTION_P (d)
> >     && decl_function_context (d) == current_function_decl);
> > 
> > here both d and current_function_decl are test()::S::S(), and
> > decl_function_context (d) is test().  (current_function_decl was
> > set to test()::S::S() by an earlier push_access_scope call.)
> > 
> > But I want it to work, and I think using maybe_ would be a way nicer
> > fix.  So what if we don't push to top level if decl_function_context
> > is non-null?  I had to add the LAMBDA_TYPE_P check though: it looks
> > that we always have to push to top level for lambdas, but sometimes
> > we get a lambda's TYPE_DECL, and LAMBDA_FUNCTION_P doesn't catch
> > that.  An example is lambda-nested4.C.
> > 
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > -- >8 --
> > Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
> > we're dealing with a noexcept-spec there, not a noexcept-expr, so
> > setting cp_noexcept_operand et al is incorrect.  Back to the drawing
> > board then.
> > 
> > To fix noexcept84.C, we should probably avoid doing push_to_top_level
> > in certain cases.  maybe_push_to_top_level didn't work here as-is, so
> > I changed it to not push to top level if decl_function_context is
> > non-null, when we are not dealing with a lambda.
> > 
> > This also fixes c++/114349, introduced by r14-9339.
> > 
> > 	PR c++/114349
> > 
> > gcc/cp/ChangeLog:
> > 
> > 	* name-lookup.cc (maybe_push_to_top_level): For a non-lambda,
> > 	don't push to top level if decl_function_context is non-null.
> > 	* pt.cc (maybe_instantiate_noexcept): Use maybe_push_to_top_level.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > 	* g++.dg/cpp0x/noexcept85.C: New test.
> > 	* g++.dg/cpp0x/noexcept86.C: New test.
> > ---
> >   gcc/cp/name-lookup.cc                   | 12 ++++++---
> >   gcc/cp/pt.cc                            | 11 ++-------
> >   gcc/testsuite/g++.dg/cpp0x/noexcept85.C | 33 +++++++++++++++++++++++++
> >   gcc/testsuite/g++.dg/cpp0x/noexcept86.C | 25 +++++++++++++++++++
> >   4 files changed, 68 insertions(+), 13 deletions(-)
> >   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept85.C
> >   create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept86.C
> > 
> > diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
> > index dce4caf8981..4b2b27bdd0d 100644
> > --- a/gcc/cp/name-lookup.cc
> > +++ b/gcc/cp/name-lookup.cc
> > @@ -8664,10 +8664,14 @@ maybe_push_to_top_level (tree d)
> >   {
> >     /* Push if D isn't function-local, or is a lambda function, for which name
> >        resolution is already done.  */
> > -  bool push_to_top
> > -    = !(current_function_decl
> > -	&& !LAMBDA_FUNCTION_P (d)
> > -	&& decl_function_context (d) == current_function_decl);
> > +  const bool push_to_top
> > +    = (LAMBDA_FUNCTION_P (d)
> > +       || (TREE_CODE (d) == TYPE_DECL
> > +	   && TREE_TYPE (d)
> > +	   && LAMBDA_TYPE_P (TREE_TYPE (d)))
> > +       || !current_function_decl
> > +       || (!decl_function_context (d)
> > +	   && decl_function_context (d) != current_function_decl));
> 
> This line seems unnecessary; the case it excludes is when
> decl_function_context and current_function_decl are both null, but if
> current_function_decl is null we already succeeded.
> 
> OK with this line removed.

Thanks a lot.  I've verified that the following version passes a bootstrap/regtest
on x86_64-pc-linux-gnu.

-- >8 --
Patrick noticed that my r14-9339-gdc6c3bfb59baab patch is wrong;
we're dealing with a noexcept-spec there, not a noexcept-expr, so
setting cp_noexcept_operand et al is incorrect.  Back to the drawing
board then.

To fix noexcept84.C, we should probably avoid doing push_to_top_level
in certain cases.  maybe_push_to_top_level didn't work here as-is, so
I changed it to not push to top level if decl_function_context is
non-null, when we are not dealing with a lambda.

This also fixes c++/114349, introduced by r14-9339.

	PR c++/114349

gcc/cp/ChangeLog:

	* name-lookup.cc (maybe_push_to_top_level): For a non-lambda,
	don't push to top level if decl_function_context is non-null.
	* pt.cc (maybe_instantiate_noexcept): Use maybe_push_to_top_level.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/noexcept85.C: New test.
	* g++.dg/cpp0x/noexcept86.C: New test.
---
 gcc/cp/name-lookup.cc                   | 11 ++++++---
 gcc/cp/pt.cc                            | 11 ++-------
 gcc/testsuite/g++.dg/cpp0x/noexcept85.C | 33 +++++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/noexcept86.C | 25 +++++++++++++++++++
 4 files changed, 67 insertions(+), 13 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept85.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept86.C

diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index dce4caf8981..7af7f00e34c 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -8664,10 +8664,13 @@ maybe_push_to_top_level (tree d)
 {
   /* Push if D isn't function-local, or is a lambda function, for which name
      resolution is already done.  */
-  bool push_to_top
-    = !(current_function_decl
-	&& !LAMBDA_FUNCTION_P (d)
-	&& decl_function_context (d) == current_function_decl);
+  const bool push_to_top
+    = (LAMBDA_FUNCTION_P (d)
+       || (TREE_CODE (d) == TYPE_DECL
+	   && TREE_TYPE (d)
+	   && LAMBDA_TYPE_P (TREE_TYPE (d)))
+       || !current_function_decl
+       || !decl_function_context (d));
 
   if (push_to_top)
     push_to_top_level ();
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 8cf0d5b7a8d..7b00a8615d2 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -26855,7 +26855,7 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	}
       else if (push_tinst_level (fn))
 	{
-	  push_to_top_level ();
+	  const bool push_to_top = maybe_push_to_top_level (fn);
 	  push_access_scope (fn);
 	  push_deferring_access_checks (dk_no_deferred);
 	  input_location = DECL_SOURCE_LOCATION (fn);
@@ -26878,17 +26878,10 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	  if (orig_fn)
 	    ++processing_template_decl;
 
-	  ++cp_unevaluated_operand;
-	  ++c_inhibit_evaluation_warnings;
-	  ++cp_noexcept_operand;
 	  /* Do deferred instantiation of the noexcept-specifier.  */
 	  noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex),
 			      DEFERRED_NOEXCEPT_ARGS (noex),
 			      tf_warning_or_error, fn);
-	  --cp_unevaluated_operand;
-	  --c_inhibit_evaluation_warnings;
-	  --cp_noexcept_operand;
-
 	  /* Build up the noexcept-specification.  */
 	  spec = build_noexcept_spec (noex, tf_warning_or_error);
 
@@ -26898,7 +26891,7 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
 	  pop_deferring_access_checks ();
 	  pop_access_scope (fn);
 	  pop_tinst_level ();
-	  pop_from_top_level ();
+	  maybe_pop_from_top_level (push_to_top);
 	}
       else
 	spec = noexcept_false_spec;
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept85.C b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
new file mode 100644
index 00000000000..b415bb46bc9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept85.C
@@ -0,0 +1,33 @@
+// PR c++/114349
+// { dg-do compile { target c++11 } }
+
+using A = struct {};
+template <template <typename> class, typename, typename>
+using B = A;
+template <typename T>
+using C = typename T::D;
+struct E {
+  using D = B<C, int, A>;
+};
+template <class> constexpr bool foo (A) { return false; }
+template <class T> struct F {
+  using G = T;
+  using H = E;
+  F(const F &);
+  void operator=(F) noexcept(foo <G> (H::D{}));
+};
+template <typename, typename, typename>
+using I = F<int>;
+template <typename K, typename V, typename H = K>
+using J = I<K, V, H>;
+struct K {
+  typedef J<long, char> L;
+  L k;
+  K();
+};
+struct M {
+  bool bar () const;
+  K::L m;
+};
+K n;
+bool M::bar () const { n.k = m; return true; }
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept86.C b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
new file mode 100644
index 00000000000..2d040c090f5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept86.C
@@ -0,0 +1,25 @@
+// PR c++/114349
+// { dg-do compile { target c++14 } }
+
+struct B
+{
+  int i;
+};
+
+template <bool BA>
+void
+goo ()
+{
+  constexpr bool is_yes = BA;
+  struct C
+  {
+    static auto g(B b) noexcept(is_yes) { }
+  };
+  C::g({});
+}
+
+void
+x ()
+{
+  goo<false>();
+}

base-commit: 18555b914316e8c1fb11ee821f2ee839d834e58e
-- 
2.44.0


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

end of thread, other threads:[~2024-03-25 22:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-21 21:01 [PATCH] c++: ICE with noexcept and local specialization, again [PR114349] Marek Polacek
2024-03-21 21:27 ` Jason Merrill
2024-03-22 21:30   ` [PATCH v2] " Marek Polacek
2024-03-25 19:40     ` Jason Merrill
2024-03-25 22:17       ` [PATCH v3] " Marek Polacek

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