From patchwork Thu Mar 24 14:32:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 52319 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A72103888C5B for ; Thu, 24 Mar 2022 14:33:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A72103888C5B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1648132412; bh=aY3MwoL469Ix/HtE84fAaHIEJbt5ksg5m+VycnD3tz8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=I9gVDr+qbFIaIdqDGNbTvhHRfjKUGQozbRIM5yM+Hm8FQIX3NJktq+sapS8SdUpMf uwR2pjBXgbh5i1PTOrxmd4iMvabK5p6Hb/qULvWV1+mwKOoXXUaMQH6GlnLS4tMsBf 8q3DiiUczNd6LqdMZR74yZ9Y4yDizlo3gzbzntoc= 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 EF3EA3858C2C for ; Thu, 24 Mar 2022 14:33:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EF3EA3858C2C Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-126-o9Efanc-NaWGvY5AtbcyeA-1; Thu, 24 Mar 2022 10:32:59 -0400 X-MC-Unique: o9Efanc-NaWGvY5AtbcyeA-1 Received: by mail-qt1-f199.google.com with SMTP id t19-20020ac86a13000000b002e1fd2c4ce5so3823201qtr.5 for ; Thu, 24 Mar 2022 07:32:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=aY3MwoL469Ix/HtE84fAaHIEJbt5ksg5m+VycnD3tz8=; b=MyT7h3W8zR+pVn38hsvu4z0lkJjvZsFUC0zmKPvOJMnmE8Kije5C8B0DrXfbe1pdXH qbhz1iVe4sloecK7d93q7GaGwqUa8JZWEJQtMNEHXy8d3FWfGFxo9zSAGIULhdrMoqSv 6M0Dyuti8OnstGe3im1C16vQl/1Bvw3qZIieNeAOYqVAopBBBq2BZcmN06xK8DvCaKbw f5oKoxXNF708YxyXmTl8moa4npeozL+YaOHczA0UgpCKQisqhp2tLNRRLee0dX3dHWV3 WMfvmJV7O+k5HPPbDKinMV2UoZiYmYm/B2FBxBuKfcfaH7tYuCiqQICZePWymEX61TwS UCvQ== X-Gm-Message-State: AOAM532cAvxcp5Yftro1/CNwHwRNyDToyrTFIpn9vSmuoS/rMtAjee8u Dr6KV/6wskaYkYHyXNL7F0S/h0+THbdxmkBI9a9Kthg+brI5DpCGuLCEhTXlItlqYTU+ev63b71 7QfI/5j2HQkKFmbmCZwzzv9usNx64KkjBpmkv6tb+EwSKfmKTwDdmEvb3IKmbJKDclQg= X-Received: by 2002:ac8:7d86:0:b0:2e1:fe79:d602 with SMTP id c6-20020ac87d86000000b002e1fe79d602mr4656141qtd.502.1648132378232; Thu, 24 Mar 2022 07:32:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzSFM5wvhTUde+wvGWTcuzmJkdoyP+DLX6TNOr1oppgzShdw2PecaZLCcm7wDvdgkdR+VYceQ== X-Received: by 2002:ac8:7d86:0:b0:2e1:fe79:d602 with SMTP id c6-20020ac87d86000000b002e1fe79d602mr4656106qtd.502.1648132377845; Thu, 24 Mar 2022 07:32:57 -0700 (PDT) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id c1-20020a05620a0ce100b0067e0cd1746fsm1537361qkj.51.2022.03.24.07.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 07:32:57 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: missing SFINAE for consteval calls [PR104620] Date: Thu, 24 Mar 2022 10:32:54 -0400 Message-Id: <20220324143254.1937777-1-ppalka@redhat.com> X-Mailer: git-send-email 2.35.1.655.ga68dfadae5 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.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_NONE, 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Cc: jakub@redhat.com Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Here we weren't respecting SFINAE when evaluating a substituted call to a consteval function, which caused us to reject the new testcase below. This patch fixes this by making build_over_call use the SFINAE-friendly version of cxx_constant_value. This change causes us to no longer diagnose ahead of time a couple of invalid non-dependent consteval calls in consteval-if2.C (with -fchecking=2). These errors were apparently coming from the call to fold_non_dependent_expr in build_non_dependent_expr (for the RHS of the +=) despite complain=tf_none being passed. Now that build_over_call respects the value of complain during constant evaluation of a consteval call, the errors are gone. That the errors don't occur without -fchecking=2 is a regression caused by r12-7264-gc19f317a78c0e4 and is the subject of PR104620. As described in comment #5, I think it was only an accident that we were diagnosing these two calls correctly before r12-7264, so perhaps we can live without them in GCC 12. To that end this patch just XFAILs the two tests. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/104620 gcc/cp/ChangeLog: * call.cc (build_over_call): Use cxx_constant_value_sfinae instead of cxx_constant_value to evaluate a consteval call. * constexpr.cc (cxx_constant_value_sfinae): Add decl parameter and pass it to cxx_eval_outermost_constant_expr. * cp-tree.h (cxx_constant_value_sfinae): Add decl parameter. * pt.cc (fold_targs_r): Pass NULL_TREE as decl to cxx_constant_value_sfinae. gcc/testsuite/ChangeLog: * g++.dg/cpp23/consteval-if2.C: XFAIL two dg-error tests where the argument to the non-dependent consteval call is wrapped in NON_DEPENDENT_EXPR. * g++.dg/cpp2a/consteval30.C: New test. --- gcc/cp/call.cc | 2 +- gcc/cp/constexpr.cc | 4 ++-- gcc/cp/cp-tree.h | 2 +- gcc/cp/pt.cc | 2 +- gcc/testsuite/g++.dg/cpp23/consteval-if2.C | 4 ++-- gcc/testsuite/g++.dg/cpp2a/consteval30.C | 12 ++++++++++++ 6 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/consteval30.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 23d3fc496b8..ec6c5d5baa2 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -9939,7 +9939,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) obj_arg = TREE_OPERAND (addr, 0); } } - call = cxx_constant_value (call, obj_arg); + call = cxx_constant_value_sfinae (call, obj_arg, complain); if (obj_arg && !error_operand_p (call)) call = build2 (INIT_EXPR, void_type_node, obj_arg, call); call = convert_from_reference (call); diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index a3136ce819d..2163a328e74 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7959,10 +7959,10 @@ cxx_constant_value (tree t, tree decl) /* As above, but respect SFINAE. */ tree -cxx_constant_value_sfinae (tree t, tsubst_flags_t complain) +cxx_constant_value_sfinae (tree t, tree decl, tsubst_flags_t complain) { bool sfinae = !(complain & tf_error); - tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true); + tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true, false, decl); if (sfinae && !TREE_CONSTANT (r)) r = error_mark_node; return r; diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 1bd7bc6fca2..2f718852ac1 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8414,7 +8414,7 @@ extern bool require_constant_expression (tree); extern bool require_rvalue_constant_expression (tree); extern bool require_potential_rvalue_constant_expression (tree); extern tree cxx_constant_value (tree, tree = NULL_TREE); -extern tree cxx_constant_value_sfinae (tree, tsubst_flags_t); +extern tree cxx_constant_value_sfinae (tree, tree, tsubst_flags_t); extern void cxx_constant_dtor (tree, tree); extern tree cxx_constant_init (tree, tree = NULL_TREE); extern tree maybe_constant_value (tree, tree = NULL_TREE, bool = false); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 715eea27577..173bc3a8c7f 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -19811,7 +19811,7 @@ fold_targs_r (tree targs, tsubst_flags_t complain) && !glvalue_p (elt) && !TREE_CONSTANT (elt)) { - elt = cxx_constant_value_sfinae (elt, complain); + elt = cxx_constant_value_sfinae (elt, NULL_TREE, complain); if (elt == error_mark_node) return false; } diff --git a/gcc/testsuite/g++.dg/cpp23/consteval-if2.C b/gcc/testsuite/g++.dg/cpp23/consteval-if2.C index f7053b91c3c..d1845da9e58 100644 --- a/gcc/testsuite/g++.dg/cpp23/consteval-if2.C +++ b/gcc/testsuite/g++.dg/cpp23/consteval-if2.C @@ -77,11 +77,11 @@ qux (int x) } else { - r += foo (8 * x); // { dg-error "is not a constant expression" } + r += foo (8 * x); // { dg-error "is not a constant expression" "" { xfail *-*-* } } } if ! consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } { - r += foo (32 * x);// { dg-error "is not a constant expression" } + r += foo (32 * x);// { dg-error "is not a constant expression" "" { xfail *-*-* } } } if consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } { diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval30.C b/gcc/testsuite/g++.dg/cpp2a/consteval30.C new file mode 100644 index 00000000000..d393dcc0857 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval30.C @@ -0,0 +1,12 @@ +// Test SFINAE behavior for an invalid consteval call. +// { dg-do compile { target c++20 } } + +consteval int deref(const int* x) { return *x; } + +template // { dg-bogus "null pointer" } +constexpr int f(int) { return 0; } + +template +constexpr int f(...) { return 1; } + +static_assert(f(0) == 1);