Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Comments
  • Loading branch information
john-h-k committed May 2, 2023
commit e9fe48c919e956df18df17b52d667e103051ecdb
2 changes: 1 addition & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ lint_redundant_semicolons =
*[false] this semicolon
}

lint_useless_send_constraint = constraining a reference to `Send` is meaningless
lint_unnecessary_send_constraint = constraining a reference to `Send` is meaningless
.suggestion = {$only_trait ->
[true] replace this with `std::any::Any`
Copy link
Member

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?

Copy link
Contributor Author

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)

*[false] remove this
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod redundant_semicolon;
mod traits;
mod types;
mod unused;
mod useless_send_constraint;
mod unnecessary_send_constraint;

pub use array_into_iter::ARRAY_INTO_ITER;

Expand Down Expand Up @@ -126,7 +126,7 @@ pub use passes::{EarlyLintPass, LateLintPass};
pub use rustc_session::lint::Level::{self, *};
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
pub use rustc_session::lint::{LintArray, LintPass};
use useless_send_constraint::UselessSendConstraint;
use unnecessary_send_constraint::UselessSendConstraint;

fluent_messages! { "../messages.ftl" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use rustc_span::sym;

use crate::hir;

use crate::{lints::UselessSendConstraintDiag, LateContext, LateLintPass};
use crate::{lints::UnnecessarySendConstraintDiag, LateContext, LateLintPass};

declare_lint! {
/// The `lint_useless_send_constraint` lints useless constraint of references to `Send`.
/// The `lint_unnecessary_send_constraint` lints unnecessary constraint of references to `Send`.
///
/// ### Example
///
Expand All @@ -17,15 +17,15 @@ declare_lint! {
///
/// ### Explanation
///
/// References cannot be sent across threads unless they have a `Sync` bound, so constraining them to `Send` without `Sync` is useless.
pub USELESS_SEND_CONSTRAINT,
/// 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 useless, consider removing it"
"constraining a reference to `Send` without `Sync` is unnecessary, consider removing it"
Copy link
Member

@RalfJung RalfJung Jun 27, 2023

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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

}

declare_lint_pass!(UselessSendConstraint => [USELESS_SEND_CONSTRAINT]);
declare_lint_pass!(UnnecessarySendConstraint => [UNNECESSARY_SEND_CONSTRAINT]);

impl<'tcx> LateLintPass<'tcx> for UselessSendConstraint {
impl<'tcx> LateLintPass<'tcx> for UnnecessarySendConstraint {
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx hir::Ty<'tcx>) {
let hir::TyKind::Ref(
..,
Expand All @@ -46,10 +46,10 @@ impl<'tcx> LateLintPass<'tcx> for UselessSendConstraint {
let only_trait = bounds.len() == 1;

cx.tcx.emit_spanned_lint(
USELESS_SEND_CONSTRAINT,
UNNECESSARY_SEND_CONSTRAINT,
send_bound.trait_ref.hir_ref_id, // is this correct?
send_bound.span,
UselessSendConstraintDiag { only_trait, suggestion: send_bound.span },
UnnecessarySendConstraintDiag { only_trait, suggestion: send_bound.span },
)
}
}
Expand Down