Skip to main content

rustc_resolve/late/
diagnostics.rs

1// ignore-tidy-filelength
2
3use std::borrow::Cow;
4use std::iter;
5use std::ops::Deref;
6
7use rustc_ast::visit::{FnCtxt, FnKind, LifetimeCtxt, Visitor, walk_ty};
8use rustc_ast::{
9    self as ast, AngleBracketedArg, AssocItemKind, DUMMY_NODE_ID, Expr, ExprKind, GenericArg,
10    GenericArgs, GenericParam, GenericParamKind, Item, ItemKind, MethodCall, NodeId, Path,
11    PathSegment, Ty, TyKind,
12};
13use rustc_ast_pretty::pprust::{path_to_string, where_bound_predicate_to_string};
14use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
15use rustc_data_structures::unord::UnordItems;
16use rustc_errors::codes::*;
17use rustc_errors::{
18    Applicability, Diag, Diagnostic, ErrorGuaranteed, MultiSpan, SuggestionStyle, pluralize,
19    struct_span_code_err,
20};
21use rustc_hir as hir;
22use rustc_hir::def::Namespace::{self, *};
23use rustc_hir::def::{CtorKind, CtorOf, DefKind, MacroKinds};
24use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
25use rustc_hir::{MissingLifetimeKind, PrimTy, find_attr};
26use rustc_middle::ty;
27use rustc_session::{Session, lint};
28use rustc_span::edit_distance::{edit_distance, find_best_match_for_name};
29use rustc_span::edition::Edition;
30use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
31use thin_vec::ThinVec;
32use tracing::debug;
33
34use super::NoConstantGenericsReason;
35use crate::error_helper::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
36use crate::late::{
37    AliasPossibility, LateResolutionVisitor, LifetimeBinderKind, LifetimeRes, LifetimeRibKind,
38    LifetimeUseSet, QSelf, RibKind,
39};
40use crate::ty::fast_reject::SimplifiedType;
41use crate::{
42    Finalize, Module, ModuleOrUniformRoot, ParentScope, PathResult, PathSource, Res, Resolver,
43    ScopeSet, Segment, diagnostics, path_names_to_string,
44};
45
46/// A field or associated item from self type suggested in case of resolution failure.
47enum AssocSuggestion {
48    Field(Span),
49    MethodWithSelf { called: bool },
50    AssocFn { called: bool },
51    AssocType,
52    AssocConst,
53}
54
55impl AssocSuggestion {
56    fn action(&self) -> &'static str {
57        match self {
58            AssocSuggestion::Field(_) => "use the available field",
59            AssocSuggestion::MethodWithSelf { called: true } => {
60                "call the method with the fully-qualified path"
61            }
62            AssocSuggestion::MethodWithSelf { called: false } => {
63                "refer to the method with the fully-qualified path"
64            }
65            AssocSuggestion::AssocFn { called: true } => "call the associated function",
66            AssocSuggestion::AssocFn { called: false } => "refer to the associated function",
67            AssocSuggestion::AssocConst => "use the associated `const`",
68            AssocSuggestion::AssocType => "use the associated type",
69        }
70    }
71}
72
73fn is_self_type(path: &[Segment], namespace: Namespace) -> bool {
74    namespace == TypeNS && path.len() == 1 && path[0].ident.name == kw::SelfUpper
75}
76
77fn is_self_value(path: &[Segment], namespace: Namespace) -> bool {
78    namespace == ValueNS && path.len() == 1 && path[0].ident.name == kw::SelfLower
79}
80
81fn path_to_string_without_assoc_item_bindings(path: &Path) -> String {
82    let mut path = path.clone();
83    for segment in &mut path.segments {
84        let mut remove_args = false;
85        if let Some(args) = segment.args.as_deref_mut()
86            && let ast::GenericArgs::AngleBracketed(angle_bracketed) = args
87        {
88            angle_bracketed.args.retain(|arg| #[allow(non_exhaustive_omitted_patterns)] match arg {
    ast::AngleBracketedArg::Arg(_) => true,
    _ => false,
}matches!(arg, ast::AngleBracketedArg::Arg(_)));
89            remove_args = angle_bracketed.args.is_empty();
90        }
91        if remove_args {
92            segment.args = None;
93        }
94    }
95    path_to_string(&path)
96}
97
98/// Gets the stringified path for an enum from an `ImportSuggestion` for an enum variant.
99fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, String) {
100    let variant_path = &suggestion.path;
101    let variant_path_string = path_names_to_string(variant_path);
102
103    let path_len = suggestion.path.segments.len();
104    let enum_path = ast::Path {
105        span: suggestion.path.span,
106        segments: suggestion.path.segments[0..path_len - 1].iter().cloned().collect(),
107        tokens: None,
108    };
109    let enum_path_string = path_names_to_string(&enum_path);
110
111    (variant_path_string, enum_path_string)
112}
113
114/// Description of an elided lifetime.
115#[derive(#[automatically_derived]
impl ::core::marker::Copy for MissingLifetime { }Copy, #[automatically_derived]
impl ::core::clone::Clone for MissingLifetime {
    #[inline]
    fn clone(&self) -> MissingLifetime {
        let _: ::core::clone::AssertParamIsClone<NodeId>;
        let _: ::core::clone::AssertParamIsClone<Span>;
        let _: ::core::clone::AssertParamIsClone<MissingLifetimeKind>;
        let _: ::core::clone::AssertParamIsClone<usize>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for MissingLifetime {
    #[inline]
    fn eq(&self, other: &MissingLifetime) -> bool {
        self.id == other.id && self.id_for_lint == other.id_for_lint &&
                    self.span == other.span && self.kind == other.kind &&
            self.count == other.count
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for MissingLifetime {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<NodeId>;
        let _: ::core::cmp::AssertParamIsEq<Span>;
        let _: ::core::cmp::AssertParamIsEq<MissingLifetimeKind>;
        let _: ::core::cmp::AssertParamIsEq<usize>;
    }
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for MissingLifetime {
    #[inline]
    fn partial_cmp(&self, other: &MissingLifetime)
        -> ::core::option::Option<::core::cmp::Ordering> {
        ::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
    }
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for MissingLifetime {
    #[inline]
    fn cmp(&self, other: &MissingLifetime) -> ::core::cmp::Ordering {
        match ::core::cmp::Ord::cmp(&self.id, &other.id) {
            ::core::cmp::Ordering::Equal =>
                match ::core::cmp::Ord::cmp(&self.id_for_lint,
                        &other.id_for_lint) {
                    ::core::cmp::Ordering::Equal =>
                        match ::core::cmp::Ord::cmp(&self.span, &other.span) {
                            ::core::cmp::Ordering::Equal =>
                                match ::core::cmp::Ord::cmp(&self.kind, &other.kind) {
                                    ::core::cmp::Ordering::Equal =>
                                        ::core::cmp::Ord::cmp(&self.count, &other.count),
                                    cmp => cmp,
                                },
                            cmp => cmp,
                        },
                    cmp => cmp,
                },
            cmp => cmp,
        }
    }
}Ord, #[automatically_derived]
impl ::core::fmt::Debug for MissingLifetime {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field5_finish(f,
            "MissingLifetime", "id", &self.id, "id_for_lint",
            &self.id_for_lint, "span", &self.span, "kind", &self.kind,
            "count", &&self.count)
    }
}Debug)]
116pub(super) struct MissingLifetime {
117    /// Used to overwrite the resolution with the suggestion, to avoid cascading errors.
118    pub id: NodeId,
119    /// As we cannot yet emit lints in this crate and have to buffer them instead,
120    /// we need to associate each lint with some `NodeId`,
121    /// however for some `MissingLifetime`s their `NodeId`s are "fake",
122    /// in a sense that they are temporary and not get preserved down the line,
123    /// which means that the lints for those nodes will not get emitted.
124    /// To combat this, we can try to use some other `NodeId`s as a fallback option.
125    pub id_for_lint: NodeId,
126    /// Where to suggest adding the lifetime.
127    pub span: Span,
128    /// How the lifetime was introduced, to have the correct space and comma.
129    pub kind: MissingLifetimeKind,
130    /// Number of elided lifetimes, used for elision in path.
131    pub count: usize,
132}
133
134/// Description of the lifetimes appearing in a function parameter.
135/// This is used to provide a literal explanation to the elision failure.
136#[derive(#[automatically_derived]
impl ::core::clone::Clone for ElisionFnParameter {
    #[inline]
    fn clone(&self) -> ElisionFnParameter {
        ElisionFnParameter {
            index: ::core::clone::Clone::clone(&self.index),
            ident: ::core::clone::Clone::clone(&self.ident),
            lifetime_count: ::core::clone::Clone::clone(&self.lifetime_count),
            span: ::core::clone::Clone::clone(&self.span),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ElisionFnParameter {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f,
            "ElisionFnParameter", "index", &self.index, "ident", &self.ident,
            "lifetime_count", &self.lifetime_count, "span", &&self.span)
    }
}Debug)]
137pub(super) struct ElisionFnParameter {
138    /// The index of the argument in the original definition.
139    pub index: usize,
140    /// The name of the argument if it's a simple ident.
141    pub ident: Option<Ident>,
142    /// The number of lifetimes in the parameter.
143    pub lifetime_count: usize,
144    /// The span of the parameter.
145    pub span: Span,
146}
147
148/// Description of lifetimes that appear as candidates for elision.
149/// This is used to suggest introducing an explicit lifetime.
150#[derive(#[automatically_derived]
impl ::core::clone::Clone for LifetimeElisionCandidate {
    #[inline]
    fn clone(&self) -> LifetimeElisionCandidate {
        let _: ::core::clone::AssertParamIsClone<MissingLifetime>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LifetimeElisionCandidate { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for LifetimeElisionCandidate {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            LifetimeElisionCandidate::Ignore =>
                ::core::fmt::Formatter::write_str(f, "Ignore"),
            LifetimeElisionCandidate::Missing(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Missing", &__self_0),
        }
    }
}Debug)]
151pub(super) enum LifetimeElisionCandidate {
152    /// This is not a real lifetime, or it is a named lifetime, in which case we won't suggest anything.
153    Ignore,
154    Missing(MissingLifetime),
155}
156
157/// Only used for diagnostics.
158#[derive(#[automatically_derived]
impl ::core::fmt::Debug for BaseError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["msg", "fallback_label", "span", "span_label", "could_be_expr",
                        "suggestion", "module"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.msg, &self.fallback_label, &self.span, &self.span_label,
                        &self.could_be_expr, &self.suggestion, &&self.module];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, "BaseError",
            names, values)
    }
}Debug)]
159struct BaseError {
160    msg: String,
161    fallback_label: String,
162    span: Span,
163    span_label: Option<(Span, &'static str)>,
164    could_be_expr: bool,
165    suggestion: Option<(Span, &'static str, String)>,
166    module: Option<DefId>,
167}
168
169#[derive(#[automatically_derived]
impl ::core::fmt::Debug for TypoCandidate {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            TypoCandidate::Typo(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Typo",
                    &__self_0),
            TypoCandidate::Shadowed(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f,
                    "Shadowed", __self_0, &__self_1),
            TypoCandidate::None =>
                ::core::fmt::Formatter::write_str(f, "None"),
        }
    }
}Debug)]
170enum TypoCandidate {
171    Typo(TypoSuggestion),
172    Shadowed(Res, Option<Span>),
173    None,
174}
175
176impl TypoCandidate {
177    fn to_opt_suggestion(self) -> Option<TypoSuggestion> {
178        match self {
179            TypoCandidate::Typo(sugg) => Some(sugg),
180            TypoCandidate::Shadowed(_, _) | TypoCandidate::None => None,
181        }
182    }
183}
184
185impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
186    fn trait_assoc_type_def_id_by_name(
187        &mut self,
188        trait_def_id: DefId,
189        assoc_name: Symbol,
190    ) -> Option<DefId> {
191        let module = self.r.get_module(trait_def_id)?;
192        self.r.resolutions(module).borrow().iter().find_map(|(key, resolution)| {
193            if key.ident.name != assoc_name {
194                return None;
195            }
196            let resolution = resolution.borrow();
197            let binding = resolution.best_decl()?;
198            match binding.res() {
199                Res::Def(DefKind::AssocTy, def_id) => Some(def_id),
200                _ => None,
201            }
202        })
203    }
204
205    /// This does best-effort work to generate suggestions for associated types.
206    fn suggest_assoc_type_from_bounds(
207        &mut self,
208        err: &mut Diag<'_>,
209        source: PathSource<'_, 'ast, 'ra>,
210        path: &[Segment],
211        ident_span: Span,
212    ) -> bool {
213        // Filter out cases where we cannot emit meaningful suggestions.
214        if source.namespace() != TypeNS {
215            return false;
216        }
217        let [segment] = path else { return false };
218        if segment.has_generic_args {
219            return false;
220        }
221        if !ident_span.can_be_used_for_suggestions() {
222            return false;
223        }
224        let assoc_name = segment.ident.name;
225        if assoc_name == kw::Underscore {
226            return false;
227        }
228
229        // Map: type parameter name -> (trait def id -> (assoc type def id, trait paths as written)).
230        // We keep a set of paths per trait so we can detect cases like
231        // `T: Trait<i32> + Trait<u32>` where suggesting `T::Assoc` would be ambiguous.
232        let mut matching_bounds: FxIndexMap<
233            Symbol,
234            FxIndexMap<DefId, (DefId, FxIndexSet<String>)>,
235        > = FxIndexMap::default();
236
237        let mut record_bound = |this: &mut Self,
238                                ty_param: Symbol,
239                                poly_trait_ref: &ast::PolyTraitRef| {
240            // Avoid generating suggestions we can't print in a well-formed way.
241            if !poly_trait_ref.bound_generic_params.is_empty() {
242                return;
243            }
244            if poly_trait_ref.modifiers != ast::TraitBoundModifiers::NONE {
245                return;
246            }
247            let Some(trait_seg) = poly_trait_ref.trait_ref.path.segments.last() else {
248                return;
249            };
250            let Some(partial_res) = this.r.partial_res_map.get(&trait_seg.id) else {
251                return;
252            };
253            let Some(trait_def_id) = partial_res.full_res().and_then(|res| res.opt_def_id()) else {
254                return;
255            };
256            let Some(assoc_type_def_id) =
257                this.trait_assoc_type_def_id_by_name(trait_def_id, assoc_name)
258            else {
259                return;
260            };
261
262            // Preserve `::` and generic args so we don't generate broken suggestions like
263            // `<T as Foo>::Assoc` for bounds written as `T: ::Foo<'a>`, while stripping
264            // associated-item bindings that are rejected in qualified paths.
265            let trait_path =
266                path_to_string_without_assoc_item_bindings(&poly_trait_ref.trait_ref.path);
267            let trait_bounds = matching_bounds.entry(ty_param).or_default();
268            let trait_bounds = trait_bounds
269                .entry(trait_def_id)
270                .or_insert_with(|| (assoc_type_def_id, FxIndexSet::default()));
271            if true {
    match (&trait_bounds.0, &assoc_type_def_id) {
        (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::None);
            }
        }
    };
};debug_assert_eq!(trait_bounds.0, assoc_type_def_id);
272            trait_bounds.1.insert(trait_path);
273        };
274
275        let mut record_from_generics = |this: &mut Self, generics: &ast::Generics| {
276            for param in &generics.params {
277                let ast::GenericParamKind::Type { .. } = param.kind else { continue };
278                for bound in &param.bounds {
279                    let ast::GenericBound::Trait(poly_trait_ref) = bound else { continue };
280                    record_bound(this, param.ident.name, poly_trait_ref);
281                }
282            }
283
284            for predicate in &generics.where_clause.predicates {
285                let ast::WherePredicateKind::BoundPredicate(where_bound) = &predicate.kind else {
286                    continue;
287                };
288
289                let ast::TyKind::Path(None, bounded_path) = &where_bound.bounded_ty.kind else {
290                    continue;
291                };
292                let [ast::PathSegment { ident, args: None, .. }] = &bounded_path.segments[..]
293                else {
294                    continue;
295                };
296
297                // Only suggest for bounds that are explicitly on an in-scope type parameter.
298                let Some(partial_res) = this.r.partial_res_map.get(&where_bound.bounded_ty.id)
299                else {
300                    continue;
301                };
302                if !#[allow(non_exhaustive_omitted_patterns)] match partial_res.full_res() {
    Some(Res::Def(DefKind::TyParam, _)) => true,
    _ => false,
}matches!(partial_res.full_res(), Some(Res::Def(DefKind::TyParam, _))) {
303                    continue;
304                }
305
306                for bound in &where_bound.bounds {
307                    let ast::GenericBound::Trait(poly_trait_ref) = bound else { continue };
308                    record_bound(this, ident.name, poly_trait_ref);
309                }
310            }
311        };
312
313        if let Some(item) = self.diag_metadata.current_item
314            && let Some(generics) = item.kind.generics()
315        {
316            record_from_generics(self, generics);
317        }
318
319        if let Some(item) = self.diag_metadata.current_item
320            && #[allow(non_exhaustive_omitted_patterns)] match item.kind {
    ItemKind::Impl(..) => true,
    _ => false,
}matches!(item.kind, ItemKind::Impl(..))
321            && let Some(assoc) = self.diag_metadata.current_impl_item
322        {
323            let generics = match &assoc.kind {
324                AssocItemKind::Const(ast::ConstItem { generics, .. })
325                | AssocItemKind::Fn(ast::Fn { generics, .. })
326                | AssocItemKind::Type(ast::TyAlias { generics, .. }) => Some(generics),
327                AssocItemKind::Delegation(..)
328                | AssocItemKind::MacCall(..)
329                | AssocItemKind::DelegationMac(..) => None,
330            };
331            if let Some(generics) = generics {
332                record_from_generics(self, generics);
333            }
334        }
335
336        let mut suggestions: FxIndexSet<String> = FxIndexSet::default();
337        for (ty_param, traits) in matching_bounds {
338            let ty_param = ty_param.to_ident_string();
339            let trait_paths_len: usize = traits.values().map(|(_, paths)| paths.len()).sum();
340            if traits.len() == 1 && trait_paths_len == 1 {
341                let assoc_type_def_id = traits.values().next().unwrap().0;
342                let assoc_segment = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}", assoc_name,
                self.r.item_required_generic_args_suggestion(assoc_type_def_id)))
    })format!(
343                    "{}{}",
344                    assoc_name,
345                    self.r.item_required_generic_args_suggestion(assoc_type_def_id)
346                );
347                suggestions.insert(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}::{1}", ty_param,
                assoc_segment))
    })format!("{ty_param}::{assoc_segment}"));
348            } else {
349                for (assoc_type_def_id, trait_paths) in traits.into_values() {
350                    let assoc_segment = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}", assoc_name,
                self.r.item_required_generic_args_suggestion(assoc_type_def_id)))
    })format!(
351                        "{}{}",
352                        assoc_name,
353                        self.r.item_required_generic_args_suggestion(assoc_type_def_id)
354                    );
355                    for trait_path in trait_paths {
356                        suggestions
357                            .insert(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0} as {1}>::{2}", ty_param,
                trait_path, assoc_segment))
    })format!("<{ty_param} as {trait_path}>::{assoc_segment}"));
358                    }
359                }
360            }
361        }
362
363        if suggestions.is_empty() {
364            return false;
365        }
366
367        let mut suggestions: Vec<String> = suggestions.into_iter().collect();
368        suggestions.sort();
369
370        err.span_suggestions_with_style(
371            ident_span,
372            "you might have meant to use an associated type of the same name",
373            suggestions,
374            Applicability::MaybeIncorrect,
375            SuggestionStyle::ShowAlways,
376        );
377
378        true
379    }
380
381    fn make_base_error(
382        &mut self,
383        path: &[Segment],
384        span: Span,
385        source: PathSource<'_, 'ast, 'ra>,
386        res: Option<Res>,
387    ) -> BaseError {
388        // Make the base error.
389        let mut expected = source.descr_expected();
390        let path_str = Segment::names_to_string(path);
391        let item_str = path.last().unwrap().ident;
392
393        if let Some(res) = res {
394            BaseError {
395                msg: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected {0}, found {1} `{2}`",
                expected, res.descr(), path_str))
    })format!("expected {}, found {} `{}`", expected, res.descr(), path_str),
396                fallback_label: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("not a {0}", expected))
    })format!("not a {expected}"),
397                span,
398                span_label: match res {
399                    Res::Def(DefKind::TyParam, def_id) => {
400                        Some((self.r.def_span(def_id), "found this type parameter"))
401                    }
402                    _ => None,
403                },
404                could_be_expr: match res {
405                    Res::Def(DefKind::Fn, _) => {
406                        // Verify whether this is a fn call or an Fn used as a type.
407                        self.r
408                            .tcx
409                            .sess
410                            .source_map()
411                            .span_to_snippet(span)
412                            .is_ok_and(|snippet| snippet.ends_with(')'))
413                    }
414                    Res::Def(
415                        DefKind::Ctor(..)
416                        | DefKind::AssocFn
417                        | DefKind::Const { .. }
418                        | DefKind::AssocConst { .. },
419                        _,
420                    )
421                    | Res::SelfCtor(_)
422                    | Res::PrimTy(_)
423                    | Res::Local(_) => true,
424                    _ => false,
425                },
426                suggestion: None,
427                module: None,
428            }
429        } else {
430            let mut span_label = None;
431            let item_ident = path.last().unwrap().ident;
432            let item_span = item_ident.span;
433            let (mod_prefix, mod_str, module, suggestion) = if path.len() == 1 {
434                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:434",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(434u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::tracing_core::field::FieldSet::new(&["self.diag_metadata.current_impl_items"],
                            ::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(&self.diag_metadata.current_impl_items)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?self.diag_metadata.current_impl_items);
435                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:435",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(435u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::tracing_core::field::FieldSet::new(&["self.diag_metadata.current_function"],
                            ::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(&self.diag_metadata.current_function)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?self.diag_metadata.current_function);
436                let suggestion = if self.current_trait_ref.is_none()
437                    && let Some((fn_kind, _)) = self.diag_metadata.current_function
438                    && let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
439                    && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = fn_kind
440                    && let Some(items) = self.diag_metadata.current_impl_items
441                    && let Some(item) = items.iter().find(|i| {
442                        i.kind.ident().is_some_and(|ident| {
443                            // Don't suggest if the item is in Fn signature arguments (#112590).
444                            ident.name == item_str.name && !sig.span.contains(item_span)
445                        })
446                    }) {
447                    let sp = item_span.shrink_to_lo();
448
449                    // Account for `Foo { field }` when suggesting `self.field` so we result on
450                    // `Foo { field: self.field }`.
451                    let field = match source {
452                        PathSource::Expr(Some(Expr { kind: ExprKind::Struct(expr), .. })) => {
453                            expr.fields.iter().find(|f| f.ident == item_ident)
454                        }
455                        _ => None,
456                    };
457                    let pre = if let Some(field) = field
458                        && field.is_shorthand
459                    {
460                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: ", item_ident))
    })format!("{item_ident}: ")
461                    } else {
462                        String::new()
463                    };
464                    // Ensure we provide a structured suggestion for an assoc fn only for
465                    // expressions that are actually a fn call.
466                    let is_call = match field {
467                        Some(ast::ExprField { expr, .. }) => {
468                            #[allow(non_exhaustive_omitted_patterns)] match expr.kind {
    ExprKind::Call(..) => true,
    _ => false,
}matches!(expr.kind, ExprKind::Call(..))
469                        }
470                        _ => #[allow(non_exhaustive_omitted_patterns)] match source {
    PathSource::Expr(Some(Expr { kind: ExprKind::Call(..), .. })) => true,
    _ => false,
}matches!(
471                            source,
472                            PathSource::Expr(Some(Expr { kind: ExprKind::Call(..), .. })),
473                        ),
474                    };
475
476                    match &item.kind {
477                        AssocItemKind::Fn(fn_)
478                            if (!sig.decl.has_self() || !is_call) && fn_.sig.decl.has_self() =>
479                        {
480                            // Ensure that we only suggest `self.` if `self` is available,
481                            // you can't call `fn foo(&self)` from `fn bar()` (#115992).
482                            // We also want to mention that the method exists.
483                            span_label = Some((
484                                fn_.ident.span,
485                                "a method by that name is available on `Self` here",
486                            ));
487                            None
488                        }
489                        AssocItemKind::Fn(fn_) if !fn_.sig.decl.has_self() && !is_call => {
490                            span_label = Some((
491                                fn_.ident.span,
492                                "an associated function by that name is available on `Self` here",
493                            ));
494                            None
495                        }
496                        AssocItemKind::Fn(fn_) if fn_.sig.decl.has_self() => {
497                            Some((sp, "consider using the method on `Self`", ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}self.", pre))
    })format!("{pre}self.")))
498                        }
499                        AssocItemKind::Fn(_) => Some((
500                            sp,
501                            "consider using the associated function on `Self`",
502                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}Self::", pre))
    })format!("{pre}Self::"),
503                        )),
504                        AssocItemKind::Const(..) => Some((
505                            sp,
506                            "consider using the associated constant on `Self`",
507                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}Self::", pre))
    })format!("{pre}Self::"),
508                        )),
509                        _ => None,
510                    }
511                } else {
512                    None
513                };
514                (String::new(), "this scope".to_string(), None, suggestion)
515            } else if path.len() == 2 && path[0].ident.name == kw::PathRoot {
516                if self.r.tcx.sess.edition() > Edition::Edition2015 {
517                    // In edition 2018 onwards, the `::foo` syntax may only pull from the extern prelude
518                    // which overrides all other expectations of item type
519                    expected = "crate";
520                    (String::new(), "the list of imported crates".to_string(), None, None)
521                } else {
522                    (
523                        String::new(),
524                        "the crate root".to_string(),
525                        Some(CRATE_DEF_ID.to_def_id()),
526                        None,
527                    )
528                }
529            } else if path.len() == 2 && path[0].ident.name == kw::Crate {
530                (String::new(), "the crate root".to_string(), Some(CRATE_DEF_ID.to_def_id()), None)
531            } else {
532                let mod_path = &path[..path.len() - 1];
533                let mod_res = self.resolve_path(mod_path, Some(TypeNS), None, source);
534                let mod_prefix = match mod_res {
535                    PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(),
536                    _ => None,
537                };
538
539                let module_did = mod_prefix.as_ref().and_then(Res::mod_def_id);
540
541                let mod_prefix =
542                    mod_prefix.map_or_else(String::new, |res| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} ", res.descr()))
    })format!("{} ", res.descr()));
543                (mod_prefix, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`",
                Segment::names_to_string(mod_path)))
    })format!("`{}`", Segment::names_to_string(mod_path)), module_did, None)
544            };
545
546            let (fallback_label, suggestion) = if path_str == "async"
547                && expected.starts_with("struct")
548            {
549                ("`async` blocks are only allowed in Rust 2018 or later".to_string(), suggestion)
550            } else {
551                // check if we are in situation of typo like `True` instead of `true`.
552                let override_suggestion =
553                    if ["true", "false"].contains(&item_str.to_string().to_lowercase().as_str()) {
554                        let item_typo = item_str.to_string().to_lowercase();
555                        Some((item_span, "you may want to use a bool value instead", item_typo))
556                    // FIXME(vincenzopalazzo): make the check smarter,
557                    // and maybe expand with levenshtein distance checks
558                    } else if item_str.as_str() == "printf" {
559                        Some((
560                            item_span,
561                            "you may have meant to use the `print` macro",
562                            "print!".to_owned(),
563                        ))
564                    } else {
565                        suggestion
566                    };
567                (::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("not found in {0}", mod_str))
    })format!("not found in {mod_str}"), override_suggestion)
568            };
569
570            BaseError {
571                msg: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("cannot find {0} `{1}` in {2}{3}",
                expected, item_str, mod_prefix, mod_str))
    })format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
572                fallback_label,
573                span: item_span,
574                span_label,
575                could_be_expr: false,
576                suggestion,
577                module,
578            }
579        }
580    }
581
582    /// Try to suggest for a module path that cannot be resolved.
583    /// Such as `fmt::Debug` where `fmt` is not resolved without importing,
584    /// here we search with `lookup_import_candidates` for a module named `fmt`
585    /// with `TypeNS` as namespace.
586    ///
587    /// We need a separate function here because we won't suggest for a path with single segment
588    /// and we won't change `SourcePath` api `is_expected` to match `Type` with `DefKind::Mod`
589    pub(crate) fn smart_resolve_partial_mod_path_errors(
590        &mut self,
591        prefix_path: &[Segment],
592        following_seg: Option<&Segment>,
593    ) -> Vec<ImportSuggestion> {
594        if let Some(segment) = prefix_path.last()
595            && let Some(following_seg) = following_seg
596        {
597            let candidates = self.r.lookup_import_candidates(
598                segment.ident,
599                Namespace::TypeNS,
600                &self.parent_scope,
601                &|res: Res| #[allow(non_exhaustive_omitted_patterns)] match res {
    Res::Def(DefKind::Mod, _) => true,
    _ => false,
}matches!(res, Res::Def(DefKind::Mod, _)),
602            );
603            // double check next seg is valid
604            candidates
605                .into_iter()
606                .filter(|candidate| {
607                    if let Some(def_id) = candidate.did
608                        && let Some(module) = self.r.get_module(def_id)
609                    {
610                        Some(def_id) != self.parent_scope.module.opt_def_id()
611                            && self
612                                .r
613                                .resolutions(module)
614                                .borrow()
615                                .iter()
616                                .any(|(key, _r)| key.ident.name == following_seg.ident.name)
617                    } else {
618                        false
619                    }
620                })
621                .collect::<Vec<_>>()
622        } else {
623            Vec::new()
624        }
625    }
626
627    /// Handles error reporting for `smart_resolve_path_fragment` function.
628    /// Creates base error and amends it with one short label and possibly some longer helps/notes.
629    #[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("smart_resolve_report_errors",
                                    "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                                    ::tracing_core::__macro_support::Option::Some(629u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                                    ::tracing_core::field::FieldSet::new(&["path",
                                                    "following_seg", "span", "source", "res", "qself"],
                                        ::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(&path)
                                                            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(&following_seg)
                                                            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(&source)
                                                            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(&res)
                                                            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(&qself)
                                                            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:
                    (Diag<'tcx>, Vec<ImportSuggestion>) = loop {};
            return __tracing_attr_fake_return;
        }
        {
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:639",
                                    "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                                    ::tracing_core::__macro_support::Option::Some(639u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                                    ::tracing_core::field::FieldSet::new(&["res", "source"],
                                        ::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(&res) as
                                                        &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&source) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            let base_error = self.make_base_error(path, span, source, res);
            let code = source.error_code(res.is_some());
            let mut err =
                self.r.dcx().struct_span_err(base_error.span,
                    base_error.msg.clone());
            err.code(code);
            if let Some(within_macro_span) =
                    base_error.span.within_macro(span,
                        self.r.tcx.sess.source_map()) {
                err.span_label(within_macro_span,
                    "due to this macro variable");
            }
            self.detect_missing_binding_available_from_pattern(&mut err, path,
                following_seg);
            self.suggest_at_operator_in_slice_pat_with_range(&mut err, path);
            self.suggest_range_struct_destructuring(&mut err, path, source);
            self.suggest_swapping_misplaced_self_ty_and_trait(&mut err,
                source, res, base_error.span);
            if let Some((span, label)) = base_error.span_label {
                err.span_label(span, label);
            }
            if let Some(ref sugg) = base_error.suggestion {
                err.span_suggestion_verbose(sugg.0, sugg.1, &sugg.2,
                    Applicability::MaybeIncorrect);
            }
            self.suggest_changing_type_to_const_param(&mut err, res, source,
                path, following_seg, span);
            self.explain_functions_in_pattern(&mut err, res, source);
            if self.suggest_pattern_match_with_let(&mut err, source, span) {
                err.span_label(base_error.span, base_error.fallback_label);
                return (err, Vec::new());
            }
            self.suggest_self_or_self_ref(&mut err, path, span);
            self.detect_assoc_type_constraint_meant_as_path(&mut err,
                &base_error);
            self.detect_rtn_with_fully_qualified_path(&mut err, path,
                following_seg, span, source, res, qself);
            if self.suggest_self_ty(&mut err, source, path, span) ||
                    self.suggest_self_value(&mut err, source, path, span) {
                return (err, Vec::new());
            }
            if let Some((did, item)) =
                    self.lookup_doc_alias_name(path, source.namespace()) {
                let item_name = item.name;
                let suggestion_name = self.r.tcx.item_name(did);
                err.span_suggestion(item.span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("`{0}` has a name defined in the doc alias attribute as `{1}`",
                                    suggestion_name, item_name))
                        }), suggestion_name, Applicability::MaybeIncorrect);
                return (err, Vec::new());
            };
            let (found, suggested_candidates, mut candidates) =
                self.try_lookup_name_relaxed(&mut err, source, path,
                    following_seg, span, res, &base_error);
            if found { return (err, candidates); }
            if self.suggest_shadowed(&mut err, source, path, following_seg,
                    span) {
                candidates.clear();
            }
            let mut fallback =
                self.suggest_trait_and_bounds(&mut err, source, res, span,
                    &base_error);
            fallback |=
                self.suggest_typo(&mut err, source, path, following_seg, span,
                    &base_error, suggested_candidates);
            if fallback {
                err.span_label(base_error.span, base_error.fallback_label);
            }
            self.err_code_special_cases(&mut err, source, path, span);
            let module =
                base_error.module.unwrap_or_else(|| CRATE_DEF_ID.to_def_id());
            self.r.find_cfg_stripped(&mut err,
                &path.last().unwrap().ident.name, module);
            (err, candidates)
        }
    }
}#[tracing::instrument(skip(self), level = "debug")]
630    pub(crate) fn smart_resolve_report_errors(
631        &mut self,
632        path: &[Segment],
633        following_seg: Option<&Segment>,
634        span: Span,
635        source: PathSource<'_, 'ast, 'ra>,
636        res: Option<Res>,
637        qself: Option<&QSelf>,
638    ) -> (Diag<'tcx>, Vec<ImportSuggestion>) {
639        debug!(?res, ?source);
640        let base_error = self.make_base_error(path, span, source, res);
641
642        let code = source.error_code(res.is_some());
643        let mut err = self.r.dcx().struct_span_err(base_error.span, base_error.msg.clone());
644        err.code(code);
645
646        // Try to get the span of the identifier within the path's syntax context
647        // (if that's different).
648        if let Some(within_macro_span) =
649            base_error.span.within_macro(span, self.r.tcx.sess.source_map())
650        {
651            err.span_label(within_macro_span, "due to this macro variable");
652        }
653
654        self.detect_missing_binding_available_from_pattern(&mut err, path, following_seg);
655        self.suggest_at_operator_in_slice_pat_with_range(&mut err, path);
656        self.suggest_range_struct_destructuring(&mut err, path, source);
657        self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span);
658
659        if let Some((span, label)) = base_error.span_label {
660            err.span_label(span, label);
661        }
662
663        if let Some(ref sugg) = base_error.suggestion {
664            err.span_suggestion_verbose(sugg.0, sugg.1, &sugg.2, Applicability::MaybeIncorrect);
665        }
666
667        self.suggest_changing_type_to_const_param(&mut err, res, source, path, following_seg, span);
668        self.explain_functions_in_pattern(&mut err, res, source);
669
670        if self.suggest_pattern_match_with_let(&mut err, source, span) {
671            // Fallback label.
672            err.span_label(base_error.span, base_error.fallback_label);
673            return (err, Vec::new());
674        }
675
676        self.suggest_self_or_self_ref(&mut err, path, span);
677        self.detect_assoc_type_constraint_meant_as_path(&mut err, &base_error);
678        self.detect_rtn_with_fully_qualified_path(
679            &mut err,
680            path,
681            following_seg,
682            span,
683            source,
684            res,
685            qself,
686        );
687        if self.suggest_self_ty(&mut err, source, path, span)
688            || self.suggest_self_value(&mut err, source, path, span)
689        {
690            return (err, Vec::new());
691        }
692
693        if let Some((did, item)) = self.lookup_doc_alias_name(path, source.namespace()) {
694            let item_name = item.name;
695            let suggestion_name = self.r.tcx.item_name(did);
696            err.span_suggestion(
697                item.span,
698                format!("`{suggestion_name}` has a name defined in the doc alias attribute as `{item_name}`"),
699                    suggestion_name,
700                    Applicability::MaybeIncorrect
701                );
702
703            return (err, Vec::new());
704        };
705
706        let (found, suggested_candidates, mut candidates) = self.try_lookup_name_relaxed(
707            &mut err,
708            source,
709            path,
710            following_seg,
711            span,
712            res,
713            &base_error,
714        );
715        if found {
716            return (err, candidates);
717        }
718
719        if self.suggest_shadowed(&mut err, source, path, following_seg, span) {
720            // if there is already a shadowed name, don'suggest candidates for importing
721            candidates.clear();
722        }
723
724        let mut fallback = self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
725        fallback |= self.suggest_typo(
726            &mut err,
727            source,
728            path,
729            following_seg,
730            span,
731            &base_error,
732            suggested_candidates,
733        );
734
735        if fallback {
736            // Fallback label.
737            err.span_label(base_error.span, base_error.fallback_label);
738        }
739        self.err_code_special_cases(&mut err, source, path, span);
740
741        let module = base_error.module.unwrap_or_else(|| CRATE_DEF_ID.to_def_id());
742        self.r.find_cfg_stripped(&mut err, &path.last().unwrap().ident.name, module);
743
744        (err, candidates)
745    }
746
747    fn detect_rtn_with_fully_qualified_path(
748        &self,
749        err: &mut Diag<'_>,
750        path: &[Segment],
751        following_seg: Option<&Segment>,
752        span: Span,
753        source: PathSource<'_, '_, '_>,
754        res: Option<Res>,
755        qself: Option<&QSelf>,
756    ) {
757        if let Some(Res::Def(DefKind::AssocFn, _)) = res
758            && let PathSource::TraitItem(TypeNS, _) = source
759            && let None = following_seg
760            && let Some(qself) = qself
761            && let TyKind::Path(None, ty_path) = &qself.ty.kind
762            && ty_path.segments.len() == 1
763            && self.diag_metadata.current_where_predicate.is_some()
764        {
765            err.span_suggestion_verbose(
766                span,
767                "you might have meant to use the return type notation syntax",
768                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}::{1}(..)",
                ty_path.segments[0].ident, path[path.len() - 1].ident))
    })format!("{}::{}(..)", ty_path.segments[0].ident, path[path.len() - 1].ident),
769                Applicability::MaybeIncorrect,
770            );
771        }
772    }
773
774    fn detect_assoc_type_constraint_meant_as_path(
775        &self,
776        err: &mut Diag<'_>,
777        base_error: &BaseError,
778    ) {
779        let Some(ty) = self.diag_metadata.current_type_path else {
780            return;
781        };
782        let TyKind::Path(_, path) = &ty.kind else {
783            return;
784        };
785        for segment in &path.segments {
786            let Some(params) = &segment.args else {
787                continue;
788            };
789            let ast::GenericArgs::AngleBracketed(params) = params.deref() else {
790                continue;
791            };
792            for param in &params.args {
793                let ast::AngleBracketedArg::Constraint(constraint) = param else {
794                    continue;
795                };
796                let ast::AssocItemConstraintKind::Bound { bounds } = &constraint.kind else {
797                    continue;
798                };
799                for bound in bounds {
800                    let ast::GenericBound::Trait(trait_ref) = bound else {
801                        continue;
802                    };
803                    if trait_ref.modifiers == ast::TraitBoundModifiers::NONE
804                        && base_error.span == trait_ref.span
805                    {
806                        err.span_suggestion_verbose(
807                            constraint.ident.span.between(trait_ref.span),
808                            "you might have meant to write a path instead of an associated type bound",
809                            "::",
810                            Applicability::MachineApplicable,
811                        );
812                    }
813                }
814            }
815        }
816    }
817
818    fn suggest_self_or_self_ref(&mut self, err: &mut Diag<'_>, path: &[Segment], span: Span) {
819        if !self.self_type_is_available() {
820            return;
821        }
822        let Some(path_last_segment) = path.last() else { return };
823        let item_str = path_last_segment.ident;
824        // Emit help message for fake-self from other languages (e.g., `this` in JavaScript).
825        if ["this", "my"].contains(&item_str.as_str()) {
826            err.span_suggestion_short(
827                span,
828                "you might have meant to use `self` here instead",
829                "self",
830                Applicability::MaybeIncorrect,
831            );
832            if !self.self_value_is_available(path[0].ident.span) {
833                if let Some((FnKind::Fn(_, _, ast::Fn { sig, .. }), fn_span)) =
834                    &self.diag_metadata.current_function
835                {
836                    let (span, sugg) = if let Some(param) = sig.decl.inputs.get(0) {
837                        (param.span.shrink_to_lo(), "&self, ")
838                    } else {
839                        (
840                            self.r
841                                .tcx
842                                .sess
843                                .source_map()
844                                .span_through_char(*fn_span, '(')
845                                .shrink_to_hi(),
846                            "&self",
847                        )
848                    };
849                    err.span_suggestion_verbose(
850                        span,
851                        "if you meant to use `self`, you are also missing a `self` receiver \
852                         argument",
853                        sugg,
854                        Applicability::MaybeIncorrect,
855                    );
856                }
857            }
858        }
859    }
860
861    fn try_lookup_name_relaxed(
862        &mut self,
863        err: &mut Diag<'_>,
864        source: PathSource<'_, '_, '_>,
865        path: &[Segment],
866        following_seg: Option<&Segment>,
867        span: Span,
868        res: Option<Res>,
869        base_error: &BaseError,
870    ) -> (bool, FxHashSet<String>, Vec<ImportSuggestion>) {
871        let span = match following_seg {
872            Some(_) if path[0].ident.span.eq_ctxt(path[path.len() - 1].ident.span) => {
873                // The path `span` that comes in includes any following segments, which we don't
874                // want to replace in the suggestions.
875                path[0].ident.span.to(path[path.len() - 1].ident.span)
876            }
877            _ => span,
878        };
879        let mut suggested_candidates = FxHashSet::default();
880        // Try to lookup name in more relaxed fashion for better error reporting.
881        let ident = path.last().unwrap().ident;
882        let is_expected = &|res| source.is_expected(res);
883        let ns = source.namespace();
884        let is_enum_variant = &|res| #[allow(non_exhaustive_omitted_patterns)] match res {
    Res::Def(DefKind::Variant, _) => true,
    _ => false,
}matches!(res, Res::Def(DefKind::Variant, _));
885        let path_str = Segment::names_to_string(path);
886        let ident_span = path.last().map_or(span, |ident| ident.ident.span);
887        let mut candidates = self
888            .r
889            .lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
890            .into_iter()
891            .filter(|ImportSuggestion { did, .. }| {
892                match (did, res.and_then(|res| res.opt_def_id())) {
893                    (Some(suggestion_did), Some(actual_did)) => *suggestion_did != actual_did,
894                    _ => true,
895                }
896            })
897            .collect::<Vec<_>>();
898        // Try to filter out intrinsics candidates, as long as we have
899        // some other candidates to suggest.
900        let intrinsic_candidates: Vec<_> = candidates
901            .extract_if(.., |sugg| {
902                let path = path_names_to_string(&sugg.path);
903                path.starts_with("core::intrinsics::") || path.starts_with("std::intrinsics::")
904            })
905            .collect();
906        if candidates.is_empty() {
907            // Put them back if we have no more candidates to suggest...
908            candidates = intrinsic_candidates;
909        }
910        let crate_def_id = CRATE_DEF_ID.to_def_id();
911        if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
912            let mut enum_candidates: Vec<_> = self
913                .r
914                .lookup_import_candidates(ident, ns, &self.parent_scope, is_enum_variant)
915                .into_iter()
916                .map(|suggestion| import_candidate_to_enum_paths(&suggestion))
917                .filter(|(_, enum_ty_path)| !enum_ty_path.starts_with("std::prelude::"))
918                .collect();
919            if !enum_candidates.is_empty() {
920                enum_candidates.sort();
921
922                // Contextualize for E0425 "cannot find type", but don't belabor the point
923                // (that it's a variant) for E0573 "expected type, found variant".
924                let preamble = if res.is_none() {
925                    let others = match enum_candidates.len() {
926                        1 => String::new(),
927                        2 => " and 1 other".to_owned(),
928                        n => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" and {0} others", n))
    })format!(" and {n} others"),
929                    };
930                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("there is an enum variant `{0}`{1}; ",
                enum_candidates[0].0, others))
    })format!("there is an enum variant `{}`{}; ", enum_candidates[0].0, others)
931                } else {
932                    String::new()
933                };
934                let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}try using the variant\'s enum",
                preamble))
    })format!("{preamble}try using the variant's enum");
935
936                suggested_candidates.extend(
937                    enum_candidates
938                        .iter()
939                        .map(|(_variant_path, enum_ty_path)| enum_ty_path.clone()),
940                );
941                err.span_suggestions(
942                    span,
943                    msg,
944                    enum_candidates.into_iter().map(|(_variant_path, enum_ty_path)| enum_ty_path),
945                    Applicability::MachineApplicable,
946                );
947            }
948        }
949
950        // Try finding a suitable replacement.
951        let typo_sugg = self
952            .lookup_typo_candidate(path, following_seg, source.namespace(), is_expected)
953            .to_opt_suggestion()
954            .filter(|sugg| !suggested_candidates.contains(sugg.candidate.as_str()));
955        if let [segment] = path
956            && !#[allow(non_exhaustive_omitted_patterns)] match source {
    PathSource::Delegation => true,
    _ => false,
}matches!(source, PathSource::Delegation)
957            && self.self_type_is_available()
958        {
959            if let Some(candidate) =
960                self.lookup_assoc_candidate(ident, ns, is_expected, source.is_call())
961            {
962                let self_is_available = self.self_value_is_available(segment.ident.span);
963                // Account for `Foo { field }` when suggesting `self.field` so we result on
964                // `Foo { field: self.field }`.
965                let pre = match source {
966                    PathSource::Expr(Some(Expr { kind: ExprKind::Struct(expr), .. }))
967                        if expr
968                            .fields
969                            .iter()
970                            .any(|f| f.ident == segment.ident && f.is_shorthand) =>
971                    {
972                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: ", path_str))
    })format!("{path_str}: ")
973                    }
974                    _ => String::new(),
975                };
976                match candidate {
977                    AssocSuggestion::Field(field_span) => {
978                        if self_is_available {
979                            let source_map = self.r.tcx.sess.source_map();
980                            let field_is_format_named_arg = #[allow(non_exhaustive_omitted_patterns)] match span.desugaring_kind() {
    Some(DesugaringKind::FormatLiteral { .. }) => true,
    _ => false,
}matches!(
981                                span.desugaring_kind(),
982                                Some(DesugaringKind::FormatLiteral { .. })
983                            ) && source_map
984                                .span_to_source(span, |s, start, _| {
985                                    Ok(s.get(start.saturating_sub(1)..start) == Some("{"))
986                                })
987                                .unwrap_or(false);
988                            if field_is_format_named_arg {
989                                err.help(
990                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might have meant to use the available field in a format string: `\"{{}}\", self.{0}`",
                segment.ident.name))
    })format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
991                                );
992                            } else {
993                                err.span_suggestion_verbose(
994                                    span.shrink_to_lo(),
995                                    "you might have meant to use the available field",
996                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}self.", pre))
    })format!("{pre}self."),
997                                    Applicability::MaybeIncorrect,
998                                );
999                            }
1000                        } else {
1001                            err.span_label(field_span, "a field by that name exists in `Self`");
1002                        }
1003                    }
1004                    AssocSuggestion::MethodWithSelf { called } if self_is_available => {
1005                        let msg = if called {
1006                            "you might have meant to call the method"
1007                        } else {
1008                            "you might have meant to refer to the method"
1009                        };
1010                        err.span_suggestion_verbose(
1011                            span.shrink_to_lo(),
1012                            msg,
1013                            "self.",
1014                            Applicability::MachineApplicable,
1015                        );
1016                    }
1017                    AssocSuggestion::MethodWithSelf { .. }
1018                    | AssocSuggestion::AssocFn { .. }
1019                    | AssocSuggestion::AssocConst
1020                    | AssocSuggestion::AssocType => {
1021                        err.span_suggestion_verbose(
1022                            span.shrink_to_lo(),
1023                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might have meant to {0}",
                candidate.action()))
    })format!("you might have meant to {}", candidate.action()),
1024                            "Self::",
1025                            Applicability::MachineApplicable,
1026                        );
1027                    }
1028                }
1029                self.r.add_typo_suggestion(err, typo_sugg, ident_span);
1030                return (true, suggested_candidates, candidates);
1031            }
1032
1033            // If the first argument in call is `self` suggest calling a method.
1034            if let Some((call_span, args_span)) = self.call_has_self_arg(source) {
1035                let mut args_snippet = String::new();
1036                if let Some(args_span) = args_span
1037                    && let Ok(snippet) = self.r.tcx.sess.source_map().span_to_snippet(args_span)
1038                {
1039                    args_snippet = snippet;
1040                }
1041
1042                if let Some(Res::Def(DefKind::Struct, def_id)) = res {
1043                    if let Some(ctor) = self.r.struct_ctor(def_id)
1044                        && ctor.has_private_fields(self.parent_scope.module, self.r)
1045                    {
1046                        if #[allow(non_exhaustive_omitted_patterns)] match ctor.res {
    Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _) => true,
    _ => false,
}matches!(
1047                            ctor.res,
1048                            Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _)
1049                        ) {
1050                            self.update_err_for_private_tuple_struct_fields(err, &source, def_id);
1051                        }
1052                        err.note("constructor is not visible here due to private fields");
1053                    }
1054                } else {
1055                    err.span_suggestion(
1056                        call_span,
1057                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("try calling `{0}` as a method",
                ident))
    })format!("try calling `{ident}` as a method"),
1058                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("self.{0}({1})", path_str,
                args_snippet))
    })format!("self.{path_str}({args_snippet})"),
1059                        Applicability::MachineApplicable,
1060                    );
1061                }
1062
1063                return (true, suggested_candidates, candidates);
1064            }
1065        }
1066
1067        // Try context-dependent help if relaxed lookup didn't work.
1068        if let Some(res) = res {
1069            if self.smart_resolve_context_dependent_help(
1070                err,
1071                span,
1072                source,
1073                path,
1074                res,
1075                &path_str,
1076                &base_error.fallback_label,
1077            ) {
1078                // We do this to avoid losing a secondary span when we override the main error span.
1079                self.r.add_typo_suggestion(err, typo_sugg, ident_span);
1080                return (true, suggested_candidates, candidates);
1081            }
1082        }
1083
1084        // Try to find in last block rib
1085        if let Some(rib) = &self.last_block_rib {
1086            for (ident, &res) in &rib.bindings {
1087                if let Res::Local(_) = res
1088                    && path.len() == 1
1089                    && ident.span.eq_ctxt(path[0].ident.span)
1090                    && ident.name == path[0].ident.name
1091                {
1092                    err.span_help(
1093                        ident.span,
1094                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the binding `{0}` is available in a different scope in the same function",
                path_str))
    })format!("the binding `{path_str}` is available in a different scope in the same function"),
1095                    );
1096                    return (true, suggested_candidates, candidates);
1097                }
1098            }
1099        }
1100
1101        if candidates.is_empty() {
1102            candidates = self.smart_resolve_partial_mod_path_errors(path, following_seg);
1103        }
1104
1105        (false, suggested_candidates, candidates)
1106    }
1107
1108    fn lookup_doc_alias_name(&mut self, path: &[Segment], ns: Namespace) -> Option<(DefId, Ident)> {
1109        let find_doc_alias_name = |r: &mut Resolver<'ra, '_>, m: Module<'ra>, item_name: Symbol| {
1110            for resolution in r.resolutions(m).borrow().values() {
1111                let Some(did) =
1112                    resolution.borrow().best_decl().and_then(|binding| binding.res().opt_def_id())
1113                else {
1114                    continue;
1115                };
1116                if did.is_local() {
1117                    // We don't record the doc alias name in the local crate
1118                    // because the people who write doc alias are usually not
1119                    // confused by them.
1120                    continue;
1121                }
1122                if let Some(d) = {
    {
        'done:
            {
            for i in ::rustc_hir::attrs::HasAttrs::get_attrs(did, &r.tcx) {
                #[allow(unused_imports)]
                use rustc_hir::attrs::AttributeKind::*;
                let i: &rustc_hir::Attribute = i;
                match i {
                    rustc_hir::Attribute::Parsed(Doc(d)) => {
                        break 'done Some(d);
                    }
                    rustc_hir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }
}hir::find_attr!(r.tcx, did, Doc(d) => d)
1123                    && d.aliases.contains_key(&item_name)
1124                {
1125                    return Some(did);
1126                }
1127            }
1128            None
1129        };
1130
1131        if path.len() == 1 {
1132            for rib in self.ribs[ns].iter().rev() {
1133                let item = path[0].ident;
1134                if let RibKind::Module(module) | RibKind::Block(Some(module)) = rib.kind
1135                    && let Some(did) = find_doc_alias_name(self.r, module.to_module(), item.name)
1136                {
1137                    return Some((did, item));
1138                }
1139            }
1140        } else {
1141            // Finds to the last resolved module item in the path
1142            // and searches doc aliases within that module.
1143            //
1144            // Example: For the path `a::b::last_resolved::not_exist::c::d`,
1145            // we will try to find any item has doc aliases named `not_exist`
1146            // in `last_resolved` module.
1147            //
1148            // - Use `skip(1)` because the final segment must remain unresolved.
1149            for (idx, seg) in path.iter().enumerate().rev().skip(1) {
1150                let Some(id) = seg.id else {
1151                    continue;
1152                };
1153                let Some(res) = self.r.partial_res_map.get(&id) else {
1154                    continue;
1155                };
1156                if let Res::Def(DefKind::Mod, module) = res.expect_full_res()
1157                    && let module = self.r.expect_module(module)
1158                    && let item = path[idx + 1].ident
1159                    && let Some(did) = find_doc_alias_name(self.r, module, item.name)
1160                {
1161                    return Some((did, item));
1162                }
1163                break;
1164            }
1165        }
1166        None
1167    }
1168
1169    fn suggest_trait_and_bounds(
1170        &self,
1171        err: &mut Diag<'_>,
1172        source: PathSource<'_, '_, '_>,
1173        res: Option<Res>,
1174        span: Span,
1175        base_error: &BaseError,
1176    ) -> bool {
1177        let is_macro =
1178            base_error.span.from_expansion() && base_error.span.desugaring_kind().is_none();
1179        let mut fallback = false;
1180
1181        if let (
1182            PathSource::Trait(AliasPossibility::Maybe),
1183            Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)),
1184            false,
1185        ) = (source, res, is_macro)
1186            && let Some(bounds @ [first_bound, .., last_bound]) =
1187                self.diag_metadata.current_trait_object
1188        {
1189            fallback = true;
1190            let spans: Vec<Span> = bounds
1191                .iter()
1192                .map(|bound| bound.span())
1193                .filter(|&sp| sp != base_error.span)
1194                .collect();
1195
1196            let start_span = first_bound.span();
1197            // `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
1198            let end_span = last_bound.span();
1199            // `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
1200            let last_bound_span = spans.last().cloned().unwrap();
1201            let mut multi_span: MultiSpan = spans.clone().into();
1202            for sp in spans {
1203                let msg = if sp == last_bound_span {
1204                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("...because of {0} bound{1}",
                if bounds.len() - 1 == 1 { "this" } else { "these" },
                if bounds.len() - 1 == 1 { "" } else { "s" }))
    })format!(
1205                        "...because of {these} bound{s}",
1206                        these = pluralize!("this", bounds.len() - 1),
1207                        s = pluralize!(bounds.len() - 1),
1208                    )
1209                } else {
1210                    String::new()
1211                };
1212                multi_span.push_span_label(sp, msg);
1213            }
1214            multi_span.push_span_label(base_error.span, "expected this type to be a trait...");
1215            err.span_help(
1216                multi_span,
1217                "`+` is used to constrain a \"trait object\" type with lifetimes or \
1218                        auto-traits; structs and enums can't be bound in that way",
1219            );
1220            if bounds.iter().all(|bound| match bound {
1221                ast::GenericBound::Outlives(_) | ast::GenericBound::Use(..) => true,
1222                ast::GenericBound::Trait(tr) => tr.span == base_error.span,
1223            }) {
1224                let mut sugg = ::alloc::vec::Vec::new()vec![];
1225                if base_error.span != start_span {
1226                    sugg.push((start_span.until(base_error.span), String::new()));
1227                }
1228                if base_error.span != end_span {
1229                    sugg.push((base_error.span.shrink_to_hi().to(end_span), String::new()));
1230                }
1231
1232                err.multipart_suggestion(
1233                    "if you meant to use a type and not a trait here, remove the bounds",
1234                    sugg,
1235                    Applicability::MaybeIncorrect,
1236                );
1237            }
1238        }
1239
1240        fallback |= self.restrict_assoc_type_in_where_clause(span, err);
1241        fallback
1242    }
1243
1244    fn suggest_typo(
1245        &mut self,
1246        err: &mut Diag<'_>,
1247        source: PathSource<'_, 'ast, 'ra>,
1248        path: &[Segment],
1249        following_seg: Option<&Segment>,
1250        span: Span,
1251        base_error: &BaseError,
1252        suggested_candidates: FxHashSet<String>,
1253    ) -> bool {
1254        let is_expected = &|res| source.is_expected(res);
1255        let ident_span = path.last().map_or(span, |ident| ident.ident.span);
1256
1257        // Prefer suggestions based on associated types from in-scope bounds (e.g. `T::Item`)
1258        // over purely edit-distance-based identifier suggestions.
1259        // Otherwise suggestions could be verbose.
1260        if self.suggest_assoc_type_from_bounds(err, source, path, ident_span) {
1261            return false;
1262        }
1263
1264        let typo_sugg =
1265            self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
1266        let mut fallback = false;
1267        let typo_sugg = typo_sugg
1268            .to_opt_suggestion()
1269            .filter(|sugg| !suggested_candidates.contains(sugg.candidate.as_str()));
1270        if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) {
1271            fallback = true;
1272            match self.diag_metadata.current_let_binding {
1273                Some((pat_sp, Some(ty_sp), None))
1274                    if ty_sp.contains(base_error.span) && base_error.could_be_expr =>
1275                {
1276                    err.span_suggestion_verbose(
1277                        pat_sp.between(ty_sp),
1278                        "use `=` if you meant to assign",
1279                        " = ",
1280                        Applicability::MaybeIncorrect,
1281                    );
1282                }
1283                _ => {}
1284            }
1285
1286            // If the trait has a single item (which wasn't matched by the algorithm), suggest it
1287            let suggestion = self.get_single_associated_item(path, &source, is_expected);
1288            self.r.add_typo_suggestion(err, suggestion, ident_span);
1289        }
1290
1291        if self.let_binding_suggestion(err, ident_span) {
1292            fallback = false;
1293        }
1294
1295        fallback
1296    }
1297
1298    fn suggest_shadowed(
1299        &mut self,
1300        err: &mut Diag<'_>,
1301        source: PathSource<'_, '_, '_>,
1302        path: &[Segment],
1303        following_seg: Option<&Segment>,
1304        span: Span,
1305    ) -> bool {
1306        let is_expected = &|res| source.is_expected(res);
1307        let typo_sugg =
1308            self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
1309        let is_in_same_file = &|sp1, sp2| {
1310            let source_map = self.r.tcx.sess.source_map();
1311            let file1 = source_map.span_to_filename(sp1);
1312            let file2 = source_map.span_to_filename(sp2);
1313            file1 == file2
1314        };
1315        // print 'you might have meant' if the candidate is (1) is a shadowed name with
1316        // accessible definition and (2) either defined in the same crate as the typo
1317        // (could be in a different file) or introduced in the same file as the typo
1318        // (could belong to a different crate)
1319        if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
1320            && res.opt_def_id().is_some_and(|id| id.is_local() || is_in_same_file(span, sugg_span))
1321        {
1322            err.span_label(
1323                sugg_span,
1324                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might have meant to refer to this {0}",
                res.descr()))
    })format!("you might have meant to refer to this {}", res.descr()),
1325            );
1326            return true;
1327        }
1328        false
1329    }
1330
1331    fn err_code_special_cases(
1332        &mut self,
1333        err: &mut Diag<'_>,
1334        source: PathSource<'_, '_, '_>,
1335        path: &[Segment],
1336        span: Span,
1337    ) {
1338        if let Some(err_code) = err.code {
1339            if err_code == E0425 {
1340                for label_rib in &self.label_ribs {
1341                    for (label_ident, node_id) in &label_rib.bindings {
1342                        let ident = path.last().unwrap().ident;
1343                        if ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\'{0}", ident))
    })format!("'{ident}") == label_ident.to_string() {
1344                            err.span_label(label_ident.span, "a label with a similar name exists");
1345                            if let PathSource::Expr(Some(Expr {
1346                                kind: ExprKind::Break(None, Some(_)),
1347                                ..
1348                            })) = source
1349                            {
1350                                err.span_suggestion(
1351                                    span,
1352                                    "use the similarly named label",
1353                                    label_ident.name,
1354                                    Applicability::MaybeIncorrect,
1355                                );
1356                                // Do not lint against unused label when we suggest them.
1357                                self.diag_metadata.unused_labels.swap_remove(node_id);
1358                            }
1359                        }
1360                    }
1361                }
1362
1363                self.suggest_ident_hidden_by_hygiene(err, path, span);
1364                // cannot find type in this scope
1365                if let Some(correct) = Self::likely_rust_type(path) {
1366                    err.span_suggestion(
1367                        span,
1368                        "perhaps you intended to use this type",
1369                        correct,
1370                        Applicability::MaybeIncorrect,
1371                    );
1372                }
1373            }
1374        }
1375    }
1376
1377    fn suggest_ident_hidden_by_hygiene(&self, err: &mut Diag<'_>, path: &[Segment], span: Span) {
1378        let [segment] = path else { return };
1379
1380        let ident = segment.ident;
1381        let callsite_span = span.source_callsite();
1382        for rib in self.ribs[ValueNS].iter().rev() {
1383            for (binding_ident, _) in &rib.bindings {
1384                // Case 1: the identifier is defined in the same scope as the macro is called
1385                if binding_ident.name == ident.name
1386                    && !binding_ident.span.eq_ctxt(span)
1387                    && !binding_ident.span.from_expansion()
1388                    && binding_ident.span.lo() < callsite_span.lo()
1389                {
1390                    err.span_help(
1391                        binding_ident.span,
1392                        "an identifier with the same name exists, but is not accessible due to macro hygiene",
1393                    );
1394                    return;
1395                }
1396
1397                // Case 2: the identifier is defined in a macro call in the same scope
1398                if binding_ident.name == ident.name
1399                    && binding_ident.span.from_expansion()
1400                    && binding_ident.span.source_callsite().eq_ctxt(callsite_span)
1401                    && binding_ident.span.source_callsite().lo() < callsite_span.lo()
1402                {
1403                    err.span_help(
1404                        binding_ident.span,
1405                        "an identifier with the same name is defined here, but is not accessible due to macro hygiene",
1406                    );
1407                    return;
1408                }
1409            }
1410        }
1411    }
1412
1413    /// Emit special messages for unresolved `Self` and `self`.
1414    fn suggest_self_ty(
1415        &self,
1416        err: &mut Diag<'_>,
1417        source: PathSource<'_, '_, '_>,
1418        path: &[Segment],
1419        span: Span,
1420    ) -> bool {
1421        if !is_self_type(path, source.namespace()) {
1422            return false;
1423        }
1424        err.code(E0411);
1425        err.span_label(span, "`Self` is only available in impls, traits, and type definitions");
1426        if let Some(item) = self.diag_metadata.current_item
1427            && let Some(ident) = item.kind.ident()
1428        {
1429            err.span_label(
1430                ident.span,
1431                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`Self` not allowed in {0} {1}",
                item.kind.article(), item.kind.descr()))
    })format!("`Self` not allowed in {} {}", item.kind.article(), item.kind.descr()),
1432            );
1433        }
1434        true
1435    }
1436
1437    fn suggest_self_value(
1438        &mut self,
1439        err: &mut Diag<'_>,
1440        source: PathSource<'_, '_, '_>,
1441        path: &[Segment],
1442        span: Span,
1443    ) -> bool {
1444        if !is_self_value(path, source.namespace()) {
1445            return false;
1446        }
1447
1448        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:1448",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(1448u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::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!("smart_resolve_path_fragment: E0424, source={0:?}",
                                                    source) as &dyn Value))])
            });
    } else { ; }
};debug!("smart_resolve_path_fragment: E0424, source={:?}", source);
1449        err.code(E0424);
1450        err.span_label(
1451            span,
1452            match source {
1453                PathSource::Pat => {
1454                    "`self` value is a keyword and may not be bound to variables or shadowed"
1455                }
1456                _ => "`self` value is a keyword only available in methods with a `self` parameter",
1457            },
1458        );
1459
1460        // using `let self` is wrong even if we're not in an associated method or if we're in a macro expansion.
1461        // So, we should return early if we're in a pattern, see issue #143134.
1462        if #[allow(non_exhaustive_omitted_patterns)] match source {
    PathSource::Pat => true,
    _ => false,
}matches!(source, PathSource::Pat) {
1463            return true;
1464        }
1465
1466        let is_assoc_fn = self.self_type_is_available();
1467        let self_from_macro = "a `self` parameter, but a macro invocation can only \
1468                               access identifiers it receives from parameters";
1469        if let Some((fn_kind, fn_span)) = &self.diag_metadata.current_function {
1470            // The current function has a `self` parameter, but we were unable to resolve
1471            // a reference to `self`. This can only happen if the `self` identifier we
1472            // are resolving came from a different hygiene context or a variable binding.
1473            // But variable binding error is returned early above.
1474            if fn_kind.decl().inputs.get(0).is_some_and(|p| p.is_self()) {
1475                err.span_label(*fn_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this function has {0}",
                self_from_macro))
    })format!("this function has {self_from_macro}"));
1476            } else {
1477                let doesnt = if is_assoc_fn {
1478                    let (span, sugg) = fn_kind
1479                        .decl()
1480                        .inputs
1481                        .get(0)
1482                        .map(|p| (p.span.shrink_to_lo(), "&self, "))
1483                        .unwrap_or_else(|| {
1484                            // Try to look for the "(" after the function name, if possible.
1485                            // This avoids placing the suggestion into the visibility specifier.
1486                            let span = fn_kind
1487                                .ident()
1488                                .map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi()));
1489                            (
1490                                self.r
1491                                    .tcx
1492                                    .sess
1493                                    .source_map()
1494                                    .span_through_char(span, '(')
1495                                    .shrink_to_hi(),
1496                                "&self",
1497                            )
1498                        });
1499                    err.span_suggestion_verbose(
1500                        span,
1501                        "add a `self` receiver parameter to make the associated `fn` a method",
1502                        sugg,
1503                        Applicability::MaybeIncorrect,
1504                    );
1505                    "doesn't"
1506                } else {
1507                    "can't"
1508                };
1509                if let Some(ident) = fn_kind.ident() {
1510                    err.span_label(
1511                        ident.span,
1512                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this function {0} have a `self` parameter",
                doesnt))
    })format!("this function {doesnt} have a `self` parameter"),
1513                    );
1514                }
1515            }
1516        } else if let Some(item) = self.diag_metadata.current_item {
1517            if #[allow(non_exhaustive_omitted_patterns)] match item.kind {
    ItemKind::Delegation(..) => true,
    _ => false,
}matches!(item.kind, ItemKind::Delegation(..)) {
1518                err.span_label(item.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("delegation supports {0}",
                self_from_macro))
    })format!("delegation supports {self_from_macro}"));
1519            } else {
1520                let span = if let Some(ident) = item.kind.ident() { ident.span } else { item.span };
1521                err.span_label(
1522                    span,
1523                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`self` not allowed in {0} {1}",
                item.kind.article(), item.kind.descr()))
    })format!("`self` not allowed in {} {}", item.kind.article(), item.kind.descr()),
1524                );
1525            }
1526        }
1527        true
1528    }
1529
1530    fn detect_missing_binding_available_from_pattern(
1531        &self,
1532        err: &mut Diag<'_>,
1533        path: &[Segment],
1534        following_seg: Option<&Segment>,
1535    ) {
1536        let [segment] = path else { return };
1537        let None = following_seg else { return };
1538        for rib in self.ribs[ValueNS].iter().rev() {
1539            let patterns_with_skipped_bindings =
1540                self.r.tcx.with_stable_hashing_context(|mut hcx| {
1541                    rib.patterns_with_skipped_bindings.to_sorted(&mut hcx, true)
1542                });
1543            for (def_id, spans) in patterns_with_skipped_bindings {
1544                if let DefKind::Struct | DefKind::Variant = self.r.tcx.def_kind(*def_id)
1545                    && let Some(fields) = self.r.field_idents(*def_id)
1546                {
1547                    for field in fields {
1548                        if field.name == segment.ident.name {
1549                            if spans.iter().all(|(_, had_error)| had_error.is_err()) {
1550                                // This resolution error will likely be fixed by fixing a
1551                                // syntax error in a pattern, so it is irrelevant to the user.
1552                                let multispan: MultiSpan =
1553                                    spans.iter().map(|(s, _)| *s).collect::<Vec<_>>().into();
1554                                err.span_note(
1555                                    multispan,
1556                                    "this pattern had a recovered parse error which likely lost \
1557                                     the expected fields",
1558                                );
1559                                err.downgrade_to_delayed_bug();
1560                            }
1561                            let ty = self.r.tcx.item_name(*def_id);
1562                            for (span, _) in spans {
1563                                err.span_label(
1564                                    *span,
1565                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this pattern doesn\'t include `{0}`, which is available in `{1}`",
                field, ty))
    })format!(
1566                                        "this pattern doesn't include `{field}`, which is \
1567                                         available in `{ty}`",
1568                                    ),
1569                                );
1570                            }
1571                        }
1572                    }
1573                }
1574            }
1575        }
1576    }
1577
1578    fn suggest_at_operator_in_slice_pat_with_range(&self, err: &mut Diag<'_>, path: &[Segment]) {
1579        let Some(pat) = self.diag_metadata.current_pat else { return };
1580        let (bound, side, range) = match &pat.kind {
1581            ast::PatKind::Range(Some(bound), None, range) => (bound, Side::Start, range),
1582            ast::PatKind::Range(None, Some(bound), range) => (bound, Side::End, range),
1583            _ => return,
1584        };
1585        if let ExprKind::Path(None, range_path) = &bound.kind
1586            && let [segment] = &range_path.segments[..]
1587            && let [s] = path
1588            && segment.ident == s.ident
1589            && segment.ident.span.eq_ctxt(range.span)
1590        {
1591            // We've encountered `[first, rest..]` (#88404) or `[first, ..rest]` (#120591)
1592            // where the user might have meant `[first, rest @ ..]`.
1593            let (span, snippet) = match side {
1594                Side::Start => (segment.ident.span.between(range.span), " @ ".into()),
1595                Side::End => (range.span.to(segment.ident.span), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} @ ..", segment.ident))
    })format!("{} @ ..", segment.ident)),
1596            };
1597            err.subdiagnostic(diagnostics::UnexpectedResUseAtOpInSlicePatWithRangeSugg {
1598                span,
1599                ident: segment.ident,
1600                snippet,
1601            });
1602        }
1603
1604        enum Side {
1605            Start,
1606            End,
1607        }
1608    }
1609
1610    fn suggest_range_struct_destructuring(
1611        &mut self,
1612        err: &mut Diag<'_>,
1613        path: &[Segment],
1614        source: PathSource<'_, '_, '_>,
1615    ) {
1616        if !#[allow(non_exhaustive_omitted_patterns)] match source {
    PathSource::Pat | PathSource::TupleStruct(..) | PathSource::Expr(..) =>
        true,
    _ => false,
}matches!(source, PathSource::Pat | PathSource::TupleStruct(..) | PathSource::Expr(..)) {
1617            return;
1618        }
1619
1620        let Some(pat) = self.diag_metadata.current_pat else { return };
1621        let ast::PatKind::Range(start, end, end_kind) = &pat.kind else { return };
1622
1623        let [segment] = path else { return };
1624        let failing_span = segment.ident.span;
1625
1626        let in_start = start.as_ref().is_some_and(|e| e.span.contains(failing_span));
1627        let in_end = end.as_ref().is_some_and(|e| e.span.contains(failing_span));
1628
1629        if !in_start && !in_end {
1630            return;
1631        }
1632
1633        let start_snippet =
1634            start.as_ref().and_then(|e| self.r.tcx.sess.source_map().span_to_snippet(e.span).ok());
1635        let end_snippet =
1636            end.as_ref().and_then(|e| self.r.tcx.sess.source_map().span_to_snippet(e.span).ok());
1637
1638        let field = |name: &str, val: String| {
1639            if val == name { val } else { ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: {1}", name, val))
    })format!("{name}: {val}") }
1640        };
1641
1642        let mut resolve_short_name = |short: Symbol, full: &str| -> String {
1643            let ident = Ident::with_dummy_span(short);
1644            let path = Segment::from_path(&Path::from_ident(ident));
1645
1646            match self.resolve_path(&path, Some(TypeNS), None, PathSource::Type) {
1647                PathResult::NonModule(..) => short.to_string(),
1648                _ => full.to_string(),
1649            }
1650        };
1651        // FIXME(new_range): Also account for new range types
1652        let (struct_path, fields) = match (start_snippet, end_snippet, &end_kind.node) {
1653            (Some(start), Some(end), ast::RangeEnd::Excluded) => (
1654                resolve_short_name(sym::Range, "std::ops::Range"),
1655                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [field("start", start), field("end", end)]))vec![field("start", start), field("end", end)],
1656            ),
1657            (Some(start), Some(end), ast::RangeEnd::Included(_)) => (
1658                resolve_short_name(sym::RangeInclusive, "std::ops::RangeInclusive"),
1659                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [field("start", start), field("end", end)]))vec![field("start", start), field("end", end)],
1660            ),
1661            (Some(start), None, _) => (
1662                resolve_short_name(sym::RangeFrom, "std::ops::RangeFrom"),
1663                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [field("start", start)]))vec![field("start", start)],
1664            ),
1665            (None, Some(end), ast::RangeEnd::Excluded) => {
1666                (resolve_short_name(sym::RangeTo, "std::ops::RangeTo"), ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [field("end", end)]))vec![field("end", end)])
1667            }
1668            (None, Some(end), ast::RangeEnd::Included(_)) => (
1669                resolve_short_name(sym::RangeToInclusive, "std::ops::RangeToInclusive"),
1670                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [field("end", end)]))vec![field("end", end)],
1671            ),
1672            _ => return,
1673        };
1674
1675        err.span_suggestion_verbose(
1676            pat.span,
1677            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if you meant to destructure a range use a struct pattern"))
    })format!("if you meant to destructure a range use a struct pattern"),
1678            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} {{ {1} }}", struct_path,
                fields.join(", ")))
    })format!("{} {{ {} }}", struct_path, fields.join(", ")),
1679            Applicability::MaybeIncorrect,
1680        );
1681
1682        err.note(
1683            "range patterns match against the start and end of a range; \
1684             to bind the components, use a struct pattern",
1685        );
1686    }
1687
1688    fn suggest_swapping_misplaced_self_ty_and_trait(
1689        &mut self,
1690        err: &mut Diag<'_>,
1691        source: PathSource<'_, 'ast, 'ra>,
1692        res: Option<Res>,
1693        span: Span,
1694    ) {
1695        if let Some((trait_ref, self_ty)) =
1696            self.diag_metadata.currently_processing_impl_trait.clone()
1697            && let TyKind::Path(_, self_ty_path) = &self_ty.kind
1698            && let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
1699                self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None, source)
1700            && module.def_kind() == Some(DefKind::Trait)
1701            && trait_ref.path.span == span
1702            && let PathSource::Trait(_) = source
1703            && let Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) = res
1704            && let Ok(self_ty_str) = self.r.tcx.sess.source_map().span_to_snippet(self_ty.span)
1705            && let Ok(trait_ref_str) =
1706                self.r.tcx.sess.source_map().span_to_snippet(trait_ref.path.span)
1707        {
1708            err.multipart_suggestion(
1709                    "`impl` items mention the trait being implemented first and the type it is being implemented for second",
1710                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)]))vec![(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)],
1711                    Applicability::MaybeIncorrect,
1712                );
1713        }
1714    }
1715
1716    fn explain_functions_in_pattern(
1717        &self,
1718        err: &mut Diag<'_>,
1719        res: Option<Res>,
1720        source: PathSource<'_, '_, '_>,
1721    ) {
1722        let PathSource::TupleStruct(_, _) = source else { return };
1723        let Some(Res::Def(DefKind::Fn, _)) = res else { return };
1724        err.primary_message("expected a pattern, found a function call");
1725        err.note("function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch19-00-patterns.html>");
1726    }
1727
1728    fn suggest_changing_type_to_const_param(
1729        &self,
1730        err: &mut Diag<'_>,
1731        res: Option<Res>,
1732        source: PathSource<'_, '_, '_>,
1733        path: &[Segment],
1734        following_seg: Option<&Segment>,
1735        span: Span,
1736    ) {
1737        if let PathSource::Expr(None) = source
1738            && let Some(Res::Def(DefKind::TyParam, _)) = res
1739            && following_seg.is_none()
1740            && let [segment] = path
1741        {
1742            // We have something like
1743            // impl<T, N> From<[T; N]> for VecWrapper<T> {
1744            //     fn from(slice: [T; N]) -> Self {
1745            //         VecWrapper(slice.to_vec())
1746            //     }
1747            // }
1748            // where `N` is a type param but should likely have been a const param.
1749            let Some(item) = self.diag_metadata.current_item else { return };
1750            let Some(generics) = item.kind.generics() else { return };
1751            let Some(span) = generics.params.iter().find_map(|param| {
1752                // Only consider type params with no bounds.
1753                if param.bounds.is_empty() && param.ident.name == segment.ident.name {
1754                    Some(param.ident.span)
1755                } else {
1756                    None
1757                }
1758            }) else {
1759                return;
1760            };
1761            err.subdiagnostic(diagnostics::UnexpectedResChangeTyParamToConstParamSugg {
1762                before: span.shrink_to_lo(),
1763                after: span.shrink_to_hi(),
1764            });
1765            return;
1766        }
1767        let PathSource::Trait(_) = source else { return };
1768
1769        // We don't include `DefKind::Str` and `DefKind::AssocTy` as they can't be reached here anyway.
1770        let applicability = match res {
1771            Some(Res::PrimTy(PrimTy::Int(_) | PrimTy::Uint(_) | PrimTy::Bool | PrimTy::Char)) => {
1772                Applicability::MachineApplicable
1773            }
1774            // FIXME(const_generics): Add `DefKind::TyParam` and `SelfTyParam` once we support generic
1775            // const generics. Of course, `Struct` and `Enum` may contain ty params, too, but the
1776            // benefits of including them here outweighs the small number of false positives.
1777            Some(Res::Def(DefKind::Struct | DefKind::Enum, _))
1778                if self.r.tcx.features().adt_const_params()
1779                    || self.r.tcx.features().min_adt_const_params() =>
1780            {
1781                Applicability::MaybeIncorrect
1782            }
1783            _ => return,
1784        };
1785
1786        let Some(item) = self.diag_metadata.current_item else { return };
1787        let Some(generics) = item.kind.generics() else { return };
1788
1789        let param = generics.params.iter().find_map(|param| {
1790            // Only consider type params with exactly one trait bound.
1791            if let [bound] = &*param.bounds
1792                && let ast::GenericBound::Trait(tref) = bound
1793                && tref.modifiers == ast::TraitBoundModifiers::NONE
1794                && tref.span == span
1795                && param.ident.span.eq_ctxt(span)
1796            {
1797                Some(param.ident.span)
1798            } else {
1799                None
1800            }
1801        });
1802
1803        if let Some(param) = param {
1804            err.subdiagnostic(diagnostics::UnexpectedResChangeTyToConstParamSugg {
1805                span: param.shrink_to_lo(),
1806                applicability,
1807            });
1808        }
1809    }
1810
1811    fn suggest_pattern_match_with_let(
1812        &self,
1813        err: &mut Diag<'_>,
1814        source: PathSource<'_, '_, '_>,
1815        span: Span,
1816    ) -> bool {
1817        if let PathSource::Expr(_) = source
1818            && let Some(Expr { span: expr_span, kind: ExprKind::Assign(lhs, _, _), .. }) =
1819                self.diag_metadata.in_if_condition
1820        {
1821            // Icky heuristic so we don't suggest:
1822            // `if (i + 2) = 2` => `if let (i + 2) = 2` (approximately pattern)
1823            // `if 2 = i` => `if let 2 = i` (lhs needs to contain error span)
1824            if lhs.is_approximately_pattern() && lhs.span.contains(span) {
1825                err.span_suggestion_verbose(
1826                    expr_span.shrink_to_lo(),
1827                    "you might have meant to use pattern matching",
1828                    "let ",
1829                    Applicability::MaybeIncorrect,
1830                );
1831                return true;
1832            }
1833        }
1834        false
1835    }
1836
1837    fn get_single_associated_item(
1838        &mut self,
1839        path: &[Segment],
1840        source: &PathSource<'_, 'ast, 'ra>,
1841        filter_fn: &impl Fn(Res) -> bool,
1842    ) -> Option<TypoSuggestion> {
1843        if let crate::PathSource::TraitItem(_, _) = source {
1844            let mod_path = &path[..path.len() - 1];
1845            if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
1846                self.resolve_path(mod_path, None, None, *source)
1847            {
1848                let targets: Vec<_> = self
1849                    .r
1850                    .resolutions(module)
1851                    .borrow()
1852                    .iter()
1853                    .filter_map(|(key, resolution)| {
1854                        let resolution = resolution.borrow();
1855                        resolution.best_decl().map(|binding| binding.res()).and_then(|res| {
1856                            if filter_fn(res) {
1857                                Some((key.ident.name, resolution.orig_ident_span, res))
1858                            } else {
1859                                None
1860                            }
1861                        })
1862                    })
1863                    .collect();
1864                if let &[(name, orig_ident_span, res)] = targets.as_slice() {
1865                    return Some(TypoSuggestion::single_item(name, orig_ident_span, res));
1866                }
1867            }
1868        }
1869        None
1870    }
1871
1872    /// Given `where <T as Bar>::Baz: String`, suggest `where T: Bar<Baz = String>`.
1873    fn restrict_assoc_type_in_where_clause(&self, span: Span, err: &mut Diag<'_>) -> bool {
1874        // Detect that we are actually in a `where` predicate.
1875        let Some(ast::WherePredicate {
1876            kind:
1877                ast::WherePredicateKind::BoundPredicate(ast::WhereBoundPredicate {
1878                    bounded_ty,
1879                    bound_generic_params,
1880                    bounds,
1881                }),
1882            span: where_span,
1883            ..
1884        }) = self.diag_metadata.current_where_predicate
1885        else {
1886            return false;
1887        };
1888        if !bound_generic_params.is_empty() {
1889            return false;
1890        }
1891
1892        // Confirm that the target is an associated type.
1893        let ast::TyKind::Path(Some(qself), path) = &bounded_ty.kind else { return false };
1894        // use this to verify that ident is a type param.
1895        let Some(partial_res) = self.r.partial_res_map.get(&bounded_ty.id) else { return false };
1896        if !#[allow(non_exhaustive_omitted_patterns)] match partial_res.full_res() {
    Some(Res::Def(DefKind::AssocTy, _)) => true,
    _ => false,
}matches!(partial_res.full_res(), Some(Res::Def(DefKind::AssocTy, _))) {
1897            return false;
1898        }
1899
1900        let peeled_ty = qself.ty.peel_refs();
1901        let ast::TyKind::Path(None, type_param_path) = &peeled_ty.kind else { return false };
1902        // Confirm that the `SelfTy` is a type parameter.
1903        let Some(partial_res) = self.r.partial_res_map.get(&peeled_ty.id) else {
1904            return false;
1905        };
1906        if !#[allow(non_exhaustive_omitted_patterns)] match partial_res.full_res() {
    Some(Res::Def(DefKind::TyParam, _)) => true,
    _ => false,
}matches!(partial_res.full_res(), Some(Res::Def(DefKind::TyParam, _))) {
1907            return false;
1908        }
1909        let ([ast::PathSegment { args: None, .. }], [ast::GenericBound::Trait(poly_trait_ref)]) =
1910            (&type_param_path.segments[..], &bounds[..])
1911        else {
1912            return false;
1913        };
1914        let [ast::PathSegment { ident, args: None, id }] =
1915            &poly_trait_ref.trait_ref.path.segments[..]
1916        else {
1917            return false;
1918        };
1919        if poly_trait_ref.modifiers != ast::TraitBoundModifiers::NONE {
1920            return false;
1921        }
1922        if ident.span == span {
1923            let Some(partial_res) = self.r.partial_res_map.get(&id) else {
1924                return false;
1925            };
1926            if !#[allow(non_exhaustive_omitted_patterns)] match partial_res.full_res() {
    Some(Res::Def(..)) => true,
    _ => false,
}matches!(partial_res.full_res(), Some(Res::Def(..))) {
1927                return false;
1928            }
1929
1930            let Some(new_where_bound_predicate) =
1931                mk_where_bound_predicate(path, poly_trait_ref, &qself.ty)
1932            else {
1933                return false;
1934            };
1935            err.span_suggestion_verbose(
1936                *where_span,
1937                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("constrain the associated type to `{0}`",
                ident))
    })format!("constrain the associated type to `{ident}`"),
1938                where_bound_predicate_to_string(&new_where_bound_predicate),
1939                Applicability::MaybeIncorrect,
1940            );
1941        }
1942        true
1943    }
1944
1945    /// Check if the source is call expression and the first argument is `self`. If true,
1946    /// return the span of whole call and the span for all arguments expect the first one (`self`).
1947    fn call_has_self_arg(&self, source: PathSource<'_, '_, '_>) -> Option<(Span, Option<Span>)> {
1948        let mut has_self_arg = None;
1949        if let PathSource::Expr(Some(parent)) = source
1950            && let ExprKind::Call(_, args) = &parent.kind
1951            && !args.is_empty()
1952        {
1953            let mut expr_kind = &args[0].kind;
1954            loop {
1955                match expr_kind {
1956                    ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
1957                        if arg_name.segments[0].ident.name == kw::SelfLower {
1958                            let call_span = parent.span;
1959                            let tail_args_span = if args.len() > 1 {
1960                                Some(Span::new(
1961                                    args[1].span.lo(),
1962                                    args.last().unwrap().span.hi(),
1963                                    call_span.ctxt(),
1964                                    None,
1965                                ))
1966                            } else {
1967                                None
1968                            };
1969                            has_self_arg = Some((call_span, tail_args_span));
1970                        }
1971                        break;
1972                    }
1973                    ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
1974                    _ => break,
1975                }
1976            }
1977        }
1978        has_self_arg
1979    }
1980
1981    fn followed_by_brace(&self, span: Span) -> (bool, Option<Span>) {
1982        // HACK(estebank): find a better way to figure out that this was a
1983        // parser issue where a struct literal is being used on an expression
1984        // where a brace being opened means a block is being started. Look
1985        // ahead for the next text to see if `span` is followed by a `{`.
1986        let sm = self.r.tcx.sess.source_map();
1987        if let Some(open_brace_span) = sm.span_followed_by(span, "{") {
1988            // In case this could be a struct literal that needs to be surrounded
1989            // by parentheses, find the appropriate span.
1990            let close_brace_span =
1991                sm.span_to_next_source(open_brace_span).ok().and_then(|next_source| {
1992                    // Find the matching `}` accounting for nested braces.
1993                    let mut depth: u32 = 1;
1994                    let offset = next_source.char_indices().find_map(|(i, c)| {
1995                        match c {
1996                            '{' => depth += 1,
1997                            '}' if depth == 1 => return Some(i),
1998                            '}' => depth -= 1,
1999                            _ => {}
2000                        }
2001                        None
2002                    })?;
2003                    let start = open_brace_span.hi() + rustc_span::BytePos(offset as u32);
2004                    Some(open_brace_span.with_lo(start).with_hi(start + rustc_span::BytePos(1)))
2005                });
2006            let closing_brace = close_brace_span.map(|sp| span.to(sp));
2007            (true, closing_brace)
2008        } else {
2009            (false, None)
2010        }
2011    }
2012
2013    fn update_err_for_private_tuple_struct_fields(
2014        &mut self,
2015        err: &mut Diag<'_>,
2016        source: &PathSource<'_, '_, '_>,
2017        def_id: DefId,
2018    ) -> Option<Vec<Span>> {
2019        match source {
2020            // e.g. `if let Enum::TupleVariant(field1, field2) = _`
2021            PathSource::TupleStruct(_, pattern_spans) => {
2022                err.primary_message(
2023                    "cannot match against a tuple struct which contains private fields",
2024                );
2025
2026                // Use spans of the tuple struct pattern.
2027                Some(Vec::from(*pattern_spans))
2028            }
2029            // e.g. `let _ = Enum::TupleVariant(field1, field2);`
2030            PathSource::Expr(Some(Expr {
2031                kind: ExprKind::Call(path, args),
2032                span: call_span,
2033                ..
2034            })) => {
2035                err.primary_message(
2036                    "cannot initialize a tuple struct which contains private fields",
2037                );
2038                self.suggest_alternative_construction_methods(
2039                    def_id,
2040                    err,
2041                    path.span,
2042                    *call_span,
2043                    &args[..],
2044                );
2045
2046                self.r
2047                    .field_idents(def_id)
2048                    .map(|fields| fields.iter().map(|f| f.span).collect::<Vec<_>>())
2049            }
2050            _ => None,
2051        }
2052    }
2053
2054    /// Provides context-dependent help for errors reported by the `smart_resolve_path_fragment`
2055    /// function.
2056    /// Returns `true` if able to provide context-dependent help.
2057    fn smart_resolve_context_dependent_help(
2058        &mut self,
2059        err: &mut Diag<'_>,
2060        span: Span,
2061        source: PathSource<'_, '_, '_>,
2062        path: &[Segment],
2063        res: Res,
2064        path_str: &str,
2065        fallback_label: &str,
2066    ) -> bool {
2067        let ns = source.namespace();
2068        let is_expected = &|res| source.is_expected(res);
2069
2070        let path_sep = |this: &Self, err: &mut Diag<'_>, expr: &Expr, kind: DefKind| {
2071            const MESSAGE: &str = "use the path separator to refer to an item";
2072
2073            let (lhs_span, rhs_span) = match &expr.kind {
2074                ExprKind::Field(base, ident) => (base.span, ident.span),
2075                ExprKind::MethodCall(MethodCall { receiver, span, .. }) => (receiver.span, *span),
2076                _ => return false,
2077            };
2078
2079            if lhs_span.eq_ctxt(rhs_span) {
2080                err.span_suggestion_verbose(
2081                    lhs_span.between(rhs_span),
2082                    MESSAGE,
2083                    "::",
2084                    Applicability::MaybeIncorrect,
2085                );
2086                true
2087            } else if #[allow(non_exhaustive_omitted_patterns)] match kind {
    DefKind::Struct | DefKind::TyAlias => true,
    _ => false,
}matches!(kind, DefKind::Struct | DefKind::TyAlias)
2088                && let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
2089                && let Ok(snippet) = this.r.tcx.sess.source_map().span_to_snippet(lhs_source_span)
2090            {
2091                // The LHS is a type that originates from a macro call.
2092                // We have to add angle brackets around it.
2093
2094                err.span_suggestion_verbose(
2095                    lhs_source_span.until(rhs_span),
2096                    MESSAGE,
2097                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0}>::", snippet))
    })format!("<{snippet}>::"),
2098                    Applicability::MaybeIncorrect,
2099                );
2100                true
2101            } else {
2102                // Either we were unable to obtain the source span / the snippet or
2103                // the LHS originates from a macro call and it is not a type and thus
2104                // there is no way to replace `.` with `::` and still somehow suggest
2105                // valid Rust code.
2106
2107                false
2108            }
2109        };
2110
2111        let find_span = |source: &PathSource<'_, '_, '_>, err: &mut Diag<'_>| {
2112            match source {
2113                PathSource::Expr(Some(Expr { span, kind: ExprKind::Call(_, _), .. }))
2114                | PathSource::TupleStruct(span, _) => {
2115                    // We want the main underline to cover the suggested code as well for
2116                    // cleaner output.
2117                    err.span(*span);
2118                    *span
2119                }
2120                _ => span,
2121            }
2122        };
2123
2124        let bad_struct_syntax_suggestion = |this: &mut Self, err: &mut Diag<'_>, def_id: DefId| {
2125            let (followed_by_brace, closing_brace) = this.followed_by_brace(span);
2126
2127            match source {
2128                PathSource::Expr(Some(
2129                    parent @ Expr { kind: ExprKind::Field(..) | ExprKind::MethodCall(..), .. },
2130                )) if path_sep(this, err, parent, DefKind::Struct) => {}
2131                PathSource::Expr(
2132                    None
2133                    | Some(Expr {
2134                        kind:
2135                            ExprKind::Path(..)
2136                            | ExprKind::Binary(..)
2137                            | ExprKind::Unary(..)
2138                            | ExprKind::If(..)
2139                            | ExprKind::While(..)
2140                            | ExprKind::ForLoop { .. }
2141                            | ExprKind::Match(..),
2142                        ..
2143                    }),
2144                ) if followed_by_brace => {
2145                    if let Some(sp) = closing_brace {
2146                        err.span_label(span, fallback_label.to_string());
2147                        err.multipart_suggestion(
2148                            "surround the struct literal with parentheses",
2149                            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(sp.shrink_to_lo(), "(".to_string()),
                (sp.shrink_to_hi(), ")".to_string())]))vec![
2150                                (sp.shrink_to_lo(), "(".to_string()),
2151                                (sp.shrink_to_hi(), ")".to_string()),
2152                            ],
2153                            Applicability::MaybeIncorrect,
2154                        );
2155                    } else {
2156                        err.span_label(
2157                            span, // Note the parentheses surrounding the suggestion below
2158                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might want to surround a struct literal with parentheses: `({0} {{ /* fields */ }})`?",
                path_str))
    })format!(
2159                                "you might want to surround a struct literal with parentheses: \
2160                                 `({path_str} {{ /* fields */ }})`?"
2161                            ),
2162                        );
2163                    }
2164                }
2165                PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
2166                    let span = find_span(&source, err);
2167                    err.span_label(this.r.def_span(def_id), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` defined here", path_str))
    })format!("`{path_str}` defined here"));
2168
2169                    let (tail, descr, applicability, old_fields) = match source {
2170                        PathSource::Pat => ("", "pattern", Applicability::MachineApplicable, None),
2171                        PathSource::TupleStruct(_, args) => (
2172                            "",
2173                            "pattern",
2174                            Applicability::MachineApplicable,
2175                            Some(
2176                                args.iter()
2177                                    .map(|a| this.r.tcx.sess.source_map().span_to_snippet(*a).ok())
2178                                    .collect::<Vec<Option<String>>>(),
2179                            ),
2180                        ),
2181                        _ => (": val", "literal", Applicability::HasPlaceholders, None),
2182                    };
2183
2184                    // Imprecise for local structs without ctors, we don't keep fields for them.
2185                    let has_private_fields = match def_id.as_local() {
2186                        Some(def_id) => this.r.struct_ctors.get(&def_id).is_some_and(|ctor| {
2187                            ctor.has_private_fields(this.parent_scope.module, this.r)
2188                        }),
2189                        None => this.r.tcx.associated_item_def_ids(def_id).iter().any(|field_id| {
2190                            let vis = this.r.tcx.visibility(*field_id);
2191                            !this.r.is_accessible_from(vis, this.parent_scope.module)
2192                        }),
2193                    };
2194                    if !has_private_fields {
2195                        // If the fields of the type are private, we shouldn't be suggesting using
2196                        // the struct literal syntax at all, as that will cause a subsequent error.
2197                        let fields = this.r.field_idents(def_id);
2198                        let has_fields = fields.as_ref().is_some_and(|f| !f.is_empty());
2199
2200                        if let PathSource::Expr(Some(Expr {
2201                            kind: ExprKind::Call(path, args),
2202                            span,
2203                            ..
2204                        })) = source
2205                            && !args.is_empty()
2206                            && let Some(fields) = &fields
2207                            && args.len() == fields.len()
2208                        // Make sure we have same number of args as fields
2209                        {
2210                            let path_span = path.span;
2211                            let mut parts = Vec::new();
2212
2213                            // Start with the opening brace
2214                            parts.push((
2215                                path_span.shrink_to_hi().until(args[0].span),
2216                                "{".to_owned(),
2217                            ));
2218
2219                            for (field, arg) in fields.iter().zip(args.iter()) {
2220                                // Add the field name before the argument
2221                                parts.push((arg.span.shrink_to_lo(), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: ", field))
    })format!("{}: ", field)));
2222                            }
2223
2224                            // Add the closing brace
2225                            parts.push((
2226                                args.last().unwrap().span.shrink_to_hi().until(span.shrink_to_hi()),
2227                                "}".to_owned(),
2228                            ));
2229
2230                            err.multipart_suggestion(
2231                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("use struct {0} syntax instead of calling",
                descr))
    })format!("use struct {descr} syntax instead of calling"),
2232                                parts,
2233                                applicability,
2234                            );
2235                        } else {
2236                            let (fields, applicability) = match fields {
2237                                Some(fields) => {
2238                                    let fields = if let Some(old_fields) = old_fields {
2239                                        fields
2240                                            .iter()
2241                                            .enumerate()
2242                                            .map(|(idx, new)| (new, old_fields.get(idx)))
2243                                            .map(|(new, old)| {
2244                                                if let Some(Some(old)) = old
2245                                                    && new.as_str() != old
2246                                                {
2247                                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: {1}", new, old))
    })format!("{new}: {old}")
2248                                                } else {
2249                                                    new.to_string()
2250                                                }
2251                                            })
2252                                            .collect::<Vec<String>>()
2253                                    } else {
2254                                        fields
2255                                            .iter()
2256                                            .map(|f| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}", f, tail))
    })format!("{f}{tail}"))
2257                                            .collect::<Vec<String>>()
2258                                    };
2259
2260                                    (fields.join(", "), applicability)
2261                                }
2262                                None => {
2263                                    ("/* fields */".to_string(), Applicability::HasPlaceholders)
2264                                }
2265                            };
2266                            let pad = if has_fields { " " } else { "" };
2267                            err.span_suggestion(
2268                                span,
2269                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("use struct {0} syntax instead",
                descr))
    })format!("use struct {descr} syntax instead"),
2270                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} {{{1}{2}{1}}}", path_str, pad,
                fields))
    })format!("{path_str} {{{pad}{fields}{pad}}}"),
2271                                applicability,
2272                            );
2273                        }
2274                    }
2275                    if let PathSource::Expr(Some(Expr {
2276                        kind: ExprKind::Call(path, args),
2277                        span: call_span,
2278                        ..
2279                    })) = source
2280                    {
2281                        this.suggest_alternative_construction_methods(
2282                            def_id,
2283                            err,
2284                            path.span,
2285                            *call_span,
2286                            &args[..],
2287                        );
2288                    }
2289                }
2290                _ => {
2291                    err.span_label(span, fallback_label.to_string());
2292                }
2293            }
2294        };
2295
2296        match (res, source) {
2297            (
2298                Res::Def(DefKind::Macro(kinds), def_id),
2299                PathSource::Expr(Some(Expr {
2300                    kind: ExprKind::Index(..) | ExprKind::Call(..), ..
2301                }))
2302                | PathSource::Struct(_),
2303            ) if kinds.contains(MacroKinds::BANG) => {
2304                // Don't suggest macro if it's unstable.
2305                let suggestable = def_id.is_local()
2306                    || self.r.tcx.lookup_stability(def_id).is_none_or(|s| s.is_stable());
2307
2308                err.span_label(span, fallback_label.to_string());
2309
2310                // Don't suggest `!` for a macro invocation if there are generic args
2311                if path
2312                    .last()
2313                    .is_some_and(|segment| !segment.has_generic_args && !segment.has_lifetime_args)
2314                    && suggestable
2315                {
2316                    err.span_suggestion_verbose(
2317                        span.shrink_to_hi(),
2318                        "use `!` to invoke the macro",
2319                        "!",
2320                        Applicability::MaybeIncorrect,
2321                    );
2322                }
2323
2324                if path_str == "try" && span.is_rust_2015() {
2325                    err.note("if you want the `try` keyword, you need Rust 2018 or later");
2326                }
2327            }
2328            (Res::Def(DefKind::Macro(kinds), _), _) if kinds.contains(MacroKinds::BANG) => {
2329                err.span_label(span, fallback_label.to_string());
2330            }
2331            (Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {
2332                err.span_label(span, "type aliases cannot be used as traits");
2333                if self.r.tcx.sess.is_nightly_build() {
2334                    let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
2335                               `type` alias";
2336                    let span = self.r.def_span(def_id);
2337                    if let Ok(snip) = self.r.tcx.sess.source_map().span_to_snippet(span) {
2338                        // The span contains a type alias so we should be able to
2339                        // replace `type` with `trait`.
2340                        let snip = snip.replacen("type", "trait", 1);
2341                        err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect);
2342                    } else {
2343                        err.span_help(span, msg);
2344                    }
2345                }
2346            }
2347            (
2348                Res::Def(kind @ (DefKind::Mod | DefKind::Trait | DefKind::TyAlias), _),
2349                PathSource::Expr(Some(parent)),
2350            ) if path_sep(self, err, parent, kind) => {
2351                return true;
2352            }
2353            (
2354                Res::Def(DefKind::Enum, def_id),
2355                PathSource::TupleStruct(..) | PathSource::Expr(..),
2356            ) => {
2357                self.suggest_using_enum_variant(err, source, def_id, span);
2358            }
2359            (Res::Def(DefKind::Struct, def_id), source) if ns == ValueNS => {
2360                if let PathSource::Expr(Some(parent)) = source
2361                    && let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind
2362                {
2363                    bad_struct_syntax_suggestion(self, err, def_id);
2364                    return true;
2365                }
2366                let Some(ctor) = self.r.struct_ctor(def_id) else {
2367                    bad_struct_syntax_suggestion(self, err, def_id);
2368                    return true;
2369                };
2370
2371                // A type is re-exported and has an inaccessible constructor because it has fields
2372                // that are inaccessible from the reexport's scope, extend the diagnostic.
2373                let is_accessible = self.r.is_accessible_from(ctor.vis, self.parent_scope.module);
2374                if is_accessible
2375                    && let mod_path = &path[..path.len() - 1]
2376                    && let PathResult::Module(ModuleOrUniformRoot::Module(import_mod)) =
2377                        self.resolve_path(mod_path, Some(TypeNS), None, PathSource::Module)
2378                    && ctor.has_private_fields(import_mod, self.r)
2379                    && let Ok(import_decl) = self.r.cm().maybe_resolve_ident_in_module(
2380                        ModuleOrUniformRoot::Module(import_mod),
2381                        path.last().unwrap().ident,
2382                        TypeNS,
2383                        &self.parent_scope,
2384                        None,
2385                    )
2386                {
2387                    err.span_note(
2388                        import_decl.span,
2389                        "the type is accessed through this re-export, but the type's constructor \
2390                         is not visible in this import's scope due to private fields",
2391                    );
2392                    if !ctor.has_private_fields(self.parent_scope.module, self.r) {
2393                        err.span_suggestion_verbose(
2394                            span,
2395                            "the type can be constructed directly, because its fields are \
2396                             available from the current scope",
2397                            // Using `tcx.def_path_str` causes the compiler to hang.
2398                            // We don't need to handle foreign crate types because in that case you
2399                            // can't access the ctor either way.
2400                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("crate{0}",
                self.r.tcx.def_path(def_id).to_string_no_crate_verbose()))
    })format!(
2401                                "crate{}", // The method already has leading `::`.
2402                                self.r.tcx.def_path(def_id).to_string_no_crate_verbose(),
2403                            ),
2404                            Applicability::MachineApplicable,
2405                        );
2406                    }
2407                    self.update_err_for_private_tuple_struct_fields(err, &source, def_id);
2408                }
2409                if !is_expected(ctor.res) || is_accessible {
2410                    return true;
2411                }
2412
2413                let field_spans =
2414                    self.update_err_for_private_tuple_struct_fields(err, &source, def_id);
2415
2416                if let Some(spans) = field_spans
2417                    .filter(|spans| spans.len() > 0 && ctor.field_visibilities.len() == spans.len())
2418                {
2419                    let non_visible_spans: Vec<Span> = iter::zip(&ctor.field_visibilities, &spans)
2420                        .filter(|(vis, _)| {
2421                            !self.r.is_accessible_from(**vis, self.parent_scope.module)
2422                        })
2423                        .map(|(_, span)| *span)
2424                        .collect();
2425
2426                    if non_visible_spans.len() > 0 {
2427                        if let Some(fields) = self.r.field_visibility_spans.get(&def_id) {
2428                            err.multipart_suggestion(
2429                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider making the field{0} publicly accessible",
                if fields.len() == 1 { "" } else { "s" }))
    })format!(
2430                                    "consider making the field{} publicly accessible",
2431                                    pluralize!(fields.len())
2432                                ),
2433                                fields.iter().map(|span| (*span, "pub ".to_string())).collect(),
2434                                Applicability::MaybeIncorrect,
2435                            );
2436                        }
2437
2438                        let mut m: MultiSpan = non_visible_spans.clone().into();
2439                        non_visible_spans
2440                            .into_iter()
2441                            .for_each(|s| m.push_span_label(s, "private field"));
2442                        err.span_note(m, "constructor is not visible here due to private fields");
2443                    }
2444
2445                    return true;
2446                }
2447
2448                err.span_label(span, "constructor is not visible here due to private fields");
2449            }
2450            (Res::Def(DefKind::Union | DefKind::Variant, def_id), _) if ns == ValueNS => {
2451                bad_struct_syntax_suggestion(self, err, def_id);
2452            }
2453            (Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id), _) if ns == ValueNS => {
2454                match source {
2455                    PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
2456                        let span = find_span(&source, err);
2457                        err.span_label(
2458                            self.r.def_span(def_id),
2459                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` defined here", path_str))
    })format!("`{path_str}` defined here"),
2460                        );
2461                        err.span_suggestion(
2462                            span,
2463                            "use this syntax instead",
2464                            path_str,
2465                            Applicability::MaybeIncorrect,
2466                        );
2467                    }
2468                    _ => return false,
2469                }
2470            }
2471            (Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_def_id), _) if ns == ValueNS => {
2472                let def_id = self.r.tcx.parent(ctor_def_id);
2473                err.span_label(self.r.def_span(def_id), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` defined here", path_str))
    })format!("`{path_str}` defined here"));
2474                let fields = self.r.field_idents(def_id).map_or_else(
2475                    || "/* fields */".to_string(),
2476                    |field_ids| ::alloc::vec::from_elem("_", field_ids.len())vec!["_"; field_ids.len()].join(", "),
2477                );
2478                err.span_suggestion(
2479                    span,
2480                    "use the tuple variant pattern syntax instead",
2481                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}({1})", path_str, fields))
    })format!("{path_str}({fields})"),
2482                    Applicability::HasPlaceholders,
2483                );
2484            }
2485            (Res::SelfTyParam { .. } | Res::SelfTyAlias { .. }, _) if ns == ValueNS => {
2486                err.span_label(span, fallback_label.to_string());
2487                err.note("can't use `Self` as a constructor, you must use the implemented struct");
2488            }
2489            (
2490                Res::Def(DefKind::TyAlias | DefKind::AssocTy, _),
2491                PathSource::TraitItem(ValueNS, PathSource::TupleStruct(whole, args)),
2492            ) => {
2493                err.note("can't use a type alias as tuple pattern");
2494
2495                let mut suggestion = Vec::new();
2496
2497                if let &&[first, ..] = args
2498                    && let &&[.., last] = args
2499                {
2500                    suggestion.extend([
2501                        // "0: " has to be included here so that the fix is machine applicable.
2502                        //
2503                        // If this would only add " { " and then the code below add "0: ",
2504                        // rustfix would crash, because end of this suggestion is the same as start
2505                        // of the suggestion below. Thus, we have to merge these...
2506                        (span.between(first), " { 0: ".to_owned()),
2507                        (last.between(whole.shrink_to_hi()), " }".to_owned()),
2508                    ]);
2509
2510                    suggestion.extend(
2511                        args.iter()
2512                            .enumerate()
2513                            .skip(1) // See above
2514                            .map(|(index, &arg)| (arg.shrink_to_lo(), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: ", index))
    })format!("{index}: "))),
2515                    )
2516                } else {
2517                    suggestion.push((span.between(whole.shrink_to_hi()), " {}".to_owned()));
2518                }
2519
2520                err.multipart_suggestion(
2521                    "use struct pattern instead",
2522                    suggestion,
2523                    Applicability::MachineApplicable,
2524                );
2525            }
2526            (
2527                Res::Def(DefKind::TyAlias | DefKind::AssocTy, _),
2528                PathSource::TraitItem(
2529                    ValueNS,
2530                    PathSource::Expr(Some(ast::Expr {
2531                        span: whole,
2532                        kind: ast::ExprKind::Call(_, args),
2533                        ..
2534                    })),
2535                ),
2536            ) => {
2537                err.note("can't use a type alias as a constructor");
2538
2539                let mut suggestion = Vec::new();
2540
2541                if let [first, ..] = &**args
2542                    && let [.., last] = &**args
2543                {
2544                    suggestion.extend([
2545                        // "0: " has to be included here so that the fix is machine applicable.
2546                        //
2547                        // If this would only add " { " and then the code below add "0: ",
2548                        // rustfix would crash, because end of this suggestion is the same as start
2549                        // of the suggestion below. Thus, we have to merge these...
2550                        (span.between(first.span), " { 0: ".to_owned()),
2551                        (last.span.between(whole.shrink_to_hi()), " }".to_owned()),
2552                    ]);
2553
2554                    suggestion.extend(
2555                        args.iter()
2556                            .enumerate()
2557                            .skip(1) // See above
2558                            .map(|(index, arg)| (arg.span.shrink_to_lo(), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: ", index))
    })format!("{index}: "))),
2559                    )
2560                } else {
2561                    suggestion.push((span.between(whole.shrink_to_hi()), " {}".to_owned()));
2562                }
2563
2564                err.multipart_suggestion(
2565                    "use struct expression instead",
2566                    suggestion,
2567                    Applicability::MachineApplicable,
2568                );
2569            }
2570            _ => return false,
2571        }
2572        true
2573    }
2574
2575    fn suggest_alternative_construction_methods(
2576        &mut self,
2577        def_id: DefId,
2578        err: &mut Diag<'_>,
2579        path_span: Span,
2580        call_span: Span,
2581        args: &[Box<Expr>],
2582    ) {
2583        if def_id.is_local() {
2584            // Doing analysis on local `DefId`s would cause infinite recursion.
2585            return;
2586        }
2587        // Look at all the associated functions without receivers in the type's
2588        // inherent impls to look for builders that return `Self`
2589        let mut items = self
2590            .r
2591            .tcx
2592            .inherent_impls(def_id)
2593            .iter()
2594            .flat_map(|&i| self.r.tcx.associated_items(i).in_definition_order())
2595            // Only assoc fn with no receivers.
2596            .filter(|item| item.is_fn() && !item.is_method())
2597            .filter_map(|item| {
2598                // Only assoc fns that return `Self`
2599                let fn_sig = self.r.tcx.fn_sig(item.def_id).skip_binder();
2600                // Don't normalize the return type, because that can cause cycle errors.
2601                let ret_ty = fn_sig.output().skip_binder();
2602                let ty::Adt(def, _args) = ret_ty.kind() else {
2603                    return None;
2604                };
2605                let input_len = fn_sig.inputs().skip_binder().len();
2606                if def.did() != def_id {
2607                    return None;
2608                }
2609                let name = item.name();
2610                let order = !name.as_str().starts_with("new");
2611                Some((order, name, input_len))
2612            })
2613            .collect::<Vec<_>>();
2614        items.sort_by_key(|(order, _, _)| *order);
2615        let suggestion = |name, args| {
2616            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("::{1}({0})",
                std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", "),
                name))
    })format!("::{name}({})", std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", "))
2617        };
2618        match &items[..] {
2619            [] => {}
2620            [(_, name, len)] if *len == args.len() => {
2621                err.span_suggestion_verbose(
2622                    path_span.shrink_to_hi(),
2623                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might have meant to use the `{0}` associated function",
                name))
    })format!("you might have meant to use the `{name}` associated function",),
2624                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("::{0}", name))
    })format!("::{name}"),
2625                    Applicability::MaybeIncorrect,
2626                );
2627            }
2628            [(_, name, len)] => {
2629                err.span_suggestion_verbose(
2630                    path_span.shrink_to_hi().with_hi(call_span.hi()),
2631                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might have meant to use the `{0}` associated function",
                name))
    })format!("you might have meant to use the `{name}` associated function",),
2632                    suggestion(name, *len),
2633                    Applicability::MaybeIncorrect,
2634                );
2635            }
2636            _ => {
2637                err.span_suggestions_with_style(
2638                    path_span.shrink_to_hi().with_hi(call_span.hi()),
2639                    "you might have meant to use an associated function to build this type",
2640                    items.iter().map(|(_, name, len)| suggestion(name, *len)),
2641                    Applicability::MaybeIncorrect,
2642                    SuggestionStyle::ShowAlways,
2643                );
2644            }
2645        }
2646        // We'd ideally use `type_implements_trait` but don't have access to
2647        // the trait solver here. We can't use `get_diagnostic_item` or
2648        // `all_traits` in resolve either. So instead we abuse the import
2649        // suggestion machinery to get `std::default::Default` and perform some
2650        // checks to confirm that we got *only* that trait. We then see if the
2651        // Adt we have has a direct implementation of `Default`. If so, we
2652        // provide a structured suggestion.
2653        let default_trait = self
2654            .r
2655            .lookup_import_candidates(
2656                Ident::with_dummy_span(sym::Default),
2657                Namespace::TypeNS,
2658                &self.parent_scope,
2659                &|res: Res| #[allow(non_exhaustive_omitted_patterns)] match res {
    Res::Def(DefKind::Trait, _) => true,
    _ => false,
}matches!(res, Res::Def(DefKind::Trait, _)),
2660            )
2661            .iter()
2662            .filter_map(|candidate| candidate.did)
2663            .find(|did| {
        {
            'done:
                {
                for i in
                    ::rustc_hir::attrs::HasAttrs::get_attrs(*did, &self.r.tcx) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(RustcDiagnosticItem(sym::Default))
                            => {
                            break 'done Some(());
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }.is_some()find_attr!(self.r.tcx, *did, RustcDiagnosticItem(sym::Default)));
2664        let Some(default_trait) = default_trait else {
2665            return;
2666        };
2667        if self
2668            .r
2669            .extern_crate_map
2670            .items()
2671            // FIXME: This doesn't include impls like `impl Default for String`.
2672            .flat_map(|(_, crate_)| {
2673                UnordItems::new(
2674                    self.r.tcx.implementations_of_trait((*crate_, default_trait)).into_iter(),
2675                )
2676            })
2677            .filter_map(|(_, simplified_self_ty)| *simplified_self_ty)
2678            .filter_map(|simplified_self_ty| match simplified_self_ty {
2679                SimplifiedType::Adt(did) => Some(did),
2680                _ => None,
2681            })
2682            .any(|did| did == def_id)
2683        {
2684            err.multipart_suggestion(
2685                "consider using the `Default` trait",
2686                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(path_span.shrink_to_lo(), "<".to_string()),
                (path_span.shrink_to_hi().with_hi(call_span.hi()),
                    " as std::default::Default>::default()".to_string())]))vec![
2687                    (path_span.shrink_to_lo(), "<".to_string()),
2688                    (
2689                        path_span.shrink_to_hi().with_hi(call_span.hi()),
2690                        " as std::default::Default>::default()".to_string(),
2691                    ),
2692                ],
2693                Applicability::MaybeIncorrect,
2694            );
2695        }
2696    }
2697
2698    /// Given the target `ident` and `kind`, search for the similarly named associated item
2699    /// in `self.current_trait_ref`.
2700    pub(crate) fn find_similarly_named_assoc_item(
2701        &mut self,
2702        ident: Symbol,
2703        kind: &AssocItemKind,
2704    ) -> Option<Symbol> {
2705        let (module, _) = self.current_trait_ref.as_ref()?;
2706        if ident == kw::Underscore {
2707            // We do nothing for `_`.
2708            return None;
2709        }
2710
2711        let targets = self
2712            .r
2713            .resolutions(*module)
2714            .borrow()
2715            .iter()
2716            .filter_map(|(key, res)| res.borrow().best_decl().map(|binding| (key, binding.res())))
2717            .filter(|(_, res)| match (kind, res) {
2718                (AssocItemKind::Const(..), Res::Def(DefKind::AssocConst { .. }, _)) => true,
2719                (AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true,
2720                (AssocItemKind::Type(..), Res::Def(DefKind::AssocTy, _)) => true,
2721                (AssocItemKind::Delegation(_), Res::Def(DefKind::AssocFn, _)) => true,
2722                _ => false,
2723            })
2724            .map(|(key, _)| key.ident.name)
2725            .collect::<Vec<_>>();
2726
2727        find_best_match_for_name(&targets, ident, None)
2728    }
2729
2730    fn lookup_assoc_candidate<FilterFn>(
2731        &mut self,
2732        ident: Ident,
2733        ns: Namespace,
2734        filter_fn: FilterFn,
2735        called: bool,
2736    ) -> Option<AssocSuggestion>
2737    where
2738        FilterFn: Fn(Res) -> bool,
2739    {
2740        fn extract_node_id(t: &Ty) -> Option<NodeId> {
2741            match t.kind {
2742                TyKind::Path(None, _) => Some(t.id),
2743                TyKind::Ref(_, ref mut_ty) => extract_node_id(&mut_ty.ty),
2744                // This doesn't handle the remaining `Ty` variants as they are not
2745                // that commonly the self_type, it might be interesting to provide
2746                // support for those in future.
2747                _ => None,
2748            }
2749        }
2750        // Fields are generally expected in the same contexts as locals.
2751        if filter_fn(Res::Local(ast::DUMMY_NODE_ID)) {
2752            if let Some(node_id) = self.diag_metadata.current_self_type.and_then(extract_node_id)
2753                && let Some(resolution) = self.r.partial_res_map.get(&node_id)
2754                && let Some(Res::Def(DefKind::Struct | DefKind::Union, did)) = resolution.full_res()
2755                && let Some(fields) = self.r.field_idents(did)
2756                && let Some(field) = fields.iter().find(|id| ident.name == id.name)
2757            {
2758                // Look for a field with the same name in the current self_type.
2759                return Some(AssocSuggestion::Field(field.span));
2760            }
2761        }
2762
2763        if let Some(items) = self.diag_metadata.current_trait_assoc_items {
2764            for assoc_item in items {
2765                if let Some(assoc_ident) = assoc_item.kind.ident()
2766                    && assoc_ident == ident
2767                {
2768                    return Some(match &assoc_item.kind {
2769                        ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
2770                        ast::AssocItemKind::Fn(ast::Fn { sig, .. }) if sig.decl.has_self() => {
2771                            AssocSuggestion::MethodWithSelf { called }
2772                        }
2773                        ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn { called },
2774                        ast::AssocItemKind::Type(..) => AssocSuggestion::AssocType,
2775                        ast::AssocItemKind::Delegation(..)
2776                            if self
2777                                .r
2778                                .owners
2779                                .get(&assoc_item.id)
2780                                .and_then(|o| self.r.delegation_fn_sigs.get(&o.def_id))
2781                                .is_some_and(|sig| sig.has_self) =>
2782                        {
2783                            AssocSuggestion::MethodWithSelf { called }
2784                        }
2785                        ast::AssocItemKind::Delegation(..) => AssocSuggestion::AssocFn { called },
2786                        ast::AssocItemKind::MacCall(_) | ast::AssocItemKind::DelegationMac(..) => {
2787                            continue;
2788                        }
2789                    });
2790                }
2791            }
2792        }
2793
2794        // Look for associated items in the current trait.
2795        if let Some((module, _)) = self.current_trait_ref
2796            && let Ok(binding) = self.r.cm().maybe_resolve_ident_in_module(
2797                ModuleOrUniformRoot::Module(module),
2798                ident,
2799                ns,
2800                &self.parent_scope,
2801                None,
2802            )
2803        {
2804            let res = binding.res();
2805            if filter_fn(res) {
2806                match res {
2807                    Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) => {
2808                        let has_self = match def_id.as_local() {
2809                            Some(def_id) => self
2810                                .r
2811                                .delegation_fn_sigs
2812                                .get(&def_id)
2813                                .is_some_and(|sig| sig.has_self),
2814                            None => {
2815                                self.r.tcx.fn_arg_idents(def_id).first().is_some_and(|&ident| {
2816                                    #[allow(non_exhaustive_omitted_patterns)] match ident {
    Some(Ident { name: kw::SelfLower, .. }) => true,
    _ => false,
}matches!(ident, Some(Ident { name: kw::SelfLower, .. }))
2817                                })
2818                            }
2819                        };
2820                        if has_self {
2821                            return Some(AssocSuggestion::MethodWithSelf { called });
2822                        } else {
2823                            return Some(AssocSuggestion::AssocFn { called });
2824                        }
2825                    }
2826                    Res::Def(DefKind::AssocConst { .. }, _) => {
2827                        return Some(AssocSuggestion::AssocConst);
2828                    }
2829                    Res::Def(DefKind::AssocTy, _) => {
2830                        return Some(AssocSuggestion::AssocType);
2831                    }
2832                    _ => {}
2833                }
2834            }
2835        }
2836
2837        None
2838    }
2839
2840    fn lookup_typo_candidate(
2841        &mut self,
2842        path: &[Segment],
2843        following_seg: Option<&Segment>,
2844        ns: Namespace,
2845        filter_fn: &impl Fn(Res) -> bool,
2846    ) -> TypoCandidate {
2847        let mut names = Vec::new();
2848        if let [segment] = path {
2849            let mut ctxt = segment.ident.span.ctxt();
2850
2851            // Search in lexical scope.
2852            // Walk backwards up the ribs in scope and collect candidates.
2853            for rib in self.ribs[ns].iter().rev() {
2854                let rib_ctxt = if rib.kind.contains_params() {
2855                    ctxt.normalize_to_macros_2_0()
2856                } else {
2857                    ctxt.normalize_to_macro_rules()
2858                };
2859
2860                // Locals and type parameters
2861                for (ident, &res) in &rib.bindings {
2862                    if filter_fn(res) && ident.span.ctxt() == rib_ctxt {
2863                        names.push(TypoSuggestion::new(ident.name, ident.span, res));
2864                    }
2865                }
2866
2867                if let RibKind::Block(Some(module)) = rib.kind {
2868                    self.r.add_module_candidates(
2869                        module.to_module(),
2870                        &mut names,
2871                        &filter_fn,
2872                        Some(ctxt),
2873                    );
2874                } else if let RibKind::Module(module) = rib.kind {
2875                    // Encountered a module item, abandon ribs and look into that module and preludes.
2876                    let parent_scope =
2877                        &ParentScope { module: module.to_module(), ..self.parent_scope };
2878                    self.r.add_scope_set_candidates(
2879                        &mut names,
2880                        ScopeSet::All(ns),
2881                        parent_scope,
2882                        segment.ident.span.with_ctxt(ctxt),
2883                        filter_fn,
2884                    );
2885                    break;
2886                }
2887
2888                if let RibKind::MacroDefinition(def) = rib.kind
2889                    && def == self.r.macro_def(ctxt)
2890                {
2891                    // If an invocation of this macro created `ident`, give up on `ident`
2892                    // and switch to `ident`'s source from the macro definition.
2893                    ctxt.remove_mark();
2894                }
2895            }
2896        } else {
2897            // Search in module.
2898            let mod_path = &path[..path.len() - 1];
2899            if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
2900                self.resolve_path(mod_path, Some(TypeNS), None, PathSource::Type)
2901            {
2902                self.r.add_module_candidates(module, &mut names, &filter_fn, None);
2903            }
2904        }
2905
2906        // if next_seg is present, let's filter everything that does not continue the path
2907        if let Some(following_seg) = following_seg {
2908            names.retain(|suggestion| match suggestion.res {
2909                Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _) => {
2910                    // FIXME: this is not totally accurate, but mostly works
2911                    suggestion.candidate != following_seg.ident.name
2912                }
2913                Res::Def(DefKind::Mod, def_id) => {
2914                    let module = self.r.expect_module(def_id);
2915                    self.r
2916                        .resolutions(module)
2917                        .borrow()
2918                        .iter()
2919                        .any(|(key, _)| key.ident.name == following_seg.ident.name)
2920                }
2921                _ => true,
2922            });
2923        }
2924        let name = path[path.len() - 1].ident.name;
2925        // Make sure error reporting is deterministic.
2926        names.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str()));
2927
2928        match find_best_match_for_name(
2929            &names.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),
2930            name,
2931            None,
2932        ) {
2933            Some(found) => {
2934                let Some(sugg) = names.into_iter().find(|suggestion| suggestion.candidate == found)
2935                else {
2936                    return TypoCandidate::None;
2937                };
2938                if found == name {
2939                    TypoCandidate::Shadowed(sugg.res, sugg.span)
2940                } else {
2941                    TypoCandidate::Typo(sugg)
2942                }
2943            }
2944            _ => TypoCandidate::None,
2945        }
2946    }
2947
2948    // Returns the name of the Rust type approximately corresponding to
2949    // a type name in another programming language.
2950    fn likely_rust_type(path: &[Segment]) -> Option<Symbol> {
2951        let name = path[path.len() - 1].ident.as_str();
2952        // Common Java types
2953        Some(match name {
2954            "byte" => sym::u8, // In Java, bytes are signed, but in practice one almost always wants unsigned bytes.
2955            "short" => sym::i16,
2956            "Bool" => sym::bool,
2957            "Boolean" => sym::bool,
2958            "boolean" => sym::bool,
2959            "int" => sym::i32,
2960            "long" => sym::i64,
2961            "float" => sym::f32,
2962            "double" => sym::f64,
2963            _ => return None,
2964        })
2965    }
2966
2967    // try to give a suggestion for this pattern: `name = blah`, which is common in other languages
2968    // suggest `let name = blah` to introduce a new binding
2969    fn let_binding_suggestion(&self, err: &mut Diag<'_>, ident_span: Span) -> bool {
2970        if ident_span.from_expansion() {
2971            return false;
2972        }
2973
2974        // only suggest when the code is a assignment without prefix code
2975        if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) = self.diag_metadata.in_assignment
2976            && let ast::ExprKind::Path(None, ref path) = lhs.kind
2977            && self.r.tcx.sess.source_map().is_line_before_span_empty(ident_span)
2978        {
2979            let (span, text) = match path.segments.first() {
2980                Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
2981                    // a special case for #117894
2982                    let name = name.trim_prefix('_');
2983                    (ident_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("let {0}", name))
    })format!("let {name}"))
2984                }
2985                _ => (ident_span.shrink_to_lo(), "let ".to_string()),
2986            };
2987
2988            err.span_suggestion_verbose(
2989                span,
2990                "you might have meant to introduce a new binding",
2991                text,
2992                Applicability::MaybeIncorrect,
2993            );
2994            return true;
2995        }
2996
2997        // a special case for #133713
2998        // '=' maybe a typo of `:`, which is a type annotation instead of assignment
2999        if err.code == Some(E0423)
3000            && let Some((let_span, None, Some(val_span))) = self.diag_metadata.current_let_binding
3001            && val_span.contains(ident_span)
3002            && val_span.lo() == ident_span.lo()
3003        {
3004            err.span_suggestion_verbose(
3005                let_span.shrink_to_hi().to(val_span.shrink_to_lo()),
3006                "you might have meant to use `:` for type annotation",
3007                ": ",
3008                Applicability::MaybeIncorrect,
3009            );
3010            return true;
3011        }
3012        false
3013    }
3014
3015    fn find_module(&self, def_id: DefId) -> Option<(Module<'ra>, ImportSuggestion)> {
3016        let mut result = None;
3017        let mut seen_modules = FxHashSet::default();
3018        let mut worklist = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(self.r.graph_root.to_module(), ThinVec::new(), true)]))vec![(self.r.graph_root.to_module(), ThinVec::new(), true)];
3019
3020        while let Some((in_module, path_segments, doc_visible)) = worklist.pop() {
3021            // abort if the module is already found
3022            if result.is_some() {
3023                break;
3024            }
3025
3026            in_module.for_each_child(self.r, |r, ident, orig_ident_span, _, name_binding| {
3027                // abort if the module is already found or if name_binding is private external
3028                if result.is_some() || !name_binding.vis().is_visible_locally() {
3029                    return;
3030                }
3031                if let Some(module_def_id) = name_binding.res().module_like_def_id() {
3032                    // form the path
3033                    let mut path_segments = path_segments.clone();
3034                    path_segments.push(ast::PathSegment::from_ident(ident.orig(orig_ident_span)));
3035                    let doc_visible = doc_visible
3036                        && (module_def_id.is_local() || !r.tcx.is_doc_hidden(module_def_id));
3037                    if module_def_id == def_id {
3038                        let path =
3039                            Path { span: name_binding.span, segments: path_segments, tokens: None };
3040                        result = Some((
3041                            r.expect_module(module_def_id),
3042                            ImportSuggestion {
3043                                did: Some(def_id),
3044                                descr: "module",
3045                                path,
3046                                accessible: true,
3047                                doc_visible,
3048                                note: None,
3049                                via_import: false,
3050                                is_stable: true,
3051                            },
3052                        ));
3053                    } else {
3054                        // add the module to the lookup
3055                        if seen_modules.insert(module_def_id) {
3056                            let module = r.expect_module(module_def_id);
3057                            worklist.push((module, path_segments, doc_visible));
3058                        }
3059                    }
3060                }
3061            });
3062        }
3063
3064        result
3065    }
3066
3067    fn collect_enum_ctors(&self, def_id: DefId) -> Option<Vec<(Path, DefId, CtorKind)>> {
3068        self.find_module(def_id).map(|(enum_module, enum_import_suggestion)| {
3069            let mut variants = Vec::new();
3070            enum_module.for_each_child(self.r, |_, ident, orig_ident_span, _, name_binding| {
3071                if let Res::Def(DefKind::Ctor(CtorOf::Variant, kind), def_id) = name_binding.res() {
3072                    let mut segms = enum_import_suggestion.path.segments.clone();
3073                    segms.push(ast::PathSegment::from_ident(ident.orig(orig_ident_span)));
3074                    let path = Path { span: name_binding.span, segments: segms, tokens: None };
3075                    variants.push((path, def_id, kind));
3076                }
3077            });
3078            variants
3079        })
3080    }
3081
3082    /// Adds a suggestion for using an enum's variant when an enum is used instead.
3083    fn suggest_using_enum_variant(
3084        &self,
3085        err: &mut Diag<'_>,
3086        source: PathSource<'_, '_, '_>,
3087        def_id: DefId,
3088        span: Span,
3089    ) {
3090        let Some(variant_ctors) = self.collect_enum_ctors(def_id) else {
3091            err.note("you might have meant to use one of the enum's variants");
3092            return;
3093        };
3094
3095        // If the expression is a field-access or method-call, try to find a variant with the field/method name
3096        // that could have been intended, and suggest replacing the `.` with `::`.
3097        // Otherwise, suggest adding `::VariantName` after the enum;
3098        // and if the expression is call-like, only suggest tuple variants.
3099        let (suggest_path_sep_dot_span, suggest_only_tuple_variants) = match source {
3100            // `Type(a, b)` in a pattern, only suggest adding a tuple variant after `Type`.
3101            PathSource::TupleStruct(..) => (None, true),
3102            PathSource::Expr(Some(expr)) => match &expr.kind {
3103                // `Type(a, b)`, only suggest adding a tuple variant after `Type`.
3104                ExprKind::Call(..) => (None, true),
3105                // `Type.Foo(a, b)`, suggest replacing `.` -> `::` if variant `Foo` exists and is a tuple variant,
3106                // otherwise suggest adding a variant after `Type`.
3107                ExprKind::MethodCall(MethodCall {
3108                    receiver,
3109                    span,
3110                    seg: PathSegment { ident, .. },
3111                    ..
3112                }) => {
3113                    let dot_span = receiver.span.between(*span);
3114                    let found_tuple_variant = variant_ctors.iter().any(|(path, _, ctor_kind)| {
3115                        *ctor_kind == CtorKind::Fn
3116                            && path.segments.last().is_some_and(|seg| seg.ident == *ident)
3117                    });
3118                    (found_tuple_variant.then_some(dot_span), false)
3119                }
3120                // `Type.Foo`, suggest replacing `.` -> `::` if variant `Foo` exists and is a unit or tuple variant,
3121                // otherwise suggest adding a variant after `Type`.
3122                ExprKind::Field(base, ident) => {
3123                    let dot_span = base.span.between(ident.span);
3124                    let found_tuple_or_unit_variant = variant_ctors.iter().any(|(path, ..)| {
3125                        path.segments.last().is_some_and(|seg| seg.ident == *ident)
3126                    });
3127                    (found_tuple_or_unit_variant.then_some(dot_span), false)
3128                }
3129                _ => (None, false),
3130            },
3131            _ => (None, false),
3132        };
3133
3134        if let Some(dot_span) = suggest_path_sep_dot_span {
3135            err.span_suggestion_verbose(
3136                dot_span,
3137                "use the path separator to refer to a variant",
3138                "::",
3139                Applicability::MaybeIncorrect,
3140            );
3141        } else if suggest_only_tuple_variants {
3142            // Suggest only tuple variants regardless of whether they have fields and do not
3143            // suggest path with added parentheses.
3144            let mut suggestable_variants = variant_ctors
3145                .iter()
3146                .filter(|(.., kind)| *kind == CtorKind::Fn)
3147                .map(|(variant, ..)| path_names_to_string(variant))
3148                .collect::<Vec<_>>();
3149            suggestable_variants.sort();
3150
3151            let non_suggestable_variant_count = variant_ctors.len() - suggestable_variants.len();
3152
3153            let source_msg = if #[allow(non_exhaustive_omitted_patterns)] match source {
    PathSource::TupleStruct(..) => true,
    _ => false,
}matches!(source, PathSource::TupleStruct(..)) {
3154                "to match against"
3155            } else {
3156                "to construct"
3157            };
3158
3159            if !suggestable_variants.is_empty() {
3160                let msg = if non_suggestable_variant_count == 0 && suggestable_variants.len() == 1 {
3161                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("try {0} the enum\'s variant",
                source_msg))
    })format!("try {source_msg} the enum's variant")
3162                } else {
3163                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("try {0} one of the enum\'s variants",
                source_msg))
    })format!("try {source_msg} one of the enum's variants")
3164                };
3165
3166                err.span_suggestions(
3167                    span,
3168                    msg,
3169                    suggestable_variants,
3170                    Applicability::MaybeIncorrect,
3171                );
3172            }
3173
3174            // If the enum has no tuple variants..
3175            if non_suggestable_variant_count == variant_ctors.len() {
3176                err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the enum has no tuple variants {0}",
                source_msg))
    })format!("the enum has no tuple variants {source_msg}"));
3177            }
3178
3179            // If there are also non-tuple variants..
3180            if non_suggestable_variant_count == 1 {
3181                err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might have meant {0} the enum\'s non-tuple variant",
                source_msg))
    })format!("you might have meant {source_msg} the enum's non-tuple variant"));
3182            } else if non_suggestable_variant_count >= 1 {
3183                err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might have meant {0} one of the enum\'s non-tuple variants",
                source_msg))
    })format!(
3184                    "you might have meant {source_msg} one of the enum's non-tuple variants"
3185                ));
3186            }
3187        } else {
3188            let needs_placeholder = |ctor_def_id: DefId, kind: CtorKind| {
3189                let def_id = self.r.tcx.parent(ctor_def_id);
3190                match kind {
3191                    CtorKind::Const => false,
3192                    CtorKind::Fn => {
3193                        !self.r.field_idents(def_id).is_some_and(|field_ids| field_ids.is_empty())
3194                    }
3195                }
3196            };
3197
3198            let mut suggestable_variants = variant_ctors
3199                .iter()
3200                .filter(|(_, def_id, kind)| !needs_placeholder(*def_id, *kind))
3201                .map(|(variant, _, kind)| (path_names_to_string(variant), kind))
3202                .map(|(variant, kind)| match kind {
3203                    CtorKind::Const => variant,
3204                    CtorKind::Fn => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0}())", variant))
    })format!("({variant}())"),
3205                })
3206                .collect::<Vec<_>>();
3207            suggestable_variants.sort();
3208            let no_suggestable_variant = suggestable_variants.is_empty();
3209
3210            if !no_suggestable_variant {
3211                let msg = if suggestable_variants.len() == 1 {
3212                    "you might have meant to use the following enum variant"
3213                } else {
3214                    "you might have meant to use one of the following enum variants"
3215                };
3216
3217                err.span_suggestions(
3218                    span,
3219                    msg,
3220                    suggestable_variants,
3221                    Applicability::MaybeIncorrect,
3222                );
3223            }
3224
3225            let mut suggestable_variants_with_placeholders = variant_ctors
3226                .iter()
3227                .filter(|(_, def_id, kind)| needs_placeholder(*def_id, *kind))
3228                .map(|(variant, _, kind)| (path_names_to_string(variant), kind))
3229                .filter_map(|(variant, kind)| match kind {
3230                    CtorKind::Fn => Some(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0}(/* fields */))", variant))
    })format!("({variant}(/* fields */))")),
3231                    _ => None,
3232                })
3233                .collect::<Vec<_>>();
3234            suggestable_variants_with_placeholders.sort();
3235
3236            if !suggestable_variants_with_placeholders.is_empty() {
3237                let msg =
3238                    match (no_suggestable_variant, suggestable_variants_with_placeholders.len()) {
3239                        (true, 1) => "the following enum variant is available",
3240                        (true, _) => "the following enum variants are available",
3241                        (false, 1) => "alternatively, the following enum variant is available",
3242                        (false, _) => {
3243                            "alternatively, the following enum variants are also available"
3244                        }
3245                    };
3246
3247                err.span_suggestions(
3248                    span,
3249                    msg,
3250                    suggestable_variants_with_placeholders,
3251                    Applicability::HasPlaceholders,
3252                );
3253            }
3254        };
3255
3256        if def_id.is_local() {
3257            err.span_note(self.r.def_span(def_id), "the enum is defined here");
3258        }
3259    }
3260
3261    /// Detects missing const parameters in `impl` blocks and suggests adding them.
3262    ///
3263    /// When a const parameter is used in the self type of an `impl` but not declared
3264    /// in the `impl`'s own generic parameter list, this function emits a targeted
3265    /// diagnostic with a suggestion to add it at the correct position.
3266    ///
3267    /// Example:
3268    ///
3269    /// ```rust,ignore (suggested field is not completely correct, it should be a single suggestion)
3270    /// struct C<const A: u8, const X: u8, const P: u32>;
3271    ///
3272    /// impl Foo for C<A, X, P> {}
3273    /// //           ^ the struct `C` in `C<A, X, P>` is used as the self type
3274    /// //             ^ ^ ^ but A, X and P are not declared on the impl
3275    ///
3276    /// Suggested fix:
3277    ///
3278    /// impl<const A: u8, const X: u8, const P: u32> Foo for C<A, X, P> {}
3279    ///
3280    /// Current behavior (suggestions are emitted one-by-one):
3281    ///
3282    /// impl<const A: u8> Foo for C<A, X, P> {}
3283    /// impl<const X: u8> Foo for C<A, X, P> {}
3284    /// impl<const P: u32> Foo for C<A, X, P> {}
3285    ///
3286    /// Ideally the suggestion should aggregate them into a single line:
3287    ///
3288    /// impl<const A: u8, const X: u8, const P: u32> Foo for C<A, X, P> {}
3289    /// ```
3290    ///
3291    pub(crate) fn detect_and_suggest_const_parameter_error(
3292        &mut self,
3293        path: &[Segment],
3294        source: PathSource<'_, 'ast, 'ra>,
3295    ) -> Option<Diag<'tcx>> {
3296        let Some(item) = self.diag_metadata.current_item else { return None };
3297        let ItemKind::Impl(impl_) = &item.kind else { return None };
3298        let self_ty = &impl_.self_ty;
3299
3300        // Represents parameter to the struct whether `A`, `X` or `P`
3301        let [current_parameter] = path else {
3302            return None;
3303        };
3304
3305        let target_ident = current_parameter.ident;
3306
3307        // Find the parent segment i.e `C` in `C<A, X, C>`
3308        let visitor = ParentPathVisitor::new(self_ty, target_ident);
3309
3310        let Some(parent_segment) = visitor.parent else {
3311            return None;
3312        };
3313
3314        let Some(args) = parent_segment.args.as_ref() else {
3315            return None;
3316        };
3317
3318        let GenericArgs::AngleBracketed(angle) = args.as_ref() else {
3319            return None;
3320        };
3321
3322        // Build map: NodeId of each usage in C<A, X, C> -> its position
3323        // e.g NodeId(A) -> 0, NodeId(X) -> 1, NodeId(C) -> 2
3324        let usage_to_pos: FxHashMap<NodeId, usize> = angle
3325            .args
3326            .iter()
3327            .enumerate()
3328            .filter_map(|(pos, arg)| {
3329                if let AngleBracketedArg::Arg(GenericArg::Type(ty)) = arg
3330                    && let TyKind::Path(_, path) = &ty.kind
3331                    && let [segment] = path.segments.as_slice()
3332                {
3333                    Some((segment.id, pos))
3334                } else {
3335                    None
3336                }
3337            })
3338            .collect();
3339
3340        // Get the position of the missing param in C<A, X, C>
3341        // e.g for missing `B` in `C<A, B, C>` this gives idx=1
3342        let Some(idx) = current_parameter.id.and_then(|id| usage_to_pos.get(&id).copied()) else {
3343            return None;
3344        };
3345
3346        // Now resolve the parent struct `C` to get its definition
3347        let ns = source.namespace();
3348        let segment = Segment::from(parent_segment);
3349        let segments = [segment];
3350        let finalize = Finalize::new(parent_segment.id, parent_segment.ident.span);
3351
3352        if let Ok(Some(resolve)) = self.resolve_qpath_anywhere(
3353            &None,
3354            &segments,
3355            ns,
3356            source.defer_to_typeck(),
3357            finalize,
3358            source,
3359        ) && let Some(resolve) = resolve.full_res()
3360            && let Res::Def(_, def_id) = resolve
3361            && def_id.is_local()
3362            && let Some(local_def_id) = def_id.as_local()
3363            && let Some(struct_generics) = self.r.struct_generics.get(&local_def_id)
3364            && let Some(target_param) = &struct_generics.params.get(idx)
3365            && let GenericParamKind::Const { ty, .. } = &target_param.kind
3366            && let TyKind::Path(_, path) = &ty.kind
3367        {
3368            let full_type = path
3369                .segments
3370                .iter()
3371                .map(|seg| seg.ident.to_string())
3372                .collect::<Vec<_>>()
3373                .join("::");
3374
3375            // Find the first impl param whose position in C<A, X, C>
3376            // is strictly greater than our missing param's index
3377            // e.g missing B(idx=1), impl has A(pos=0) and C(pos=2)
3378            // C has pos=2 > 1 so insert before C
3379            let next_impl_param = impl_.generics.params.iter().find(|impl_param| {
3380                angle
3381                    .args
3382                    .iter()
3383                    .find_map(|arg| {
3384                        if let AngleBracketedArg::Arg(GenericArg::Type(ty)) = arg
3385                            && let TyKind::Path(_, path) = &ty.kind
3386                            && let [segment] = path.segments.as_slice()
3387                            && segment.ident == impl_param.ident
3388                        {
3389                            usage_to_pos.get(&segment.id).copied()
3390                        } else {
3391                            None
3392                        }
3393                    })
3394                    .map_or(false, |pos| pos > idx)
3395            });
3396
3397            let (insert_span, snippet) = match next_impl_param {
3398                Some(next_param) => {
3399                    // Insert in the middle before next_param
3400                    // e.g impl<A, C> -> impl<A, const B: u8, C>
3401                    (
3402                        next_param.span().shrink_to_lo(),
3403                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("const {0}: {1}, ", target_ident,
                full_type))
    })format!("const {}: {}, ", target_ident, full_type),
3404                    )
3405                }
3406                None => match impl_.generics.params.last() {
3407                    Some(last) => {
3408                        // Append after last existing param
3409                        // e.g impl<A, B> -> impl<A, B, const C: u8>
3410                        (
3411                            last.span().shrink_to_hi(),
3412                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(", const {0}: {1}", target_ident,
                full_type))
    })format!(", const {}: {}", target_ident, full_type),
3413                        )
3414                    }
3415                    None => {
3416                        // No generics at all on impl
3417                        // e.g impl Foo for C<A> -> impl<const A: u8> Foo for C<A>
3418                        (
3419                            impl_.generics.span.shrink_to_hi(),
3420                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<const {0}: {1}>", target_ident,
                full_type))
    })format!("<const {}: {}>", target_ident, full_type),
3421                        )
3422                    }
3423                },
3424            };
3425
3426            let mut err = self.r.dcx().struct_span_err(
3427                target_ident.span,
3428                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("cannot find const `{0}` in this scope",
                target_ident))
    })format!("cannot find const `{}` in this scope", target_ident),
3429            );
3430
3431            err.code(E0425);
3432
3433            err.span_label(target_ident.span, "not found in this scope");
3434
3435            err.span_label(
3436                target_param.span(),
3437                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("corresponding const parameter on the type defined here"))
    })format!("corresponding const parameter on the type defined here",),
3438            );
3439
3440            err.subdiagnostic(diagnostics::UnexpectedMissingConstParameter {
3441                span: insert_span,
3442                snippet,
3443                item_name: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", target_ident))
    })format!("{}", target_ident),
3444                item_location: String::from("impl"),
3445            });
3446
3447            return Some(err);
3448        }
3449
3450        None
3451    }
3452
3453    pub(crate) fn suggest_adding_generic_parameter(
3454        &mut self,
3455        path: &[Segment],
3456        source: PathSource<'_, 'ast, 'ra>,
3457    ) -> (Option<(Span, &'static str, String, Applicability)>, Option<Diag<'tcx>>) {
3458        let (ident, span) = match path {
3459            [segment]
3460                if !segment.has_generic_args
3461                    && segment.ident.name != kw::SelfUpper
3462                    && segment.ident.name != kw::Dyn =>
3463            {
3464                (segment.ident.to_string(), segment.ident.span)
3465            }
3466            _ => return (None, None),
3467        };
3468        let mut iter = ident.chars().map(|c| c.is_uppercase());
3469        let single_uppercase_char =
3470            #[allow(non_exhaustive_omitted_patterns)] match iter.next() {
    Some(true) => true,
    _ => false,
}matches!(iter.next(), Some(true)) && #[allow(non_exhaustive_omitted_patterns)] match iter.next() {
    None => true,
    _ => false,
}matches!(iter.next(), None);
3471        if !self.diag_metadata.currently_processing_generic_args && !single_uppercase_char {
3472            return (None, None);
3473        }
3474        match (
3475            self.diag_metadata.current_item,
3476            single_uppercase_char,
3477            self.diag_metadata.currently_processing_generic_args,
3478        ) {
3479            (Some(Item { kind: ItemKind::Fn(fn_), .. }), _, _) if fn_.ident.name == sym::main => {
3480                // Ignore `fn main()` as we don't want to suggest `fn main<T>()`
3481            }
3482            (
3483                Some(Item {
3484                    kind:
3485                        kind @ ItemKind::Fn(..)
3486                        | kind @ ItemKind::Enum(..)
3487                        | kind @ ItemKind::Struct(..)
3488                        | kind @ ItemKind::Union(..),
3489                    ..
3490                }),
3491                true,
3492                _,
3493            )
3494            // Without the 2nd `true`, we'd suggest `impl <T>` for `impl T` when a type `T` isn't found
3495            | (Some(Item { kind: kind @ ItemKind::Impl(..), .. }), true, true)
3496            | (Some(Item { kind, .. }), false, _) => {
3497                if let Some(generics) = kind.generics() {
3498                    if span.overlaps(generics.span) {
3499                        // Avoid the following:
3500                        // error[E0405]: cannot find trait `A` in this scope
3501                        //  --> $DIR/typo-suggestion-named-underscore.rs:CC:LL
3502                        //   |
3503                        // L | fn foo<T: A>(x: T) {} // Shouldn't suggest underscore
3504                        //   |           ^- help: you might be missing a type parameter: `, A`
3505                        //   |           |
3506                        //   |           not found in this scope
3507                        return (None, None);
3508                    }
3509
3510                    let (msg, sugg) = match source {
3511                        PathSource::Type | PathSource::PreciseCapturingArg(TypeNS) => {
3512                            if let Some(err) =
3513                                self.detect_and_suggest_const_parameter_error(path, source)
3514                            {
3515                                return (None, Some(err));
3516                            }
3517                            ("you might be missing a type parameter", ident)
3518                        }
3519                        PathSource::Expr(_) | PathSource::PreciseCapturingArg(ValueNS) => (
3520                            "you might be missing a const parameter",
3521                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("const {0}: /* Type */", ident))
    })format!("const {ident}: /* Type */"),
3522                        ),
3523                        _ => return (None, None),
3524                    };
3525                    let (span, sugg) = if let [.., param] = &generics.params[..] {
3526                        let span = if let [.., bound] = &param.bounds[..] {
3527                            bound.span()
3528                        } else if let GenericParam {
3529                            kind: GenericParamKind::Const { ty, span: _, default },
3530                            ..
3531                        } = param
3532                        {
3533                            default.as_ref().map(|def| def.value.span).unwrap_or(ty.span)
3534                        } else {
3535                            param.ident.span
3536                        };
3537                        (span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(", {0}", sugg))
    })format!(", {sugg}"))
3538                    } else {
3539                        (generics.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0}>", sugg))
    })format!("<{sugg}>"))
3540                    };
3541                    // Do not suggest if this is coming from macro expansion.
3542                    if span.can_be_used_for_suggestions() {
3543                        return (
3544                            Some((span.shrink_to_hi(), msg, sugg, Applicability::MaybeIncorrect)),
3545                            None,
3546                        );
3547                    }
3548                }
3549            }
3550            _ => {}
3551        }
3552        (None, None)
3553    }
3554
3555    /// Given the target `label`, search the `rib_index`th label rib for similarly named labels,
3556    /// optionally returning the closest match and whether it is reachable.
3557    pub(crate) fn suggestion_for_label_in_rib(
3558        &self,
3559        rib_index: usize,
3560        label: Ident,
3561    ) -> Option<LabelSuggestion> {
3562        // Are ribs from this `rib_index` within scope?
3563        let within_scope = self.is_label_valid_from_rib(rib_index);
3564
3565        let rib = &self.label_ribs[rib_index];
3566        let names = rib
3567            .bindings
3568            .iter()
3569            .filter(|(id, _)| id.span.eq_ctxt(label.span))
3570            .map(|(id, _)| id.name)
3571            .collect::<Vec<Symbol>>();
3572
3573        find_best_match_for_name(&names, label.name, None).map(|symbol| {
3574            // Upon finding a similar name, get the ident that it was from - the span
3575            // contained within helps make a useful diagnostic. In addition, determine
3576            // whether this candidate is within scope.
3577            let (ident, _) = rib.bindings.iter().find(|(ident, _)| ident.name == symbol).unwrap();
3578            (*ident, within_scope)
3579        })
3580    }
3581
3582    pub(crate) fn maybe_report_lifetime_uses(
3583        &mut self,
3584        generics_span: Span,
3585        params: &[ast::GenericParam],
3586    ) {
3587        for (param_index, param) in params.iter().enumerate() {
3588            let GenericParamKind::Lifetime = param.kind else { continue };
3589
3590            let def_id = self.r.local_def_id(param.id);
3591
3592            let use_set = self.lifetime_uses.remove(&def_id);
3593            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:3593",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(3593u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::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!("Use set for {0:?}({1:?} at {2:?}) is {3:?}",
                                                    def_id, param.ident, param.ident.span, use_set) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(
3594                "Use set for {:?}({:?} at {:?}) is {:?}",
3595                def_id, param.ident, param.ident.span, use_set
3596            );
3597
3598            let deletion_span = || {
3599                if params.len() == 1 {
3600                    // if sole lifetime, remove the entire `<>` brackets
3601                    Some(generics_span)
3602                } else if param_index == 0 {
3603                    // if removing within `<>` brackets, we also want to
3604                    // delete a leading or trailing comma as appropriate
3605                    match (
3606                        param.span().find_ancestor_inside(generics_span),
3607                        params[param_index + 1].span().find_ancestor_inside(generics_span),
3608                    ) {
3609                        (Some(param_span), Some(next_param_span)) => {
3610                            Some(param_span.to(next_param_span.shrink_to_lo()))
3611                        }
3612                        _ => None,
3613                    }
3614                } else {
3615                    // if removing within `<>` brackets, we also want to
3616                    // delete a leading or trailing comma as appropriate
3617                    match (
3618                        param.span().find_ancestor_inside(generics_span),
3619                        params[param_index - 1].span().find_ancestor_inside(generics_span),
3620                    ) {
3621                        (Some(param_span), Some(prev_param_span)) => {
3622                            Some(prev_param_span.shrink_to_hi().to(param_span))
3623                        }
3624                        _ => None,
3625                    }
3626                }
3627            };
3628            match use_set {
3629                Some(LifetimeUseSet::Many) => {}
3630                // A lifetime bound is a real use of that lifetime parameter, even
3631                // though visiting a bound like `'b: 'a` only records a use of `'a`.
3632                Some(LifetimeUseSet::One { .. }) if !param.bounds.is_empty() => {}
3633                Some(LifetimeUseSet::One { use_span, use_ctxt }) => {
3634                    let param_ident = param.ident;
3635                    let deletion_span =
3636                        if param.bounds.is_empty() { deletion_span() } else { None };
3637                    self.r.lint_buffer.dyn_buffer_lint_any(
3638                        lint::builtin::SINGLE_USE_LIFETIMES,
3639                        param.id,
3640                        param_ident.span,
3641                        move |dcx, level, sess| {
3642                            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:3642",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(3642u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::tracing_core::field::FieldSet::new(&["param_ident",
                                        "param_ident.span", "use_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(&param_ident)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&param_ident.span)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&use_span)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?param_ident, ?param_ident.span, ?use_span);
3643
3644                            let elidable = #[allow(non_exhaustive_omitted_patterns)] match use_ctxt {
    LifetimeCtxt::Ref => true,
    _ => false,
}matches!(use_ctxt, LifetimeCtxt::Ref);
3645                            let suggestion = if let Some(deletion_span) = deletion_span {
3646                                let (use_span, replace_lt) = if elidable {
3647                                    let use_span = sess
3648                                        .downcast_ref::<Session>()
3649                                        .expect("expected a `Session`")
3650                                        .source_map()
3651                                        .span_extend_while_whitespace(use_span);
3652                                    (use_span, String::new())
3653                                } else {
3654                                    (use_span, "'_".to_owned())
3655                                };
3656                                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:3656",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(3656u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::tracing_core::field::FieldSet::new(&["deletion_span",
                                        "use_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(&deletion_span)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&use_span)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?deletion_span, ?use_span);
3657
3658                                // issue 107998 for the case such as a wrong function pointer type
3659                                // `deletion_span` is empty and there is no need to report lifetime uses here
3660                                let deletion_span = if deletion_span.is_empty() {
3661                                    None
3662                                } else {
3663                                    Some(deletion_span)
3664                                };
3665                                Some(diagnostics::SingleUseLifetimeSugg {
3666                                    deletion_span,
3667                                    use_span,
3668                                    replace_lt,
3669                                })
3670                            } else {
3671                                None
3672                            };
3673                            diagnostics::SingleUseLifetime {
3674                                suggestion,
3675                                param_span: param_ident.span,
3676                                use_span,
3677                                ident: param_ident,
3678                            }
3679                            .into_diag(dcx, level)
3680                        },
3681                    );
3682                }
3683                None => {
3684                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:3684",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(3684u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::tracing_core::field::FieldSet::new(&["param.ident",
                                        "param.ident.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(&param.ident)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&param.ident.span)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?param.ident, ?param.ident.span);
3685                    let deletion_span = deletion_span();
3686
3687                    // if the lifetime originates from expanded code, we won't be able to remove it #104432
3688                    if deletion_span.is_some_and(|sp| !sp.in_derive_expansion()) {
3689                        self.r.lint_buffer.buffer_lint(
3690                            lint::builtin::UNUSED_LIFETIMES,
3691                            param.id,
3692                            param.ident.span,
3693                            diagnostics::UnusedLifetime { deletion_span, ident: param.ident },
3694                        );
3695                    }
3696                }
3697            }
3698        }
3699    }
3700
3701    pub(crate) fn emit_undeclared_lifetime_error(
3702        &self,
3703        lifetime_ref: &ast::Lifetime,
3704        outer_lifetime_ref: Option<Ident>,
3705    ) -> ErrorGuaranteed {
3706        if true {
    match (&lifetime_ref.ident.name, &kw::UnderscoreLifetime) {
        (left_val, right_val) => {
            if *left_val == *right_val {
                let kind = ::core::panicking::AssertKind::Ne;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_ne!(lifetime_ref.ident.name, kw::UnderscoreLifetime);
3707        let mut err = if let Some(outer) = outer_lifetime_ref {
3708            {
    self.r.dcx().struct_span_err(lifetime_ref.ident.span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("can\'t use generic parameters from outer item"))
                })).with_code(E0401)
}struct_span_code_err!(
3709                self.r.dcx(),
3710                lifetime_ref.ident.span,
3711                E0401,
3712                "can't use generic parameters from outer item",
3713            )
3714            .with_span_label(lifetime_ref.ident.span, "use of generic parameter from outer item")
3715            .with_span_label(outer.span, "lifetime parameter from outer item")
3716        } else {
3717            {
    self.r.dcx().struct_span_err(lifetime_ref.ident.span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("use of undeclared lifetime name `{0}`",
                            lifetime_ref.ident))
                })).with_code(E0261)
}struct_span_code_err!(
3718                self.r.dcx(),
3719                lifetime_ref.ident.span,
3720                E0261,
3721                "use of undeclared lifetime name `{}`",
3722                lifetime_ref.ident
3723            )
3724            .with_span_label(lifetime_ref.ident.span, "undeclared lifetime")
3725        };
3726
3727        // Check if this is a typo of `'static`.
3728        if edit_distance(lifetime_ref.ident.name.as_str(), "'static", 2).is_some() {
3729            err.span_suggestion_verbose(
3730                lifetime_ref.ident.span,
3731                "you may have misspelled the `'static` lifetime",
3732                "'static",
3733                Applicability::MachineApplicable,
3734            );
3735        } else {
3736            self.suggest_introducing_lifetime(
3737                &mut err,
3738                Some(lifetime_ref.ident),
3739                |err, _, span, message, suggestion, span_suggs| {
3740                    err.multipart_suggestion(
3741                        message,
3742                        std::iter::once((span, suggestion)).chain(span_suggs).collect(),
3743                        Applicability::MaybeIncorrect,
3744                    );
3745                    true
3746                },
3747            );
3748        }
3749
3750        err.emit()
3751    }
3752
3753    fn suggest_introducing_lifetime(
3754        &self,
3755        err: &mut Diag<'_>,
3756        name: Option<Ident>,
3757        suggest: impl Fn(
3758            &mut Diag<'_>,
3759            bool,
3760            Span,
3761            Cow<'static, str>,
3762            String,
3763            Vec<(Span, String)>,
3764        ) -> bool,
3765    ) {
3766        let mut suggest_note = true;
3767        for rib in self.lifetime_ribs.iter().rev() {
3768            let mut should_continue = true;
3769            match rib.kind {
3770                LifetimeRibKind::Generics { binder, span, kind } => {
3771                    // Avoid suggesting placing lifetime parameters on constant items unless the relevant
3772                    // feature is enabled. Suggest the parent item as a possible location if applicable.
3773                    if let LifetimeBinderKind::ConstItem = kind
3774                        && !self.r.tcx().features().generic_const_items()
3775                    {
3776                        continue;
3777                    }
3778                    if let LifetimeBinderKind::ImplAssocType = kind {
3779                        continue;
3780                    }
3781
3782                    if !span.can_be_used_for_suggestions()
3783                        && suggest_note
3784                        && let Some(name) = name
3785                    {
3786                        suggest_note = false; // Avoid displaying the same help multiple times.
3787                        err.span_label(
3788                            span,
3789                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("lifetime `{0}` is missing in item created through this procedural macro",
                name))
    })format!(
3790                                "lifetime `{name}` is missing in item created through this procedural macro",
3791                            ),
3792                        );
3793                        continue;
3794                    }
3795
3796                    let higher_ranked = #[allow(non_exhaustive_omitted_patterns)] match kind {
    LifetimeBinderKind::FnPtrType | LifetimeBinderKind::PolyTrait |
        LifetimeBinderKind::WhereBound => true,
    _ => false,
}matches!(
3797                        kind,
3798                        LifetimeBinderKind::FnPtrType
3799                            | LifetimeBinderKind::PolyTrait
3800                            | LifetimeBinderKind::WhereBound
3801                    );
3802
3803                    let mut rm_inner_binders: FxIndexSet<Span> = Default::default();
3804                    let (span, sugg) = if span.is_empty() {
3805                        let mut binder_idents: FxIndexSet<Ident> = Default::default();
3806                        binder_idents.insert(name.unwrap_or(Ident::from_str("'a")));
3807
3808                        // We need to special case binders in the following situation:
3809                        // Change `T: for<'a> Trait<T> + 'b` to `for<'a, 'b> T: Trait<T> + 'b`
3810                        // T: for<'a> Trait<T> + 'b
3811                        //    ^^^^^^^  remove existing inner binder `for<'a>`
3812                        // for<'a, 'b> T: Trait<T> + 'b
3813                        // ^^^^^^^^^^^  suggest outer binder `for<'a, 'b>`
3814                        if let LifetimeBinderKind::WhereBound = kind
3815                            && let Some(predicate) = self.diag_metadata.current_where_predicate
3816                            && let ast::WherePredicateKind::BoundPredicate(
3817                                ast::WhereBoundPredicate { bounded_ty, bounds, .. },
3818                            ) = &predicate.kind
3819                            && bounded_ty.id == binder
3820                        {
3821                            for bound in bounds {
3822                                if let ast::GenericBound::Trait(poly_trait_ref) = bound
3823                                    && let span = poly_trait_ref
3824                                        .span
3825                                        .with_hi(poly_trait_ref.trait_ref.path.span.lo())
3826                                    && !span.is_empty()
3827                                {
3828                                    rm_inner_binders.insert(span);
3829                                    poly_trait_ref.bound_generic_params.iter().for_each(|v| {
3830                                        binder_idents.insert(v.ident);
3831                                    });
3832                                }
3833                            }
3834                        }
3835
3836                        let binders_sugg: String = binder_idents
3837                            .into_iter()
3838                            .map(|ident| ident.to_string())
3839                            .intersperse(", ".to_owned())
3840                            .collect();
3841                        let sugg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}<{1}>{2}",
                if higher_ranked { "for" } else { "" }, binders_sugg,
                if higher_ranked { " " } else { "" }))
    })format!(
3842                            "{}<{}>{}",
3843                            if higher_ranked { "for" } else { "" },
3844                            binders_sugg,
3845                            if higher_ranked { " " } else { "" },
3846                        );
3847                        (span, sugg)
3848                    } else {
3849                        let span = self
3850                            .r
3851                            .tcx
3852                            .sess
3853                            .source_map()
3854                            .span_through_char(span, '<')
3855                            .shrink_to_hi();
3856                        let sugg =
3857                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}, ",
                name.map(|i| i.to_string()).as_deref().unwrap_or("'a")))
    })format!("{}, ", name.map(|i| i.to_string()).as_deref().unwrap_or("'a"));
3858                        (span, sugg)
3859                    };
3860
3861                    if higher_ranked {
3862                        let message = Cow::from(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider making the {0} lifetime-generic with a new `{1}` lifetime",
                kind.descr(),
                name.map(|i| i.to_string()).as_deref().unwrap_or("'a")))
    })format!(
3863                            "consider making the {} lifetime-generic with a new `{}` lifetime",
3864                            kind.descr(),
3865                            name.map(|i| i.to_string()).as_deref().unwrap_or("'a"),
3866                        ));
3867                        should_continue = suggest(
3868                            err,
3869                            true,
3870                            span,
3871                            message,
3872                            sugg,
3873                            if !rm_inner_binders.is_empty() {
3874                                rm_inner_binders
3875                                    .into_iter()
3876                                    .map(|v| (v, "".to_string()))
3877                                    .collect::<Vec<_>>()
3878                            } else {
3879                                ::alloc::vec::Vec::new()vec![]
3880                            },
3881                        );
3882                        err.note_once(
3883                            "for more information on higher-ranked polymorphism, visit \
3884                             https://doc.rust-lang.org/nomicon/hrtb.html",
3885                        );
3886                    } else if let Some(name) = name {
3887                        let message =
3888                            Cow::from(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider introducing lifetime `{0}` here",
                name))
    })format!("consider introducing lifetime `{name}` here"));
3889                        should_continue = suggest(err, false, span, message, sugg, ::alloc::vec::Vec::new()vec![]);
3890                    } else {
3891                        let message = Cow::from("consider introducing a named lifetime parameter");
3892                        should_continue = suggest(err, false, span, message, sugg, ::alloc::vec::Vec::new()vec![]);
3893                    }
3894                }
3895                LifetimeRibKind::Item | LifetimeRibKind::ConstParamTy => break,
3896                _ => {}
3897            }
3898            if !should_continue {
3899                break;
3900            }
3901        }
3902    }
3903
3904    pub(crate) fn emit_non_static_lt_in_const_param_ty_error(
3905        &self,
3906        lifetime_ref: &ast::Lifetime,
3907    ) -> ErrorGuaranteed {
3908        self.r
3909            .dcx()
3910            .create_err(diagnostics::ParamInTyOfConstParam {
3911                span: lifetime_ref.ident.span,
3912                name: lifetime_ref.ident.name,
3913            })
3914            .emit()
3915    }
3916
3917    /// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
3918    /// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
3919    /// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
3920    pub(crate) fn emit_forbidden_non_static_lifetime_error(
3921        &self,
3922        cause: NoConstantGenericsReason,
3923        lifetime_ref: &ast::Lifetime,
3924    ) -> ErrorGuaranteed {
3925        match cause {
3926            NoConstantGenericsReason::IsEnumDiscriminant => self
3927                .r
3928                .dcx()
3929                .create_err(diagnostics::ParamInEnumDiscriminant {
3930                    span: lifetime_ref.ident.span,
3931                    name: lifetime_ref.ident.name,
3932                    param_kind: diagnostics::ParamKindInEnumDiscriminant::Lifetime,
3933                })
3934                .emit(),
3935            NoConstantGenericsReason::NonTrivialConstArg => {
3936                if !!self.r.tcx.features().generic_const_exprs() {
    ::core::panicking::panic("assertion failed: !self.r.tcx.features().generic_const_exprs()")
};assert!(!self.r.tcx.features().generic_const_exprs());
3937                self.r
3938                    .dcx()
3939                    .create_err(diagnostics::ParamInNonTrivialAnonConst {
3940                        span: lifetime_ref.ident.span,
3941                        name: lifetime_ref.ident.name,
3942                        param_kind: diagnostics::ParamKindInNonTrivialAnonConst::Lifetime,
3943                        help: self.r.tcx.sess.is_nightly_build(),
3944                        is_gca: self.r.tcx.features().generic_const_args(),
3945                        help_gca: self.r.tcx.features().generic_const_args(),
3946                    })
3947                    .emit()
3948            }
3949        }
3950    }
3951
3952    pub(crate) fn report_missing_lifetime_specifiers<'a>(
3953        &mut self,
3954        lifetime_refs: impl Clone + IntoIterator<Item = &'a MissingLifetime>,
3955        function_param_lifetimes: Option<(Vec<MissingLifetime>, Vec<ElisionFnParameter>)>,
3956    ) -> ErrorGuaranteed {
3957        let num_lifetimes: usize = lifetime_refs.clone().into_iter().map(|lt| lt.count).sum();
3958        let spans: Vec<_> = lifetime_refs.clone().into_iter().map(|lt| lt.span).collect();
3959
3960        let mut err = {
    self.r.dcx().struct_span_err(spans,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("missing lifetime specifier{0}",
                            if num_lifetimes == 1 { "" } else { "s" }))
                })).with_code(E0106)
}struct_span_code_err!(
3961            self.r.dcx(),
3962            spans,
3963            E0106,
3964            "missing lifetime specifier{}",
3965            pluralize!(num_lifetimes)
3966        );
3967        self.add_missing_lifetime_specifiers_label(
3968            &mut err,
3969            lifetime_refs,
3970            function_param_lifetimes,
3971        );
3972        err.emit()
3973    }
3974
3975    fn add_missing_lifetime_specifiers_label<'a>(
3976        &mut self,
3977        err: &mut Diag<'_>,
3978        lifetime_refs: impl Clone + IntoIterator<Item = &'a MissingLifetime>,
3979        function_param_lifetimes: Option<(Vec<MissingLifetime>, Vec<ElisionFnParameter>)>,
3980    ) {
3981        for &lt in lifetime_refs.clone() {
3982            err.span_label(
3983                lt.span,
3984                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected {0} lifetime parameter{1}",
                if lt.count == 1 {
                    "named".to_string()
                } else { lt.count.to_string() },
                if lt.count == 1 { "" } else { "s" }))
    })format!(
3985                    "expected {} lifetime parameter{}",
3986                    if lt.count == 1 { "named".to_string() } else { lt.count.to_string() },
3987                    pluralize!(lt.count),
3988                ),
3989            );
3990        }
3991
3992        let mut in_scope_lifetimes: Vec<_> = self
3993            .lifetime_ribs
3994            .iter()
3995            .rev()
3996            .take_while(|rib| {
3997                !#[allow(non_exhaustive_omitted_patterns)] match rib.kind {
    LifetimeRibKind::Item | LifetimeRibKind::ConstParamTy => true,
    _ => false,
}matches!(rib.kind, LifetimeRibKind::Item | LifetimeRibKind::ConstParamTy)
3998            })
3999            .flat_map(|rib| rib.bindings.iter())
4000            .map(|(&ident, &res)| (ident, res))
4001            .filter(|(ident, _)| ident.name != kw::UnderscoreLifetime)
4002            .collect();
4003        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:4003",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(4003u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::tracing_core::field::FieldSet::new(&["in_scope_lifetimes"],
                            ::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(&in_scope_lifetimes)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?in_scope_lifetimes);
4004
4005        let mut maybe_static = false;
4006        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:4006",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(4006u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::tracing_core::field::FieldSet::new(&["function_param_lifetimes"],
                            ::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(&function_param_lifetimes)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?function_param_lifetimes);
4007        if let Some((param_lifetimes, params)) = &function_param_lifetimes {
4008            let elided_len = param_lifetimes.len();
4009            let num_params = params.len();
4010
4011            let mut m = String::new();
4012
4013            for (i, info) in params.iter().enumerate() {
4014                let ElisionFnParameter { ident, index, lifetime_count, span } = *info;
4015                if true {
    match (&lifetime_count, &0) {
        (left_val, right_val) => {
            if *left_val == *right_val {
                let kind = ::core::panicking::AssertKind::Ne;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_ne!(lifetime_count, 0);
4016
4017                err.span_label(span, "");
4018
4019                if i != 0 {
4020                    if i + 1 < num_params {
4021                        m.push_str(", ");
4022                    } else if num_params == 2 {
4023                        m.push_str(" or ");
4024                    } else {
4025                        m.push_str(", or ");
4026                    }
4027                }
4028
4029                let help_name = if let Some(ident) = ident {
4030                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", ident))
    })format!("`{ident}`")
4031                } else {
4032                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("argument {0}", index + 1))
    })format!("argument {}", index + 1)
4033                };
4034
4035                if lifetime_count == 1 {
4036                    m.push_str(&help_name[..])
4037                } else {
4038                    m.push_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("one of {0}\'s {1} lifetimes",
                help_name, lifetime_count))
    })format!("one of {help_name}'s {lifetime_count} lifetimes")[..])
4039                }
4040            }
4041
4042            if num_params == 0 {
4043                err.help(
4044                    "this function's return type contains a borrowed value, but there is no value \
4045                     for it to be borrowed from",
4046                );
4047                if in_scope_lifetimes.is_empty() {
4048                    maybe_static = true;
4049                    in_scope_lifetimes = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(Ident::with_dummy_span(kw::StaticLifetime),
                    (DUMMY_NODE_ID, LifetimeRes::Static))]))vec![(
4050                        Ident::with_dummy_span(kw::StaticLifetime),
4051                        (DUMMY_NODE_ID, LifetimeRes::Static),
4052                    )];
4053                }
4054            } else if elided_len == 0 {
4055                err.help(
4056                    "this function's return type contains a borrowed value with an elided \
4057                     lifetime, but the lifetime cannot be derived from the arguments",
4058                );
4059                if in_scope_lifetimes.is_empty() {
4060                    maybe_static = true;
4061                    in_scope_lifetimes = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(Ident::with_dummy_span(kw::StaticLifetime),
                    (DUMMY_NODE_ID, LifetimeRes::Static))]))vec![(
4062                        Ident::with_dummy_span(kw::StaticLifetime),
4063                        (DUMMY_NODE_ID, LifetimeRes::Static),
4064                    )];
4065                }
4066            } else if num_params == 1 {
4067                err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this function\'s return type contains a borrowed value, but the signature does not say which {0} it is borrowed from",
                m))
    })format!(
4068                    "this function's return type contains a borrowed value, but the signature does \
4069                     not say which {m} it is borrowed from",
4070                ));
4071            } else {
4072                err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this function\'s return type contains a borrowed value, but the signature does not say whether it is borrowed from {0}",
                m))
    })format!(
4073                    "this function's return type contains a borrowed value, but the signature does \
4074                     not say whether it is borrowed from {m}",
4075                ));
4076            }
4077        }
4078
4079        #[allow(rustc::symbol_intern_string_literal)]
4080        let existing_name = match &in_scope_lifetimes[..] {
4081            [] => Symbol::intern("'a"),
4082            [(existing, _)] => existing.name,
4083            _ => Symbol::intern("'lifetime"),
4084        };
4085
4086        let mut spans_suggs: Vec<_> = Vec::new();
4087        let source_map = self.r.tcx.sess.source_map();
4088        let build_sugg = |lt: MissingLifetime| match lt.kind {
4089            MissingLifetimeKind::Underscore => {
4090                if true {
    match (&lt.count, &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::None);
            }
        }
    };
};debug_assert_eq!(lt.count, 1);
4091                (lt.span, existing_name.to_string())
4092            }
4093            MissingLifetimeKind::Ampersand => {
4094                if true {
    match (&lt.count, &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::None);
            }
        }
    };
};debug_assert_eq!(lt.count, 1);
4095                (lt.span.shrink_to_hi(), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} ", existing_name))
    })format!("{existing_name} "))
4096            }
4097            MissingLifetimeKind::Comma => {
4098                let sugg: String = std::iter::repeat_n(existing_name.as_str(), lt.count)
4099                    .intersperse(", ")
4100                    .collect();
4101                let is_empty_brackets = source_map.span_followed_by(lt.span, ">").is_some();
4102                let sugg = if is_empty_brackets { sugg } else { ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}, ", sugg))
    })format!("{sugg}, ") };
4103                (lt.span.shrink_to_hi(), sugg)
4104            }
4105            MissingLifetimeKind::Brackets => {
4106                let sugg: String = std::iter::once("<")
4107                    .chain(std::iter::repeat_n(existing_name.as_str(), lt.count).intersperse(", "))
4108                    .chain([">"])
4109                    .collect();
4110                (lt.span.shrink_to_hi(), sugg)
4111            }
4112        };
4113        for &lt in lifetime_refs.clone() {
4114            spans_suggs.push(build_sugg(lt));
4115        }
4116        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:4116",
                        "rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
                        ::tracing_core::__macro_support::Option::Some(4116u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
                        ::tracing_core::field::FieldSet::new(&["spans_suggs"],
                            ::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(&spans_suggs)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?spans_suggs);
4117        match in_scope_lifetimes.len() {
4118            0 => {
4119                if let Some((param_lifetimes, _)) = function_param_lifetimes {
4120                    for lt in param_lifetimes {
4121                        spans_suggs.push(build_sugg(lt))
4122                    }
4123                }
4124                self.suggest_introducing_lifetime(
4125                    err,
4126                    None,
4127                    |err, higher_ranked, span, message, intro_sugg, _| {
4128                        err.multipart_suggestion(
4129                            message,
4130                            std::iter::once((span, intro_sugg))
4131                                .chain(spans_suggs.clone())
4132                                .collect(),
4133                            Applicability::MaybeIncorrect,
4134                        );
4135                        higher_ranked
4136                    },
4137                );
4138            }
4139            1 => {
4140                let post = if maybe_static {
4141                    let mut lifetime_refs = lifetime_refs.clone().into_iter();
4142                    let owned = if let Some(lt) = lifetime_refs.next()
4143                        && lifetime_refs.next().is_none()
4144                        && lt.kind != MissingLifetimeKind::Ampersand
4145                    {
4146                        ", or if you will only have owned values"
4147                    } else {
4148                        ""
4149                    };
4150                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(", but this is uncommon unless you\'re returning a borrowed value from a `const` or a `static`{0}",
                owned))
    })format!(
4151                        ", but this is uncommon unless you're returning a borrowed value from a \
4152                         `const` or a `static`{owned}",
4153                    )
4154                } else {
4155                    String::new()
4156                };
4157                err.multipart_suggestion(
4158                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider using the `{0}` lifetime{1}",
                existing_name, post))
    })format!("consider using the `{existing_name}` lifetime{post}"),
4159                    spans_suggs,
4160                    Applicability::MaybeIncorrect,
4161                );
4162                if maybe_static {
4163                    // FIXME: what follows are general suggestions, but we'd want to perform some
4164                    // minimal flow analysis to provide more accurate suggestions. For example, if
4165                    // we identified that the return expression references only one argument, we
4166                    // would suggest borrowing only that argument, and we'd skip the prior
4167                    // "use `'static`" suggestion entirely.
4168                    let mut lifetime_refs = lifetime_refs.clone().into_iter();
4169                    if let Some(lt) = lifetime_refs.next()
4170                        && lifetime_refs.next().is_none()
4171                        && (lt.kind == MissingLifetimeKind::Ampersand
4172                            || lt.kind == MissingLifetimeKind::Underscore)
4173                    {
4174                        let pre = if let Some((kind, _span)) = self.diag_metadata.current_function
4175                            && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = kind
4176                            && !sig.decl.inputs.is_empty()
4177                            && let sugg = sig
4178                                .decl
4179                                .inputs
4180                                .iter()
4181                                .filter_map(|param| {
4182                                    if param.ty.span.contains(lt.span) {
4183                                        // We don't want to suggest `fn elision(_: &fn() -> &i32)`
4184                                        // when we have `fn elision(_: fn() -> &i32)`
4185                                        None
4186                                    } else if let TyKind::CVarArgs = param.ty.kind {
4187                                        // Don't suggest `&...` for ffi fn with varargs
4188                                        None
4189                                    } else if let TyKind::ImplTrait(..) = &param.ty.kind {
4190                                        // We handle these in the next `else if` branch.
4191                                        None
4192                                    } else {
4193                                        Some((param.ty.span.shrink_to_lo(), "&".to_string()))
4194                                    }
4195                                })
4196                                .collect::<Vec<_>>()
4197                            && !sugg.is_empty()
4198                        {
4199                            let (the, s) = if sig.decl.inputs.len() == 1 {
4200                                ("the", "")
4201                            } else {
4202                                ("one of the", "s")
4203                            };
4204                            let dotdotdot =
4205                                if lt.kind == MissingLifetimeKind::Ampersand { "..." } else { "" };
4206                            err.multipart_suggestion(
4207                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("instead, you are more likely to want to change {0} argument{1} to be borrowed{2}",
                the, s, dotdotdot))
    })format!(
4208                                    "instead, you are more likely to want to change {the} \
4209                                     argument{s} to be borrowed{dotdotdot}",
4210                                ),
4211                                sugg,
4212                                Applicability::MaybeIncorrect,
4213                            );
4214                            "...or alternatively, you might want"
4215                        } else if (lt.kind == MissingLifetimeKind::Ampersand
4216                            || lt.kind == MissingLifetimeKind::Underscore)
4217                            && let Some((kind, _span)) = self.diag_metadata.current_function
4218                            && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = kind
4219                            && let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
4220                            && !sig.decl.inputs.is_empty()
4221                            && let arg_refs = sig
4222                                .decl
4223                                .inputs
4224                                .iter()
4225                                .filter_map(|param| match &param.ty.kind {
4226                                    TyKind::ImplTrait(_, bounds) => Some(bounds),
4227                                    _ => None,
4228                                })
4229                                .flat_map(|bounds| bounds.into_iter())
4230                                .collect::<Vec<_>>()
4231                            && !arg_refs.is_empty()
4232                        {
4233                            // We have a situation like
4234                            // fn g(mut x: impl Iterator<Item = &()>) -> Option<&()>
4235                            // So we look at every ref in the trait bound. If there's any, we
4236                            // suggest
4237                            // fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()>
4238                            let mut lt_finder =
4239                                LifetimeFinder { lifetime: lt.span, found: None, seen: ::alloc::vec::Vec::new()vec![] };
4240                            for bound in arg_refs {
4241                                if let ast::GenericBound::Trait(trait_ref) = bound {
4242                                    lt_finder.visit_trait_ref(&trait_ref.trait_ref);
4243                                }
4244                            }
4245                            lt_finder.visit_ty(ret_ty);
4246                            let spans_suggs: Vec<_> = lt_finder
4247                                .seen
4248                                .iter()
4249                                .filter_map(|ty| match &ty.kind {
4250                                    TyKind::Ref(_, mut_ty) => {
4251                                        let span = ty.span.with_hi(mut_ty.ty.span.lo());
4252                                        Some((span, "&'a ".to_string()))
4253                                    }
4254                                    _ => None,
4255                                })
4256                                .collect();
4257                            self.suggest_introducing_lifetime(
4258                                err,
4259                                None,
4260                                |err, higher_ranked, span, message, intro_sugg, _| {
4261                                    err.multipart_suggestion(
4262                                        message,
4263                                        std::iter::once((span, intro_sugg))
4264                                            .chain(spans_suggs.clone())
4265                                            .collect(),
4266                                        Applicability::MaybeIncorrect,
4267                                    );
4268                                    higher_ranked
4269                                },
4270                            );
4271                            "alternatively, you might want"
4272                        } else {
4273                            "instead, you are more likely to want"
4274                        };
4275                        let mut owned_sugg = lt.kind == MissingLifetimeKind::Ampersand;
4276                        let mut sugg_is_str_to_string = false;
4277                        let mut sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(lt.span, String::new())]))vec![(lt.span, String::new())];
4278                        if let Some((kind, _span)) = self.diag_metadata.current_function
4279                            && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = kind
4280                        {
4281                            let mut lt_finder =
4282                                LifetimeFinder { lifetime: lt.span, found: None, seen: ::alloc::vec::Vec::new()vec![] };
4283                            for param in &sig.decl.inputs {
4284                                lt_finder.visit_ty(&param.ty);
4285                            }
4286                            if let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output {
4287                                lt_finder.visit_ty(ret_ty);
4288                                let mut ret_lt_finder =
4289                                    LifetimeFinder { lifetime: lt.span, found: None, seen: ::alloc::vec::Vec::new()vec![] };
4290                                ret_lt_finder.visit_ty(ret_ty);
4291                                if let [Ty { span, kind: TyKind::Ref(_, mut_ty), .. }] =
4292                                    &ret_lt_finder.seen[..]
4293                                {
4294                                    // We might have a situation like
4295                                    // fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()>
4296                                    // but `lt.span` only points at `'_`, so to suggest `-> Option<()>`
4297                                    // we need to find a more accurate span to end up with
4298                                    // fn g<'a>(mut x: impl Iterator<Item = &'_ ()>) -> Option<()>
4299                                    sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.with_hi(mut_ty.ty.span.lo()), String::new())]))vec![(span.with_hi(mut_ty.ty.span.lo()), String::new())];
4300                                    owned_sugg = true;
4301                                }
4302                            }
4303                            if let Some(ty) = lt_finder.found {
4304                                if let TyKind::Path(None, path) = &ty.kind {
4305                                    // Check if the path being borrowed is likely to be owned.
4306                                    let path: Vec<_> = Segment::from_path(path);
4307                                    match self.resolve_path(
4308                                        &path,
4309                                        Some(TypeNS),
4310                                        None,
4311                                        PathSource::Type,
4312                                    ) {
4313                                        PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
4314                                            match module.res() {
4315                                                Some(Res::PrimTy(PrimTy::Str)) => {
4316                                                    // Don't suggest `-> str`, suggest `-> String`.
4317                                                    sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(lt.span.with_hi(ty.span.hi()), "String".to_string())]))vec![(
4318                                                        lt.span.with_hi(ty.span.hi()),
4319                                                        "String".to_string(),
4320                                                    )];
4321                                                    sugg_is_str_to_string = true;
4322                                                }
4323                                                Some(Res::PrimTy(..)) => {}
4324                                                Some(Res::Def(
4325                                                    DefKind::Struct
4326                                                    | DefKind::Union
4327                                                    | DefKind::Enum
4328                                                    | DefKind::ForeignTy
4329                                                    | DefKind::AssocTy
4330                                                    | DefKind::OpaqueTy
4331                                                    | DefKind::TyParam,
4332                                                    _,
4333                                                )) => {}
4334                                                _ => {
4335                                                    // Do not suggest in all other cases.
4336                                                    owned_sugg = false;
4337                                                }
4338                                            }
4339                                        }
4340                                        PathResult::NonModule(res) => {
4341                                            match res.base_res() {
4342                                                Res::PrimTy(PrimTy::Str) => {
4343                                                    // Don't suggest `-> str`, suggest `-> String`.
4344                                                    sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(lt.span.with_hi(ty.span.hi()), "String".to_string())]))vec![(
4345                                                        lt.span.with_hi(ty.span.hi()),
4346                                                        "String".to_string(),
4347                                                    )];
4348                                                    sugg_is_str_to_string = true;
4349                                                }
4350                                                Res::PrimTy(..) => {}
4351                                                Res::Def(
4352                                                    DefKind::Struct
4353                                                    | DefKind::Union
4354                                                    | DefKind::Enum
4355                                                    | DefKind::ForeignTy
4356                                                    | DefKind::AssocTy
4357                                                    | DefKind::OpaqueTy
4358                                                    | DefKind::TyParam,
4359                                                    _,
4360                                                ) => {}
4361                                                _ => {
4362                                                    // Do not suggest in all other cases.
4363                                                    owned_sugg = false;
4364                                                }
4365                                            }
4366                                        }
4367                                        _ => {
4368                                            // Do not suggest in all other cases.
4369                                            owned_sugg = false;
4370                                        }
4371                                    }
4372                                }
4373                                if let TyKind::Slice(inner_ty) = &ty.kind {
4374                                    // Don't suggest `-> [T]`, suggest `-> Vec<T>`.
4375                                    sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(lt.span.with_hi(inner_ty.span.lo()), "Vec<".to_string()),
                (ty.span.with_lo(inner_ty.span.hi()), ">".to_string())]))vec![
4376                                        (lt.span.with_hi(inner_ty.span.lo()), "Vec<".to_string()),
4377                                        (ty.span.with_lo(inner_ty.span.hi()), ">".to_string()),
4378                                    ];
4379                                }
4380                            }
4381                        }
4382                        if owned_sugg {
4383                            if let Some(span) =
4384                                self.find_ref_prefix_span_for_owned_suggestion(lt.span)
4385                                && !sugg_is_str_to_string
4386                            {
4387                                sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span, String::new())]))vec![(span, String::new())];
4388                            }
4389                            err.multipart_suggestion(
4390                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} to return an owned value",
                pre))
    })format!("{pre} to return an owned value"),
4391                                sugg,
4392                                Applicability::MaybeIncorrect,
4393                            );
4394                        }
4395                    }
4396                }
4397            }
4398            _ => {
4399                let lifetime_spans: Vec<_> =
4400                    in_scope_lifetimes.iter().map(|(ident, _)| ident.span).collect();
4401                err.span_note(lifetime_spans, "these named lifetimes are available to use");
4402
4403                if spans_suggs.len() > 0 {
4404                    // This happens when we have `Foo<T>` where we point at the space before `T`,
4405                    // but this can be confusing so we give a suggestion with placeholders.
4406                    err.multipart_suggestion(
4407                        "consider using one of the available lifetimes here",
4408                        spans_suggs,
4409                        Applicability::HasPlaceholders,
4410                    );
4411                }
4412            }
4413        }
4414    }
4415
4416    fn find_ref_prefix_span_for_owned_suggestion(&self, lifetime: Span) -> Option<Span> {
4417        let mut finder = RefPrefixSpanFinder { lifetime, span: None };
4418        if let Some(item) = self.diag_metadata.current_item {
4419            finder.visit_item(item);
4420        } else if let Some((kind, _span)) = self.diag_metadata.current_function
4421            && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = kind
4422        {
4423            for param in &sig.decl.inputs {
4424                finder.visit_ty(&param.ty);
4425            }
4426            if let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output {
4427                finder.visit_ty(ret_ty);
4428            }
4429        }
4430        finder.span
4431    }
4432}
4433
4434fn mk_where_bound_predicate(
4435    path: &Path,
4436    poly_trait_ref: &ast::PolyTraitRef,
4437    ty: &Ty,
4438) -> Option<ast::WhereBoundPredicate> {
4439    let modified_segments = {
4440        let mut segments = path.segments.clone();
4441        let [preceding @ .., second_last, last] = segments.as_mut_slice() else {
4442            return None;
4443        };
4444        let mut segments = ThinVec::from(preceding);
4445
4446        let added_constraint = ast::AngleBracketedArg::Constraint(ast::AssocItemConstraint {
4447            id: DUMMY_NODE_ID,
4448            ident: last.ident,
4449            gen_args: None,
4450            kind: ast::AssocItemConstraintKind::Equality {
4451                term: ast::Term::Ty(Box::new(ast::Ty {
4452                    kind: ast::TyKind::Path(None, poly_trait_ref.trait_ref.path.clone()),
4453                    id: DUMMY_NODE_ID,
4454                    span: DUMMY_SP,
4455                    tokens: None,
4456                })),
4457            },
4458            span: DUMMY_SP,
4459        });
4460
4461        match second_last.args.as_deref_mut() {
4462            Some(ast::GenericArgs::AngleBracketed(ast::AngleBracketedArgs { args, .. })) => {
4463                args.push(added_constraint);
4464            }
4465            Some(_) => return None,
4466            None => {
4467                second_last.args =
4468                    Some(Box::new(ast::GenericArgs::AngleBracketed(ast::AngleBracketedArgs {
4469                        args: ThinVec::from([added_constraint]),
4470                        span: DUMMY_SP,
4471                    })));
4472            }
4473        }
4474
4475        segments.push(second_last.clone());
4476        segments
4477    };
4478
4479    let new_where_bound_predicate = ast::WhereBoundPredicate {
4480        bound_generic_params: ThinVec::new(),
4481        bounded_ty: Box::new(ty.clone()),
4482        bounds: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ast::GenericBound::Trait(ast::PolyTraitRef {
                        bound_generic_params: ThinVec::new(),
                        modifiers: ast::TraitBoundModifiers::NONE,
                        trait_ref: ast::TraitRef {
                            path: ast::Path {
                                segments: modified_segments,
                                span: DUMMY_SP,
                                tokens: None,
                            },
                            ref_id: DUMMY_NODE_ID,
                        },
                        span: DUMMY_SP,
                        parens: ast::Parens::No,
                    })]))vec![ast::GenericBound::Trait(ast::PolyTraitRef {
4483            bound_generic_params: ThinVec::new(),
4484            modifiers: ast::TraitBoundModifiers::NONE,
4485            trait_ref: ast::TraitRef {
4486                path: ast::Path { segments: modified_segments, span: DUMMY_SP, tokens: None },
4487                ref_id: DUMMY_NODE_ID,
4488            },
4489            span: DUMMY_SP,
4490            parens: ast::Parens::No,
4491        })],
4492    };
4493
4494    Some(new_where_bound_predicate)
4495}
4496
4497/// Report lifetime/lifetime shadowing as an error.
4498pub(super) fn signal_lifetime_shadowing(
4499    sess: &Session,
4500    orig: Ident,
4501    shadower: Ident,
4502) -> ErrorGuaranteed {
4503    {
    sess.dcx().struct_span_err(shadower.span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("lifetime name `{0}` shadows a lifetime name that is already in scope",
                            orig.name))
                })).with_code(E0496)
}struct_span_code_err!(
4504        sess.dcx(),
4505        shadower.span,
4506        E0496,
4507        "lifetime name `{}` shadows a lifetime name that is already in scope",
4508        orig.name,
4509    )
4510    .with_span_label(orig.span, "first declared here")
4511    .with_span_label(shadower.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("lifetime `{0}` already in scope",
                orig.name))
    })format!("lifetime `{}` already in scope", orig.name))
4512    .emit()
4513}
4514
4515struct LifetimeFinder<'ast> {
4516    lifetime: Span,
4517    found: Option<&'ast Ty>,
4518    seen: Vec<&'ast Ty>,
4519}
4520
4521impl<'ast> Visitor<'ast> for LifetimeFinder<'ast> {
4522    fn visit_ty(&mut self, t: &'ast Ty) {
4523        if let TyKind::Ref(_, mut_ty) | TyKind::PinnedRef(_, mut_ty) = &t.kind {
4524            self.seen.push(t);
4525            if t.span.lo() == self.lifetime.lo() {
4526                self.found = Some(&mut_ty.ty);
4527            }
4528        }
4529        walk_ty(self, t)
4530    }
4531}
4532
4533struct RefPrefixSpanFinder {
4534    lifetime: Span,
4535    span: Option<Span>,
4536}
4537
4538impl<'ast> Visitor<'ast> for RefPrefixSpanFinder {
4539    fn visit_ty(&mut self, t: &'ast Ty) {
4540        if self.span.is_some() {
4541            return;
4542        }
4543        if let TyKind::Ref(_, mut_ty) | TyKind::PinnedRef(_, mut_ty) = &t.kind
4544            && t.span.lo() == self.lifetime.lo()
4545        {
4546            self.span = Some(t.span.with_hi(mut_ty.ty.span.lo()));
4547            return;
4548        }
4549        walk_ty(self, t);
4550    }
4551}
4552
4553/// Shadowing involving a label is only a warning for historical reasons.
4554//FIXME: make this a proper lint.
4555pub(super) fn signal_label_shadowing(sess: &Session, orig: Span, shadower: Ident) {
4556    let name = shadower.name;
4557    let shadower = shadower.span;
4558    sess.dcx()
4559        .struct_span_warn(
4560            shadower,
4561            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("label name `{0}` shadows a label name that is already in scope",
                name))
    })format!("label name `{name}` shadows a label name that is already in scope"),
4562        )
4563        .with_span_label(orig, "first declared here")
4564        .with_span_label(shadower, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("label `{0}` already in scope",
                name))
    })format!("label `{name}` already in scope"))
4565        .emit();
4566}
4567
4568struct ParentPathVisitor<'a> {
4569    target: Ident,
4570    parent: Option<&'a PathSegment>,
4571    stack: Vec<&'a Ty>,
4572}
4573
4574impl<'a> ParentPathVisitor<'a> {
4575    fn new(self_ty: &'a Ty, target: Ident) -> Self {
4576        let mut v = ParentPathVisitor { target, parent: None, stack: Vec::new() };
4577
4578        v.visit_ty(self_ty);
4579        v
4580    }
4581}
4582
4583impl<'a> Visitor<'a> for ParentPathVisitor<'a> {
4584    fn visit_ty(&mut self, ty: &'a Ty) {
4585        if self.parent.is_some() {
4586            return;
4587        }
4588
4589        // push current type
4590        self.stack.push(ty);
4591
4592        if let TyKind::Path(_, path) = &ty.kind
4593            // is this just `N`?
4594            && let [segment] = path.segments.as_slice()
4595            && segment.ident == self.target
4596            // parent is previous element in stack
4597            && let [.., parent_ty, _ty] = self.stack.as_slice()
4598            && let TyKind::Path(_, parent_path) = &parent_ty.kind
4599        {
4600            self.parent = parent_path.segments.first();
4601        }
4602
4603        walk_ty(self, ty);
4604
4605        self.stack.pop();
4606    }
4607}