-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Allow #[must_use]
on associated types to warn on unused values in generic contexts
#142590
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in compiler/rustc_passes/src/check_attr.rs Some changes occurred to the core trait solver cc @rust-lang/initiative-trait-system-refactor |
This needs T-lang sign-off. |
This comment has been minimized.
This comment has been minimized.
5e16f3a
to
6e93cee
Compare
The Miri subtree was changed cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
6e93cee
to
bb19361
Compare
Makes sense to me. Thanks @compiler-errors for putting this together and putting it forward. @rfcbot fcp merge |
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
@rustbot labels +S-waiting-on-documentation We do document the cc @ehuss |
This seems about right, and I'm glad we have evidence for its usefulness. But are there use cases considered besides the one trait? I want to avoid overfitting to that specific use case if there are more. The behavior strikes me as a little bit subtle. I think it's the best and most practical option, though there is a possibility of a mismatch in expectations, which would be clarified with something like |
bb19361 is perhaps mistitled, because it applies the I'm not certain how this would be overfitted because there really isn't any other obvious semantics I could give to
It doesn't really strike me as necessary to clarify this an additional disambiguator like In the case that this type can be projected, then if the projected type is not |
I could imagine that you want an I guess the statement is that this is like Regardless, this is a lint so can be tuned later, so from the perspective of "is it reasonable to allow the attribute here, even if we tweak the exact behaviour later" I'm happy to |
My take: This is the correct semantics and makes sense, but I think some folks will get confused, especially given the way we allow you to apply @rfcbot reviewed I think in particular the use case of "I want you to treat this as must-use in generic code where you MIGHT have to 'use' it (but in specific contexts, I don't want to worry about it)" will be common. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
This PR enables
#[must_use]
on associated type definitions in traits. When the unused results visitor encounters a rigid associated type marked with#[must_use]
(i.e. someT::Result
in generic code), then it will trigger an unused warning.This is particularly useful when users want to be generic over some
Result
associated type. In the compiler, our visitors typically return one of()
,ControlFlow<T>
, orResult<T, E>
. In generic code, though, we are dealing withT::Result
, which is a footgun since today we can't detect when a value of that rigid associated type goes unused.This does not apply after an associated type can be normalized. For example, this code will not trigger the warning, since
<NoOp as Foo>::Result
is()
, which is not#[must_use]
:The commit 5e16f3a demonstrates some places in the compiler that had unhandled results, and so it should make it very clear that this change is useful.