<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/102606>102606</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] [coroutines] impossible happens, incorect behavior (bug)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kelbon
      </td>
    </tr>
</table>

<pre>
    Appears, that compiler passes .done() handle into await_suspend, which should never happen.
Returning handle of coroutine from its final_suspend should be correct and may be used to "continue" coroutine and free memory

https://godbolt.org/z/oeh5Tooxb

```cpp

#include <coroutine>
#include <iostream>

struct transfer_control_to {
 std::coroutine_handle<> waiter;

  bool await_ready() const noexcept { return false; }
  std::coroutine_handle<> await_suspend(std::coroutine_handle<> h) noexcept {
    if (h.done()) {
      std::cout << "IMPOSSIBLE HAPPEN" << std::endl;
 h.destroy();
      return std::noop_coroutine();
    }
    return waiter;
  }
  static void await_resume() noexcept {}
};

struct task_promise {
  std::coroutine_handle<void> waiter;

  static constexpr std::suspend_always initial_suspend() noexcept { return {}; }
  static void return_void() {}

  auto get_return_object() {
    return std::coroutine_handle<task_promise>::from_promise(*this);
  }
  void unhandled_exception() noexcept {}
  auto final_suspend() noexcept { return transfer_control_to{waiter}; }
};

struct task {
  using promise_type = task_promise;
  using handle_type = std::coroutine_handle<promise_type>;

  handle_type handle_;

 constexpr task(handle_type handle) noexcept : handle_(handle) {}

 void start_and_detach() {
    handle_.promise().waiter = handle_;
 handle_.resume();
    handle_ = nullptr;
  }
};

task foo() { co_return; }
void bar() {
  auto t = foo();
 t.start_and_detach();
}

int main() { bar(); }
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVt2OozgTfRrnpjQRMfnjgouku6NvpG93Rzt7jwwugneMjWzT09mnX9n8p7OZVosgqDpVdc6xMbNWXBViSnZnsntdsdZ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