From patchwork Thu Apr 21 13:20:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1620131 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=sgYKA85h; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (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 4KkdVB5Yk6z9sCq for ; Thu, 21 Apr 2022 23:21:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5C32A393BC20 for ; Thu, 21 Apr 2022 13:21:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C32A393BC20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1650547295; bh=Gctsw2149nd2IzwHhlIGcG6sBWUvJKpJBDlHQVQdPVQ=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=sgYKA85hEuAiuHLVaBC0jupi7xic9BR1pDYdrhjGE/pcLbLdZqCmG7vDZfZ07Y8N9 uLR6MB4Y00Iut1bYjZ8cw0hAwXo6BhMg1qxCcNV0NN3jy5qKjF8ElmTw1WXON6kja3 iVokId335PjKjl5Ib1WKB7EImxYHYcOXgRpEMHb0= 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 CB15E389EC69 for ; Thu, 21 Apr 2022 13:20:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CB15E389EC69 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-462-X1F21p7nPj-AswCjGnl-DQ-1; Thu, 21 Apr 2022 09:20:51 -0400 X-MC-Unique: X1F21p7nPj-AswCjGnl-DQ-1 Received: by mail-qk1-f198.google.com with SMTP id q12-20020ae9e40c000000b0069f091022dbso465083qkc.3 for ; Thu, 21 Apr 2022 06:20:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Gctsw2149nd2IzwHhlIGcG6sBWUvJKpJBDlHQVQdPVQ=; b=zdVfaEr2ZMPnAhGSt6KGb/zbfD2XUQSSy5RCSB/WPAKXCN/gKt23EfKBoSKlBCi8PH ZvKO+kfBelh03NHr7a3ywKlDUpwkyejypyztDrxqcIpeRLPc8Hdks5nSpLvVhjE4bh8j HH8CPTZ7MW1CDBxwYIN8dqJ0J/j/mIIli0TaKdmvPAioMQ61stJbCtSU2SUgX50Fl72a A7XAgoJZXQFLK7+sgay+1PGIS562SEXzaU5Rvt3IFkqLhIXS23h4RmHtozwlxnwc1Zsh kXJeiFIck1Vjvbk6UA5IK5+d4EQ3YU8O0TOtKWApi2nG3QbY+rZgcFS+9xClzjrx1eFM kycw== X-Gm-Message-State: AOAM530Dt4jLiOK6pe5WEEdEA0oLiy7/wxwEI3Y0XCff3wsxNkaQ7obP tm/cGDMdKnplgBRj6xJ/v4hEuk9UWGYAPffPJSUEiNQlbh1Gk/ryIkgg3G00E1mVIi9v0zojpcB OafAdQse/PQGQsZOLpw== X-Received: by 2002:a05:6214:27ec:b0:443:fd5b:ba92 with SMTP id jt12-20020a05621427ec00b00443fd5bba92mr19483003qvb.80.1650547251080; Thu, 21 Apr 2022 06:20:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxFQ63IzW0Yjm+8KP+UViuFIT7mOtEbDRNqBeYYz/41FLiQOdRyDJ7kBsmhYhXLkFaMM0lzjA== X-Received: by 2002:a05:6214:27ec:b0:443:fd5b:ba92 with SMTP id jt12-20020a05621427ec00b00443fd5bba92mr19482980qvb.80.1650547250825; Thu, 21 Apr 2022 06:20:50 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::d1bb]) by smtp.gmail.com with ESMTPSA id p17-20020a37a611000000b0069ee79a16e3sm1609226qke.0.2022.04.21.06.20.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Apr 2022 06:20:50 -0700 (PDT) Date: Thu, 21 Apr 2022 09:20:48 -0400 To: Jason Merrill Subject: [PATCH v2] c++: wrong error with constexpr COMPOUND_EXPR [PR105321] Message-ID: References: <20220420224003.374946-1-polacek@redhat.com> <157d2fb2-7a34-7ff9-4901-117d315cfc90@redhat.com> MIME-Version: 1.0 In-Reply-To: <157d2fb2-7a34-7ff9-4901-117d315cfc90@redhat.com> User-Agent: Mutt/2.1.5 (2021-12-30) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_TRUTHINESS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP 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 Cc: GCC Patches Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" On Thu, Apr 21, 2022 at 08:56:23AM -0400, Jason Merrill wrote: > On 4/20/22 18:40, Marek Polacek wrote: > > 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. > > In a discarded-value expression, we don't do the lvalue-rvalue conversion; > whether we want an lvalue for the RHS of the comma is irrelevant. Ah, that's what I misread -- [expr.context]/2.8 cares only about the right operand :(. > The bug here seems to be that we aren't doing the l->r conversion for the > LHS of the TRUTH_OR_EXPR; I'd think that cxx_eval_logical_expression should > pass false for lval to both recursive calls, there's no case where we > actually expect an lvalue from a TRUTH_*. Yeah, that makes sense. Bootstrap/regtest running on x86_64-pc-linux-gnu, ok for trunk/11.3 if it passes? -- >8 -- 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. 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.cc (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. --- gcc/cp/constexpr.cc | 9 ++++----- gcc/testsuite/g++.dg/cpp0x/constexpr-105321.C | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-105321.C base-commit: 1e6c0e69af8da436e1d1d2d23d8c38410d78ecf2 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index e89440e770f..fa65290e938 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -4566,19 +4566,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, "");