From patchwork Fri Sep 1 17:23:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 137404 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp1035044vqu; Fri, 1 Sep 2023 10:25:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH/E0bue2oRn6u3Xj9NcoBhn5zQMPhlujQC3HCpHjisJVSJyt2imc36iUCbdt84SRIrbchU X-Received: by 2002:a17:907:77da:b0:9a1:e66e:b69a with SMTP id kz26-20020a17090777da00b009a1e66eb69amr2319363ejc.21.1693589101941; Fri, 01 Sep 2023 10:25:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693589101; cv=none; d=google.com; s=arc-20160816; b=ZRV10NErIELMK9HYOVD7xZyDfNVKLWKPH92/NX7H3jfBkFtFNs69FXZIg+D/fVhZ8b hYr+xYpUeAIAC8qL50Qd8tF4ceBhkEPWW7+6q8g9X9J68p0nQ6e5v4Osg3efUG3rlM2L VVm/ms6KkFkX02NgpNhc7Gz0/IlcBJc5W810HuXnBwY/ANO5fQac5rbDHC+rs9NmhEew 0+0xt8RyE0Jb/VnC4dXx35Az+uTV5okSlTrUXNq3G+7XeYQ7ZaZMVBUzdwNtEfitLZZA RuCjh2iaKGkVBIKxz094q9U/t5RrTO5VZd2Hc2qVceapnpWS2Yl35ItoyQx35xZZR6X3 Kk0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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=Wl43xwOhZFUgx0qm1bdjM1T1qUwAsAig9wdauXp6yRw=; fh=fpCc4VkYXmQrurtjjuGMl3oKQaOAjGwXgTAjHxumZ4s=; b=XSE84aGiqzxLnDK28tCRLzN1O1Mtb6G3+G+r5hbO+3/u7Wf2yw8yE/6MnJOXps68iZ C26exklkPAJoWDg+Y+FNzrJILjS0bLUdksy55ajMibIkYeO8IdQ8GQyCQrMi+CrO4PIO kXIJxzGXNxY59if+Xkt5qdx7BLT4DXKOTXyLDPmwY176dmfvqg+yeIuSo2OO05JA3AmX E4LHl5jF2HG+ttKmrVr6muNmNiiv+XH29TwyJM+fPfHkJrmh4Z3DvbgMMFxaucBrkNSg Qe34hNDYlcM2VaUMDHSA6BtVNMp0HiAaKc9HVE17o48SZSz1RTq2xbxb8/qYQrMCCtEU 5gzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=r4ppMojO; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id p11-20020a1709061b4b00b009a210885fb4si2653033ejg.83.2023.09.01.10.25.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 10:25:01 -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=r4ppMojO; 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 613083858407 for ; Fri, 1 Sep 2023 17:25:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 613083858407 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693589100; bh=Wl43xwOhZFUgx0qm1bdjM1T1qUwAsAig9wdauXp6yRw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=r4ppMojO2TLCEElerGEkzxTrVtUrQtNtcWoQj3B+fCjXRpbH6Vfs2rhdd9VZEZa2q KQEBUxClbR034bj9ZegNe65Zgy7CymC5t2dI/xmz8/HvLdsdvvnn30o+5rY2+/lKeV YoDtzUkYgSKqEhGQoc5V45LTUC4r5KyqLV/V2G5A= 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 375C03858D20 for ; Fri, 1 Sep 2023 17:24:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 375C03858D20 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-314-FeJSoKiiNAywnn_qPxZ7vg-1; Fri, 01 Sep 2023 13:23:53 -0400 X-MC-Unique: FeJSoKiiNAywnn_qPxZ7vg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5C934805F6E for ; Fri, 1 Sep 2023 17:23:53 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3414A400E13E; Fri, 1 Sep 2023 17:23:53 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Move consteval folding to cp_fold_r Date: Fri, 1 Sep 2023 13:23:48 -0400 Message-ID: <20230901172348.445289-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX 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.30 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775856886258525196 X-GMAIL-MSGID: 1775856886258525196 Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- In the review of P2564: it turned out that in order to correctly handle an example in the paper, we should stop doing immediate evaluation in build_over_call and bot_replace, and instead do it in cp_fold_r. This patch does that. Another benefit is that this is a pretty significant simplification, at least in my opinion. Also, this fixes the c++/110997 ICE (but the test doesn't compile yet). The main drawback seems to be that cp_fold_r doesn't process as much code as we did before: uninstantiated templates and things like "false ? foo () : 1". You'll see that I've reintroduced ADDR_EXPR_DENOTES_CALL_P here. This is to detect *(&foo)) () (s.*&S::foo) () which were deemed ill-formed. gcc/cp/ChangeLog: * call.cc (in_immediate_context): No longer static. (build_over_call): Set ADDR_EXPR_DENOTES_CALL_P. Don't handle immediate_invocation_p here. * constexpr.cc (cxx_eval_call_expression): Use mce_true for immediate_invocation_p. * cp-gimplify.cc (cp_fold_r): Expand immediate invocations. * cp-tree.h (ADDR_EXPR_DENOTES_CALL_P): Define. (immediate_invocation_p): Declare. * tree.cc (bot_replace): Don't handle immediate invocations here. gcc/testsuite/ChangeLog: * g++.dg/cpp23/consteval-if2.C: Add xfail. * g++.dg/cpp2a/consteval-memfn1.C: Adjust. * g++.dg/cpp2a/consteval11.C: Remove dg-message. * g++.dg/cpp2a/consteval3.C: Remove dg-message and dg-error. * g++.dg/cpp2a/consteval9.C: Remove dg-message. * g++.dg/cpp2a/consteval32.C: New test. * g++.dg/cpp2a/consteval33.C: New test. libstdc++-v3/ChangeLog: * testsuite/20_util/allocator/105975.cc: Add dg-error. --- gcc/cp/call.cc | 42 +++---------------- gcc/cp/constexpr.cc | 5 +++ gcc/cp/cp-gimplify.cc | 14 ++++++- gcc/cp/cp-tree.h | 6 +++ gcc/cp/tree.cc | 23 +--------- gcc/testsuite/g++.dg/cpp23/consteval-if2.C | 2 +- gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C | 7 ++++ gcc/testsuite/g++.dg/cpp2a/consteval11.C | 37 ++++++++-------- gcc/testsuite/g++.dg/cpp2a/consteval3.C | 3 +- gcc/testsuite/g++.dg/cpp2a/consteval32.C | 4 ++ gcc/testsuite/g++.dg/cpp2a/consteval33.C | 34 +++++++++++++++ gcc/testsuite/g++.dg/cpp2a/consteval9.C | 2 +- .../testsuite/20_util/allocator/105975.cc | 2 +- 13 files changed, 100 insertions(+), 81 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/consteval32.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/consteval33.C base-commit: 419c423d3aeca754e47e1ce1bf707735603a90a3 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 40d9fdc0516..abdbc8fff8c 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -9763,7 +9763,7 @@ in_immediate_context () /* Return true if a call to FN with number of arguments NARGS is an immediate invocation. */ -static bool +bool immediate_invocation_p (tree fn) { return (TREE_CODE (fn) == FUNCTION_DECL @@ -10471,6 +10471,10 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) fn = build_addr_func (fn, complain); if (fn == error_mark_node) return error_mark_node; + + /* We're actually invoking the function. (Immediate functions get an + & when invoking it even though the user didn't use &.) */ + ADDR_EXPR_DENOTES_CALL_P (fn) = true; } tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag); @@ -10488,41 +10492,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) if (TREE_CODE (c) == CALL_EXPR) suppress_warning (c /* Suppress all warnings. */); } - if (TREE_CODE (fn) == ADDR_EXPR) - { - tree fndecl = STRIP_TEMPLATE (TREE_OPERAND (fn, 0)); - if (immediate_invocation_p (fndecl)) - { - tree obj_arg = NULL_TREE; - /* Undo convert_from_reference called by build_cxx_call. */ - if (REFERENCE_REF_P (call)) - call = TREE_OPERAND (call, 0); - if (DECL_CONSTRUCTOR_P (fndecl)) - obj_arg = cand->first_arg ? cand->first_arg : (*args)[0]; - if (obj_arg && is_dummy_object (obj_arg)) - { - call = build_cplus_new (DECL_CONTEXT (fndecl), call, complain); - obj_arg = NULL_TREE; - } - /* Look through *(const T *)&obj. */ - else if (obj_arg && INDIRECT_REF_P (obj_arg)) - { - tree addr = TREE_OPERAND (obj_arg, 0); - STRIP_NOPS (addr); - if (TREE_CODE (addr) == ADDR_EXPR) - { - tree typeo = TREE_TYPE (obj_arg); - tree typei = TREE_TYPE (TREE_OPERAND (addr, 0)); - if (same_type_ignoring_top_level_qualifiers_p (typeo, typei)) - obj_arg = TREE_OPERAND (addr, 0); - } - } - call = cxx_constant_value (call, obj_arg, complain); - if (obj_arg && !error_operand_p (call)) - call = cp_build_init_expr (obj_arg, call); - call = convert_from_reference (call); - } - } + return call; } diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 8bd5c4a47f8..af4f98b1fe1 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3135,6 +3135,11 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; + /* Make sure we fold std::is_constant_evaluated to true in an + immediate function. */ + if (immediate_invocation_p (fun)) + call_ctx.manifestly_const_eval = mce_true; + /* If this is a constexpr destructor, the object's const and volatile semantics are no longer in effect; see [class.dtor]p5. */ if (new_obj && DECL_DESTRUCTOR_P (fun)) diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 206e791fcfd..29132aad158 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -1058,9 +1058,21 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_) } break; + /* Expand immediate invocations. */ + case CALL_EXPR: + case AGGR_INIT_EXPR: + if (!in_immediate_context ()) + if (tree fn = cp_get_callee (stmt)) + if (TREE_CODE (fn) != ADDR_EXPR || ADDR_EXPR_DENOTES_CALL_P (fn)) + if (tree fndecl = cp_get_fndecl_from_callee (fn, /*fold*/false)) + if (DECL_IMMEDIATE_FUNCTION_P (fndecl)) + *stmt_p = stmt = cxx_constant_value (stmt); + break; + case ADDR_EXPR: if (TREE_CODE (TREE_OPERAND (stmt, 0)) == FUNCTION_DECL - && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (stmt, 0))) + && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (stmt, 0)) + && !in_immediate_context ()) { error_at (EXPR_LOCATION (stmt), "taking address of an immediate function %qD", diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 3ca011c61c8..8bd794c91fb 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -4784,6 +4784,11 @@ get_vec_init_expr (tree t) #define PTRMEM_OK_P(NODE) \ TREE_LANG_FLAG_0 (TREE_CHECK3 ((NODE), ADDR_EXPR, OFFSET_REF, SCOPE_REF)) +/* True if this ADDR_EXPR denotes a function call; that is, it's + fn() rather than &fn. */ +#define ADDR_EXPR_DENOTES_CALL_P(NODE) \ + (ADDR_EXPR_CHECK(NODE)->base.protected_flag) + /* Get the POINTER_TYPE to the METHOD_TYPE associated with this pointer to member function. TYPE_PTRMEMFUNC_P _must_ be true, before using this macro. */ @@ -6713,6 +6718,7 @@ extern tree perform_direct_initialization_if_possible (tree, tree, bool, extern vec *resolve_args (vec*, tsubst_flags_t); extern tree in_charge_arg_for_name (tree); extern bool in_immediate_context (); +extern bool immediate_invocation_p (tree); extern tree build_cxx_call (tree, int, tree *, tsubst_flags_t, tree = NULL_TREE); diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 799183dc646..7dfb6de2da3 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -3254,7 +3254,7 @@ bot_manip (tree* tp, int* walk_subtrees, void* data_) variables. */ static tree -bot_replace (tree* t, int* walk_subtrees, void* data_) +bot_replace (tree* t, int*, void* data_) { bot_data &data = *(bot_data*)data_; splay_tree target_remap = data.target_remap; @@ -3284,27 +3284,6 @@ bot_replace (tree* t, int* walk_subtrees, void* data_) /*check_access=*/false, /*nonnull=*/true, tf_warning_or_error); } - else if (cxx_dialect >= cxx20 - && (TREE_CODE (*t) == CALL_EXPR - || TREE_CODE (*t) == AGGR_INIT_EXPR) - && !in_immediate_context ()) - { - /* Expand immediate invocations. */ - if (tree fndecl = cp_get_callee_fndecl_nofold (*t)) - if (DECL_IMMEDIATE_FUNCTION_P (fndecl)) - { - /* Make in_immediate_context true within the args. */ - in_consteval_if_p_temp_override ito; - in_consteval_if_p = true; - int nargs = call_expr_nargs (*t); - for (int i = 0; i < nargs; ++i) - cp_walk_tree (&get_nth_callarg (*t, i), bot_replace, data_, NULL); - *t = cxx_constant_value (*t); - if (*t == error_mark_node) - return error_mark_node; - *walk_subtrees = 0; - } - } return NULL_TREE; } diff --git a/gcc/testsuite/g++.dg/cpp23/consteval-if2.C b/gcc/testsuite/g++.dg/cpp23/consteval-if2.C index d1845da9e58..9fa95295c43 100644 --- a/gcc/testsuite/g++.dg/cpp23/consteval-if2.C +++ b/gcc/testsuite/g++.dg/cpp23/consteval-if2.C @@ -65,7 +65,7 @@ qux (int x) int r = 0; if not consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } { - r += foo (x); // { dg-error "'x' is not a constant expression" } + r += foo (x); // { dg-error "'x' is not a constant expression" "" { xfail *-*-* } } } else { diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C b/gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C index 910e7a1ac1e..63f4f1d526a 100644 --- a/gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C +++ b/gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C @@ -25,3 +25,10 @@ void VerifyHash(fixed_string s) { fixed_string::size_static(-1); // { dg-message "expansion of" } s(); // { dg-bogus "" } } + +void +do_test () +{ + fixed_string f; + VerifyHash(f); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval11.C b/gcc/testsuite/g++.dg/cpp2a/consteval11.C index 2f68ec0f892..9fd32dcab7b 100644 --- a/gcc/testsuite/g++.dg/cpp2a/consteval11.C +++ b/gcc/testsuite/g++.dg/cpp2a/consteval11.C @@ -5,25 +5,25 @@ consteval int bar (int i) { if (i != 1) throw 1; return 0; } // { dg-error "is n constexpr int a = bar (1); constexpr int b = bar (2); // { dg-message "in 'constexpr' expansion of" } -constexpr int c = 0 ? bar (3) : 1; // { dg-message "in 'constexpr' expansion of" } +constexpr int c = 0 ? bar (3) : 1; const int d = bar (4); // { dg-message "in 'constexpr' expansion of" } -const int e = 0 ? bar (5) : 1; // { dg-message "in 'constexpr' expansion of" } +const int e = 0 ? bar (5) : 1; int f = bar (1); int g = bar (6); // { dg-message "in 'constexpr' expansion of" } -int h = 0 ? bar (7) : 1; // { dg-message "in 'constexpr' expansion of" } +int h = 0 ? bar (7) : 1; void foo () { constexpr int a = bar (1); constexpr int b = bar (2); // { dg-message "in 'constexpr' expansion of" } - constexpr int c = 0 ? bar (3) : 1; // { dg-message "in 'constexpr' expansion of" } + constexpr int c = 0 ? bar (3) : 1; const int d = bar (4); // { dg-message "in 'constexpr' expansion of" } - const int e = 0 ? bar (5) : 1; // { dg-message "in 'constexpr' expansion of" } + const int e = 0 ? bar (5) : 1; int f = bar (1); int g = bar (6); // { dg-message "in 'constexpr' expansion of" } - int h = 0 ? bar (7) : 1; // { dg-message "in 'constexpr' expansion of" } - h += 0 ? bar (8) : 1; // { dg-message "in 'constexpr' expansion of" } + int h = 0 ? bar (7) : 1; + h += 0 ? bar (8) : 1; if (0) bar (9); // { dg-message "in 'constexpr' expansion of" } else @@ -33,13 +33,13 @@ foo () else bar (12); // { dg-message "in 'constexpr' expansion of" } if constexpr (0) - bar (13); // { dg-message "in 'constexpr' expansion of" } + bar (13); else bar (14); // { dg-message "in 'constexpr' expansion of" } if constexpr (1) bar (15); // { dg-message "in 'constexpr' expansion of" } else - bar (16); // { dg-message "in 'constexpr' expansion of" } + bar (16); } consteval int @@ -77,22 +77,25 @@ template void qux () { + // Used to give errors errors here, but not since we moved consteval + // function folding to cp_fold_r which isn't called on uninstantiated + // templates. if (0) - bar (2); // { dg-message "in 'constexpr' expansion of" } + bar (2); else - bar (3); // { dg-message "in 'constexpr' expansion of" } + bar (3); if (1) - bar (4); // { dg-message "in 'constexpr' expansion of" } + bar (4); else - bar (5); // { dg-message "in 'constexpr' expansion of" } + bar (5); if constexpr (0) - bar (6); // { dg-message "in 'constexpr' expansion of" } + bar (6); else - bar (7); // { dg-message "in 'constexpr' expansion of" } + bar (7); if constexpr (1) - bar (8); // { dg-message "in 'constexpr' expansion of" } + bar (8); else - bar (9); // { dg-message "in 'constexpr' expansion of" } + bar (9); if (0) bar ((T) 2); else diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval3.C b/gcc/testsuite/g++.dg/cpp2a/consteval3.C index 627ab142d5a..9efac8c8eae 100644 --- a/gcc/testsuite/g++.dg/cpp2a/consteval3.C +++ b/gcc/testsuite/g++.dg/cpp2a/consteval3.C @@ -18,8 +18,7 @@ consteval int f6 (int x) { return x; } int d = 6; // { dg-message "'int d' is not const" } int e = f6 (d); // { dg-error "the value of 'd' is not usable in a constant expression" } constexpr int f7 (int x) { return f6 (x); } // { dg-error "'x' is not a constant expression" } -constexpr int f = f7 (5); // { dg-error "" } - // { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 } +constexpr int f = f7 (5); using fnptr = int (int); fnptr *g = f6; // { dg-error "taking address of an immediate function 'consteval int f6\\(int\\)'" } int f8 (fnptr *); diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval32.C b/gcc/testsuite/g++.dg/cpp2a/consteval32.C new file mode 100644 index 00000000000..f1de63e41b9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval32.C @@ -0,0 +1,4 @@ +// { dg-do compile { target c++20 } } + +consteval int foo () { return 42; } +int bar () { return (*(&foo)) (); } // { dg-error "taking address" } diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval33.C b/gcc/testsuite/g++.dg/cpp2a/consteval33.C new file mode 100644 index 00000000000..3d50b00c7a3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval33.C @@ -0,0 +1,34 @@ +// { dg-do compile { target c++20 } } + +consteval int id (int i) { return i; } +consteval int add (int i, int j) { return i + j; } + +constexpr int +foo (int i = id (42)) +{ + return i + id (id (id (0))); +} + +constexpr int +bar (int i = id (id (id (42)))) +{ + return i; +} + +constexpr int +baz (int i = add (add (id (1), id (2)), id (3))) +{ + return i; +} + +void +g () +{ + foo (); + bar (); + baz (); +} + +static_assert (foo () == 42); +static_assert (bar () == 42); +static_assert (baz () == 6); diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval9.C b/gcc/testsuite/g++.dg/cpp2a/consteval9.C index 489286a12d2..230a6e9951c 100644 --- a/gcc/testsuite/g++.dg/cpp2a/consteval9.C +++ b/gcc/testsuite/g++.dg/cpp2a/consteval9.C @@ -18,7 +18,7 @@ void qux () template void quux () { - int a = bar (5); // { dg-message "in 'constexpr' expansion of 'bar\\(5\\)'" } + int a = bar (5); } void diff --git a/libstdc++-v3/testsuite/20_util/allocator/105975.cc b/libstdc++-v3/testsuite/20_util/allocator/105975.cc index 09f27ba86e3..06f1d96d9b7 100644 --- a/libstdc++-v3/testsuite/20_util/allocator/105975.cc +++ b/libstdc++-v3/testsuite/20_util/allocator/105975.cc @@ -14,6 +14,6 @@ consteval bool test_pr105957() a.deallocate(p, n); return true; } -static_assert( test_pr105957() ); +static_assert( test_pr105957() ); // { dg-error "non-constant" } // { dg-error "throw_bad_array_new_length" "" { target *-*-* } 0 }