<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta charset="utf-8">
<title>C++ Coroutine TS Issues</title>
<style type="text/css">
  p {text-align:justify}
  li {text-align:justify}
  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 {border-collapse: collapse;}
  table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
  } 
</style>
</head>
<body>
<h1>Coroutine TS ready issues (25 and 27)</h1>
<table>
<tr>
<td align="left">Doc. no.</td>
<td align="left">P1356R0</td>
</tr>
<tr>
<td align="left">Revises</td>
<td align="left">none</td>
</tr>
<tr>
<td align="left">Date:</td>
<td align="left">2018-11-08</td>
</tr>
<tr>
<td align="left">Project:</td>
<td align="left">Programming Language C++</td>
</tr>
<tr>
<td align="left">Reference:</td>
<td align="left">ISO/IEC TS 22277, C++ Extensions for Coroutines</td>
</tr>
<tr>
<td align="left">Audience:</td>
<td align="left">WG21</td>
</tr>
<tr>
<td align="left">Reply to:</td>
<td align="left">Gor Nishanov &lt;<a href="mailto:gorn@microsoft.com">gorn@microsoft.com</a>&gt;</td>
</tr>
</table>
<h2> Introduction </h2>
<p>All proposed resolutions wording is relative to <a href="https://wg21.link/N4775">N4775</a>.</p>
<!-- <p>Current issue list is <a href="wg21.link/P0664R6">P0664R6</a>.</p> -->
<h2> Table of content </h2>

<ul>
  <li>
      Ready Issues:
      <a href="#25">25</a>
      <a href="#27">27</a>  
  </li>
</ul>

<!-- ================================================================================ -->
<h2>Ready Issues</h2>
<hr>

<!-- --------------------------------------------------------------------------------------- -->
<h3><a name="25" href="#25">25.</a> Allow unhandled exception escape the user-defined body of 
  the coroutine and give it well defined semantics </h3>
  <p><b>Section:</b> 11.4.4 [dcl.fct.def.coroutine] <b>Status:</b>
    Ready
   <b>Submitter:</b> Eric Niebler <b>Opened:</b> 2018-03-10 <b>Last modified:</b> 2018-05-05</p>

   <p><i>[EWG Approved Rapperswil-2018/06/09]</i></p>
   <p><i>[CWG Approved San Diego-2018/11/08]</i></p>

   <p><b>Issue:</b></p>
  <p>
    Currently, an unhandled exception can never escape the user-authored 
    body of the coroutine with triggering undefined behavior. 
  
    <blockquote>
        <pre>
            {
              P p;
              co_await p.initial_suspend(); <i>// initial suspend point</i>
              try {
                F  // user authored body
              } catch(...) { p.unhandled_exception(); }
            <i>final_suspend</i>:
              co_await p.final_suspend(); <i></i>// final suspend point</i>
            }  
          </pre>
    </blockquote>
    
  <p>
  An exception from <code>F</code> is captured by the try-catch and
  a customization point <code>unhandled_exception</code> is called,
  where, typically, an exception_ptr is created and propagated to
  the consumer awaiting on async task, or, in case of a generator,
  will be delivered to the user
  when they dereference the iterator.
  </p>
  <p>
  Though the current behavior is perfectly reasonable for asynchronous
  scenarios, it is sub-optimal <!-- creates optimizing difficulties in the case of --> 
  for synchronous generators. Capturing an exception, storing it in an 
  <code>exception_ptr</code> and then rethrowing the exception during,
  say, iterator's <code>operator*</code> is a needless work if the desired
  behavior is to let the exception propagate to the caller
  whenever it asks for the next value.
   <!-- out if no exception
  was actually thrown by the user-authored body.-->
  </p>
  <p><b>Background information:</b>
  When a coroutine is first invoked, any exception thrown before entering
  the user-authored body (for example allocation failure, promise constructor failure,
  failure to copy parameters, etc) propagates into the caller as with any 
  normal function call. However, when the coroutine suspends and subsequently resumed, 
  if an exception is thrown by an evaluation of 
  <code><em>p</em>.unhandled_exception()</code> or 
  an evaluation of <code>co_await <em>p</em>.final_suspend()</code>
  the behavior is undefined. Note that a coroutine can be only resumed or destroyed
  when suspended at a particular suspend point. An exception leaving the coroutine
  at arbitrary point of the execution leaves the coroutine in the undefined state.
  </p>
  <p>
    The proposed resolution is to eliminate the undefined behavior in the following manner:
    <ol>
      <li>Allow an exception to escape <code><em>p</em>.unhandled_exception()</code>
        and, in that case, consider the coroutine to be at the final suspend point. Reminder: when
        a coroutine is at the final suspend point, the coroutine can only be
        destroyed and a call to member function <code>done()</code> of the 
        coroutine handle associated with that coroutine returns <code>true</code>.
      </li>
      <li>
        Eliminate possibility of an exception being thrown from 
        evaluation of an expression <code>co_await <em>p</em>.final_suspend()</code>
        by stating that <code>final_suspend</code> member function of
        the coroutine promise and <code>await_resume</code>, <code>await_ready</code>,
        and <code>await_suspend</code> members of the object returned from 
        <code>final_suspend</code> shall have non-throwing exception specification.
      </li>
    </ol>
  </p>
  <p>
    This resolution allows generator implementations to define <code>unhandled_exception</code>
    as follows:
    <pre>  void unhandled_exception() { throw; } </pre>
    With this implementation, if a user of the generator pulls the next value,
    and during computation of the next value an exception will occur in the 
    user authored body it will be propagate back to the user and the coroutine
    will be put into a final suspend state and ready to be destroyed when
    generator destructors is run.
  </p>
  <p><b>Proposed Wording:</b></p>
  In subclause 11.4.4, add two new paragraph after paragraph 11.

  <p>
  <ol start=12>
    <li><ins>If the evaluation of the expression <code><em>p</em>.unhandled_exception()</code>
      exits via an exception, the coroutine is considered suspended at the final suspend point.
     </ins></li>
     <li><ins>
        The expression <code>co_await <em>p</em>.final_suspend()</code> shall not be potentially-throwing ([except.spec]).
        <!--
      The member functions <code>final_suspend</code>, 
      <code>await_resume</code>, <code>await_ready</code>,
      <code>await_suspend</code> and <code>operator co_await</code> (if any)
      used in evaluation of <code></code>
      expression <code>co_await <em>p</em>.final_suspend()</code> shall 
      have non-throwing exception specification.-->
</ins></li>
  </ol>
</p>

<hr>
<!-- ================================================================================ -->

<h3><a name="27" href="#27">27.</a> Make suspension in dynamic initializers of <code>static</code> and <code>thread_local</code> local variables ill-formed</h3>
    <p><b>Section:</b> 8.3.8/2 [expr.await] <b>Status:</b>
      Ready
     <b>Submitter:</b> Richard Smith <b>Opened:</b> 2018-03-25 <b>Last modified:</b> 2018-03-25</p>

     <p><i>[EWG Approved Rapperswil-2018/06/09]</i></p>
     <p><i>[CWG Approved San Diego-2018/11/08]</i></p>

     <p><b>Proposed wording:</b></p>

Add underlined text to 8.3.8/2:

<blockquote>
An <i>await-expression</i> shall appear only in a potentially-evaluated
expression within the <i>compound-statement</i> of a <i>function-body</i> 
outside of a <i>handler</i> (Clause 18). 
In a declaration-statement or in
the simple-declaration (if any) of a for-init-statement, an <del>await-expression</del><ins><i>await-expression</i></ins> shall appear only in
an initializer of that declaration-statement or 
simple-declaration<ins>. An <i>await-expression</i> shall not appear in the 
initializer of a block-scope variable with static or thread storage duration.</ins>
<!-- <ins> unless it is used to initialize a block-scope variable with static or thread storage duration</ins>.-->
</blockquote>

</body>
</html>
