Skip to main content

rustc_trait_selection/error_reporting/traits/
call_kind.rs

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
3//! context.
4
5use rustc_hir::def::DefKind;
6use rustc_hir::def_id::DefId;
7use rustc_hir::{LangItem, lang_items};
8use rustc_middle::ty::{AssocContainer, GenericArgsRef, Instance, Ty, TyCtxt, TypingEnv};
9use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, sym};
10use tracing::debug;
11
12use crate::traits::specialization_graph;
13
14#[derive(#[automatically_derived]
impl ::core::clone::Clone for CallDesugaringKind {
    #[inline]
    fn clone(&self) -> CallDesugaringKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for CallDesugaringKind { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for CallDesugaringKind {
    #[inline]
    fn eq(&self, other: &CallDesugaringKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for CallDesugaringKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for CallDesugaringKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                CallDesugaringKind::ForLoopIntoIter => "ForLoopIntoIter",
                CallDesugaringKind::ForLoopNext => "ForLoopNext",
                CallDesugaringKind::QuestionBranch => "QuestionBranch",
                CallDesugaringKind::QuestionFromResidual =>
                    "QuestionFromResidual",
                CallDesugaringKind::TryBlockFromOutput =>
                    "TryBlockFromOutput",
                CallDesugaringKind::Await => "Await",
            })
    }
}Debug)]
15pub enum CallDesugaringKind {
16    /// for _ in x {} calls x.into_iter()
17    ForLoopIntoIter,
18    /// for _ in x {} calls iter.next()
19    ForLoopNext,
20    /// x? calls x.branch()
21    QuestionBranch,
22    /// x? calls type_of(x)::from_residual()
23    QuestionFromResidual,
24    /// try { ..; x } calls type_of(x)::from_output(x)
25    TryBlockFromOutput,
26    /// `.await` calls `IntoFuture::into_future`
27    Await,
28}
29
30impl CallDesugaringKind {
31    pub fn trait_def_id(self, tcx: TyCtxt<'_>) -> DefId {
32        match self {
33            Self::ForLoopIntoIter => tcx.get_diagnostic_item(sym::IntoIterator).unwrap(),
34            Self::ForLoopNext => tcx.require_lang_item(LangItem::Iterator, DUMMY_SP),
35            Self::QuestionBranch | Self::TryBlockFromOutput => {
36                tcx.require_lang_item(LangItem::Try, DUMMY_SP)
37            }
38            Self::QuestionFromResidual => tcx.get_diagnostic_item(sym::FromResidual).unwrap(),
39            Self::Await => tcx.get_diagnostic_item(sym::IntoFuture).unwrap(),
40        }
41    }
42}
43
44#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for CallKind<'tcx> {
    #[inline]
    fn clone(&self) -> CallKind<'tcx> {
        let _: ::core::clone::AssertParamIsClone<Option<Ident>>;
        let _:
                ::core::clone::AssertParamIsClone<Option<(CallDesugaringKind,
                Ty<'tcx>)>>;
        let _: ::core::clone::AssertParamIsClone<DefId>;
        let _: ::core::clone::AssertParamIsClone<GenericArgsRef<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<Option<Ident>>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<Option<Span>>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for CallKind<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for CallKind<'tcx> {
    #[inline]
    fn eq(&self, other: &CallKind<'tcx>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (CallKind::Normal {
                    self_arg: __self_0,
                    desugaring: __self_1,
                    method_did: __self_2,
                    method_args: __self_3 }, CallKind::Normal {
                    self_arg: __arg1_0,
                    desugaring: __arg1_1,
                    method_did: __arg1_2,
                    method_args: __arg1_3 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                            __self_2 == __arg1_2 && __self_3 == __arg1_3,
                (CallKind::FnCall { fn_trait_id: __self_0, self_ty: __self_1
                    }, CallKind::FnCall {
                    fn_trait_id: __arg1_0, self_ty: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (CallKind::Operator {
                    self_arg: __self_0, trait_id: __self_1, self_ty: __self_2 },
                    CallKind::Operator {
                    self_arg: __arg1_0, trait_id: __arg1_1, self_ty: __arg1_2 })
                    =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                        __self_2 == __arg1_2,
                (CallKind::DerefCoercion {
                    deref_target_span: __self_0,
                    deref_target_ty: __self_1,
                    self_ty: __self_2 }, CallKind::DerefCoercion {
                    deref_target_span: __arg1_0,
                    deref_target_ty: __arg1_1,
                    self_ty: __arg1_2 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                        __self_2 == __arg1_2,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for CallKind<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Option<Ident>>;
        let _:
                ::core::cmp::AssertParamIsEq<Option<(CallDesugaringKind,
                Ty<'tcx>)>>;
        let _: ::core::cmp::AssertParamIsEq<DefId>;
        let _: ::core::cmp::AssertParamIsEq<GenericArgsRef<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<Option<Ident>>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<Option<Span>>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for CallKind<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            CallKind::Normal {
                self_arg: __self_0,
                desugaring: __self_1,
                method_did: __self_2,
                method_args: __self_3 } =>
                ::core::fmt::Formatter::debug_struct_field4_finish(f,
                    "Normal", "self_arg", __self_0, "desugaring", __self_1,
                    "method_did", __self_2, "method_args", &__self_3),
            CallKind::FnCall { fn_trait_id: __self_0, self_ty: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "FnCall", "fn_trait_id", __self_0, "self_ty", &__self_1),
            CallKind::Operator {
                self_arg: __self_0, trait_id: __self_1, self_ty: __self_2 } =>
                ::core::fmt::Formatter::debug_struct_field3_finish(f,
                    "Operator", "self_arg", __self_0, "trait_id", __self_1,
                    "self_ty", &__self_2),
            CallKind::DerefCoercion {
                deref_target_span: __self_0,
                deref_target_ty: __self_1,
                self_ty: __self_2 } =>
                ::core::fmt::Formatter::debug_struct_field3_finish(f,
                    "DerefCoercion", "deref_target_span", __self_0,
                    "deref_target_ty", __self_1, "self_ty", &__self_2),
        }
    }
}Debug)]
45pub enum CallKind<'tcx> {
46    /// A normal method call of the form `receiver.foo(a, b, c)`
47    Normal {
48        self_arg: Option<Ident>,
49        desugaring: Option<(CallDesugaringKind, Ty<'tcx>)>,
50        method_did: DefId,
51        method_args: GenericArgsRef<'tcx>,
52    },
53    /// A call to `Fn(..)::call(..)`, desugared from `my_closure(a, b, c)`
54    FnCall { fn_trait_id: DefId, self_ty: Ty<'tcx> },
55    /// A call to an operator trait, desugared from operator syntax (e.g. `a << b`)
56    Operator { self_arg: Option<Ident>, trait_id: DefId, self_ty: Ty<'tcx> },
57    DerefCoercion {
58        /// The `Span` of the `Target` associated type
59        /// in the `Deref` impl we are using.
60        deref_target_span: Option<Span>,
61        /// The type `T::Deref` we are dereferencing to
62        deref_target_ty: Ty<'tcx>,
63        self_ty: Ty<'tcx>,
64    },
65}
66
67pub fn call_kind<'tcx>(
68    tcx: TyCtxt<'tcx>,
69    typing_env: TypingEnv<'tcx>,
70    method_did: DefId,
71    method_args: GenericArgsRef<'tcx>,
72    fn_call_span: Span,
73    from_hir_call: bool,
74    self_arg: Option<Ident>,
75) -> CallKind<'tcx> {
76    let parent = tcx.opt_associated_item(method_did).and_then(|assoc| {
77        let container_id = assoc.container_id(tcx);
78        match assoc.container {
79            AssocContainer::InherentImpl => None,
80            AssocContainer::TraitImpl(_) => Some(tcx.impl_trait_id(container_id)),
81            AssocContainer::Trait => Some(container_id),
82        }
83    });
84
85    let fn_call = parent.filter(|&p| tcx.fn_trait_kind_from_def_id(p).is_some());
86
87    let operator = if !from_hir_call && let Some(p) = parent {
88        lang_items::OPERATORS.iter().filter_map(|&l| tcx.lang_items().get(l)).find(|&id| id == p)
89    } else {
90        None
91    };
92
93    // Check for a 'special' use of 'self' -
94    // an FnOnce call, an operator (e.g. `<<`), or a
95    // deref coercion.
96    if let Some(trait_id) = fn_call {
97        return CallKind::FnCall { fn_trait_id: trait_id, self_ty: method_args.type_at(0) };
98    } else if let Some(trait_id) = operator {
99        return CallKind::Operator { self_arg, trait_id, self_ty: method_args.type_at(0) };
100    } else if !from_hir_call && tcx.is_diagnostic_item(sym::deref_method, method_did) {
101        let deref_target_def_id =
102            tcx.get_diagnostic_item(sym::deref_target).expect("deref method but no deref target");
103        let deref_target_ty = tcx.normalize_erasing_regions(
104            typing_env,
105            Ty::new_projection(tcx, deref_target_def_id, method_args),
106        );
107        let deref_target_span = if let Ok(Some(instance)) =
108            Instance::try_resolve(tcx, typing_env, method_did, method_args)
109            && let instance_parent_def_id = tcx.parent(instance.def_id())
110            && #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(instance_parent_def_id)
    {
    DefKind::Impl { .. } => true,
    _ => false,
}matches!(tcx.def_kind(instance_parent_def_id), DefKind::Impl { .. })
111            && let Ok(instance) =
112                specialization_graph::assoc_def(tcx, instance_parent_def_id, deref_target_def_id)
113            && instance.is_final()
114        {
115            Some(tcx.def_span(instance.item.def_id))
116        } else {
117            None
118        };
119        return CallKind::DerefCoercion {
120            deref_target_ty,
121            deref_target_span,
122            self_ty: method_args.type_at(0),
123        };
124    }
125
126    // This isn't a 'special' use of `self`
127    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/call_kind.rs:127",
                        "rustc_trait_selection::error_reporting::traits::call_kind",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/call_kind.rs"),
                        ::tracing_core::__macro_support::Option::Some(127u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::call_kind"),
                        ::tracing_core::field::FieldSet::new(&["method_did",
                                        "fn_call_span"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&method_did)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&fn_call_span)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?method_did, ?fn_call_span);
128    let desugaring = if tcx.is_lang_item(method_did, LangItem::IntoIterIntoIter)
129        && fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop)
130    {
131        Some((CallDesugaringKind::ForLoopIntoIter, method_args.type_at(0)))
132    } else if tcx.is_lang_item(method_did, LangItem::IteratorNext)
133        && fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop)
134    {
135        Some((CallDesugaringKind::ForLoopNext, method_args.type_at(0)))
136    } else if fn_call_span.desugaring_kind() == Some(DesugaringKind::QuestionMark) {
137        if tcx.is_lang_item(method_did, LangItem::TryTraitBranch) {
138            Some((CallDesugaringKind::QuestionBranch, method_args.type_at(0)))
139        } else if tcx.is_lang_item(method_did, LangItem::TryTraitFromResidual) {
140            Some((CallDesugaringKind::QuestionFromResidual, method_args.type_at(0)))
141        } else {
142            None
143        }
144    } else if tcx.is_lang_item(method_did, LangItem::TryTraitFromOutput)
145        && fn_call_span.desugaring_kind() == Some(DesugaringKind::TryBlock)
146    {
147        Some((CallDesugaringKind::TryBlockFromOutput, method_args.type_at(0)))
148    } else if fn_call_span.is_desugaring(DesugaringKind::Await) {
149        Some((CallDesugaringKind::Await, method_args.type_at(0)))
150    } else {
151        None
152    };
153    CallKind::Normal { self_arg, desugaring, method_did, method_args }
154}