From patchwork Thu Aug 1 16:48:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Arsen_Arsenovi=C4=87?= X-Patchwork-Id: 1967924 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=aarsen.me header.i=@aarsen.me header.a=rsa-sha256 header.s=MBO0001 header.b=UzMGQVmw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZZk567p2z1ybV for ; Fri, 2 Aug 2024 02:51:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 24DE13858C39 for ; Thu, 1 Aug 2024 16:51:40 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) by sourceware.org (Postfix) with ESMTPS id 71AA23858D3C for ; Thu, 1 Aug 2024 16:51:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 71AA23858D3C Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=aarsen.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=aarsen.me ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 71AA23858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=80.241.56.161 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722531079; cv=none; b=wIzo4Rhc78D+dQmWu1n9pyyxhD+4XicGVuKuBPnonqfaU4gzhybNB5dw7vOv5146JThJObwzViY9U/tdK8hzG/XoskH1vjG72q37u+6FnyADL5nkW4oShGsLZu/QMYqrkau44b/QeCHgWwwX3iLllJefjYVMj9nBfaxMKlOQy28= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722531079; c=relaxed/simple; bh=LBrL8izhEpQGjkLGCXR5g0wbAj8qkpxJvM7etaXbHIE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pgyF/YyMw7JB95IL8WaM12U1On1ej6K04z7C9QPN3FD2kQYo0olc7tgLV2AO+f4h8VI1A1XKT73jZxB6+IKN83nbKkBv927/FxfmZf5seH+UJrG17HPgVaag578HrczHgiAu7j6SjXHXFH1o8EBTWMuaozT3ijdGV/3/eEWSkvw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (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 mout-p-103.mailbox.org (Postfix) with ESMTPS id 4WZZjR2Y8Rz9sJ5; Thu, 1 Aug 2024 18:51:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aarsen.me; s=MBO0001; t=1722531067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Q45BBgR8JZpCfawqwG/k8TCrnTen7He2PVRx0bD3Cu4=; b=UzMGQVmwpEPWwvICHlQclKd5kVctufrexeoCw+O4qLrhR4neEheIDEmLTjHnJV5IR1Tlvx 6pnf+6ab+RnkmzSORTnVbzOsH6tW/gSg5CtTFTBomULvtP2A07N+aoj3XKtSfaTU/NT8lA tRR87d4WRoeeqOBGDNIpzm7kLtF51RUF0CWgCjukC+Ht3RZvPOrXnU3IYLO+nPvxp8Bl09 bJ8ZmCL47Mnzf2zX33qy63DqF9Veb7kV+A8178sOvMR1CtUuIDROCxf2Y67xaJBVLZECVO Q/ee6kTk4cIIheZ4zOuRlTzIYbmKYfaQQjpjmijKmrw2P3+KcaCEUuiSIYU5cw== From: =?utf-8?q?Arsen_Arsenovi=C4=87?= To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Iain Sandoe , =?utf-8?q?Arsen_Arsenovi=C4=87?= Subject: [PATCH] c++/coroutines: fix actor cases not being added to the current switch [PR109867] Date: Thu, 1 Aug 2024 18:48:24 +0200 Message-ID: <20240801165029.2485261-1-arsen@aarsen.me> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_INFOUSMEBIZ, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested on x86_64-pc-linux-gnu, no regression. OK for trunk? TIA, have a lovely day. ---------- >8 ---------- Previously, we were building and inserting case_labels manually, which lead to them not being added into the currently running switch via c_add_case_label. This lead to false diagnostics that the user could not act on. The use of temp_override on current_function_decl is a temporary hack, as we intend to factor out generating the outlined functions (including the actor) to happen later in finish_function, when we can create a new function using the standard routines for that, similar to finish_function_contracts. PR c++/109867 - -Wswitch-default reports missing default in coroutine gcc/cp/ChangeLog: PR c++/109867 * coroutines.cc (expand_one_await_expression): Replace uses of build_case_label with finish_case_label. (build_actor_fn): Ditto. Also adjust current_function_decl. (create_anon_label_with_ctx): Remove now-unused function. gcc/testsuite/ChangeLog: PR c++/109867 * g++.dg/coroutines/torture/pr109867.C: New test. Reviewed-by: Iain Sandoe --- gcc/cp/coroutines.cc | 57 +++++-------------- .../g++.dg/coroutines/torture/pr109867.C | 23 ++++++++ 2 files changed, 38 insertions(+), 42 deletions(-) create mode 100644 gcc/testsuite/g++.dg/coroutines/torture/pr109867.C diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc index 91bbe6b0a0eb..9e8382269ed0 100644 --- a/gcc/cp/coroutines.cc +++ b/gcc/cp/coroutines.cc @@ -1629,22 +1629,8 @@ coro_build_artificial_var (location_t loc, const char *name, tree type, type, ctx, init); } -/* Helpers for label creation: - 1. Create a named label in the specified context. */ -static tree -create_anon_label_with_ctx (location_t loc, tree ctx) -{ - tree lab = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node); - - DECL_CONTEXT (lab) = ctx; - DECL_ARTIFICIAL (lab) = true; - DECL_IGNORED_P (lab) = true; - TREE_USED (lab) = true; - return lab; -} - -/* 2. Create a named label in the specified context. */ +/* Creates a named label in the specified context. */ static tree create_named_label_with_ctx (location_t loc, const char *name, tree ctx) @@ -1856,22 +1842,16 @@ expand_one_await_expression (tree *stmt, tree *await_expr, void *d) data->coro_fp); r = cp_build_init_expr (cond, r); finish_switch_cond (r, sw); - r = build_case_label (integer_zero_node, NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (r); /* case 0: */ + finish_case_label (loc, integer_zero_node, NULL_TREE); /* case 0: */ /* Implement the suspend, a scope exit without clean ups. */ r = build_call_expr_internal_loc (loc, IFN_CO_SUSPN, void_type_node, 1, is_cont ? cont : susp); r = coro_build_cvt_void_expr_stmt (r, loc); add_stmt (r); /* goto ret; */ - r = build_case_label (integer_one_node, NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (r); /* case 1: */ + finish_case_label (loc, integer_one_node, NULL_TREE); /* case 1: */ r = build1_loc (loc, GOTO_EXPR, void_type_node, resume_label); add_stmt (r); /* goto resume; */ - r = build_case_label (NULL_TREE, NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (r); /* default:; */ + finish_case_label (loc, NULL_TREE, NULL_TREE); /* default:; */ r = build1_loc (loc, GOTO_EXPR, void_type_node, destroy_label); add_stmt (r); /* goto destroy; */ @@ -2250,6 +2230,9 @@ build_actor_fn (location_t loc, tree coro_frame_type, tree actor, tree fnbody, tree top_block = make_node (BLOCK); BIND_EXPR_BLOCK (actor_bind) = top_block; + /* From here on, we're generating code for the actor. */ + auto cfdo = make_temp_override (current_function_decl, actor); + tree continuation = coro_build_artificial_var (loc, coro_actor_continue_id, void_coro_handle_type, actor, NULL_TREE); @@ -2303,9 +2286,7 @@ build_actor_fn (location_t loc, tree coro_frame_type, tree actor, tree fnbody, tree destroy_dispatcher = begin_switch_stmt (); finish_switch_cond (rat, destroy_dispatcher); - tree ddeflab = build_case_label (NULL_TREE, NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (ddeflab); + tree ddeflab = finish_case_label (loc, NULL_TREE, NULL_TREE); tree b = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0); b = coro_build_cvt_void_expr_stmt (b, loc); add_stmt (b); @@ -2316,18 +2297,15 @@ build_actor_fn (location_t loc, tree coro_frame_type, tree actor, tree fnbody, frame itself. */ tree del_promise_label = create_named_label_with_ctx (loc, "coro.delete.promise", actor); - b = build_case_label (build_int_cst (short_unsigned_type_node, 1), NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (b); + finish_case_label (loc, build_int_cst (short_unsigned_type_node, 1), + NULL_TREE); add_stmt (build_stmt (loc, GOTO_EXPR, del_promise_label)); short unsigned lab_num = 3; for (unsigned destr_pt = 0; destr_pt < body_count; destr_pt++) { tree l_num = build_int_cst (short_unsigned_type_node, lab_num); - b = build_case_label (l_num, NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (b); + finish_case_label (loc, l_num, NULL_TREE); b = build_call_expr_internal_loc (loc, IFN_CO_ACTOR, void_type_node, 1, l_num); b = coro_build_cvt_void_expr_stmt (b, loc); @@ -2345,15 +2323,12 @@ build_actor_fn (location_t loc, tree coro_frame_type, tree actor, tree fnbody, tree dispatcher = begin_switch_stmt (); finish_switch_cond (rat, dispatcher); - b = build_case_label (build_int_cst (short_unsigned_type_node, 0), NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (b); + finish_case_label (loc, build_int_cst (short_unsigned_type_node, 0), + NULL_TREE); b = build1 (GOTO_EXPR, void_type_node, actor_begin_label); add_stmt (b); - tree rdeflab = build_case_label (NULL_TREE, NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (rdeflab); + tree rdeflab = finish_case_label (loc, NULL_TREE, NULL_TREE); b = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0); b = coro_build_cvt_void_expr_stmt (b, loc); add_stmt (b); @@ -2365,9 +2340,7 @@ build_actor_fn (location_t loc, tree coro_frame_type, tree actor, tree fnbody, for (unsigned resu_pt = 0; resu_pt < body_count; resu_pt++) { tree l_num = build_int_cst (short_unsigned_type_node, lab_num); - b = build_case_label (l_num, NULL_TREE, - create_anon_label_with_ctx (loc, actor)); - add_stmt (b); + finish_case_label (loc, l_num, NULL_TREE); b = build_call_expr_internal_loc (loc, IFN_CO_ACTOR, void_type_node, 1, l_num); b = coro_build_cvt_void_expr_stmt (b, loc); diff --git a/gcc/testsuite/g++.dg/coroutines/torture/pr109867.C b/gcc/testsuite/g++.dg/coroutines/torture/pr109867.C new file mode 100644 index 000000000000..d4663771ea40 --- /dev/null +++ b/gcc/testsuite/g++.dg/coroutines/torture/pr109867.C @@ -0,0 +1,23 @@ +// https://gcc.gnu.org/PR109867 +// { dg-additional-options "-Werror=switch-default" } +#include + +struct task +{ + struct promise_type + { + std::suspend_always initial_suspend(); + std::suspend_always final_suspend() noexcept; + void unhandled_exception(); + task get_return_object(); + void return_value(int); + }; +}; + +int main() +{ + auto t = []() -> task + { + co_return 2; + }(); +}