Skip to main content

rustc_hir_typeck/method/
mod.rs

1//! Method lookup: the secret sauce of Rust. See the [rustc dev guide] for more information.
2//!
3//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/method-lookup.html
4
5mod confirm;
6mod prelude_edition_lints;
7pub(crate) mod probe;
8mod suggest;
9
10use rustc_errors::{Applicability, Diag, SubdiagMessage};
11use rustc_hir as hir;
12use rustc_hir::def::{CtorOf, DefKind, Namespace};
13use rustc_hir::def_id::DefId;
14use rustc_infer::infer::{BoundRegionConversionTime, InferOk};
15use rustc_infer::traits::PredicateObligations;
16use rustc_middle::traits::ObligationCause;
17use rustc_middle::ty::{
18    self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TypeVisitableExt,
19};
20use rustc_middle::{bug, span_bug};
21use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
22use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
23use rustc_trait_selection::traits::{self, NormalizeExt};
24use tracing::{debug, instrument};
25
26pub(crate) use self::MethodError::*;
27use self::probe::{IsSuggestion, ProbeScope};
28use crate::FnCtxt;
29use crate::method::probe::UnsatisfiedPredicates;
30
31#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for MethodCallee<'tcx> {
    #[inline]
    fn clone(&self) -> MethodCallee<'tcx> {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        let _: ::core::clone::AssertParamIsClone<GenericArgsRef<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<ty::FnSig<'tcx>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for MethodCallee<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for MethodCallee<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "MethodCallee",
            "def_id", &self.def_id, "args", &self.args, "sig", &&self.sig)
    }
}Debug)]
32pub(crate) struct MethodCallee<'tcx> {
33    /// Impl method ID, for inherent methods, or trait method ID, otherwise.
34    pub def_id: DefId,
35    pub args: GenericArgsRef<'tcx>,
36
37    /// Instantiated method signature, i.e., it has been
38    /// instantiated, normalized, and has had late-bound
39    /// lifetimes replaced with inference variables.
40    pub sig: ty::FnSig<'tcx>,
41}
42
43#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for MethodError<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            MethodError::NoMatch(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "NoMatch", &__self_0),
            MethodError::Ambiguity(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Ambiguity", &__self_0),
            MethodError::PrivateMatch(__self_0, __self_1, __self_2) =>
                ::core::fmt::Formatter::debug_tuple_field3_finish(f,
                    "PrivateMatch", __self_0, __self_1, &__self_2),
            MethodError::IllegalSizedBound {
                candidates: __self_0,
                needs_mut: __self_1,
                bound_span: __self_2,
                self_expr: __self_3 } =>
                ::core::fmt::Formatter::debug_struct_field4_finish(f,
                    "IllegalSizedBound", "candidates", __self_0, "needs_mut",
                    __self_1, "bound_span", __self_2, "self_expr", &__self_3),
            MethodError::BadReturnType =>
                ::core::fmt::Formatter::write_str(f, "BadReturnType"),
            MethodError::ErrorReported(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "ErrorReported", &__self_0),
        }
    }
}Debug)]
44pub(crate) enum MethodError<'tcx> {
45    /// Did not find an applicable method, but we did find various near-misses that may work.
46    NoMatch(NoMatchData<'tcx>),
47
48    /// Multiple methods might apply.
49    Ambiguity(Vec<CandidateSource>),
50
51    /// Found an applicable method, but it is not visible. The third argument contains a list of
52    /// not-in-scope traits which may work.
53    PrivateMatch(DefKind, DefId, Vec<DefId>),
54
55    /// Found a `Self: Sized` bound where `Self` is a trait object.
56    IllegalSizedBound {
57        candidates: Vec<DefId>,
58        needs_mut: bool,
59        bound_span: Span,
60        self_expr: &'tcx hir::Expr<'tcx>,
61    },
62
63    /// Found a match, but the return type is wrong
64    BadReturnType,
65
66    /// Error has already been emitted, no need to emit another one.
67    ErrorReported(ErrorGuaranteed),
68}
69
70// Contains a list of static methods that may apply, a list of unsatisfied trait predicates which
71// could lead to matches if satisfied, and a list of not-in-scope traits which may work.
72#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for NoMatchData<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field5_finish(f, "NoMatchData",
            "static_candidates", &self.static_candidates,
            "unsatisfied_predicates", &self.unsatisfied_predicates,
            "out_of_scope_traits", &self.out_of_scope_traits,
            "similar_candidate", &self.similar_candidate, "mode", &&self.mode)
    }
}Debug)]
73pub(crate) struct NoMatchData<'tcx> {
74    pub static_candidates: Vec<CandidateSource>,
75    pub unsatisfied_predicates: UnsatisfiedPredicates<'tcx>,
76    pub out_of_scope_traits: Vec<DefId>,
77    pub similar_candidate: Option<ty::AssocItem>,
78    pub mode: probe::Mode,
79}
80
81// A pared down enum describing just the places from which a method
82// candidate can arise. Used for error reporting only.
83#[derive(#[automatically_derived]
impl ::core::marker::Copy for CandidateSource { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CandidateSource {
    #[inline]
    fn clone(&self) -> CandidateSource {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for CandidateSource {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            CandidateSource::Impl(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Impl",
                    &__self_0),
            CandidateSource::Trait(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Trait",
                    &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for CandidateSource {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<DefId>;
    }
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for CandidateSource {
    #[inline]
    fn eq(&self, other: &CandidateSource) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (CandidateSource::Impl(__self_0),
                    CandidateSource::Impl(__arg1_0)) => __self_0 == __arg1_0,
                (CandidateSource::Trait(__self_0),
                    CandidateSource::Trait(__arg1_0)) => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq)]
84pub(crate) enum CandidateSource {
85    Impl(DefId),
86    Trait(DefId /* trait id */),
87}
88
89impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
90    /// Determines whether the type `self_ty` supports a visible method named `method_name` or not.
91    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("method_exists_for_diagnostic",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(91u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["method_name",
                                                    "self_ty", "call_expr_id", "return_type"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&method_name)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&call_expr_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&return_type)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: bool = loop {};
            return __tracing_attr_fake_return;
        }
        {
            match self.probe_for_name(probe::Mode::MethodCall, method_name,
                    return_type, IsSuggestion(true), self_ty, call_expr_id,
                    ProbeScope::TraitsInScope) {
                Ok(pick) => {
                    pick.maybe_emit_unstable_name_collision_hint(self.tcx,
                        method_name.span, call_expr_id);
                    true
                }
                Err(NoMatch(..)) => false,
                Err(Ambiguity(..)) => true,
                Err(PrivateMatch(..)) => false,
                Err(IllegalSizedBound { .. }) => true,
                Err(BadReturnType) => true,
                Err(ErrorReported(_)) => false,
            }
        }
    }
}#[instrument(level = "debug", skip(self))]
92    pub(crate) fn method_exists_for_diagnostic(
93        &self,
94        method_name: Ident,
95        self_ty: Ty<'tcx>,
96        call_expr_id: hir::HirId,
97        return_type: Option<Ty<'tcx>>,
98    ) -> bool {
99        match self.probe_for_name(
100            probe::Mode::MethodCall,
101            method_name,
102            return_type,
103            IsSuggestion(true),
104            self_ty,
105            call_expr_id,
106            ProbeScope::TraitsInScope,
107        ) {
108            Ok(pick) => {
109                pick.maybe_emit_unstable_name_collision_hint(
110                    self.tcx,
111                    method_name.span,
112                    call_expr_id,
113                );
114                true
115            }
116            Err(NoMatch(..)) => false,
117            Err(Ambiguity(..)) => true,
118            Err(PrivateMatch(..)) => false,
119            Err(IllegalSizedBound { .. }) => true,
120            Err(BadReturnType) => true,
121            Err(ErrorReported(_)) => false,
122        }
123    }
124
125    /// Adds a suggestion to call the given method to the provided diagnostic.
126    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("suggest_method_call",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(126u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["msg", "method_name",
                                                    "self_ty", "span"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&msg)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&method_name)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let params =
                self.lookup_probe_for_diagnostic(method_name, self_ty,
                            call_expr, ProbeScope::TraitsInScope,
                            None).map(|pick|
                            {
                                let sig = self.tcx.fn_sig(pick.item.def_id);
                                sig.skip_binder().inputs().skip_binder().len().saturating_sub(1)
                            }).unwrap_or(0);
            let sugg_span = span.unwrap_or(call_expr.span).shrink_to_hi();
            let (suggestion, applicability) =
                (::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("({0})",
                                    (0..params).map(|_| "_").collect::<Vec<_>>().join(", ")))
                        }),
                    if params > 0 {
                        Applicability::HasPlaceholders
                    } else { Applicability::MaybeIncorrect });
            err.span_suggestion_verbose(sugg_span, msg, suggestion,
                applicability);
        }
    }
}#[instrument(level = "debug", skip(self, err, call_expr))]
127    pub(crate) fn suggest_method_call(
128        &self,
129        err: &mut Diag<'_>,
130        msg: impl Into<SubdiagMessage> + std::fmt::Debug,
131        method_name: Ident,
132        self_ty: Ty<'tcx>,
133        call_expr: &hir::Expr<'tcx>,
134        span: Option<Span>,
135    ) {
136        let params = self
137            .lookup_probe_for_diagnostic(
138                method_name,
139                self_ty,
140                call_expr,
141                ProbeScope::TraitsInScope,
142                None,
143            )
144            .map(|pick| {
145                let sig = self.tcx.fn_sig(pick.item.def_id);
146                sig.skip_binder().inputs().skip_binder().len().saturating_sub(1)
147            })
148            .unwrap_or(0);
149
150        // Account for `foo.bar<T>`;
151        let sugg_span = span.unwrap_or(call_expr.span).shrink_to_hi();
152        let (suggestion, applicability) = (
153            format!("({})", (0..params).map(|_| "_").collect::<Vec<_>>().join(", ")),
154            if params > 0 { Applicability::HasPlaceholders } else { Applicability::MaybeIncorrect },
155        );
156
157        err.span_suggestion_verbose(sugg_span, msg, suggestion, applicability);
158    }
159
160    /// Performs method lookup. If lookup is successful, it will return the callee
161    /// and store an appropriate adjustment for the self-expr. In some cases it may
162    /// report an error (e.g., invoking the `drop` method).
163    ///
164    /// # Arguments
165    ///
166    /// Given a method call like `foo.bar::<T1,...Tn>(a, b + 1, ...)`:
167    ///
168    /// * `self`:                  the surrounding `FnCtxt` (!)
169    /// * `self_ty`:               the (unadjusted) type of the self expression (`foo`)
170    /// * `segment`:               the name and generic arguments of the method (`bar::<T1, ...Tn>`)
171    /// * `span`:                  the span for the method call
172    /// * `call_expr`:             the complete method call: (`foo.bar::<T1,...Tn>(...)`)
173    /// * `self_expr`:             the self expression (`foo`)
174    /// * `args`:                  the expressions of the arguments (`a, b + 1, ...`)
175    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lookup_method",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(175u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["self_ty", "segment",
                                                    "span", "call_expr", "self_expr", "args"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&segment)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&call_expr)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_expr)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&args)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<MethodCallee<'tcx>, MethodError<'tcx>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let scope =
                if let Some(only_method) = segment.res.opt_def_id() {
                    ProbeScope::Single(only_method)
                } else { ProbeScope::TraitsInScope };
            let pick =
                self.lookup_probe(segment.ident, self_ty, call_expr, scope)?;
            self.lint_edition_dependent_dot_call(self_ty, segment, span,
                call_expr, self_expr, &pick, args);
            for &import_id in &pick.import_ids {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/mod.rs:200",
                                        "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                        ::tracing_core::__macro_support::Option::Some(200u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                        ::tracing_core::field::FieldSet::new(&["message"],
                                            ::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(&format_args!("used_trait_import: {0:?}",
                                                                    import_id) as &dyn Value))])
                            });
                    } else { ; }
                };
                self.typeck_results.borrow_mut().used_trait_imports.insert(import_id);
            }
            self.tcx.check_stability(pick.item.def_id, Some(call_expr.hir_id),
                span, None);
            let result =
                self.confirm_method(span, self_expr, call_expr, self_ty,
                    &pick, segment);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/mod.rs:207",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(207u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::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(&format_args!("result = {0:?}",
                                                                result) as &dyn Value))])
                        });
                } else { ; }
            };
            if let Some(span) = result.illegal_sized_bound {
                let mut needs_mut = false;
                if let ty::Ref(region, t_type, mutability) = self_ty.kind() {
                    let trait_type =
                        Ty::new_ref(self.tcx, *region, *t_type,
                            mutability.invert());
                    match self.lookup_probe(segment.ident, trait_type,
                            call_expr, ProbeScope::TraitsInScope) {
                        Ok(ref new_pick) if pick.differs_from(new_pick) => {
                            needs_mut =
                                new_pick.self_ty.ref_mutability() !=
                                    self_ty.ref_mutability();
                        }
                        _ => {}
                    }
                }
                let candidates =
                    match self.lookup_probe_for_diagnostic(segment.ident,
                            self_ty, call_expr, ProbeScope::AllTraits, None) {
                        Ok(ref new_pick) if pick.differs_from(new_pick) => {
                            <[_]>::into_vec(::alloc::boxed::box_new([new_pick.item.container_id(self.tcx)]))
                        }
                        Err(Ambiguity(ref sources)) =>
                            sources.iter().filter_map(|source|
                                        {
                                            match *source {
                                                CandidateSource::Impl(def) =>
                                                    Some(self.tcx.impl_trait_id(def)),
                                                CandidateSource::Trait(_) => None,
                                            }
                                        }).collect(),
                        _ => Vec::new(),
                    };
                return Err(IllegalSizedBound {
                            candidates,
                            needs_mut,
                            bound_span: span,
                            self_expr,
                        });
            }
            Ok(result.callee)
        }
    }
}#[instrument(level = "debug", skip(self))]
176    pub(crate) fn lookup_method(
177        &self,
178        self_ty: Ty<'tcx>,
179        segment: &'tcx hir::PathSegment<'tcx>,
180        span: Span,
181        call_expr: &'tcx hir::Expr<'tcx>,
182        self_expr: &'tcx hir::Expr<'tcx>,
183        args: &'tcx [hir::Expr<'tcx>],
184    ) -> Result<MethodCallee<'tcx>, MethodError<'tcx>> {
185        let scope = if let Some(only_method) = segment.res.opt_def_id() {
186            ProbeScope::Single(only_method)
187        } else {
188            ProbeScope::TraitsInScope
189        };
190
191        let pick = self.lookup_probe(segment.ident, self_ty, call_expr, scope)?;
192
193        self.lint_edition_dependent_dot_call(
194            self_ty, segment, span, call_expr, self_expr, &pick, args,
195        );
196
197        // NOTE: on the failure path, we also record the possibly-used trait methods
198        // since an unused import warning is kinda distracting from the method error.
199        for &import_id in &pick.import_ids {
200            debug!("used_trait_import: {:?}", import_id);
201            self.typeck_results.borrow_mut().used_trait_imports.insert(import_id);
202        }
203
204        self.tcx.check_stability(pick.item.def_id, Some(call_expr.hir_id), span, None);
205
206        let result = self.confirm_method(span, self_expr, call_expr, self_ty, &pick, segment);
207        debug!("result = {:?}", result);
208
209        if let Some(span) = result.illegal_sized_bound {
210            let mut needs_mut = false;
211            if let ty::Ref(region, t_type, mutability) = self_ty.kind() {
212                let trait_type = Ty::new_ref(self.tcx, *region, *t_type, mutability.invert());
213                // We probe again to see if there might be a borrow mutability discrepancy.
214                match self.lookup_probe(
215                    segment.ident,
216                    trait_type,
217                    call_expr,
218                    ProbeScope::TraitsInScope,
219                ) {
220                    Ok(ref new_pick) if pick.differs_from(new_pick) => {
221                        needs_mut = new_pick.self_ty.ref_mutability() != self_ty.ref_mutability();
222                    }
223                    _ => {}
224                }
225            }
226
227            // We probe again, taking all traits into account (not only those in scope).
228            let candidates = match self.lookup_probe_for_diagnostic(
229                segment.ident,
230                self_ty,
231                call_expr,
232                ProbeScope::AllTraits,
233                None,
234            ) {
235                // If we find a different result the caller probably forgot to import a trait.
236                Ok(ref new_pick) if pick.differs_from(new_pick) => {
237                    vec![new_pick.item.container_id(self.tcx)]
238                }
239                Err(Ambiguity(ref sources)) => sources
240                    .iter()
241                    .filter_map(|source| {
242                        match *source {
243                            // Note: this cannot come from an inherent impl,
244                            // because the first probing succeeded.
245                            CandidateSource::Impl(def) => Some(self.tcx.impl_trait_id(def)),
246                            CandidateSource::Trait(_) => None,
247                        }
248                    })
249                    .collect(),
250                _ => Vec::new(),
251            };
252
253            return Err(IllegalSizedBound { candidates, needs_mut, bound_span: span, self_expr });
254        }
255
256        Ok(result.callee)
257    }
258
259    pub(crate) fn lookup_method_for_diagnostic(
260        &self,
261        self_ty: Ty<'tcx>,
262        segment: &hir::PathSegment<'tcx>,
263        span: Span,
264        call_expr: &'tcx hir::Expr<'tcx>,
265        self_expr: &'tcx hir::Expr<'tcx>,
266    ) -> Result<MethodCallee<'tcx>, MethodError<'tcx>> {
267        let pick = self.lookup_probe_for_diagnostic(
268            segment.ident,
269            self_ty,
270            call_expr,
271            ProbeScope::TraitsInScope,
272            None,
273        )?;
274
275        Ok(self
276            .confirm_method_for_diagnostic(span, self_expr, call_expr, self_ty, &pick, segment)
277            .callee)
278    }
279
280    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lookup_probe",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(280u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["method_name",
                                                    "self_ty", "scope"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&method_name)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&scope)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: probe::PickResult<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let pick =
                self.probe_for_name(probe::Mode::MethodCall, method_name,
                        None, IsSuggestion(false), self_ty, call_expr.hir_id,
                        scope)?;
            pick.maybe_emit_unstable_name_collision_hint(self.tcx,
                method_name.span, call_expr.hir_id);
            Ok(pick)
        }
    }
}#[instrument(level = "debug", skip(self, call_expr))]
281    pub(crate) fn lookup_probe(
282        &self,
283        method_name: Ident,
284        self_ty: Ty<'tcx>,
285        call_expr: &hir::Expr<'_>,
286        scope: ProbeScope,
287    ) -> probe::PickResult<'tcx> {
288        let pick = self.probe_for_name(
289            probe::Mode::MethodCall,
290            method_name,
291            None,
292            IsSuggestion(false),
293            self_ty,
294            call_expr.hir_id,
295            scope,
296        )?;
297        pick.maybe_emit_unstable_name_collision_hint(self.tcx, method_name.span, call_expr.hir_id);
298        Ok(pick)
299    }
300
301    pub(crate) fn lookup_probe_for_diagnostic(
302        &self,
303        method_name: Ident,
304        self_ty: Ty<'tcx>,
305        call_expr: &hir::Expr<'_>,
306        scope: ProbeScope,
307        return_type: Option<Ty<'tcx>>,
308    ) -> probe::PickResult<'tcx> {
309        let pick = self.probe_for_name(
310            probe::Mode::MethodCall,
311            method_name,
312            return_type,
313            IsSuggestion(true),
314            self_ty,
315            call_expr.hir_id,
316            scope,
317        )?;
318        Ok(pick)
319    }
320}
321
322/// Used by [FnCtxt::lookup_method_for_operator] with `-Znext-solver`.
323///
324/// With `AsRigid` we error on `impl Opaque: NotInItemBounds` while
325/// `AsInfer` just treats it as ambiguous and succeeds. This is necessary
326/// as we want [FnCtxt::check_expr_call] to treat not-yet-defined opaque
327/// types as rigid to support `impl Deref<Target = impl FnOnce()>` and
328/// `Box<impl FnOnce()>`.
329///
330/// We only want to treat opaque types as rigid if we need to eagerly choose
331/// between multiple candidates. We otherwise treat them as ordinary inference
332/// variable to avoid rejecting otherwise correct code.
333#[derive(#[automatically_derived]
impl ::core::fmt::Debug for TreatNotYetDefinedOpaques {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                TreatNotYetDefinedOpaques::AsInfer => "AsInfer",
                TreatNotYetDefinedOpaques::AsRigid => "AsRigid",
            })
    }
}Debug)]
334pub(super) enum TreatNotYetDefinedOpaques {
335    AsInfer,
336    AsRigid,
337}
338
339impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
340    /// `lookup_method_in_trait` is used for overloaded operators.
341    /// It does a very narrow slice of what the normal probe/confirm path does.
342    /// In particular, it doesn't really do any probing: it simply constructs
343    /// an obligation for a particular trait with the given self type and checks
344    /// whether that trait is implemented.
345    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lookup_method_for_operator",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(345u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["cause",
                                                    "method_name", "trait_def_id", "self_ty", "opt_rhs_ty",
                                                    "treat_opaques"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&cause)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&method_name)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&trait_def_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&opt_rhs_ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&treat_opaques)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Option<InferOk<'tcx, MethodCallee<'tcx>>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let args =
                GenericArgs::for_item(self.tcx, trait_def_id,
                    |param, _|
                        match param.kind {
                            GenericParamDefKind::Lifetime | GenericParamDefKind::Const {
                                .. } => {
                                {
                                    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                            format_args!("did not expect operator trait to have lifetime/const")));
                                }
                            }
                            GenericParamDefKind::Type { .. } => {
                                if param.index == 0 {
                                    self_ty.into()
                                } else if let Some(rhs_ty) = opt_rhs_ty {
                                    match (&param.index, &1) {
                                        (left_val, right_val) => {
                                            if !(*left_val == *right_val) {
                                                let kind = ::core::panicking::AssertKind::Eq;
                                                ::core::panicking::assert_failed(kind, &*left_val,
                                                    &*right_val,
                                                    ::core::option::Option::Some(format_args!("did not expect >1 param on operator trait")));
                                            }
                                        }
                                    };
                                    rhs_ty.into()
                                } else { self.var_for_def(cause.span, param) }
                            }
                        });
            let obligation =
                traits::Obligation::new(self.tcx, cause, self.param_env,
                    ty::TraitRef::new_from_args(self.tcx, trait_def_id, args));
            let matches_trait =
                match treat_opaques {
                    TreatNotYetDefinedOpaques::AsInfer =>
                        self.predicate_may_hold(&obligation),
                    TreatNotYetDefinedOpaques::AsRigid => {
                        self.predicate_may_hold_opaque_types_jank(&obligation)
                    }
                };
            if !matches_trait {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/mod.rs:391",
                                        "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                        ::tracing_core::__macro_support::Option::Some(391u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                        ::tracing_core::field::FieldSet::new(&["message"],
                                            ::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(&format_args!("--> Cannot match obligation")
                                                            as &dyn Value))])
                            });
                    } else { ; }
                };
                return None;
            }
            let tcx = self.tcx;
            let Some(method_item) =
                self.associated_value(trait_def_id,
                    Ident::with_dummy_span(method_name)) else {
                    ::rustc_middle::util::bug::bug_fmt(format_args!("expected associated item for operator trait"))
                };
            let def_id = method_item.def_id;
            if !method_item.is_fn() {
                ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(def_id),
                    format_args!("expected `{0}` to be an associated function",
                        method_name));
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/mod.rs:415",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(415u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::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(&format_args!("lookup_in_trait_adjusted: method_item={0:?}",
                                                                method_item) as &dyn Value))])
                        });
                } else { ; }
            };
            let mut obligations = PredicateObligations::new();
            let fn_sig = tcx.fn_sig(def_id).instantiate(self.tcx, args);
            let fn_sig =
                self.instantiate_binder_with_fresh_vars(obligation.cause.span,
                    BoundRegionConversionTime::FnCall, fn_sig);
            let InferOk { value: fn_sig, obligations: o } =
                self.at(&obligation.cause, self.param_env).normalize(fn_sig);
            obligations.extend(o);
            let bounds =
                self.tcx.predicates_of(def_id).instantiate(self.tcx, args);
            let InferOk { value: bounds, obligations: o } =
                self.at(&obligation.cause, self.param_env).normalize(bounds);
            obligations.extend(o);
            if !!bounds.has_escaping_bound_vars() {
                ::core::panicking::panic("assertion failed: !bounds.has_escaping_bound_vars()")
            };
            let predicates_cause = obligation.cause.clone();
            obligations.extend(traits::predicates_for_generics(move |_, _|
                        predicates_cause.clone(), self.param_env, bounds));
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/mod.rs:458",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(458u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::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(&format_args!("lookup_method_in_trait: matched method fn_sig={0:?} obligation={1:?}",
                                                                fn_sig, obligation) as &dyn Value))])
                        });
                } else { ; }
            };
            for ty in fn_sig.inputs_and_output {
                obligations.push(traits::Obligation::new(tcx,
                        obligation.cause.clone(), self.param_env,
                        ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(ty.into())))));
            }
            let callee = MethodCallee { def_id, args, sig: fn_sig };
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/method/mod.rs:472",
                                    "rustc_hir_typeck::method", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(472u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::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(&format_args!("callee = {0:?}",
                                                                callee) as &dyn Value))])
                        });
                } else { ; }
            };
            Some(InferOk { obligations, value: callee })
        }
    }
}#[instrument(level = "debug", skip(self))]
346    pub(super) fn lookup_method_for_operator(
347        &self,
348        cause: ObligationCause<'tcx>,
349        method_name: Symbol,
350        trait_def_id: DefId,
351        self_ty: Ty<'tcx>,
352        opt_rhs_ty: Option<Ty<'tcx>>,
353        treat_opaques: TreatNotYetDefinedOpaques,
354    ) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
355        // Construct a trait-reference `self_ty : Trait<input_tys>`
356        let args = GenericArgs::for_item(self.tcx, trait_def_id, |param, _| match param.kind {
357            GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => {
358                unreachable!("did not expect operator trait to have lifetime/const")
359            }
360            GenericParamDefKind::Type { .. } => {
361                if param.index == 0 {
362                    self_ty.into()
363                } else if let Some(rhs_ty) = opt_rhs_ty {
364                    assert_eq!(param.index, 1, "did not expect >1 param on operator trait");
365                    rhs_ty.into()
366                } else {
367                    // FIXME: We should stop passing `None` for the failure case
368                    // when probing for call exprs. I.e. `opt_rhs_ty` should always
369                    // be set when it needs to be.
370                    self.var_for_def(cause.span, param)
371                }
372            }
373        });
374
375        let obligation = traits::Obligation::new(
376            self.tcx,
377            cause,
378            self.param_env,
379            ty::TraitRef::new_from_args(self.tcx, trait_def_id, args),
380        );
381
382        // Now we want to know if this can be matched
383        let matches_trait = match treat_opaques {
384            TreatNotYetDefinedOpaques::AsInfer => self.predicate_may_hold(&obligation),
385            TreatNotYetDefinedOpaques::AsRigid => {
386                self.predicate_may_hold_opaque_types_jank(&obligation)
387            }
388        };
389
390        if !matches_trait {
391            debug!("--> Cannot match obligation");
392            // Cannot be matched, no such method resolution is possible.
393            return None;
394        }
395
396        // Trait must have a method named `m_name` and it should not have
397        // type parameters or early-bound regions.
398        let tcx = self.tcx;
399        // We use `Ident::with_dummy_span` since no built-in operator methods have
400        // any macro-specific hygiene, so the span's context doesn't really matter.
401        let Some(method_item) =
402            self.associated_value(trait_def_id, Ident::with_dummy_span(method_name))
403        else {
404            bug!("expected associated item for operator trait")
405        };
406
407        let def_id = method_item.def_id;
408        if !method_item.is_fn() {
409            span_bug!(
410                tcx.def_span(def_id),
411                "expected `{method_name}` to be an associated function"
412            );
413        }
414
415        debug!("lookup_in_trait_adjusted: method_item={:?}", method_item);
416        let mut obligations = PredicateObligations::new();
417
418        // Instantiate late-bound regions and instantiate the trait
419        // parameters into the method type to get the actual method type.
420        //
421        // N.B., instantiate late-bound regions before normalizing the
422        // function signature so that normalization does not need to deal
423        // with bound regions.
424        let fn_sig = tcx.fn_sig(def_id).instantiate(self.tcx, args);
425        let fn_sig = self.instantiate_binder_with_fresh_vars(
426            obligation.cause.span,
427            BoundRegionConversionTime::FnCall,
428            fn_sig,
429        );
430
431        let InferOk { value: fn_sig, obligations: o } =
432            self.at(&obligation.cause, self.param_env).normalize(fn_sig);
433        obligations.extend(o);
434
435        // Register obligations for the parameters. This will include the
436        // `Self` parameter, which in turn has a bound of the main trait,
437        // so this also effectively registers `obligation` as well. (We
438        // used to register `obligation` explicitly, but that resulted in
439        // double error messages being reported.)
440        //
441        // Note that as the method comes from a trait, it should not have
442        // any late-bound regions appearing in its bounds.
443        let bounds = self.tcx.predicates_of(def_id).instantiate(self.tcx, args);
444
445        let InferOk { value: bounds, obligations: o } =
446            self.at(&obligation.cause, self.param_env).normalize(bounds);
447        obligations.extend(o);
448        assert!(!bounds.has_escaping_bound_vars());
449
450        let predicates_cause = obligation.cause.clone();
451        obligations.extend(traits::predicates_for_generics(
452            move |_, _| predicates_cause.clone(),
453            self.param_env,
454            bounds,
455        ));
456
457        // Also add an obligation for the method type being well-formed.
458        debug!(
459            "lookup_method_in_trait: matched method fn_sig={:?} obligation={:?}",
460            fn_sig, obligation
461        );
462        for ty in fn_sig.inputs_and_output {
463            obligations.push(traits::Obligation::new(
464                tcx,
465                obligation.cause.clone(),
466                self.param_env,
467                ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(ty.into()))),
468            ));
469        }
470
471        let callee = MethodCallee { def_id, args, sig: fn_sig };
472        debug!("callee = {:?}", callee);
473
474        Some(InferOk { obligations, value: callee })
475    }
476
477    /// Performs a [full-qualified function call] (formerly "universal function call") lookup. If
478    /// lookup is successful, it will return the type of definition and the [`DefId`] of the found
479    /// function definition.
480    ///
481    /// [full-qualified function call]: https://doc.rust-lang.org/reference/expressions/call-expr.html#disambiguating-function-calls
482    ///
483    /// # Arguments
484    ///
485    /// Given a function call like `Foo::bar::<T1,...Tn>(...)`:
486    ///
487    /// * `self`:                  the surrounding `FnCtxt` (!)
488    /// * `span`:                  the span of the call, excluding arguments (`Foo::bar::<T1, ...Tn>`)
489    /// * `method_name`:           the identifier of the function within the container type (`bar`)
490    /// * `self_ty`:               the type to search within (`Foo`)
491    /// * `self_ty_span`           the span for the type being searched within (span of `Foo`)
492    /// * `expr_id`:               the [`hir::HirId`] of the expression composing the entire call
493    x;#[instrument(level = "debug", skip(self), ret)]
494    pub(crate) fn resolve_fully_qualified_call(
495        &self,
496        span: Span,
497        method_name: Ident,
498        self_ty: Ty<'tcx>,
499        self_ty_span: Span,
500        expr_id: hir::HirId,
501    ) -> Result<(DefKind, DefId), MethodError<'tcx>> {
502        let tcx = self.tcx;
503
504        // Check if we have an enum variant.
505        let mut struct_variant = None;
506        if let ty::Adt(adt_def, _) = self_ty.kind() {
507            if adt_def.is_enum() {
508                let variant_def = adt_def
509                    .variants()
510                    .iter()
511                    .find(|vd| tcx.hygienic_eq(method_name, vd.ident(tcx), adt_def.did()));
512                if let Some(variant_def) = variant_def {
513                    if let Some((ctor_kind, ctor_def_id)) = variant_def.ctor {
514                        tcx.check_stability(
515                            ctor_def_id,
516                            Some(expr_id),
517                            span,
518                            Some(method_name.span),
519                        );
520                        return Ok((DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id));
521                    } else {
522                        struct_variant = Some((DefKind::Variant, variant_def.def_id));
523                    }
524                }
525            }
526        }
527
528        let pick = self.probe_for_name(
529            probe::Mode::Path,
530            method_name,
531            None,
532            IsSuggestion(false),
533            self_ty,
534            expr_id,
535            ProbeScope::TraitsInScope,
536        );
537        let pick = match (pick, struct_variant) {
538            // Fall back to a resolution that will produce an error later.
539            (Err(_), Some(res)) => return Ok(res),
540            (pick, _) => pick?,
541        };
542
543        pick.maybe_emit_unstable_name_collision_hint(self.tcx, span, expr_id);
544
545        self.lint_fully_qualified_call_from_2018(
546            span,
547            method_name,
548            self_ty,
549            self_ty_span,
550            expr_id,
551            &pick,
552        );
553
554        debug!(?pick);
555        {
556            let mut typeck_results = self.typeck_results.borrow_mut();
557            for import_id in pick.import_ids {
558                debug!(used_trait_import=?import_id);
559                typeck_results.used_trait_imports.insert(import_id);
560            }
561        }
562
563        let def_kind = pick.item.as_def_kind();
564        tcx.check_stability(pick.item.def_id, Some(expr_id), span, Some(method_name.span));
565        Ok((def_kind, pick.item.def_id))
566    }
567
568    /// Finds item with name `item_ident` defined in impl/trait `def_id`
569    /// and return it, or `None`, if no such item was defined there.
570    fn associated_value(&self, def_id: DefId, item_ident: Ident) -> Option<ty::AssocItem> {
571        self.tcx
572            .associated_items(def_id)
573            .find_by_ident_and_namespace(self.tcx, item_ident, Namespace::ValueNS, def_id)
574            .copied()
575    }
576}