* [PATCH] c++: constexpr folding in unevaluated context [PR105931]
@ 2022-06-23 15:06 Patrick Palka
2022-06-23 19:18 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2022-06-23 15:06 UTC (permalink / raw)
To: gcc-patches
Changing the type of N from int to unsigned in decltype82.C from
r12-8472-g47ea22015c90df reveals another spot where we perform
constexpr evaluation in an unevaluated context for sake of diagnostics,
this time from the call to shorten_compare in cp_build_binary_op,
which calls fold_for_warn.
We could (and probably should?) suppress the shorten_compare diagnostics
when in an unevaluated context, but there's probably other spots
that have the same bug. So this patch takes the approach of suppressing
fold_for_warn more generally when in an unevaluated context.
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk/12?
PR c++/105931
gcc/cp/ChangeLog:
* expr.cc (fold_for_warn): Don't fold when in an unevaluated
context.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/decltype82a.C: New test.
---
gcc/cp/expr.cc | 5 +++++
gcc/testsuite/g++.dg/cpp0x/decltype82a.C | 11 +++++++++++
2 files changed, 16 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype82a.C
diff --git a/gcc/cp/expr.cc b/gcc/cp/expr.cc
index 31fe0980337..b71245f937d 100644
--- a/gcc/cp/expr.cc
+++ b/gcc/cp/expr.cc
@@ -397,6 +397,11 @@ fold_for_warn (tree x)
{
/* C++ implementation. */
+ if (cp_unevaluated_operand)
+ /* In an unevaluated context, we don't care about the reduced value
+ of an expression, so neither should any warnings. */
+ return x;
+
/* Prevent warning-dependent constexpr evaluation from changing
DECL_UID (which breaks -fcompare-debug) and from instantiating
templates. */
diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype82a.C b/gcc/testsuite/g++.dg/cpp0x/decltype82a.C
new file mode 100644
index 00000000000..cbbcdfd8e58
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/decltype82a.C
@@ -0,0 +1,11 @@
+// PR c++/105931
+// { dg-do compile { target c++11 } }
+// A version of decltype82.C where N is unsigned.
+
+template<unsigned N>
+void f() {
+ using ty1 = decltype((5 % N) == 0);
+ using ty2 = decltype((5 / N) == 0);
+}
+
+template void f<0>();
--
2.37.0.rc1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: constexpr folding in unevaluated context [PR105931]
2022-06-23 15:06 [PATCH] c++: constexpr folding in unevaluated context [PR105931] Patrick Palka
@ 2022-06-23 19:18 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-06-23 19:18 UTC (permalink / raw)
To: Patrick Palka, gcc-patches
On 6/23/22 11:06, Patrick Palka wrote:
> Changing the type of N from int to unsigned in decltype82.C from
> r12-8472-g47ea22015c90df reveals another spot where we perform
> constexpr evaluation in an unevaluated context for sake of diagnostics,
> this time from the call to shorten_compare in cp_build_binary_op,
> which calls fold_for_warn.
>
> We could (and probably should?) suppress the shorten_compare diagnostics
> when in an unevaluated context, but there's probably other spots
> that have the same bug. So this patch takes the approach of suppressing
> fold_for_warn more generally when in an unevaluated context.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk/12?
OK.
> PR c++/105931
>
> gcc/cp/ChangeLog:
>
> * expr.cc (fold_for_warn): Don't fold when in an unevaluated
> context.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/decltype82a.C: New test.
> ---
> gcc/cp/expr.cc | 5 +++++
> gcc/testsuite/g++.dg/cpp0x/decltype82a.C | 11 +++++++++++
> 2 files changed, 16 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype82a.C
>
> diff --git a/gcc/cp/expr.cc b/gcc/cp/expr.cc
> index 31fe0980337..b71245f937d 100644
> --- a/gcc/cp/expr.cc
> +++ b/gcc/cp/expr.cc
> @@ -397,6 +397,11 @@ fold_for_warn (tree x)
> {
> /* C++ implementation. */
>
> + if (cp_unevaluated_operand)
> + /* In an unevaluated context, we don't care about the reduced value
> + of an expression, so neither should any warnings. */
> + return x;
> +
> /* Prevent warning-dependent constexpr evaluation from changing
> DECL_UID (which breaks -fcompare-debug) and from instantiating
> templates. */
> diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype82a.C b/gcc/testsuite/g++.dg/cpp0x/decltype82a.C
> new file mode 100644
> index 00000000000..cbbcdfd8e58
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/decltype82a.C
> @@ -0,0 +1,11 @@
> +// PR c++/105931
> +// { dg-do compile { target c++11 } }
> +// A version of decltype82.C where N is unsigned.
> +
> +template<unsigned N>
> +void f() {
> + using ty1 = decltype((5 % N) == 0);
> + using ty2 = decltype((5 / N) == 0);
> +}
> +
> +template void f<0>();
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-06-23 19:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-23 15:06 [PATCH] c++: constexpr folding in unevaluated context [PR105931] Patrick Palka
2022-06-23 19:18 ` 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).