<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3460: Unimplementable noop_coroutine_handle guarantees</title>
<meta property="og:title" content="Issue 3460: Unimplementable noop_coroutine_handle guarantees">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3460.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++23">C++23</a> status.</em></p>
<h3 id="3460"><a href="lwg-defects.html#3460">3460</a>. Unimplementable <code>noop_coroutine_handle</code> guarantees</h3>
<p><b>Section:</b> 17.13.5.2.4 <a href="https://wg21.link/coroutine.handle.noop.resumption">[coroutine.handle.noop.resumption]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2020-07-01 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
17.13.5.2.4 <a href="https://wg21.link/coroutine.handle.noop.resumption">[coroutine.handle.noop.resumption]</a>/2 states "<i>Remarks:</i> If <code>noop_coroutine_handle</code> 
is converted to <code>coroutine_handle&lt;&gt;</code>, calls to <code>operator()</code>, <code>resume</code> and 
<code>destroy</code> on that handle will also have no observable effects." This suggests that e.g. in this function:
</p>
<blockquote><pre>
void f(coroutine_handle&lt;&gt; meow) 
{
  auto woof = noop_coroutine();
  static_cast&lt;coroutine_handle&lt;&gt;&amp;&gt;(woof) = meow;
  static_cast&lt;coroutine_handle&lt;&gt;&amp;&gt;(woof).resume();
}
</pre></blockquote>
<p>
the final call to <code>coroutine_handle&lt;&gt;::resume</code> must have no effect regardless of what 
coroutine (if any) <code>meow</code> refers to, contradicting the specification of 
<code>coroutine_handle&lt;&gt;::resume</code>. Even absent this contradiction, implementing the specification 
requires <code>coroutine_handle&lt;&gt;::resume</code> to determine if <code>*this</code> is a base subobject of a 
<code>noop_coroutine_handle</code>, which seems pointlessly expensive to implement.
<p/>
17.13.5.2.6 <a href="https://wg21.link/coroutine.handle.noop.address">[coroutine.handle.noop.address]</a>/2 states "<i>Remarks:</i> A <code>noop_coroutine_handle</code>'s 
<code>ptr</code> is always a non-null pointer value." Similar to the above case, a slicing assignment of a 
default-initialized <code>coroutine_handle&lt;&gt;</code> to a <code>noop_coroutine_handle</code> must result in 
<code>ptr</code> having a null pointer value &mdash; another contradiction between the requirements of 
<code>noop_coroutine_handle</code> and <code>coroutine_handle&lt;&gt;</code>.
</p>

<p><i>[2020-07-12; Reflector prioritization]</i></p>

<p>
Set priority to 2 after reflector discussions.
</p>

<p><i>[2020-07-29 Tim adds PR and comments]</i></p>

<p>
The root cause for this issue as well as issue <a href="lwg-defects.html#3469" title="Precondition of coroutine_handle::promise may be insufficient (Status: Resolved)">3469</a><sup><a href="https://cplusplus.github.io/LWG/issue3469" title="Latest snapshot">(i)</a></sup> is the unnecessary 
public derivation from <code>coroutine_handle&lt;void&gt;</code>. The proposed resolution below
replaces the derivation with a conversion function and adds explicit declarations for
members that were previously inherited. It also modifies the preconditions
on <code>from_address</code> with goal of making it impossible to obtain a <code>coroutine_handle&lt;P&gt;</code>
to a coroutine whose promise type is not <code>P</code> in well-defined code.
</p>

<p><i>[2020-08-21; Issue processing telecon: moved to Tentatively Ready]</i></p>

<p><i>[2020-11-09 Approved In November virtual meeting. Status changed: Tentatively Ready &rarr; WP.]</i></p>



<p id="res-3460"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4861">N4861</a> and also resolves
LWG issue <a href="lwg-defects.html#3469" title="Precondition of coroutine_handle::promise may be insufficient (Status: Resolved)">3469</a><sup><a href="https://cplusplus.github.io/LWG/issue3469" title="Latest snapshot">(i)</a></sup>. 
</p>

<ol>
<li><p> Edit 17.13.4 <a href="https://wg21.link/coroutine.handle">[coroutine.handle]</a> as indicated:</p>
<blockquote>
<pre>
namespace std {

  [&hellip;]

  template&lt;class Promise&gt;
  struct coroutine_handle <del>: coroutine_handle&lt;&gt;</del>
  {
    // [coroutine.handle.con], construct/reset
<del>    using coroutine_handle&lt;&gt;::coroutine_handle;</del>
<ins>    constexpr coroutine_handle() noexcept;
    constexpr coroutine_handle(nullptr_t) noexcept;</ins>
    static coroutine_handle from_promise(Promise&amp;);
    coroutine_handle&amp; operator=(nullptr_t) noexcept;
  
    // [coroutine.handle.export.import], export/import
<ins>    constexpr void* address() const noexcept;</ins>
    static constexpr coroutine_handle from_address(void* addr);
  
<ins>    // [coroutine.handle.conv], conversion
    constexpr operator coroutine_handle&lt;&gt;() const noexcept;
  
    // [coroutine.handle.observers], observers
    constexpr explicit operator bool() const noexcept;
    bool done() const;
  
    // [coroutine.handle.resumption], resumption
    void operator()() const;
    void resume() const;
    void destroy() const;</ins>
  
    // [coroutine.handle.promise], promise access
    Promise&amp; promise() const;
  
<ins>  private:
    void* ptr;  // <i>exposition only</i> </ins>
  };
}
</pre>

<p> -1- An object of type <code>coroutine_­handle&lt;T&gt;</code> is called a <i>coroutine handle</i>
and can be used to refer to a suspended or executing coroutine.
A <del>default-constructed</del> <code>coroutine_­handle</code> object <ins>whose member <code>address()</code>
returns a null pointer value</ins> does not refer to any coroutine.
<ins>Two <code>coroutine_handle</code> objects refer to the same coroutine if and only if their
member <code>address()</code> returns the same value.</ins>
</p>
</blockquote>
</li>
<li>
<p>Add the following subclause under 17.13.4 <a href="https://wg21.link/coroutine.handle">[coroutine.handle]</a>, immediately after 17.13.4.2 <a href="https://wg21.link/coroutine.handle.con">[coroutine.handle.con]</a>:</p>
<blockquote>
<p>
<ins><b>?.?.?.? Conversion [coroutine.handle.conv]</b></ins>
</p>
<pre>
    <ins>constexpr operator coroutine_handle&lt;&gt;() const noexcept;</ins>
</pre>
<blockquote>
<p><ins>-1- <i>Effects:</i> Equivalent to: <code>return coroutine_handle&lt;&gt;::from_address(address());</code>.</ins></p>
</blockquote>
</blockquote>
</li>
<li>
<p> Edit 17.13.4.4 <a href="https://wg21.link/coroutine.handle.export.import">[coroutine.handle.export.import]</a> as indicated, splitting the two versions:</p>
<blockquote>
<pre>
static constexpr coroutine_handle&lt;&gt; coroutine_handle&lt;&gt;::from_address(void* addr);
</pre>
<blockquote>
<p><ins>-?- <i>Preconditions:</i> <code>addr</code> was obtained via a prior call to <code>address</code> on
an object whose type is a specialization of <code>coroutine_handle</code>.</ins>
<p/>
<ins>-?- <i>Postconditions:</i> <code>from_­address(address()) == *this</code>.</ins>
</p>
</blockquote>
<pre>
static constexpr coroutine_handle&lt;Promise&gt; coroutine_handle&lt;Promise&gt;::from_address(void* addr);
</pre>
<blockquote>
<p>
-2- <i>Preconditions:</i> <code>addr</code> was obtained via a prior call to <code>address</code>
<ins>on an object of type <i>cv</i> <code>coroutine_handle&lt;Promise&gt;</code></ins>.
<p/>
-3- <i>Postconditions:</i> <code>from_­address(address()) == *this</code>.
</p>
</blockquote>
</blockquote>
</li>
<li>
<p>Edit 17.13.5.2 <a href="https://wg21.link/coroutine.handle.noop">[coroutine.handle.noop]</a> as indicated:</p>
<blockquote>
<pre>
namespace std {
  template&lt;&gt;
  struct coroutine_handle&lt;noop_coroutine_promise&gt; <del>: coroutine_handle&lt;&gt;</del>
  {
<ins>    // [coroutine.handle.noop.conv], conversion
    constexpr operator coroutine_handle&lt;&gt;() const noexcept;</ins>

    // [coroutine.handle.noop.observers], observers
    constexpr explicit operator bool() const noexcept;
    constexpr bool done() const noexcept;

    // [coroutine.handle.noop.resumption], resumption
    constexpr void operator()() const noexcept;
    constexpr void resume() const noexcept;
    constexpr void destroy() const noexcept;

    // [coroutine.handle.noop.promise], promise access
    noop_coroutine_promise&amp; promise() const noexcept;

    // [coroutine.handle.noop.address], address
    constexpr void* address() const noexcept;
  
  private:
    coroutine_handle(unspecified);
<ins>    void* ptr; // <i>exposition only</i> </ins>
  };
}
</pre>
</blockquote>
</li>
<li>
<p>Add the following subclause under 17.13.5.2 <a href="https://wg21.link/coroutine.handle.noop">[coroutine.handle.noop]</a>, immediately before 17.13.5.2.3 <a href="https://wg21.link/coroutine.handle.noop.observers">[coroutine.handle.noop.observers]</a>:</p>
<blockquote>
<p>
<ins><b>?.?.?.?.? Conversion [coroutine.handle.noop.conv]</b></ins>
</p>
<pre>
    <ins>constexpr operator coroutine_handle&lt;&gt;() const noexcept;</ins>
</pre>
<blockquote>
<p><ins>-1- <i>Effects:</i> Equivalent to: <code>return coroutine_handle&lt;&gt;::from_address(address());</code>.</ins></p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
