From patchwork Wed Apr 20 22:40:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1619870 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=EHh4I3lF; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KkFy65sv2z9sFq for ; Thu, 21 Apr 2022 08:41:01 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 33A743857372 for ; Wed, 20 Apr 2022 22:40:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33A743857372 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1650494457; bh=2pufbVSDYsqXW1PdmKNLAtRG/yORle/DzZBhj0dSQEs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=EHh4I3lFZMBAUR4A3dm3OZs1VO1KS/RCpzUA44JQy2SAbbQRAjPKXKGFIB1PzRlhg dshqidXqpgjKEgKXW3uLoJ7NHkc3i/XYL+2IhmdMK1RiIvo5ETS3ETJ7qoL2vsNPN0 EW4LrAO0mZ1ksOWMm1R0a9qz1It0B6dmSZe9P3R0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 9C37F3858C54 for ; Wed, 20 Apr 2022 22:40:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9C37F3858C54 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-440-dXN6sfpKNZSrmCUqviGICQ-1; Wed, 20 Apr 2022 18:40:12 -0400 X-MC-Unique: dXN6sfpKNZSrmCUqviGICQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7C6CE1815CFB for ; Wed, 20 Apr 2022 22:40:12 +0000 (UTC) Received: from pdp-11.hsd1.ma.comcast.net (unknown [10.22.32.119]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5AE4C145BEE0; Wed, 20 Apr 2022 22:40:12 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: wrong error with constexpr COMPOUND_EXPR [PR105321] Date: Wed, 20 Apr 2022 18:40:03 -0400 Message-Id: <20220420224003.374946-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Here we issue a bogus error for the first assert in the test. Therein we have = (void) (VIEW_CONVERT_EXPR(yes) || handle_error ());, VIEW_CONVERT_EXPR(value); which has a COMPOUND_EXPR, so we get to cxx_eval_constant_expression . 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. r218832 changed the argument for 'lval' from false to true: (cxx_eval_constant_expression) [COMPOUND_EXPR]: Pass true for lval. but I think we want to pass 'lval' instead. Jakub tells me that's what we do for "(void) expr" as well. [expr.comma] says that the left expression is a discarded-value expression, but [expr.context] doesn't suggest that we should always be passing false for lval as pre-r218832. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/11.3? PR c++/105321 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_constant_expression) : Pass lval to cxx_eval_constant_expression. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-105321.C: New test. --- gcc/cp/constexpr.cc | 2 +- gcc/testsuite/g++.dg/cpp0x/constexpr-105321.C | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-105321.C base-commit: 5bde80f48bcc594658c788895ad1fd86d0916fc2 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index e89440e770f..28271d4405d 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7043,7 +7043,7 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, { /* Check that the LHS is constant and then discard it. */ cxx_eval_constant_expression (ctx, op0, - true, non_constant_p, overflow_p, + lval, non_constant_p, overflow_p, jump_target); if (*non_constant_p) return t; 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, "");