diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -10527,6 +10527,9 @@ def note_await_ready_no_bool_conversion : Note< "return type of 'await_ready' is required to be contextually convertible to 'bool'" >; +def warn_coroutine_handle_address_invalid_return_type : Warning < + "return type of 'coroutine_handle<>::address should be 'void*' (have %0) in order to get capability with existing async C API.">, + InGroup; def err_coroutine_promise_final_suspend_requires_nothrow : Error< "the expression 'co_await __promise.final_suspend()' is required to be non-throwing" >; diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -1751,6 +1751,7 @@ ~InlinedRegionBodyRAII() { CGF.AllocaInsertPt = OldAllocaIP; } }; }; + private: /// CXXThisDecl - When generating code for a C++ member function, /// this will hold the implicit 'this' declaration. diff --git a/clang/lib/Sema/SemaCoroutine.cpp b/clang/lib/Sema/SemaCoroutine.cpp --- a/clang/lib/Sema/SemaCoroutine.cpp +++ b/clang/lib/Sema/SemaCoroutine.cpp @@ -391,7 +391,13 @@ return nullptr; Expr *JustAddress = AddressExpr.get(); - // FIXME: Check that the type of AddressExpr is void* + + // Check that the type of AddressExpr is void* + if (!JustAddress->getType().getTypePtr()->isVoidPointerType()) + S.Diag(cast(JustAddress)->getCalleeDecl()->getLocation(), + diag::warn_coroutine_handle_address_invalid_return_type) + << JustAddress->getType(); + return buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_resume, JustAddress); } diff --git a/clang/test/SemaCXX/coroutine_handle-addres-return-type.cpp b/clang/test/SemaCXX/coroutine_handle-addres-return-type.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/coroutine_handle-addres-return-type.cpp @@ -0,0 +1,75 @@ +// RUN: %clang_cc1 -verify %s -stdlib=libc++ -std=c++1z -fcoroutines-ts -fsyntax-only + +namespace std::experimental { +template +struct coroutine_handle; + +template <> +struct coroutine_handle { + coroutine_handle() = default; + static coroutine_handle from_address(void *) noexcept; + void *address() const; +}; + +template +struct coroutine_handle : public coroutine_handle<> { +}; + +template +struct void_t_imp { + using type = void; +}; +template +using void_t = typename void_t_imp::type; + +template +struct traits_sfinae_base {}; + +template +struct traits_sfinae_base> { + using promise_type = typename T::promise_type; +}; + +template +struct coroutine_traits : public traits_sfinae_base {}; +} // namespace std::experimental + +struct suspend_never { + bool await_ready() noexcept; + void await_suspend(std::experimental::coroutine_handle<>) noexcept; + void await_resume() noexcept; +}; + +struct task { + struct promise_type { + auto initial_suspend() { return suspend_never{}; } + auto final_suspend() noexcept { return suspend_never{}; } + auto get_return_object() { return task{}; } + static void unhandled_exception() {} + void return_void() {} + }; +}; + +namespace std::experimental { +template <> +struct coroutine_handle : public coroutine_handle<> { + coroutine_handle *address() const; // expected-warning {{return type of 'coroutine_handle<>::address should be 'void*'}} +}; +} // namespace std::experimental + +struct awaitable { + bool await_ready(); + + std::experimental::coroutine_handle + await_suspend(std::experimental::coroutine_handle<> handle); + void await_resume(); +} a; + +task f() { + co_await a; +} + +int main() { + f(); + return 0; +}