<!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>
Attributes for the result name in a postcondition assertion
</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.code
{
    white-space: pre;
    font-family: monospace;
}
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>P3167R0</td>
  </tr>
  <tr>
    <th>Date:</th>
    <td>2024-02-28</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>Attributes for the result name in a postcondition assertion</h1>


<ul>
  <li><a href="#introduction">Introduction</a></li>
  <li><a href="#motivation">Motivation</a></li>
  <li><a href="#proposal">Proposal</a></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 confirmed support for
<a title="Attributes for contract assertions"
   href="https://wg21.link/p3088r1">
P3088R1 (Attributes for contract assertions)</a>
<sup><a title="Attributes for contract assertions"
        href="#ref_p3088r1">[P3088R1]</a></sup>
during its
<a href="https://wiki.edg.com/bin/view/Wg21telecons2024/Teleconference2024-02-15">2024-02-15 telecon</a>
and the approved changes were subsequently added to
<a title="Contracts for C++"
   href="https://wg21.link/p2900r5">
P2900R5 (Contracts for C++)</a>
<sup><a title="Contracts for C++"
        href="#ref_p2900r5">[P2900R5]</a></sup>.
This established syntactic locations for attributes that appertain to
contract assertions and the assertion statement as follows:
<blockquote class="code">
int f(int i, int j)
  pre <b>[[foo]]</b> (i &gt; 0 &amp;&amp; j &gt; 0)
  post <b>[[bar]]</b> (<b><span style="color:red">r</span></b>: r &gt; 0)
{
  <b>[[likely]]</b> contract_assert <b>[[baz]]</b> (i + j &lt; 10);
  return i + j;
}
</blockquote>
However, P3088R1 did not provide for an attribute to appertain to the
result name optionally declared in a postcondition specifier; the
<tt><b><span style="color:red">r</span></b></tt> declared in the example
postcondition above.
This proposal seeks to fill that gap by allowing an attribute to be
specified in the result name introducer.
<blockquote class="code">
int g()
  post (r <b>[[maybe_unused]]</b>: r &gt; 0);
</blockquote>
</p>


<h1 id="motivation">Motivation</h1>

<p>
As evidenced by
<a href="http://eel.is/c++draft/gram">annex A (Grammar summary)</a>
in the C++ standard, an attribute may appertain to almost every kind of
declared name or entity including:
<ul>
  <li>Type aliases (when declared with an <i>alias-declaration</i>)</li>
  <li>Enumerations</li>
  <li>Enumerators</li>
  <li>Variables (including the unnamed object for structured bindings)</li>
  <li>Structured bindings (if
<a title="Attributes for Structured Bindings"
   href="https://wg21.link/p0609r2">
P0609R2</a>
<sup><a title="Attributes for Structured Bindings"
        href="#ref_p0609r2">[P0609R2]</a></sup>
      is adopted)</li>
  <li>Classes</li>
  <li>Data members</li>
  <li>Functions</li>
  <li>Parameters (function parameters, non-type template parameters,
      exception handlers)</li>
  <li>Concepts</li>
  <li>Labels</li>
  <li>Namespaces</li>
  <li>Modules</li>
</ul>
Exceptions include:
<ul>
  <li>Namespace aliases</li>
  <li>Type template parameters</li>
  <li>Template template parameters</li>
  <li>Lambda captures</li>
</ul>
It is not clear why attributes are not supported for the items in the
exceptions list.
The recommended practice for the <tt>[[deprecated]]</tt> attribute in
<a href="http://eel.is/c++draft/dcl.attr.deprecated#4">[dcl.attr.deprecated]p4</a>
seems applicable to namespace aliases:
<blockquote class="quote">
<em>Recommended practice:</em> Implementations should use the deprecated
attribute to produce a diagnostic message in case the program refers to a name
or entity other than to declare it, after a declaration that specifies the
attribute.
The diagnostic message should include the text provided within the
<a href="http://eel.is/c++draft/dcl.attr.grammar#nt:attribute-argument-clause"><i>attribute-argument-clause</i></a>
of any deprecated attribute applied to the name or entity.
The value of a
<a href="http://eel.is/c++draft/cpp.cond#nt:has-attribute-expression"><i>has-attribute-expression</i></a>
for the deprecated attribute should be <tt>0</tt> unless the implementation can
issue such diagnostic messages.
</blockquote>
Likewise, the recommended practice for the <tt>[[maybe_unused]]</tt> attribute
in
<a href="http://eel.is/c++draft/dcl.attr.unused#4">[dcl.attr.unused]p4</a>
seems applicable to the other entities:
<blockquote class="quote">
<em>Recommended practice:</em> For an entity marked <tt>maybe_unused</tt>,
implementations should not emit a warning that the entity or its structured
bindings (if any) are used or unused.
For a structured binding declaration not marked <tt>maybe_unused</tt>,
implementations should not emit such a warning unless all of its structured
bindings are unused.
For a label to which <tt>maybe_unused</tt> is applied, implementations should
not emit a warning that the label is used or unused.
The value of a
<a href="http://eel.is/c++draft/cpp.cond#nt:has-attribute-expression"><i>has-attribute-expression</i></a>
for the <tt>maybe_unused</tt> attribute should be <tt>0</tt> if the attribute
does not cause suppression of such warnings.
</blockquote>
</p>

<p>
The <tt>[[deprecated]]</tt> attribute was adopted for C++14 via
<a title="[[deprecated]] attribute"
   href="https://wg21.link/n3760">
N3760</a>
<sup><a title="[[deprecated]] attribute"
        href="#ref_n3760">[N3760]</a></sup>.
Neither that paper, its previous revision, or the minutes from EWG and CWG
review during the following meetings demonstrate consideration for use of
the attribute with a namespace alias.
<ul>
  <li><a href="https://wiki.edg.com/bin/view/Wg21portland2012/EvolutionWorkingGroup">EWG review of N3394 in Portland, 2012</a></li>
  <li><a href="https://wiki.edg.com/bin/view/Wg21chicago2013/CoreWorkingGroup">CWG review of N3760 in Chicago, 2013</a></li>
</ul>
</p>

<p>
The <tt>[[maybe_unused]]</tt> attribute was adopted for C++17 via
<a title="Wording for [[maybe_unused]] attribute."
   href="https://wg21.link/p0212r1">
P0212R1</a>
<sup><a title="Wording for [[maybe_unused]] attribute."
        href="#ref_p0212r1">[P0212R1]</a></sup>.
Neither that paper, its previous revision, the rationale provided in the
previous
<a title="Proposal of [[unused]], [[nodiscard]] and [[fallthrough]] attributes."
   href="https://wg21.link/p0068r0">
P0068R0</a>
<sup><a title="Proposal of [[unused]], [[nodiscard]] and [[fallthrough]] attributes."
        href="#ref_p0068r0">[P0068R0]</a></sup>,
or the minutes from EWG and CWG review during the following meetings
demonstrate consideration for use of the attribute with a
type template parameter,
template template parameter, or
lambda capture.
<ul>
  <li><a href="https://wiki.edg.com/bin/view/Wg21kona2015/P0068R0">EWG review of P0068R0 in Kona, 2015</a></li>
  <li><a href="https://wiki.edg.com/bin/view/Wg21jacksonville/CoreWorkingGroup">CWG review of D0212R1 in Jacksonville, 2016</a></li>
</ul>
</p>

<p>
A search of both active and closed CWG issues failed to identify any existing
issues that concern attribute appertainment for the excluded items.
</p>

<p>
There does not appear to be a recorded rationale for omitting attribute support
for the excluded items above.
</p>

<p>
Absent compelling rationale otherwise, precedent indicates that new kinds of
declared names or entities should allow for attribute appertainment and their
respective decarations should therefore include syntactic locations for an
attribute specifier sequence.
</p>


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

<p>
<ul>
  <li>Allow <i>attribute-specifier-seq</i> to optionally follow
      <i>identifier</i> in a <i>result-name-introducer</i> with the effect that
      the specified attributes (if any) appertain to the declared
      result name.</li>
  <li>Allow the <tt>[[maybe_unused]]</tt> attribute to appertain to a result
      name.</li>
</ul>
</p>


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

<p>
None.
The experimental implementation of
<a title="Contracts for C++"
   href="https://wg21.link/p2900r5">
P2900R5</a>
<sup><a title="Contracts for C++"
        href="#ref_p2900r5">[P2900R5]</a></sup>
in gcc hosted on
<a href="https://godbolt.org">https://godbolt.org</a>
does not currently diagnose an unused result name, nor does it allow an
attribute specifier sequence to appear in a <i>result-name-introducer</i>
as demonstrated at
<a href="https://godbolt.org/z/339WEr7c3">https://godbolt.org/z/339WEr7c3</a>.
</p>


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

<p>
Thanks to Timur Doumler and Joshua Berne for their previous work to enable
attributes for contract assertions in
<a title="Attributes for contract assertions"
   href="https://wg21.link/p3088r1">
P3088R1</a>
<sup><a title="Attributes for contract assertions"
        href="#ref_p3088r1">[P3088R1]</a></sup>
and for the tremendous amount of time, energy, and expertise they have each
contributed to SG21 and advancing contracts for C++.
</p>

<p>
Additional thanks to Timur Doumler for suggesting much of the wording included
in this proposal.
</p>


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

<table id="references">
  <tr>
    <td id="ref_n3760"><sup>[N3760]</sup></td>
    <td>
      "[[deprecated]] attribute", N3760, 2013.<br/>
      <a href="https://wg21.link/n3760">
      https://wg21.link/n3760</a></td>
  </tr>
  <tr>
    <td id="ref_p0068r0"><sup>[P0068R0]</sup></td>
    <td>
      "Proposal of [[unused]], [[nodiscard]] and [[fallthrough]] attributes.", P0068R0, 2015.<br/>
      <a href="https://wg21.link/p0068r0">
      https://wg21.link/p0068r0</a></td>
  </tr>
  <tr>
    <td id="ref_p0212r1"><sup>[P0212R1]</sup></td>
    <td>
      "Wording for [[maybe_unused]] attribute.", P0212R1, 2016.<br/>
      <a href="https://wg21.link/p0212r1">
      https://wg21.link/p0212r1</a></td>
  </tr>
  <tr>
    <td id="ref_p0609r2"><sup>[P0609R2]</sup></td>
    <td>
      "Attributes for Structured Bindings", P0609R2, 2023.<br/>
      <a href="https://wg21.link/p0609r2">
      https://wg21.link/p0609r2</a></td>
  </tr>
  <tr>
    <td id="ref_p2900r5"><sup>[P2900R5]</sup></td>
    <td>
      "Contracts for C++", P2900R5, 2024.<br/>
      <a href="https://wg21.link/p2900r5">
      https://wg21.link/p2900r5</a></td>
  </tr>
  <tr>
    <td id="ref_p3088r1"><sup>[P3088R1]</sup></td>
    <td>
      "Attributes for contract assertions", P3088R1, 2024.<br/>
      <a href="https://wg21.link/p3088r1">
      https://wg21.link/p3088r1</a></td>
  </tr>
</table>


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

<p>
These changes are relative to
<a title="Contracts for C++"
   href="https://wg21.link/p2900r5">
P2900R5</a>
<sup><a title="Contracts for C++"
        href="#ref_p2900r5">[P2900R5]</a></sup>
and
<a title="Attributes for Structured Bindings"
   href="https://wg21.link/p0609r2">
P0609R2</a>
<sup><a title="Attributes for Structured Bindings"
        href="#ref_p0609r2">[P0609R2]</a></sup>.
</p>

<input type="checkbox" id="hideins">Hide inserted text</input><br/>
<input type="checkbox" id="hidedel">Hide deleted text</input>

<p>
Modify [dcl.contract.func]:<br/>
<em>Drafting note:</em> This depends on the addition of
<i>attributed-identifier</i> from
<a title="Attributes for Structured Bindings"
   href="https://wg21.link/p0609r2">
P0609R2</a>
<sup><a title="Attributes for Structured Bindings"
        href="#ref_p0609r2">[P0609R2]</a></sup>.
<blockquote>
<div style="margin-left: 1em;">
<i>function-contract-specifier-seq</i> :
<div style="margin-left: 1em;">
<i>function-contract-specifier</i> <i>function-contract-specifier-seq</i>
</div>
</div>
<br/>
<div style="margin-left: 1em;">
<i>function-contract-specifier</i> :
<div style="margin-left: 1em;">
<i>precondition-specifier</i>
<i>postcondition-specifier</i>
</div>
</div>
<br/>
<div style="margin-left: 1em;">
<i>precondition-specifier</i> :
<div style="margin-left: 1em;">
<tt>pre</tt> <i>attribute-specifier-seq<sub>opt</sub></i> <tt>(</tt> <i>conditional-expression</i> <tt>)</tt>
</div>
</div>
<br/>
<div style="margin-left: 1em;">
<i>postcondition-specifier</i> :
<div style="margin-left: 1em;">
<tt>post</tt> <i>attribute-specifier-seq<sub>opt</sub></i> <tt>(</tt> <i>result-name-introducer<sub>opt</sub></i> conditional-expression <tt>)</tt>
</div>
</div>
<br/>
<div style="margin-left: 1em;">
<i>result-name-introducer</i> :
<div style="margin-left: 1em;">
<i><ins>attributed-</ins>identifier</i> :
</div>
</div>
<br/>
A <em>function contract assertion</em> is a contract assertion
([basic.contract]) associated with a function.
Each <i>function-contract-specifier</i> of a
<i>function-contract-specifier-seq</i> (if any) of an unspecified first
declaration of a function introduces a corresponding function contract
assertion for that function.
The optional <i>attribute-specifier-seq</i><ins>
following <tt>pre</tt> or <tt>post</tt></ins>
appertains to the introduced
contract assertion.<ins>
The optional <i>attribute-specifier-seq</i> of the
<i>attributed-identifier</i> in a <i>result-name-introducer</i> appertains
to the introduced result name.</ins>
[ <em>Note:</em> The <i>function-contract-specifier-seq</i> of a
<i>lambda-declarator</i> applies to the call operator or operator template
of the corresponding closure type ([expr.prim.lambda.closure]).
&mdash; <em>end note</em> ]
<br/>
<br/>
[ &hellip; ]
</blockquote>
</p>

<p>
Add a proposed modification to
<a href="http://eel.is/c++draft/dcl.attr.unused#2">[dcl.attr.unused], paragraph 2</a>:<br/>
<em>Drafting note:</em> This includes
<nop>modifications from</nop>
<a title="Attributes for Structured Bindings"
   href="https://wg21.link/p0609r2">
P0609R2</a>
<sup><a title="Attributes for Structured Bindings"
        href="#ref_p0609r2">[P0609R2]</a></sup>.
<blockquote>
The attribute may be applied to the declaration of a class,
<a href="http://eel.is/c++draft/dcl.typedef#nt:typedef-name"><i>typedef-name</i></a>,
variable (including a structured binding declaration),<nop>
structured binding,</nop><ins>
result name,</ins>
non-static data member,
function,
enumeration,
or enumerator,
or to an
<a href="http://eel.is/c++draft/lex.name#nt:identifier"><i>identifier</i></a> label
(<a href="http://eel.is/c++draft/stmt.label">[stmt.label]</a>).
</blockquote>
</p>


</body>
