2021-03-25 13:29:11 -05:00
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
|
use clippy_utils::return_ty;
|
2020-04-07 08:39:07 -05:00
|
|
|
|
use rustc_hir::intravisit::FnKind;
|
|
|
|
|
use rustc_hir::{Body, FnDecl, HirId};
|
|
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-06-30 16:41:57 -05:00
|
|
|
|
use rustc_middle::ty::subst::Subst;
|
2022-05-08 00:17:58 -05:00
|
|
|
|
use rustc_middle::ty::{EarlyBinder, Opaque, PredicateKind::Trait};
|
2020-04-07 08:39:07 -05:00
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
use rustc_span::{sym, Span};
|
|
|
|
|
use rustc_trait_selection::traits::error_reporting::suggestions::InferCtxtExt;
|
|
|
|
|
use rustc_trait_selection::traits::{self, FulfillmentError, TraitEngine};
|
|
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
|
/// ### What it does
|
|
|
|
|
/// This lint requires Future implementations returned from
|
2020-04-07 08:39:07 -05:00
|
|
|
|
/// functions and methods to implement the `Send` marker trait. It is mostly
|
|
|
|
|
/// used by library authors (public and internal) that target an audience where
|
|
|
|
|
/// multithreaded executors are likely to be used for running these Futures.
|
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
|
/// ### Why is this bad?
|
|
|
|
|
/// A Future implementation captures some state that it
|
2020-04-07 08:39:07 -05:00
|
|
|
|
/// needs to eventually produce its final value. When targeting a multithreaded
|
|
|
|
|
/// executor (which is the norm on non-embedded devices) this means that this
|
|
|
|
|
/// state may need to be transported to other threads, in other words the
|
|
|
|
|
/// whole Future needs to implement the `Send` marker trait. If it does not,
|
|
|
|
|
/// then the resulting Future cannot be submitted to a thread pool in the
|
|
|
|
|
/// end user’s code.
|
|
|
|
|
///
|
|
|
|
|
/// Especially for generic functions it can be confusing to leave the
|
|
|
|
|
/// discovery of this problem to the end user: the reported error location
|
|
|
|
|
/// will be far from its cause and can in many cases not even be fixed without
|
|
|
|
|
/// modifying the library where the offending Future implementation is
|
|
|
|
|
/// produced.
|
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
|
/// ### Example
|
2020-04-07 08:39:07 -05:00
|
|
|
|
/// ```rust
|
|
|
|
|
/// async fn not_send(bytes: std::rc::Rc<[u8]>) {}
|
|
|
|
|
/// ```
|
|
|
|
|
/// Use instead:
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// async fn is_send(bytes: std::sync::Arc<[u8]>) {}
|
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
|
#[clippy::version = "1.44.0"]
|
2020-04-07 08:39:07 -05:00
|
|
|
|
pub FUTURE_NOT_SEND,
|
|
|
|
|
nursery,
|
|
|
|
|
"public Futures must be Send"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
declare_lint_pass!(FutureNotSend => [FUTURE_NOT_SEND]);
|
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
|
2020-04-07 08:39:07 -05:00
|
|
|
|
fn check_fn(
|
|
|
|
|
&mut self,
|
2020-06-25 15:41:36 -05:00
|
|
|
|
cx: &LateContext<'tcx>,
|
2020-04-07 08:39:07 -05:00
|
|
|
|
kind: FnKind<'tcx>,
|
|
|
|
|
decl: &'tcx FnDecl<'tcx>,
|
|
|
|
|
_: &'tcx Body<'tcx>,
|
|
|
|
|
_: Span,
|
|
|
|
|
hir_id: HirId,
|
|
|
|
|
) {
|
2020-11-27 02:24:42 -06:00
|
|
|
|
if let FnKind::Closure = kind {
|
2020-04-07 08:39:07 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-25 13:29:11 -05:00
|
|
|
|
let ret_ty = return_ty(cx, hir_id);
|
2020-08-03 17:18:29 -05:00
|
|
|
|
if let Opaque(id, subst) = *ret_ty.kind() {
|
2020-06-30 16:41:57 -05:00
|
|
|
|
let preds = cx.tcx.explicit_item_bounds(id);
|
2020-04-07 08:39:07 -05:00
|
|
|
|
let mut is_future = false;
|
2020-06-30 16:41:57 -05:00
|
|
|
|
for &(p, _span) in preds {
|
2022-05-08 00:17:58 -05:00
|
|
|
|
let p = EarlyBinder(p).subst(cx.tcx, subst);
|
2021-12-11 22:34:46 -06:00
|
|
|
|
if let Some(trait_pred) = p.to_opt_poly_trait_pred() {
|
|
|
|
|
if Some(trait_pred.skip_binder().trait_ref.def_id) == cx.tcx.lang_items().future_trait() {
|
2020-04-07 08:39:07 -05:00
|
|
|
|
is_future = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if is_future {
|
2021-10-02 18:51:01 -05:00
|
|
|
|
let send_trait = cx.tcx.get_diagnostic_item(sym::Send).unwrap();
|
2020-04-07 08:39:07 -05:00
|
|
|
|
let span = decl.output.span();
|
2021-11-08 09:35:23 -06:00
|
|
|
|
let send_errors = cx.tcx.infer_ctxt().enter(|infcx| {
|
2020-04-07 08:39:07 -05:00
|
|
|
|
let cause = traits::ObligationCause::misc(span, hir_id);
|
|
|
|
|
let mut fulfillment_cx = traits::FulfillmentContext::new();
|
|
|
|
|
fulfillment_cx.register_bound(&infcx, cx.param_env, ret_ty, send_trait, cause);
|
|
|
|
|
fulfillment_cx.select_all_or_error(&infcx)
|
|
|
|
|
});
|
2021-11-08 09:35:23 -06:00
|
|
|
|
if !send_errors.is_empty() {
|
2021-03-25 13:29:11 -05:00
|
|
|
|
span_lint_and_then(
|
2020-04-07 08:39:07 -05:00
|
|
|
|
cx,
|
|
|
|
|
FUTURE_NOT_SEND,
|
|
|
|
|
span,
|
|
|
|
|
"future cannot be sent between threads safely",
|
|
|
|
|
|db| {
|
|
|
|
|
cx.tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
|
for FulfillmentError { obligation, .. } in send_errors {
|
2021-01-30 11:06:34 -06:00
|
|
|
|
infcx.maybe_note_obligation_cause_for_async_await(db, &obligation);
|
2021-07-22 08:56:07 -05:00
|
|
|
|
if let Trait(trait_pred) = obligation.predicate.kind().skip_binder() {
|
2020-06-19 03:22:25 -05:00
|
|
|
|
db.note(&format!(
|
2020-04-07 08:39:07 -05:00
|
|
|
|
"`{}` doesn't implement `{}`",
|
2020-06-19 03:22:25 -05:00
|
|
|
|
trait_pred.self_ty(),
|
|
|
|
|
trait_pred.trait_ref.print_only_trait_path(),
|
2020-04-07 08:39:07 -05:00
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-03 01:41:37 -05:00
|
|
|
|
});
|
2020-04-07 08:39:07 -05:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|