From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678935726; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Dnr418NKRcrizfeyah6gPlJzwcCUrTKoqF9athHPYAw=; b=EyB+5Eknx4ymuD8+U70SEQIVOeAm23iiG7YMvOLGV7+nD6ejszohm1q2fhk61ZKDOuHYLg nZ43/VQudSGmG/jGAt1uD2HnAukisWM95uQy0PLXW4zdQEo2vINyjUKMFmsHJgYiM6vp5M WhpRU9C3hoeDPbI0rEPhDzK4ynSPUuM= 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-Google-Smtp-Source: AK7set/wVvExPqPG72N0QRuMCKYFxJtkosKo5EEd4jm6tgi0gZk7nL/Z0vsjWc/JbTLvtDtN+6ASLg== 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) From: Jason Merrill 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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; + } +} base-commit: ea4dd8f512979db247c54d6b41377bb73699bcd7 -- 2.31.1