-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3167r0.html
517 lines (468 loc) · 15.6 KB
/
p3167r0.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<!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 <[email protected]></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 > 0 && j > 0)
post <b>[[bar]]</b> (<b><span style="color:red">r</span></b>: r > 0)
{
<b>[[likely]]</b> contract_assert <b>[[baz]]</b> (i + j < 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 > 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]).
— <em>end note</em> ]
<br/>
<br/>
[ … ]
</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>