* [PATCH] c++: diagnosing if-stmt with non-constant branches [PR105050]
@ 2022-03-25 16:07 Patrick Palka
2022-03-25 16:13 ` Marek Polacek
2022-03-25 18:48 ` Jason Merrill
0 siblings, 2 replies; 3+ messages in thread
From: Patrick Palka @ 2022-03-25 16:07 UTC (permalink / raw)
To: gcc-patches; +Cc: jason, jwakely, Patrick Palka
When an if-stmt is deemed non-constant because both of its branches are
non-constant, we issue a rather generic error which, given that it points
to the 'if' token, misleadingly suggests the condition is at fault:
constexpr-105050.C:8:3: error: expression ‘<statement>’ is not a constant expression
8 | if (p != q && *p < 0)
| ^~
This patch clarifies the error message to read:
constexpr-105050.C:8:3: error: neither branch of ‘if’ is a constant expression
8 | if (p != q && *p < 0)
| ^~
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?
PR c++/105050
gcc/cp/ChangeLog:
* constexpr.cc (potential_constant_expression_1) <case IF_STMT>:
Clarify error message when a if-stmt is non-constant because its
branches are non-constant.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1y/constexpr-105050.C: New test.
---
gcc/cp/constexpr.cc | 7 ++++++-
gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C | 12 ++++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 778680b8270..9c40b051574 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -9439,7 +9439,12 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
}
}
if (flags & tf_error)
- error_at (loc, "expression %qE is not a constant expression", t);
+ {
+ if (TREE_CODE (t) == IF_STMT)
+ error_at (loc, "neither branch of %<if%> is a constant expression");
+ else
+ error_at (loc, "expression %qE is not a constant expression", t);
+ }
return false;
case VEC_INIT_EXPR:
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
new file mode 100644
index 00000000000..99d5c9960ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
@@ -0,0 +1,12 @@
+// PR c++/105050
+// { dg-do compile { target c++14 } }
+
+void g();
+void h();
+
+constexpr void f(int* p, int *q) {
+ if (p != q && *p < 0) // { dg-error "neither branch of 'if' is a constant expression" }
+ g();
+ else
+ h();
+}
--
2.35.1.655.ga68dfadae5
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] c++: diagnosing if-stmt with non-constant branches [PR105050]
2022-03-25 16:07 [PATCH] c++: diagnosing if-stmt with non-constant branches [PR105050] Patrick Palka
@ 2022-03-25 16:13 ` Marek Polacek
2022-03-25 18:48 ` Jason Merrill
1 sibling, 0 replies; 3+ messages in thread
From: Marek Polacek @ 2022-03-25 16:13 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, jwakely
On Fri, Mar 25, 2022 at 12:07:31PM -0400, Patrick Palka via Gcc-patches wrote:
> When an if-stmt is deemed non-constant because both of its branches are
> non-constant, we issue a rather generic error which, given that it points
> to the 'if' token, misleadingly suggests the condition is at fault:
>
> constexpr-105050.C:8:3: error: expression ‘<statement>’ is not a constant expression
> 8 | if (p != q && *p < 0)
> | ^~
>
> This patch clarifies the error message to read:
>
> constexpr-105050.C:8:3: error: neither branch of ‘if’ is a constant expression
> 8 | if (p != q && *p < 0)
> | ^~
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
LGTM.
> PR c++/105050
>
> gcc/cp/ChangeLog:
>
> * constexpr.cc (potential_constant_expression_1) <case IF_STMT>:
> Clarify error message when a if-stmt is non-constant because its
> branches are non-constant.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp1y/constexpr-105050.C: New test.
> ---
> gcc/cp/constexpr.cc | 7 ++++++-
> gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C | 12 ++++++++++++
> 2 files changed, 18 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
>
> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
> index 778680b8270..9c40b051574 100644
> --- a/gcc/cp/constexpr.cc
> +++ b/gcc/cp/constexpr.cc
> @@ -9439,7 +9439,12 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
> }
> }
> if (flags & tf_error)
> - error_at (loc, "expression %qE is not a constant expression", t);
> + {
> + if (TREE_CODE (t) == IF_STMT)
> + error_at (loc, "neither branch of %<if%> is a constant expression");
> + else
> + error_at (loc, "expression %qE is not a constant expression", t);
> + }
> return false;
>
> case VEC_INIT_EXPR:
> diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
> new file mode 100644
> index 00000000000..99d5c9960ac
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
> @@ -0,0 +1,12 @@
> +// PR c++/105050
> +// { dg-do compile { target c++14 } }
> +
> +void g();
> +void h();
> +
> +constexpr void f(int* p, int *q) {
> + if (p != q && *p < 0) // { dg-error "neither branch of 'if' is a constant expression" }
> + g();
> + else
> + h();
> +}
> --
> 2.35.1.655.ga68dfadae5
>
Marek
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] c++: diagnosing if-stmt with non-constant branches [PR105050]
2022-03-25 16:07 [PATCH] c++: diagnosing if-stmt with non-constant branches [PR105050] Patrick Palka
2022-03-25 16:13 ` Marek Polacek
@ 2022-03-25 18:48 ` Jason Merrill
1 sibling, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2022-03-25 18:48 UTC (permalink / raw)
To: Patrick Palka, gcc-patches; +Cc: jwakely
On 3/25/22 12:07, Patrick Palka wrote:
> When an if-stmt is deemed non-constant because both of its branches are
> non-constant, we issue a rather generic error which, given that it points
> to the 'if' token, misleadingly suggests the condition is at fault:
>
> constexpr-105050.C:8:3: error: expression ‘<statement>’ is not a constant expression
> 8 | if (p != q && *p < 0)
> | ^~
>
> This patch clarifies the error message to read:
>
> constexpr-105050.C:8:3: error: neither branch of ‘if’ is a constant expression
> 8 | if (p != q && *p < 0)
> | ^~
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
OK. I wonder if we want to then diagnose the branches individually, but
let's leave that for stage 1, if at all.
> PR c++/105050
>
> gcc/cp/ChangeLog:
>
> * constexpr.cc (potential_constant_expression_1) <case IF_STMT>:
> Clarify error message when a if-stmt is non-constant because its
> branches are non-constant.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp1y/constexpr-105050.C: New test.
> ---
> gcc/cp/constexpr.cc | 7 ++++++-
> gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C | 12 ++++++++++++
> 2 files changed, 18 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
>
> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
> index 778680b8270..9c40b051574 100644
> --- a/gcc/cp/constexpr.cc
> +++ b/gcc/cp/constexpr.cc
> @@ -9439,7 +9439,12 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
> }
> }
> if (flags & tf_error)
> - error_at (loc, "expression %qE is not a constant expression", t);
> + {
> + if (TREE_CODE (t) == IF_STMT)
> + error_at (loc, "neither branch of %<if%> is a constant expression");
> + else
> + error_at (loc, "expression %qE is not a constant expression", t);
> + }
> return false;
>
> case VEC_INIT_EXPR:
> diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
> new file mode 100644
> index 00000000000..99d5c9960ac
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C
> @@ -0,0 +1,12 @@
> +// PR c++/105050
> +// { dg-do compile { target c++14 } }
> +
> +void g();
> +void h();
> +
> +constexpr void f(int* p, int *q) {
> + if (p != q && *p < 0) // { dg-error "neither branch of 'if' is a constant expression" }
> + g();
> + else
> + h();
> +}
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-03-25 18:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-25 16:07 [PATCH] c++: diagnosing if-stmt with non-constant branches [PR105050] Patrick Palka
2022-03-25 16:13 ` Marek Polacek
2022-03-25 18:48 ` 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).