public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marek Polacek <mpolacek@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-9930] c++: wrong error with constexpr COMPOUND_EXPR [PR105321]
Date: Fri, 22 Apr 2022 21:00:59 +0000 (GMT)	[thread overview]
Message-ID: <20220422210059.F0C063858427@sourceware.org> (raw)

https://gcc.gnu.org/g:afec66b054a7603ef394dc712ccbba37ae645fd9

commit r11-9930-gafec66b054a7603ef394dc712ccbba37ae645fd9
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed Apr 20 16:02:52 2022 -0400

    c++: wrong error with constexpr COMPOUND_EXPR [PR105321]
    
    Here we issue a bogus error for the first assert in the test.  Therein
    we have
    
    <retval> = (void) (VIEW_CONVERT_EXPR<bool>(yes) || handle_error ());, VIEW_CONVERT_EXPR<int>(value);
    
    which has a COMPOUND_EXPR, so we get to cxx_eval_constant_expression
    <case COMPOUND_EXPR>.  The problem here is that we call
    
    7044             /* Check that the LHS is constant and then discard it.  */
    7045             cxx_eval_constant_expression (ctx, op0,
    7046                                           true, non_constant_p, overflow_p,
    7047                                           jump_target);
    
    where lval is always true, so the PARM_DECL 'yes' is not evaluated into
    its value.
    
    Fixed by always passing false for 'lval' in cxx_eval_logical_expression;
    there's no case where we actually expect an lvalue from a TRUTH_*.
    
            PR c++/105321
    
    gcc/cp/ChangeLog:
    
            * constexpr.c (cxx_eval_logical_expression): Always pass false for lval
            to cxx_eval_constant_expression.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-105321.C: New test.
    
    (cherry picked from commit 93b65ed9706e2ceb4be7b28c9ff9be759e68a614)

Diff:
---
 gcc/cp/constexpr.c                            |  9 ++++-----
 gcc/testsuite/g++.dg/cpp0x/constexpr-105321.C | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index c13c920ade3..d72a8712dab 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -4280,19 +4280,18 @@ cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
 static tree
 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
                              tree bailout_value, tree continue_value,
-			     bool lval,
-			     bool *non_constant_p, bool *overflow_p)
+			     bool, bool *non_constant_p, bool *overflow_p)
 {
   tree r;
   tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
-					   lval,
-					   non_constant_p, overflow_p);
+					   /*lval*/false, non_constant_p,
+					   overflow_p);
   VERIFY_CONSTANT (lhs);
   if (tree_int_cst_equal (lhs, bailout_value))
     return lhs;
   gcc_assert (tree_int_cst_equal (lhs, continue_value));
   r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
-				    lval, non_constant_p,
+				    /*lval*/false, non_constant_p,
 				    overflow_p);
   VERIFY_CONSTANT (r);
   return r;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-105321.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-105321.C
new file mode 100644
index 00000000000..adb6830ff22
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-105321.C
@@ -0,0 +1,18 @@
+// PR c++/105321
+// { dg-do compile { target c++11 } }
+
+bool handle_error();
+
+constexpr int echo(int value, bool yes = true) noexcept
+{
+    return (yes || handle_error()), value;
+}
+
+static_assert(echo(10) == 10, "");
+
+constexpr int echo2(int value, bool no = false) noexcept
+{
+    return (!no || handle_error()), value;
+}
+
+static_assert(echo2(10) == 10, "");


                 reply	other threads:[~2022-04-22 21:00 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=20220422210059.F0C063858427@sourceware.org \
    --to=mpolacek@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: link
Be 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).