From patchwork Fri Nov 5 15:46:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Sandoe X-Patchwork-Id: 47120 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 80B8D385842C for ; Fri, 5 Nov 2021 15:47:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 80B8D385842C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636127248; bh=jbtfo6XMt79hyli3eb6yZj0mi+R+r664OZJcOYIO4+A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=sLpE+FVNEwKA9MtraQBGUDTt+TRuSeDh+Q1kz++3u0UADLPNzLdhfoBALy3dIAaOO lr1d5ehddekAVp3uUz6t8maRFmaJXKsx7z/OgD/qmSHuTnFIinLB2vCLYFBj5UUTTO gmWr7RNvq7nsESlC+Edx8eqrkuPfnBRk+sPY+QO8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 2395D3858D28 for ; Fri, 5 Nov 2021 15:46:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2395D3858D28 Received: by mail-wr1-x42c.google.com with SMTP id u18so14389553wrg.5 for ; Fri, 05 Nov 2021 08:46: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:reply-to :mime-version:content-transfer-encoding; bh=jbtfo6XMt79hyli3eb6yZj0mi+R+r664OZJcOYIO4+A=; b=mA0zcWx1QO2QcxyJzxgbQFQnHfpr3gfBYY98Ey6t+TBtya2ufn9mprXMYoquTOL3xL mY31lR2zSbchzZ7v5fkF1hefknktFtBExjbnS9pU8PDKyzZSxLMV3vEeFvdCm207S3Uh 8clPff7p6AQe8eWqyM2/YRfa464rSBdVC/Kv64hc6GA8srIbSO5fI6gR4L8/ZrbOIVG8 h7o5RGSUA4mEuGw4IeuFULOLKjEH4TEml8SX5NCKs2B4so6AftYNgZvc1GWM0HiADcGx 2jQUxBYUoP/6EZlPGJzxHA4O8vd6AmFiOcQ4+3Rioe4aakdJvsY8nb/oP9vtDTBDOlMQ B9bw== X-Gm-Message-State: AOAM53162J+FdQvGb3ZeHAKD3ysWmt/UkbQSd6zoGXSQ4eRgXyqNrFY7 JKz9KEyezYlkPLGJ4LQZMdFPDmFs8f4= X-Google-Smtp-Source: ABdhPJxptSZ4eSlt3YnIvMihlcFksFJ5Z0TxSrFVGUyQisFRBsrCZ1wakuqZgcfMk3OAbP5VR9nVwg== X-Received: by 2002:a5d:4ed1:: with SMTP id s17mr57935091wrv.310.1636127218147; Fri, 05 Nov 2021 08:46:58 -0700 (PDT) Received: from localhost.localdomain (host81-138-1-83.in-addr.btopenworld.com. [81.138.1.83]) by smtp.gmail.com with ESMTPSA id l26sm8111661wms.15.2021.11.05.08.46.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 Nov 2021 08:46:57 -0700 (PDT) X-Google-Original-From: Iain Sandoe To: gcc-patches@gcc.gnu.org Subject: [PATCH] coroutines: Handle initial awaiters with non-void returns [PR 100127]. Date: Fri, 5 Nov 2021 15:46:51 +0000 Message-Id: <20211105154651.77786-1-iain@sandoe.co.uk> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: Iain Sandoe via Gcc-patches From: Iain Sandoe Reply-To: iain@sandoe.co.uk Cc: Iain Sandoe Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The way in which a C++20 coroutine is specified discards any value that might be returned from the initial or final await expressions. This PR ICE was caused by an initial await expression with an await_resume () returning a reference, the function rewrite code was not set up to expect this. Fixed by looking through any indirection present and by explicitly discarding the value, if any, returned by await_resume(). It does not seem useful to make a diagnostic for this, since the user could define a generic awaiter that usefully returns values when used in a different position from the initial (or final) await expressions. tested on x86_64 darwin, linux, OK for master and backports? thanks Iain Signed-off-by: Iain Sandoe PR c++/100127 gcc/cp/ChangeLog: * coroutines.cc (coro_rewrite_function_body): Handle initial await expressions that try to produce a reference value. gcc/testsuite/ChangeLog: * g++.dg/coroutines/pr100127.C: New test. --- gcc/cp/coroutines.cc | 9 ++- gcc/testsuite/g++.dg/coroutines/pr100127.C | 65 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/coroutines/pr100127.C diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc index 9017902e6fb..6db4b70f028 100644 --- a/gcc/cp/coroutines.cc +++ b/gcc/cp/coroutines.cc @@ -4211,9 +4211,16 @@ coro_rewrite_function_body (location_t fn_start, tree fnbody, tree orig, { /* Build a compound expression that sets the initial-await-resume-called variable true and then calls the - initial suspend expression await resume. */ + initial suspend expression await resume. + In the case that the user decides to make the initial await + await_resume() return a value, we need to discard it and, it is + a reference type, look past the indirection. */ + if (INDIRECT_REF_P (initial_await)) + initial_await = TREE_OPERAND (initial_await, 0); tree vec = TREE_OPERAND (initial_await, 3); tree aw_r = TREE_VEC_ELT (vec, 2); + if (!VOID_TYPE_P (TREE_TYPE (aw_r))) + aw_r = build1 (CONVERT_EXPR, void_type_node, aw_r); tree update = build2 (MODIFY_EXPR, boolean_type_node, i_a_r_c, boolean_true_node); aw_r = cp_build_compound_expr (update, aw_r, tf_warning_or_error); diff --git a/gcc/testsuite/g++.dg/coroutines/pr100127.C b/gcc/testsuite/g++.dg/coroutines/pr100127.C new file mode 100644 index 00000000000..374cd710077 --- /dev/null +++ b/gcc/testsuite/g++.dg/coroutines/pr100127.C @@ -0,0 +1,65 @@ +#ifdef __clang__ +#include +namespace std { + using namespace std::experimental; +} +#else +#include +#endif +#include + +struct future +{ + using value_type = int; + struct promise_type; + using handle_type = std::coroutine_handle; + + handle_type _coroutine; + + future(handle_type h) : _coroutine{h} {} + + ~future() noexcept{ + if (_coroutine) { + _coroutine.destroy(); + } + } + + value_type get() { + auto ptr = _coroutine.promise()._value; + return *ptr; + } + + struct promise_type { + std::optional _value = std::nullopt; + + future get_return_object() { + return future{handle_type::from_promise(*this)}; + } + void return_value(value_type val) { + _value = static_cast(val); + } + auto initial_suspend() noexcept { + class awaiter { + std::optional & value; + public: + explicit awaiter(std::optional & val) noexcept : value{val} {} + bool await_ready() noexcept { return value.has_value(); } + void await_suspend(handle_type) noexcept { } + value_type & await_resume() noexcept { return *value; } + }; + + return awaiter{_value}; + } + std::suspend_always final_suspend() noexcept { + return {}; + } + //void return_void() {} + void unhandled_exception() {} + }; +}; + +future create_future() +{ co_return 2021; } + +int main() +{ auto f = create_future(); }