<!doctype html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<head>

<title>
Contract violation handling semantics for the contracts MVP
</title>

<style type="text/css">

pre {
    display: inline;
}

table#header th,
table#header td
{
    text-align: left;
}

table#references th,
table#references td
{
    vertical-align: top;
}

#hideins:checked ~ * ins, #hideins:checked ~ * ins * { display:none; visibility:hidden }
#hidedel:checked ~ * del, #hidedel:checked ~ * del * { display:none; visibility:hidden }

ins, ins *
{
    text-decoration: underline;
    color: #000000;
    background-color:#C8FFC8
}
del, del *
{
    text-decoration: line-through;
    color: #000000;
    background-color:#FFA0A0
}
nop, nop *
{
    color: #000000;
    background-color:#B0B0FF
}

blockquote
{
    color: #000000;
    background-color: #F1F1F1;
    border: 1px solid #D1D1D1;
    padding-left: 0.5em;
    padding-right: 0.5em;
}
blockquote.stdins
{
    /* text-decoration: underline; */
    color: #000000;
    background-color: #C8FFC8;
    border: 1px solid #B3EBB3;
    padding: 0.5em;
}
blockquote.stddel
{
    text-decoration: line-through;
    color: #000000;
    background-color: #FFA0A0;
    border: 1px solid #ECD7EC;
    padding-left: 0.5empadding-right: 0.5em;
}
blockquote.stdnop
{
    color: #000000;
    background-color: #B0B0FF;
    border: 1px solid #ECD7EC;
    padding-left: 0.5empadding-right: 0.5em;
}
</style>

</head>


<body style="max-width: 8.5in">

<table id="header">
  <tr>
    <th>Document Number:</th>
    <td>P2852R0</td>
  </tr>
  <tr>
    <th>Date:</th>
    <td>2023-04-24</td>
  </tr>
  <tr>
    <th>Audience:</th>
    <td>SG21</td>
  </tr>
  <tr>
    <th>Reply-to:</th>
    <td>Tom Honermann &lt;tom@honermann.net&gt;</td>
  </tr>
</table>


<h1>Contract violation handling semantics for the contracts MVP</h1>


<ul>
  <li><a href="#introduction">Introduction</a></li>
  <li><a href="#proposal">Proposal</a></li>
  <li><a href="#design">Design considerations</a>
    <ul>
      <li><a href="#design-pred-uneval">Predicate is unevaluated</a></li>
      <li><a href="#design-pred-true">Predicate evaluation yields a true result</a></li>
      <li><a href="#design-pred-false">Predicate evaluation yields a false result</a></li>
      <li><a href="#design-pred-ub">Predicate evaluation encounters undefined-behavior</a></li>
      <li><a href="#design-exceptions">Predicate evaluation or violation handler throws</a></li>
      <li><a href="#design-longjmp">Predicate evaluation or violation handler calls <tt>std::longjmp</tt></a></li>

    </ul>
  </li>
  <li><a href="#implementation-exp">Implementation experience</a></li>
  <li><a href="#ack">Acknowledgements</a></li>
  <li><a href="#references">References</a></li>
  <li><a href="#wording">Wording</a></li>
</ul>


<h1 id="introduction">Introduction</h1>

<p>
SG21 endeavors to specify a minimal contracts design that is both simple enough
to be confidently adopted for C++26 and sufficiently extensible to satisfy both
known and unknown future needs.
</p>

<p>
Recent SG21 discussions have been focussed on the semantics to ascribe to
behaviors that may occur during evaluation of contract predicates.
For details of these discussions, see the minutes and papers linked below for
recent SG21 telecons.
<ul>
  <li><a href="https://wiki.edg.com/bin/view/Wg21telecons2023/Teleconference2023-03-09">2023-03-09 SG21 telecon</a>
    <ul>
      <li><a title="Unconditional termination is a serious problem"
             href="https://wg21.link/p2698r0">P2698R0: Unconditional termination is a serious problem</a>
          <sup><a title="Unconditional termination is a serious problem"
                  href="#ref_p2698r0">[P2698R0]</a></sup>
      </li>
      <li><a title="Not halting the program after detected contract violation"
             href="https://wg21.link/p2784r0">P2784R0: Not halting the program after detected contract violation</a>
          <sup><a title="Not halting the program after detected contract violation"
                  href="#ref_p2784r0">[P2784R0]</a></sup>
      </li>
    </ul>
  </li>
  <li><a href="https://wiki.edg.com/bin/view/Wg21telecons2023/Teleconference2023-03-23">2023-03-23 SG21 telecon</a>
    <ul>
      <li><a title="The idea behind the contracts MVP"
             href="https://wg21.link/p2817r0">P2817R0: The idea behind the contracts MVP</a>
          <sup><a title="The idea behind the contracts MVP"
                  href="#ref_p2817r0">[P2817R0]</a></sup>
      </li>
    </ul>
  </li>
  <li><a href="https://wiki.edg.com/bin/view/Wg21telecons2023/Teleconference2023-04-20">2023-04-20 SG21 telecon</a>
    <ul>
      <li><a title="Unconditional contract violation handling of any kind is a serious problem"
             href="https://wg21.link/p2838r0">P2838R0: Unconditional contract violation handling of any kind is a serious problem</a>
          <sup><a title="Unconditional contract violation handling of any kind is a serious problem"
                  href="#ref_p2838r0">[P2838R0]</a></sup>
      </li>
      <li><a title="Contract-Violation Handlers"
             href="https://wg21.link/p2811r1">P2811R1: Contract-Violation Handlers</a>
          <sup><a title="Contract-Violation Handlers"
                  href="#ref_p2811r1">[P2811R1]</a></sup>
      </li>
    </ul>
  </li>
</ul>
</p>

<p>
SG21 has not yet established consensus regarding:
<ol>
  <li>which behaviors that occur during predicate evaluation are
      well-defined.</li>
  <li>which behaviors that occur during execution of a violation handler are
      well-defined.</li>
</ol>
For the behaviors enumerated in 
<a title="Contract-Violation Handlers"
   href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
                                                       href="#ref_p2811r1">[P2811R1]</a></sup>,
this paper proposes which ones to specify with well-defined semantics for the
MVP, and which ones to be relegated to unspecified or undefined behavior pending
further consideration.
</p>

<p>
It is not the intent of this paper to propose these semantics as the final set
for C++26. The intent is rather to specify well-defined semantics for those
cases for which strong consensus is established and to leave the rest to be
pursued in future papers. In the meantime, the use of unspecified behavior
leaves implementations free to experiment with semantics in the cases that are
not well-defined.
</p>

<p>
This paper does not discuss how violation handlers are defined or registered, or
the mechanics of their invocations; those concerns are covered by
<a title="Contract-Violation Handlers"
   href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
                                                       href="#ref_p2811r1">[P2811R1]</a></sup>.
Likewise, this paper does not discuss build modes, the details of contract
checking enablement, optimization based on contract predicates or consequences
thereof, or one-definition rule (ODR) concerns regarding mixed contract
checking enablement across the translation units that comprise a program;
those concerns are also left to other papers.
</p>


<h1 id="proposal">Proposal</h1>

<p>
1. Adopt the contract violation handler specified in
<a title="Contract-Violation Handlers"
   href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
                                                       href="#ref_p2811r1">[P2811R1]</a></sup>
with or without minor adjustments.
</p>

<p>
2. The implementation provided default contract violation handler is specified
to unconditionally call <tt>std::abort()</tt> after performing any
implementation-defined actions such as generating diagnostics.
</p>

<p>
3. A user-defined contract violation handler may call the implementation
provided default contract violation handler.
This ability enables a user-provided replacement handler to perform some
actions and then defer to the implementation provided handler for
implementation-specific generation of diagnostics.
The mechanism by which this can be done is TBD since
<a title="Contract-Violation Handlers"
   href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
                                                       href="#ref_p2811r1">[P2811R1]</a></sup>
does not expose the implementation provided handler when a user-provided handler
is present.
</p>

<p>
4. The semantics of predicate evaluation and contract violation handler
invocation are specified as follows.
<table border="1">
  <tr>
    <th>Behavior</th>
    <th>Specified semantics</th>
  </tr>
  <tr>
    <td>Predicate evaluation is not performed.</td>
    <td><b>well-defined</b>; execution continues.</td>
  </tr>
  <tr>
    <td>Predicate evaluation yields a true result.</td>
    <td><b>well-defined</b>; execution continues.</td>
  </tr>
  <tr>
    <td>Predicate evaluation yields a false result.</td>
    <td><b>well-defined</b>; the contract violation handler is invoked
        and, if it returns, execution continues.</td>
  </tr>
  <tr>
    <td>Predicate evaluation encounters undefined-behavior</td>
    <td><b>undefined behavior</b>.</td>
  </tr>
  <tr>
    <td>Predicate evaluation throws.</td>
    <td><b>unspecified behavior</b>.</td>
  </tr>
  <tr>
    <td>Predicate evaluation calls <tt>std::longjmp</tt></td>
    <td><b>well-defined</b>; execution continues as specified by
        <tt>std::longjmp</tt>.</td>
  </tr>
  <tr>
    <td>Violation handler terminates<br/>
        (calls <tt>std::abort()</tt>,
        <tt>std::exit()</tt>,
        <tt>std::quick_exit()</tt>,
        <tt>std::terminate()</tt>,
        etc...)
    </td>
    <td><b>well-defined</b>; the program terminates as specified by the called
        function.</td>
  </tr>
  <tr>
    <td>Violation handler returns</td>
    <td><b>well-defined</b>; execution continues.</td>
  </tr>
  <tr>
    <td>Violation hander throws</td>
    <td><b>unspecified behavior</b>.</td>
  </tr>
  <tr>
    <td>Violation handler calls <tt>std::longjmp</tt></td>
    <td><b>well-defined</b>; execution continues as specified by
        <tt>std::longjmp</tt>.</td>
  </tr>
</table>
</p>


<h1 id="design">Design considerations</h1>

<p>
The following discussion provides the rationale for each of the proposed
semantics enumerated in the
<a href="#proposal">proposal</a>
section above.
</p>

<p>
The proposed design avoids the need for the C++ standard to acknowledge or
to specify build modes.
As always, implementations may offer options to optimize code generation
based on an assumption that a contract violation handler will not return
nor throw an exception.
</p>

<div style="margin-left: 1em;">

<h2 id="design-pred-uneval">Predicate is unevaluated</h2>

<p>
When predicate evaluation is not enabled, it seems clear that there is just
one reasonable choice of semantics; that execution enters the function body.
However, that does not mean that an implementation should completely ignore
predicates; logic errors that involve predicates can still be diagnosed and
can still affect observed program behavior.
The following example contains such an error; the precondition unconditionally
dereferences parameter <tt>p</tt> but the function body has behavior that is
conditional on <tt>p</tt> being a null pointer.
Implementors should be encouraged to diagnose such issues as a matter of QoI.
Static analysis tools commonly detect and report issues like this.
<div style="margin-left: 1em;">
<pre><code class="c++">int f(int *p)
  PRE( *p &lt; 5 )
{
  if (!p)
    return 0;
  return *p;
}</code></pre>
</div>
</p>

<p>
Note that the logic error in the above example matches the one in the following
example using <tt>assert</tt>.
One of the benefits that contracts brings is that observance of the logic
error is not dependent on a build mode; the predicate above is always present
whereas the expression in the <tt>assert</tt> expression below is hidden from
the compiler when compiling with <tt>NDEBUG</tt> defined.
<div style="margin-left: 1em;">
<pre><code class="c++">int f(int *p)
{
  assert( *p &lt; 5 );
  if (!p)
    return 0;
  return *p;
}</code></pre>
</div>
</p>

<h2 id="design-pred-true">Predicate evaluation yields a true result</h2>

<p>
When predicate evaluation is enabled and evaluation yields a true result, it
is likewise clear that the desired behavior is that execution enters the
function body.
The concerns about potential logic errors above are likewise applicable.
</p>

<h2 id="design-pred-false">Predicate evaluation yields a false result</h2>

<p>
When predicate evaluation is enabled and evaluation yields a false result,
this proposal calls for a contract violation handler to be invoked.
Unlike 
<a title="Contract-Violation Handlers"
   href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
                                                       href="#ref_p2811r1">[P2811R1]</a></sup>,
this proposal delegates all responsibility for handling the violation to
the handler.
If program termination is desired (the "enforce" or "Eval_and_abort" semantic
described in other papers), then it is up to the handler to perform
that termination (perhaps by calling the implementation-provided handler).
if it is desired to continue program execution (the "observe" or
"Eval_and_log" semantic), then the handler can simply return; this is consistent
with the behavior of a contract violation when contract checking is not enabled.
</p>

<h2 id="design-pred-ub">Predicate evaluation encounters undefined-behavior</h2>

<p>
It is unlikely to be surprising that the proposed semantic for
undefined-behavior is undefined-behavior.
This case is explicitly called out to acknowledge previous discussion of
<a title="Contracts for C++: Prioritizing Safety"
   href="https://wg21.link/p2680r1">P2680R1</a><sup><a title="Contracts for C++: Prioritizing Safety"
                                                       href="#ref_p2680r1">[P2680R1]</a></sup>.
Though consensus for that proposal has not been established, the potential
for another proposal that seeks to constrain undefined-behavior within
contract predicates to attain consensus is well recognized.
In the meantime, this proposal reflects the status quo.
</p>


<h2 id="design-exceptions">Predicate evaluation or violation handler throws</h2>

<p>
It remains unclear what semantics are desired in the case of exceptions that
propagate from a predicate evaluation or violation hander invocation;
particularly with regard to a function declared with a <tt>noexcept</tt>
exception specification.
Does such an exception originate from within the function such that it
immediately hits the <tt>noexcept</tt> boundary?
Or is it considered to have been thrown from the caller side of the function
as is the case for exceptions thrown during parameter initialization?
If the latter, how does that interact with the <tt>noexcept</tt> operator?
Due to these open questions, this paper proposes that throwing an exception
from a predicate or a violation handler have unspecified behavior until
consensus is established for a direction as
<a title="Caller-side precondition checking, and Eval_and_throw"
   href="https://wg21.link/p2780r0">P2780R0</a><sup><a title="Caller-side precondition checking, and Eval_and_throw"
                                                       href="#ref_p2780r0">[P2780R0]</a></sup>
seeks to do.
</p>

<h2 id="design-longjmp">Predicate evaluation or violation handler calls <tt>std::longjmp</tt></h2>

<p>
The open questions regarding exception handling don't apply to calls to
<tt>longjmp</tt> since questions of <tt>noexcept</tt> exception specifications
and the <tt>noexcept</tt> operator don't apply.
The absence of a reason to deviate <tt>longjmp</tt> behavior in contract
contexts therefore motivates making use in these contexts well-formed.
</p>

</div>


<h1 id="implementation-exp">Implementation experience</h1>

<p>
None.
</p>


<h1 id="ack">Acknowledgements</h1>

<p>
Thank you to Josua Berne for his excellent exposition of possible contract
violation behaviors in 
<a title="Contract-Violation Handlers"
   href="https://wg21.link/p2811r1">
P2811R1</a>
<sup><a title="Contract-Violation Handlers"
        href="#ref_p2811r1">[P2811R1]</a></sup>.
</p>


<h1 id="references">References</h1>

<table id="references">
  <tr>
    <td id="ref_p2680r1"><sup>[P2680R1]</sup></td>
    <td>
      Gabriel Dos Reis,
      "Contracts for C++: Prioritizing Safety", P2680R1, 2022.<br/>
      <a href="https://wg21.link/p2680r1">
      https://wg21.link/p2680r1</a></td>
  </tr>
  <tr>
    <td id="ref_p2698r0"><sup>[P2698R0]</sup></td>
    <td>
      Bjarne Stroustrup,
      "Unconditional termination is a serious problem", P2698R0, 2022.<br/>
      <a href="https://wg21.link/p2698r0">
      https://wg21.link/p2698r0</a></td>
  </tr>
  <tr>
    <td id="ref_p2780r0"><sup>[P2780R0]</sup></td>
    <td>
      Ville Voutilainen,
      "Caller-side precondition checking, and Eval_and_throw", P2780R0, 2023.<br/>
      <a href="https://wg21.link/p2780r0">
      https://wg21.link/p2780r0</a></td>
  </tr>
  <tr>
    <td id="ref_p2784r0"><sup>[P2784R0]</sup></td>
    <td>
      Andrzej Krzemieński,
      "Not halting the program after detected contract violation", P2784R0, 2023.<br/>
      <a href="https://wg21.link/p2784r0">
      https://wg21.link/p2784r0</a></td>
  </tr>
  <tr>
    <td id="ref_p2811r1"><sup>[P2811R1]</sup></td>
    <td>
      Joshua Berne,
      "Contract-Violation Handlers", P2811R1, 2023.<br/>
      <a href="https://wg21.link/p2811r1">
      https://wg21.link/p2811r1</a></td>
  </tr>
  <tr>
    <td id="ref_p2817r0"><sup>[P2817R0]</sup></td>
    <td>
      Andrzej Krzemieński,
      "The idea behind the contracts MVP", P2817R0, 2023.<br/>
      <a href="https://wg21.link/p2817r0">
      https://wg21.link/p2817r0</a></td>
  </tr>
  <tr>
    <td id="ref_p2838r0"><sup>[P2838R0]</sup></td>
    <td>
      Ville Voutilainen,
      "Unconditional contract violation handling of any kind is a serious problem", P2838R0, 2023.<br/>
      <a href="https://wg21.link/p2838r0">
      https://wg21.link/p2838r0</a></td>
  </tr>
</table>


<h1 id="wording">Wording</h1>

<p>
Wording will be provided in a future revision contingent on SG21 establishing
consensus for a proposed direction.
</p>


</body>
