-
-
Notifications
You must be signed in to change notification settings - Fork 610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PoC] Add @nodiscard attribute #11765
Conversation
|
Thanks for your pull request and interest in making D better, @pbackus! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#11765" |
9cd94d2 to
a943388
Compare
|
It seems to me that |
|
@TurkeyMan Yes, the compiler already gives a warning (not an error) for discarding the result of a |
|
What is the effect of using this attribute on a constructor? If none, may want to prohibit there. |
|
@chloekek Good question. It does work on constructors, because they return a reference to the constructed object. That's not really documented in the language spec, though (closest thing is a mention of constructors' return type under "Ref Return Scope Parameters"), so I should probably mention constructors explicitly in the DIP. |
Fixes issue 5464 - Attribute to not ignore function result
a943388 to
0101bc9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind the comments. Also, this need to be tested more thoroughly:
- for BinAssignExps
- for aliases to nodiscard types and functions
- for overloaded @NODISCARD functions
- for @NODISCARD member functions
- for @NODISCARD overriden functions (how do you override a nodiscard function?)
- @NODISCARD auto functions
- @NODISCARD templated functions
| @@ -1444,6 +1444,8 @@ final class Parser(AST) : Lexer | |||
| stc = isBuiltinAtAttribute(token.ident); | |||
| if (!stc) | |||
| { | |||
| if (token.ident == Id.nodiscard) | |||
| deprecation("use of `@nodiscard` as a user-defined attribute is deprecated."); | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DIP should be amended to contain this deprecation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DIP is correct; this PR is based on a previous revision and needs to be updated.
| scope IsAssignment v = new IsAssignment(); | ||
| e.accept(v); | ||
| return v.result; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function would be more efficient if it would simply be replaced by an if statement:
private bool isAssignmentExp(Expression e) @safe pure @nogc nothrow
{
return e.isAssignExp || e.isBinAssignExp;
}
| return false; | ||
| if (auto ce = e.isCallExp()) | ||
| { | ||
| if (ce.f && (ce.f.storage_class & STC.nodiscard)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curly brace on the next line
| { | ||
| auto sym = e.type.toDsymbol(null); | ||
| auto ad = sym ? sym.isAggregateDeclaration() : null; | ||
| if (ad && (ad.storage_class & STC.nodiscard) != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curly brace on the next line
| /** | ||
| * Check whether the e is a call to a @nodiscard function, or is | ||
| * a non-assignment expression with @nodiscard type. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment should specify that this function issues an error in the above mentioned cases.
Also, this function is called "isNodiscard", therefore the expectation would be that it simply verifies if an expression contains an @NODISCARD expression. However, this function does more than that: it also errors if the expression is nodiscard. I suggest you rename the function to checkNodiscard. This implies that an error might be issued.
|
Since the final accepted version of DIP 1038 has diverged significantly from the draft this PR was based on, I am closing it. DIP 1038 will be implemented in a new PR. Thank you, @RazvanN7, for your review—I plan to incorporate your feedback into the final implementation. |
Edit: This PR was based on an early draft version of DIP 1038. It has been superseded by #13589. The original PR description follows below.
Fixes issue 5464 - Attribute to not ignore function result
Still need to write up a DIP for this, but I figured I'd post it early to get feedback. The design is the same as Rust's
#[must_use]: on a function, it applies to the return value; on a type, it applies to all expressions of that type.