KEMBAR78
Coroutine ignores overloaded `operator new` · Issue #54881 · llvm/llvm-project · GitHub
Skip to content

Coroutine ignores overloaded operator new #54881

@vogelsgesang

Description

@vogelsgesang

Expected behavior

clang should reject the code

#include<coroutine>

struct resumable {
   struct promise_type;
   using coro_handle = std::coroutine_handle<promise_type>;

   resumable(coro_handle handle) : handle_(handle) {}
   resumable(resumable&) = delete;
   ~resumable() { if (handle_) { handle_.destroy(); } }

   coro_handle handle_;
};

struct Allocator;

struct resumable::promise_type {
   void* operator new(std::size_t sz, Allocator&);

   std::coroutine_handle<promise_type> get_return_object() { return std::coroutine_handle<promise_type>::from_promise(*this); }
   auto initial_suspend() { return std::suspend_always(); }
   auto final_suspend() noexcept { return std::suspend_always(); }
   void unhandled_exception() {}
   void return_void() {};
};

resumable foo() {
    co_return;
}

because the operator new cannot be called for foo().

Both MSVC and gcc reject it. gcc provides the error message:

<source>:26:11: error: 'operator new' is provided by 'std::__n4861::__coroutine_traits_impl<resumable, void>::promise_type' {aka 'resumable::promise_type'} but is not usable with the function signature 'resumable foo()'
   26 | resumable foo() {
      |           ^~~

Full example: https://godbolt.org/z/a89vjc77Y

Observed behavior

clang accepts the code, and calls the global ::operator new instead of the user-provided overload

Metadata

Metadata

Assignees

Labels

c++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions