From patchwork Thu Mar 16 03:01:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 70547 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp254582wrt; Wed, 15 Mar 2023 20:02:52 -0700 (PDT) X-Google-Smtp-Source: AK7set8QNQ6eWmMZ98EmuOT5paYmp7y3BpGr3rXfG4yfMnLrfdJld/D35jLbDEKNa373RW4nHeMc X-Received: by 2002:a17:907:1690:b0:878:7c18:8fd9 with SMTP id hc16-20020a170907169000b008787c188fd9mr11507259ejc.44.1678935772133; Wed, 15 Mar 2023 20:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678935772; cv=none; d=google.com; s=arc-20160816; b=CYj6cNsxspCWRaUoxmEnUQnrL6Q9CstUUZkFwl4njTpzBvFNXyw6mulSN0uIGHt+bX 77hKAZ24N4LNABy56Sir4p+ipf5TvTMM/1XTVIlx/bZQcWbZvjdi77SLBmuxlN9Es7I6 xg5pKOOA21cZcb8KjO1AKK+s4MBm7e4LeTBxPB/OepS0WcUFzahgI4f7i+RcwDoK+lki stSn6Qlk89fc5YcNUYxGNcmamocHRbFF41RvQfnU6hSiWwFhbNIcRm+5m3otQ6o6BFC6 8VoZHuiTRULzBI9zigYPNgKyTeBe2x5MLIMnWxQbp7AL6/wjpKZLy57QgtpDWe+/8qg9 MoBw== 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=Dnr418NKRcrizfeyah6gPlJzwcCUrTKoqF9athHPYAw=; b=EItGb6P7gMLF+EhQc+BFpPr1ndI91USJ6sbJCMFJ/QhSQPrIOVMxx8jBBKYR9vcIb+ 9IolioixVKeFBZa4JogZ0wBjF8udur50xO2jtANuNCJhA84iY2g0FSN3tzsdOiUiZJDc 9xAEmy51HgbH9+nK7pHdE6NzhL+brFDpjheuq+uq1yHXjn6VGqeFSKoflmkrWicpF9f9 PBVQkmbv1gxnXfNu4FQ4lCx9za7I4pGP3XLO+KwYK0uQs6F7OA6WIaJN9Xmmim4Ufbxi SpnNEdEMvxIgCrjhCHYvfz3KgbnHIZdOhGOWPSaBjfm90XeqlISDCRaQ26McpE9d9lbr k/UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=V5kI9DuD; 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 e21-20020a170906375500b0092bdd0da6fesi7478544ejc.646.2023.03.15.20.02.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 20:02:52 -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=V5kI9DuD; 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 E6F96385843D for ; Thu, 16 Mar 2023 03:02:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E6F96385843D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678935770; bh=Dnr418NKRcrizfeyah6gPlJzwcCUrTKoqF9athHPYAw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=V5kI9DuDk37/ooePGpxwrnJedVSx159r/0znJSdNMxQo1Yd66G1phYBBPKMfeghdD wSwIvP3DJaIKK6O0Gh4Wlx+ovR4ToCbpSNd1SnFXEltCDZENkKlIDMaCswEUPXmAQt CUV6PPI2arewaFkFHBnTZmVKi8U18Hnj3MF796Hc= 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 9A86C3858D39 for ; Thu, 16 Mar 2023 03:02:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A86C3858D39 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-654-89dOMDh7PXGmcqH4Ezm3oQ-1; Wed, 15 Mar 2023 23:02:02 -0400 X-MC-Unique: 89dOMDh7PXGmcqH4Ezm3oQ-1 Received: by mail-qk1-f197.google.com with SMTP id l27-20020a05620a211b00b00745b3e62004so219167qkl.4 for ; Wed, 15 Mar 2023 20:02:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678935722; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Dnr418NKRcrizfeyah6gPlJzwcCUrTKoqF9athHPYAw=; b=OF4+muyiW00S8nt1eiMbDSQ3tyMO+hFMIMzlsG/He0B7ViahiaVUF5QicR2vFyJgZQ i9aIASh+Vvqq7WVr5Ra3rF158DLODHKedt3Czzya5HOKXqInHrW3dEsZfmlJeWigOMKV YywgMIo+7Rjx9XQWNqlJ5SzLWLxDNoZ6zdjPu0KimO0ezPbyY9y2MJv179oVHtphhGb2 EJe6rT4Q3sQUZ1ShVpNbTlZqZVIEd8F0g7f+vjt6H1gvzSEDQAj5PuFBkDp/2cL+cRz7 sczPdp8S51y+KqLKtZicXbmreykdLbs30p/BUE4P1ss/GUHfXq2a/YgnZAAs1zHvbH/H hJlw== X-Gm-Message-State: AO0yUKUH5TqTt1rrNW3Q1ebtgLswimDPpilE22P87z60zcKKznbQnopE YdKx4DQVzi2huZujh2jQFihIRIOogs3mM3BZpDBE9G5CRwjXwGpDr573Co6MdC7k6okIU1v86zq Phm9yUc9kELg+0ssO9FJpQlrtFh2IcdzSvT564u69YIPVXqOnieR7l8OtXMuatAcWTYfZkawy7Q == X-Received: by 2002:a05:6214:3018:b0:56e:b557:2d4e with SMTP id ke24-20020a056214301800b0056eb5572d4emr23945074qvb.6.1678935722045; Wed, 15 Mar 2023 20:02:02 -0700 (PDT) X-Received: by 2002:a05:6214:3018:b0:56e:b557:2d4e with SMTP id ke24-20020a056214301800b0056eb5572d4emr23945057qvb.6.1678935721634; Wed, 15 Mar 2023 20:02:01 -0700 (PDT) Received: from jason.cygnus.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 75-20020a37044e000000b00741b312c899sm5041893qke.6.2023.03.15.20.02.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 20:02:00 -0700 (PDT) To: gcc-patches@gcc.gnu.org, Iain Sandoe Subject: [PATCH RFC] c++: co_await and move-only type [PR105406] Date: Wed, 15 Mar 2023 23:01:57 -0400 Message-Id: <20230316030157.882778-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=-12.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_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 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?1760491755887837102?= X-GMAIL-MSGID: =?utf-8?q?1760491755887837102?= Tested x86_64-pc-linux-gnu. As with the array issue, I know you have WIP to deal with larger issues, but this seems like a reasonable local fix. Does it make sense to you? -- 8< -- Here we were building a temporary MoveOnlyAwaitable to hold the result of evaluating 'o', but since 'o' is an lvalue we should build a reference instead. PR c++/105406 gcc/cp/ChangeLog: * coroutines.cc (build_co_await): Handle lvalue 'o'. gcc/testsuite/ChangeLog: * g++.dg/coroutines/co-await-moveonly1.C: New test. --- gcc/cp/coroutines.cc | 9 ++- .../g++.dg/coroutines/co-await-moveonly1.C | 63 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/coroutines/co-await-moveonly1.C base-commit: ea4dd8f512979db247c54d6b41377bb73699bcd7 diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc index 7f8cbc3d95e..a2189e43db8 100644 --- a/gcc/cp/coroutines.cc +++ b/gcc/cp/coroutines.cc @@ -1024,9 +1024,13 @@ build_co_await (location_t loc, tree a, suspend_point_kind suspend_kind) } else { - e_proxy = get_awaitable_var (suspend_kind, o_type); + tree p_type = o_type; + if (glvalue_p (o)) + p_type = cp_build_reference_type (p_type, !lvalue_p (o)); + e_proxy = get_awaitable_var (suspend_kind, p_type); o = cp_build_modify_expr (loc, e_proxy, INIT_EXPR, o, tf_warning_or_error); + e_proxy = convert_from_reference (e_proxy); } /* I suppose we could check that this is contextually convertible to bool. */ @@ -1120,6 +1124,9 @@ build_co_await (location_t loc, tree a, suspend_point_kind suspend_kind) } TREE_VEC_ELT (awaiter_calls, 2) = awrs_call; /* await_resume(). */ + if (REFERENCE_REF_P (e_proxy)) + e_proxy = TREE_OPERAND (e_proxy, 0); + tree await_expr = build5_loc (loc, CO_AWAIT_EXPR, TREE_TYPE (TREE_TYPE (awrs_func)), a, e_proxy, o, awaiter_calls, diff --git a/gcc/testsuite/g++.dg/coroutines/co-await-moveonly1.C b/gcc/testsuite/g++.dg/coroutines/co-await-moveonly1.C new file mode 100644 index 00000000000..e2831c104bf --- /dev/null +++ b/gcc/testsuite/g++.dg/coroutines/co-await-moveonly1.C @@ -0,0 +1,63 @@ +// PR c++/105406 +// { dg-do compile { target c++20 } } + +#include +#include + +// A move-only awaitable +class MoveOnlyAwaitable { +public: + MoveOnlyAwaitable() = default; + MoveOnlyAwaitable(MoveOnlyAwaitable &&) = default; + MoveOnlyAwaitable &operator=(MoveOnlyAwaitable &&) = default; + + MoveOnlyAwaitable(const MoveOnlyAwaitable &) = delete; + MoveOnlyAwaitable &operator=(const MoveOnlyAwaitable &) = delete; + + bool await_ready() const noexcept { return false; } + void await_suspend(std::coroutine_handle<>) noexcept {} + void await_resume() {} +}; + +struct task { + struct promise_type { + auto initial_suspend() const { return std::suspend_never{}; } + auto final_suspend() const noexcept { return std::suspend_never(); } + auto get_return_object() { return task{}; } + void return_void() {} + void unhandled_exception() {} + + template + T &&await_transform(T &&t) { + return static_cast(t); + } + + + }; + + bool await_ready() const { return false; } + void await_suspend(std::coroutine_handle<> awaiter) {} + void await_resume() {} +}; + +task myCoroutine() { + // GCC: OK + // clang: OK + { + co_await MoveOnlyAwaitable(); + } + // GCC: OK + // clang: OK + { + auto moveonly = MoveOnlyAwaitable(); + co_await std::move(moveonly); + } + + // GCC <= 11.2: OK + // GCC 11.3:ERROR: error: use of deleted function 'MoveOnlyAwaitable::MoveOnlyAwaitable(const MoveOnlyAwaitable&) + // clang: OK + { + auto moveonly = MoveOnlyAwaitable(); + co_await moveonly; + } +}