public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-7831] c++: diagnosing if-stmt with non-constant branches [PR105050] Date: Sat, 26 Mar 2022 14:20:56 +0000 (GMT) [thread overview] Message-ID: <20220326142056.D88883857811@sourceware.org> (raw) https://gcc.gnu.org/g:ff465bd8a0f0f96a00d3067018442917b194b7af commit r12-7831-gff465bd8a0f0f96a00d3067018442917b194b7af Author: Patrick Palka <ppalka@redhat.com> Date: Sat Mar 26 10:20:18 2022 -0400 c++: diagnosing if-stmt with non-constant branches [PR105050] When an if-stmt is determined to be non-constant because both of its branches are non-constant, we issue a somewhat generic error which, since the error also 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 instead read: constexpr-105050.C:8:3: error: neither branch of ‘if’ is a constant expression 8 | if (p != q && *p < 0) | ^~ 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. Diff: --- gcc/cp/constexpr.cc | 7 ++++++- gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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..e0688fbd38e --- /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(); +}
reply other threads:[~2022-03-26 14:20 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220326142056.D88883857811@sourceware.org \ --to=ppalka@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).