Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit dbd0c87

Browse files
authored
Merge pull request #3712 from pbackus/implement-dip1038
DIP 1038: add core.attribute.mustUse Signed-off-by: Max Haughton <maxhaton@users.noreply.github.com> Merged-on-behalf-of: Max Haughton <maxhaton@users.noreply.github.com>
2 parents e133c26 + b6c05cf commit dbd0c87

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/core/attribute.d

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,52 @@ version (UdaGNUAbiTag) struct gnuAbiTag
241241
this.tags = tags;
242242
}
243243
}
244+
245+
/**
246+
* Use this attribute to ensure that values of a `struct` or `union` type are
247+
* not discarded.
248+
*
249+
* The value of an expression is considered to be discarded if
250+
*
251+
* $(UL
252+
* $(LI
253+
* the expression is the top-level expression in a statement or the
254+
* left-hand expression in a comma expression, and
255+
* ),
256+
* $(LI
257+
* the expression is not an assignment (`=`, `+=`, etc.), increment
258+
* (`++`), or decrement (`--`) expression.
259+
* ),
260+
* )
261+
*
262+
* If the declaration of a `struct` or `union` type has the `@mustUse`
263+
* attribute, the compiler will emit an error any time a value of that type
264+
* would be discarded.
265+
*
266+
* Currently, `@mustUse` is only recognized by the compiler when attached to
267+
* `struct` and `union` declarations. To allow for future expansion, attaching
268+
* `@mustUse` to a `class`, `interface`, `enum`, or function declaration is
269+
* currently forbidden, and will result in a compile-time error. All other uses
270+
* of `@mustUse` are ignored.
271+
*
272+
* Examples:
273+
* ---
274+
* @mustUse struct ErrorCode { int value; }
275+
*
276+
* extern(C) ErrorCode doSomething();
277+
*
278+
* void main()
279+
* {
280+
* // error: would discard a value of type ErrorCode
281+
* //doSomething();
282+
*
283+
* ErrorCode result;
284+
* // ok: value is assigned to a variable
285+
* result = doSomething();
286+
*
287+
* // ok: can ignore the value explicitly with a cast
288+
* cast(void) doSomething();
289+
* }
290+
* ---
291+
*/
292+
enum mustUse;

0 commit comments

Comments
 (0)