1 //! Common logic for borrowck use-after-move errors when moved into a `fn(self)`,
2 //! as well as errors when attempting to call a non-const function in a const
5 use rustc_hir::def_id::DefId;
6 use rustc_hir::lang_items::LangItemGroup;
7 use rustc_middle::ty::subst::SubstsRef;
8 use rustc_middle::ty::{self, AssocItemContainer, DefIdTree, Instance, ParamEnv, Ty, TyCtxt};
9 use rustc_span::symbol::Ident;
10 use rustc_span::{sym, DesugaringKind, Span};
12 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
13 pub enum CallDesugaringKind {
14 /// for _ in x {} calls x.into_iter()
16 /// x? calls x.branch()
18 /// x? calls type_of(x)::from_residual()
20 /// try { ..; x } calls type_of(x)::from_output(x)
24 impl CallDesugaringKind {
25 pub fn trait_def_id(self, tcx: TyCtxt<'_>) -> DefId {
27 Self::ForLoopIntoIter => tcx.get_diagnostic_item(sym::IntoIterator).unwrap(),
28 Self::QuestionBranch | Self::TryBlockFromOutput => {
29 tcx.lang_items().try_trait().unwrap()
31 Self::QuestionFromResidual => tcx.get_diagnostic_item(sym::FromResidual).unwrap(),
36 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
37 pub enum CallKind<'tcx> {
38 /// A normal method call of the form `receiver.foo(a, b, c)`
40 self_arg: Option<Ident>,
41 desugaring: Option<(CallDesugaringKind, Ty<'tcx>)>,
42 /// Whether the self type of the method call has an `.as_ref()` method.
43 /// Used for better diagnostics.
44 is_option_or_result: bool,
46 /// A call to `Fn(..)::call(..)`, desugared from `my_closure(a, b, c)`
47 FnCall { fn_trait_id: DefId, self_ty: Ty<'tcx> },
48 /// A call to an operator trait, desugared from operator syntax (e.g. `a << b`)
49 Operator { self_arg: Option<Ident>, trait_id: DefId, self_ty: Ty<'tcx> },
51 /// The `Span` of the `Target` associated type
52 /// in the `Deref` impl we are using.
54 /// The type `T::Deref` we are dereferencing to
55 deref_target_ty: Ty<'tcx>,
60 pub fn call_kind<'tcx>(
62 param_env: ParamEnv<'tcx>,
64 method_substs: SubstsRef<'tcx>,
67 self_arg: Option<Ident>,
69 let parent = tcx.opt_associated_item(method_did).and_then(|assoc| {
70 let container_id = assoc.container_id(tcx);
71 match assoc.container {
72 AssocItemContainer::ImplContainer => tcx.trait_id_of_impl(container_id),
73 AssocItemContainer::TraitContainer => Some(container_id),
78 .and_then(|p| tcx.lang_items().group(LangItemGroup::Fn).iter().find(|did| **did == p));
80 let operator = (!from_hir_call)
83 .and_then(|p| tcx.lang_items().group(LangItemGroup::Op).iter().find(|did| **did == p));
85 let is_deref = !from_hir_call && tcx.is_diagnostic_item(sym::deref_method, method_did);
87 // Check for a 'special' use of 'self' -
88 // an FnOnce call, an operator (e.g. `<<`), or a
90 let kind = if let Some(&trait_id) = fn_call {
91 Some(CallKind::FnCall { fn_trait_id: trait_id, self_ty: method_substs.type_at(0) })
92 } else if let Some(&trait_id) = operator {
93 Some(CallKind::Operator { self_arg, trait_id, self_ty: method_substs.type_at(0) })
95 let deref_target = tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
96 Instance::resolve(tcx, param_env, deref_target, method_substs).transpose()
98 if let Some(Ok(instance)) = deref_target {
99 let deref_target_ty = instance.ty(tcx, param_env);
100 Some(CallKind::DerefCoercion {
101 deref_target: tcx.def_span(instance.def_id()),
103 self_ty: method_substs.type_at(0),
112 kind.unwrap_or_else(|| {
113 // This isn't a 'special' use of `self`
114 debug!(?method_did, ?fn_call_span);
115 let desugaring = if Some(method_did) == tcx.lang_items().into_iter_fn()
116 && fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop)
118 Some((CallDesugaringKind::ForLoopIntoIter, method_substs.type_at(0)))
119 } else if fn_call_span.desugaring_kind() == Some(DesugaringKind::QuestionMark) {
120 if Some(method_did) == tcx.lang_items().branch_fn() {
121 Some((CallDesugaringKind::QuestionBranch, method_substs.type_at(0)))
122 } else if Some(method_did) == tcx.lang_items().from_residual_fn() {
123 Some((CallDesugaringKind::QuestionFromResidual, method_substs.type_at(0)))
127 } else if Some(method_did) == tcx.lang_items().from_output_fn()
128 && fn_call_span.desugaring_kind() == Some(DesugaringKind::TryBlock)
130 Some((CallDesugaringKind::TryBlockFromOutput, method_substs.type_at(0)))
134 let parent_did = tcx.parent(method_did);
135 let parent_self_ty = (tcx.def_kind(parent_did) == rustc_hir::def::DefKind::Impl)
136 .then_some(parent_did)
137 .and_then(|did| match tcx.type_of(did).kind() {
138 ty::Adt(def, ..) => Some(def.did()),
141 let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
142 matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
144 CallKind::Normal { self_arg, desugaring, is_option_or_result }