-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
(request) Extend #[must_use]
to Pin
#67387
Comments
If we do this I'd like to see an attribute |
|
@jonas-schievink's idea seems like an elegant way to afford users a lot of flexibility while also limiting (and reducing) the overall compiler complexity cost. It would still require some user intervention, but it seems well balanced. cc @oli-obk -- I assume we'd use roughly the following impl strategy:
|
At that point we can make impl MustUse for TheType {
const REASON: Option<&'static str> = "the must use reason string";
} Wrt marker types: Should |
If we use expansion, we need to think about
Currently there's no such leakage: #![allow(dead_code)]
#[must_use]
struct A;
trait T {}
impl T for A {}
fn f() -> impl T { A }
fn g() {
f();
} I believe we also didn't want the structural auto-trait behavior here so I don't think it should leak. (Also, auto traits are all entirely empty at the moment whereas |
We discussed this in our @rust-lang/lang meeting on 2020-01-02. A few notes: We don't have to block extending In general, there wasn't much consensus in the meeting about the idea of exposing this mechanism to end-users. At minimum an RFC would be required to hash out the design. (I personally am not that keen on the trait-based design, but I'll leave those concerns to a follow-up comment.) |
As far as the idea of adding a trait for must-use, I have various concerns. First and foremost, that opens up a lot of expressive power, and I am far from convinced it makes sense. Like, do we really want the ability to have types that are "must use" but only if their argument types are Going along with the overkill theme, now this trait is something people can write code against. So they can write Finally, the trait doesn't solve the entire problem. It covers one use -- must-use types -- but not must-use functions. I feel like it's overall "not a net win" to have two distinct ways of declaring things must-use. So yeah, as of this moment it seems to me like some kind of more limited |
That said, I am 👍 on extending |
I think my preferred generalization for now is to consider |
I would be happy with that. |
I guess it also introduces the trait system into the lint :) but other than that it seems ok. |
I'm not convinced deref types that don't imply ownership should be must_use since the target value could be used by another piece of code. |
Yeah, it may be overkill. Are you thinking of "convenience" Deref impls that (for example) emulate subclassing, @withoutboats? In any case I'd be happy to start with just an attribute and apply it to |
No I'm thinking of references. For example I don't think an |
Good point. Seems correct that |
Can we extend this to |
Another case: it'd be nice if executors'
Here, Of course, the bug there is actually a missing |
Interesting example. In a recent @rust-lang/lang meeting we were wondering about the extent to which we ought to warn about a type |
Is it fixed by #118054 ? |
Yes. |
A type
T
with#[must_use]
does not imply thatPin<T>
is#[must_use]
. This means that there is no hint to.await
aPin<Box<dyn Future>>
, which is one of the current workarounds for the lack ofaysnc fn
in traits. This is used byasync-trait
in its expansion, and in some crates that have traits that "ought to" haveasync fn
s.See also #39524, which would propagate
must_use
"everywhere" but was rejected, and #62228, which special-casedmust_use
propagation throughBox
only.cc @varkor, who indicated they would provide mentoring advice.
The text was updated successfully, but these errors were encountered: