From patchwork Tue Oct 25 16:37:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 10886 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1103908wru; Tue, 25 Oct 2022 09:38:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4l2xlX3GIRl2WImH6fZ8xmuQaEhFga0jyPe9kUu4DbMJ8juJFh0LPkPWFdQwQjRYhUXj1n X-Received: by 2002:a17:906:7314:b0:791:a45a:bc84 with SMTP id di20-20020a170906731400b00791a45abc84mr31831312ejc.394.1666715890465; Tue, 25 Oct 2022 09:38:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666715890; cv=none; d=google.com; s=arc-20160816; b=jdBH5OTq62geYluMPLP3BnozyG2TqHrukAmMUKtrcHplysYRPShwmkEPF0JsQLNDCN wBMxv888pVccehmA37swvAIbUXZrFHvf6hN/Xu1dmERYvSstki2z2cumRm+1IWHXKh7z UyScwR3Zzkhg+BaFh2/2oNVvq5eH19eimBqeNfAQFehnG/28Vxe4nszCl6Ta3vPLQ120 reLDpRYRspSvuNniDkOpBOt7TXnvGPk8IxUGeNHD5hM4UxByfO4zyBVm+lYbuC1b113t BTMllks4cIRjtTDY3hn7daAfeL5Y58ZG+FwzRSRtgJkG6eh5DyhL62pt5cUD7lcS/APt BHqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=sEqTkkmuoa2JMrKZ+Z6IPc7EbYtMUcCDELoeLrQjMgw=; b=BgQa1FObrjTwIrBzw8s55bEJLXtn4K6RUwkHFYCfP+Lomn2qxDX6ecRr+dIUseJyRQ DxNuRIrO58xNWMx21B5ezKYUI6+R+mu0Adfbr+k4Rj/6mYmY2beSxa4EOiNswRlsVOL1 HVKJr39l/KyNsnEuN9bnDzvAiIwzX01DuDCVkbdx/gidwr7pW/ytcVgZ8tNeUV0ms5Gq J1+lAl5UXJ6zqFYU6LGlmeA7GOAjUtEWOkk/kqHdI26WPXjLt8OrY5uqdkeRpfhFEHL8 nEoUQVA7X40pGLjZnas91pfwNQEtTx2LE3XGbaGzQTDP8dHedJ+bJZVF2I9CdVyiIcj+ 0+hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=G4SdJMaq; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j10-20020a170906410a00b0073d710fec77si2856992ejk.251.2022.10.25.09.38.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 09:38:10 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=G4SdJMaq; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 85DF73856969 for ; Tue, 25 Oct 2022 16:38:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85DF73856969 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666715889; bh=sEqTkkmuoa2JMrKZ+Z6IPc7EbYtMUcCDELoeLrQjMgw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=G4SdJMaqJjDOm4ysxpsZuLuNE6kpijFJIT5EEZZzE82V4qXbOuaiNQ9UBD/9EurtI yuyP/EhRljOc/jRjTVe74r36rSHGctapDc8dmmxk9gO7dM72QaSCM9789bTkcFpdIk d4ZMEXUMhl+leFrxdbI6pjuBRtMF8YhHDYkRLs40= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 19C3A38582B1 for ; Tue, 25 Oct 2022 16:37:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 19C3A38582B1 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-515-J0m_KrNFM9-cTmaqo1kiGA-1; Tue, 25 Oct 2022 12:37:13 -0400 X-MC-Unique: J0m_KrNFM9-cTmaqo1kiGA-1 Received: by mail-qt1-f198.google.com with SMTP id bz12-20020a05622a1e8c00b0039ae6e887ffso9295901qtb.8 for ; Tue, 25 Oct 2022 09:37:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=sEqTkkmuoa2JMrKZ+Z6IPc7EbYtMUcCDELoeLrQjMgw=; b=1g/1pRKjsJhjadrHbeizcQhDbAmsEQg1F67WWzptJy0m9JSR94bsCnEbZ47Gjl1kZc l0aIeuKE+wL2YNg5xu+mjcAWWaVt3vqx2StjTL7T/ZAXBnnkpC1fUhOOVPwhtlxh/48q F2No6O15oqwxMdbwJ9QKdvIzywe7ZCOn3KH1yq6mSahWkIF2vQTnRPwsrh9JYGFQfr0a V6Qnz5EcD2us02NztdEVFZnt+nwjHKk89Y3rUMVs2OhLb+BrAW3NXG5XME1xks0PYs6D 1Uo9YDnZHFMqkLStLV3chReE8LX9fbweJwMFw0snKOV0r/kAfSWhKDf7cCtrKMfnJCBh sP2A== X-Gm-Message-State: ACrzQf1SMhOsnachbhUYPDjv8JPG9U1tuwpLgZ6GIF0dbXhhhRodP/Me FnYu0h81/07NnMg4anJfLr+Aupj0oZg6ndN1PMF9FoF06EwttY1MQmraa1ID/dV4CHQARJY+vaE e4ZSupTmrwI3bkqRgZc6UJikZgqkIF9IZKy/eqCV9jOLeZxaUO8E2lQscJoEVWgWylQ== X-Received: by 2002:a05:6214:1c4a:b0:4bb:64a3:727a with SMTP id if10-20020a0562141c4a00b004bb64a3727amr13070242qvb.16.1666715832740; Tue, 25 Oct 2022 09:37:12 -0700 (PDT) X-Received: by 2002:a05:6214:1c4a:b0:4bb:64a3:727a with SMTP id if10-20020a0562141c4a00b004bb64a3727amr13070203qvb.16.1666715832335; Tue, 25 Oct 2022 09:37:12 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id y20-20020a37f614000000b006eecc4a0de9sm2233409qkj.62.2022.10.25.09.37.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 09:37:11 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: improve failed constexpr assume diagnostic Date: Tue, 25 Oct 2022 12:37:09 -0400 Message-Id: <20221025163709.95817-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-25.2 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Cc: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747678281465114123?= X-GMAIL-MSGID: =?utf-8?q?1747678281465114123?= Tested x86_64-pc-linux-gnu, applying to trunk. -- >8 -- I noticed that we were printing "the comparison reduces to (x == 42)" when we should be able to give the value of x. Fixed by doing the same evaluation in diagnose_failing_condition that we already do in find_failing_clause. gcc/cp/ChangeLog: * constexpr.cc (fold_operand): New function. (find_failing_clause_r): Add const. (find_failing_clause): Add const. (diagnose_failing_condition): Add ctx parameter. (cxx_eval_internal_function): Pass it. * semantics.cc (diagnose_failing_condition): Move to constexpr.cc. * cp-tree.h: Adjust. gcc/testsuite/ChangeLog: * g++.dg/cpp23/attr-assume2.C: Expect constant values. --- gcc/cp/cp-tree.h | 5 +- gcc/cp/constexpr.cc | 63 ++++++++++++++++++----- gcc/cp/semantics.cc | 27 ---------- gcc/testsuite/g++.dg/cpp23/attr-assume2.C | 4 +- 4 files changed, 55 insertions(+), 44 deletions(-) base-commit: 3ee675724cb27eb2e8cfd82f6e234edc4a63f6be diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 2cca20be6c1..cec376d90af 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7758,7 +7758,6 @@ extern tree build_transaction_expr (location_t, tree, int, tree); extern bool cxx_omp_create_clause_info (tree, tree, bool, bool, bool, bool); extern tree baselink_for_fns (tree); -extern void diagnose_failing_condition (tree, location_t, bool); extern void finish_static_assert (tree, tree, location_t, bool, bool); extern tree finish_decltype_type (tree, bool, tsubst_flags_t); @@ -8497,7 +8496,9 @@ extern void clear_cv_and_fold_caches (void); extern tree unshare_constructor (tree CXX_MEM_STAT_INFO); extern bool decl_implicit_constexpr_p (tree); struct constexpr_ctx; -extern tree find_failing_clause (constexpr_ctx *ctx, tree); +extern tree find_failing_clause (const constexpr_ctx *ctx, tree); +extern void diagnose_failing_condition (tree, location_t, bool, + const constexpr_ctx * = nullptr); extern bool replace_decl (tree *, tree, tree); /* An RAII sentinel used to restrict constexpr evaluation so that it diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 94b54fc71dc..fc1bc53f68a 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -1799,6 +1799,24 @@ cx_error_context (void) return r; } +/* E is an operand of a failed assertion, fold it either with or without + constexpr context. */ + +static tree +fold_operand (tree e, const constexpr_ctx *ctx) +{ + if (ctx) + { + bool new_non_constant_p = false, new_overflow_p = false; + e = cxx_eval_constant_expression (ctx, e, vc_prvalue, + &new_non_constant_p, + &new_overflow_p); + } + else + e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true); + return e; +} + /* If we have a condition in conjunctive normal form (CNF), find the first failing clause. In other words, given an expression like @@ -1807,7 +1825,7 @@ cx_error_context (void) return the first 'false'. EXPR is the expression. */ static tree -find_failing_clause_r (constexpr_ctx *ctx, tree expr) +find_failing_clause_r (const constexpr_ctx *ctx, tree expr) { if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) { @@ -1818,16 +1836,7 @@ find_failing_clause_r (constexpr_ctx *ctx, tree expr) e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1)); return e; } - tree e = contextual_conv_bool (expr, tf_none); - if (ctx) - { - bool new_non_constant_p = false, new_overflow_p = false; - e = cxx_eval_constant_expression (ctx, e, vc_prvalue, - &new_non_constant_p, - &new_overflow_p); - } - else - e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true); + tree e = fold_operand (expr, ctx); if (integer_zerop (e)) /* This is the failing clause. */ return expr; @@ -1837,7 +1846,7 @@ find_failing_clause_r (constexpr_ctx *ctx, tree expr) /* Wrapper for find_failing_clause_r. */ tree -find_failing_clause (constexpr_ctx *ctx, tree expr) +find_failing_clause (const constexpr_ctx *ctx, tree expr) { if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) if (tree e = find_failing_clause_r (ctx, expr)) @@ -1845,6 +1854,34 @@ find_failing_clause (constexpr_ctx *ctx, tree expr) return expr; } +/* Emit additional diagnostics for failing condition BAD. + Used by finish_static_assert and IFN_ASSUME constexpr diagnostics. + If SHOW_EXPR_P is true, print the condition (because it was + instantiation-dependent). */ + +void +diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p, + const constexpr_ctx *ctx /* = nullptr */) +{ + /* Nobody wants to see the artificial (bool) cast. */ + bad = tree_strip_nop_conversions (bad); + + /* Actually explain the failure if this is a concept check or a + requires-expression. */ + if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR) + diagnose_constraints (cloc, bad, NULL_TREE); + else if (COMPARISON_CLASS_P (bad) + && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))) + { + tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx); + tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx); + tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1); + inform (cloc, "the comparison reduces to %qE", cond); + } + else if (show_expr_p) + inform (cloc, "%qE evaluates to false", bad); +} + /* Evaluate a call T to a GCC internal function when possible and return the evaluated result or, under the control of CTX, give an error, set NON_CONSTANT_P, and return the unevaluated call T otherwise. */ @@ -1897,7 +1934,7 @@ cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, /* Report the error. */ error_at (cloc, "failed % attribute assumption"); - diagnose_failing_condition (bad, cloc, false); + diagnose_failing_condition (bad, cloc, false, &new_ctx); } *non_constant_p = true; diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 82f9dd8dd4e..36aa9c4499f 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -11183,33 +11183,6 @@ init_cp_semantics (void) } -/* Emit additional diagnostics for failing condition BAD. - Used by finish_static_assert and IFN_ASSUME constexpr diagnostics. - If SHOW_EXPR_P is true, print the condition (because it was - instantiation-dependent). */ - -void -diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p) -{ - /* Nobody wants to see the artificial (bool) cast. */ - bad = tree_strip_nop_conversions (bad); - - /* Actually explain the failure if this is a concept check or a - requires-expression. */ - if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR) - diagnose_constraints (cloc, bad, NULL_TREE); - else if (COMPARISON_CLASS_P (bad) - && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))) - { - tree op0 = fold_non_dependent_expr (TREE_OPERAND (bad, 0)); - tree op1 = fold_non_dependent_expr (TREE_OPERAND (bad, 1)); - tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1); - inform (cloc, "the comparison reduces to %qE", cond); - } - else if (show_expr_p) - inform (cloc, "%qE evaluates to false", bad); -} - /* Build a STATIC_ASSERT for a static assertion with the condition CONDITION and the message text MESSAGE. LOCATION is the location of the static assertion in the source code. When MEMBER_P, this diff --git a/gcc/testsuite/g++.dg/cpp23/attr-assume2.C b/gcc/testsuite/g++.dg/cpp23/attr-assume2.C index 9e54c14974f..4dd12216d06 100644 --- a/gcc/testsuite/g++.dg/cpp23/attr-assume2.C +++ b/gcc/testsuite/g++.dg/cpp23/attr-assume2.C @@ -26,7 +26,7 @@ f2 (int x) { #if __cpp_constexpr >= 201304L [[assume (x == 42)]]; // { dg-error "failed 'assume' attribute assumption" "" { target c++14 } } -#endif // { dg-message "the comparison reduces to '\\\(x == 42\\\)'" "" { target c++14 } .-1 } +#endif // { dg-message "the comparison reduces to '\\\(44 == 42\\\)'" "" { target c++14 } .-1 } return x; } @@ -76,7 +76,7 @@ f7 (int x, int y, int z, int w) { #if __cpp_constexpr >= 201304L [[assume (x == 42 && y == 43 && z == 44 && w == 45)]]; // { dg-error "failed 'assume' attribute assumption" "" { target c++14 } } -#endif // { dg-message "the comparison reduces to '\\\(z == 44\\\)'" "" { target c++14 } .-1 } +#endif // { dg-message "the comparison reduces to '\\\(45 == 44\\\)'" "" { target c++14 } .-1 } return x; }