-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Create unnecessary_send_constraint
lint for &(dyn ... + Send)
#110961
Conversation
Does not yet handle `Send` being only constraint (will still recommend removing it)
r? @davidtwco (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
This comment has been minimized.
This comment has been minimized.
Also, |
Also, it appears no lint is generated for duplicate auto traits (like |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Co-authored-by: est31 <est31@users.noreply.github.com>
🔔 This is now entering its final comment period, as per the review above. 🔔 |
/// References cannot be sent across threads unless they have a `Sync` bound, so constraining them to `Send` without `Sync` is unnecessary. | ||
pub UNNECESSARY_SEND_CONSTRAINT, | ||
Warn, | ||
"constraining a reference to `Send` without `Sync` is unnecessary, consider removing it" |
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.
Shouldn't the recommendation be to replace Send
by Sync
? That seems more likely to be what the author intended.
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.
See this comment: #110961 (comment)
In those situations, it stems from a Box<dyn Any + Send>
to which a &dyn was made. It's easy to just copy Any + Send
, but the Send part should be removed for shared references.
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.
That seems like a fairly special case to me, I would not expect this to be common enough to warrant a general recommendation of dropping the Send
. The more symmetric thing to do would be to have &dyn Any+Sync
, to get a shared reference that can still be shared across thread boundaries. Of course then you'd need Box<dyn Any+Send+Sync>
for the owned case.
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.
So it should recommend replacing Send
with Sync
instead? just confirming before making the change
@@ -192,6 +192,12 @@ lint_redundant_semicolons = | |||
*[false] this semicolon | |||
} | |||
|
|||
lint_unnecessary_send_constraint = constraining a reference to `Send` is meaningless | |||
.suggestion = {$only_trait -> | |||
[true] replace this with `std::any::Any` |
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.
Replacing by Any
is a very strange suggestion. Why would type_id
machinery be necessary? Can't the 'static
bound cause a problem?
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.
I wasn't sure what else to put, this is for the scenario where Send
is the only trait specified (so removing it would be invalid)
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
It looks like we agreed to this. I'm going to drop nomination. @rustbot labels -I-lang-nominated |
Explicitly document how Send and Sync relate to references Some of these relations were already mentioned in the text, but that Send is implemented for &mut impl Send was not mentioned, neither did the docs list when &T is Sync. Inspired by the discussion in #110961. [Proof](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ed77bfc3c77ba664400ebc2734f500e6) based on `@lukas-code` 's [example](rust-lang/rust#110961 (comment)).
@rustbot author The team role is done here, FCP is complete, we are now waiting on the author to address feedback though. |
@rustbot ready (still 1 part of the review I am unsure about so have asked for clarification, but other comments have been addressed) |
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> { | ||
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Sync + 'static)> { |
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 is a mutable reference. For &mut … + Send
, the Send
makes sense, doesn’t it?
pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> { | ||
pub fn get_ref(&self) -> Option<&(dyn error::Error + Sync + 'static)> { |
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.
dyn Error + Send + Sync
has some API that dyn Error + Sync
doesn’t have. Like an is
method and a downcast_ref
method.
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.
Also, in any case, changing these methods is a breaking change the impact of which I’m not sure has been tested anywhere yet.
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.
that
dyn Error + Sync
doesn’t have. Like anis
method and adowncast_ref
method.
dyn Error
also implements is
and downcast_*
with exact the same signature.
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.
dyn Error
also implementsis
anddowncast_*
with exact the same signature.
Oops, my bad. We cannot use dyn Error
's methods on dyn Error + Sync
, though it seems odd to me.
@john-h-k if you can rebase and resolve the conflicts, we can push this for a review |
@john-h-k |
Fixes #110937