]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_resolve/src/late/diagnostics.rs
Rollup merge of #101151 - jethrogb:jb/sgx-platform, r=JohnTitor
[rust.git] / compiler / rustc_resolve / src / late / diagnostics.rs
1 use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
2 use crate::late::{AliasPossibility, LateResolutionVisitor, RibKind};
3 use crate::late::{LifetimeBinderKind, LifetimeRes, LifetimeRibKind, LifetimeUseSet};
4 use crate::path_names_to_string;
5 use crate::{Module, ModuleKind, ModuleOrUniformRoot};
6 use crate::{PathResult, PathSource, Segment};
7
8 use rustc_ast::visit::{FnCtxt, FnKind, LifetimeCtxt};
9 use rustc_ast::{
10     self as ast, AssocItemKind, Expr, ExprKind, GenericParam, GenericParamKind, Item, ItemKind,
11     NodeId, Path, Ty, TyKind, DUMMY_NODE_ID,
12 };
13 use rustc_ast_pretty::pprust::path_segment_to_string;
14 use rustc_data_structures::fx::FxHashSet;
15 use rustc_errors::{
16     pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
17     MultiSpan,
18 };
19 use rustc_hir as hir;
20 use rustc_hir::def::Namespace::{self, *};
21 use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
22 use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE};
23 use rustc_hir::PrimTy;
24 use rustc_session::lint;
25 use rustc_session::parse::feature_err;
26 use rustc_session::Session;
27 use rustc_span::edition::Edition;
28 use rustc_span::hygiene::MacroKind;
29 use rustc_span::lev_distance::find_best_match_for_name;
30 use rustc_span::symbol::{kw, sym, Ident, Symbol};
31 use rustc_span::{BytePos, Span};
32
33 use std::iter;
34 use std::ops::Deref;
35
36 type Res = def::Res<ast::NodeId>;
37
38 /// A field or associated item from self type suggested in case of resolution failure.
39 enum AssocSuggestion {
40     Field,
41     MethodWithSelf,
42     AssocFn,
43     AssocType,
44     AssocConst,
45 }
46
47 impl AssocSuggestion {
48     fn action(&self) -> &'static str {
49         match self {
50             AssocSuggestion::Field => "use the available field",
51             AssocSuggestion::MethodWithSelf => "call the method with the fully-qualified path",
52             AssocSuggestion::AssocFn => "call the associated function",
53             AssocSuggestion::AssocConst => "use the associated `const`",
54             AssocSuggestion::AssocType => "use the associated type",
55         }
56     }
57 }
58
59 fn is_self_type(path: &[Segment], namespace: Namespace) -> bool {
60     namespace == TypeNS && path.len() == 1 && path[0].ident.name == kw::SelfUpper
61 }
62
63 fn is_self_value(path: &[Segment], namespace: Namespace) -> bool {
64     namespace == ValueNS && path.len() == 1 && path[0].ident.name == kw::SelfLower
65 }
66
67 /// Gets the stringified path for an enum from an `ImportSuggestion` for an enum variant.
68 fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, String) {
69     let variant_path = &suggestion.path;
70     let variant_path_string = path_names_to_string(variant_path);
71
72     let path_len = suggestion.path.segments.len();
73     let enum_path = ast::Path {
74         span: suggestion.path.span,
75         segments: suggestion.path.segments[0..path_len - 1].to_vec(),
76         tokens: None,
77     };
78     let enum_path_string = path_names_to_string(&enum_path);
79
80     (variant_path_string, enum_path_string)
81 }
82
83 /// Description of an elided lifetime.
84 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
85 pub(super) struct MissingLifetime {
86     /// Used to overwrite the resolution with the suggestion, to avoid cascasing errors.
87     pub id: NodeId,
88     /// Where to suggest adding the lifetime.
89     pub span: Span,
90     /// How the lifetime was introduced, to have the correct space and comma.
91     pub kind: MissingLifetimeKind,
92     /// Number of elided lifetimes, used for elision in path.
93     pub count: usize,
94 }
95
96 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
97 pub(super) enum MissingLifetimeKind {
98     /// An explicit `'_`.
99     Underscore,
100     /// An elided lifetime `&' ty`.
101     Ampersand,
102     /// An elided lifetime in brackets with written brackets.
103     Comma,
104     /// An elided lifetime with elided brackets.
105     Brackets,
106 }
107
108 /// Description of the lifetimes appearing in a function parameter.
109 /// This is used to provide a literal explanation to the elision failure.
110 #[derive(Clone, Debug)]
111 pub(super) struct ElisionFnParameter {
112     /// The index of the argument in the original definition.
113     pub index: usize,
114     /// The name of the argument if it's a simple ident.
115     pub ident: Option<Ident>,
116     /// The number of lifetimes in the parameter.
117     pub lifetime_count: usize,
118     /// The span of the parameter.
119     pub span: Span,
120 }
121
122 /// Description of lifetimes that appear as candidates for elision.
123 /// This is used to suggest introducing an explicit lifetime.
124 #[derive(Debug)]
125 pub(super) enum LifetimeElisionCandidate {
126     /// This is not a real lifetime.
127     Ignore,
128     /// There is a named lifetime, we won't suggest anything.
129     Named,
130     Missing(MissingLifetime),
131 }
132
133 impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
134     fn def_span(&self, def_id: DefId) -> Option<Span> {
135         match def_id.krate {
136             LOCAL_CRATE => self.r.opt_span(def_id),
137             _ => Some(self.r.cstore().get_span_untracked(def_id, self.r.session)),
138         }
139     }
140
141     /// Handles error reporting for `smart_resolve_path_fragment` function.
142     /// Creates base error and amends it with one short label and possibly some longer helps/notes.
143     pub(crate) fn smart_resolve_report_errors(
144         &mut self,
145         path: &[Segment],
146         span: Span,
147         source: PathSource<'_>,
148         res: Option<Res>,
149     ) -> (DiagnosticBuilder<'a, ErrorGuaranteed>, Vec<ImportSuggestion>) {
150         let ident_span = path.last().map_or(span, |ident| ident.ident.span);
151         let ns = source.namespace();
152         let is_expected = &|res| source.is_expected(res);
153         let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _));
154
155         debug!(?res, ?source);
156
157         // Make the base error.
158         struct BaseError<'a> {
159             msg: String,
160             fallback_label: String,
161             span: Span,
162             span_label: Option<(Span, &'a str)>,
163             could_be_expr: bool,
164             suggestion: Option<(Span, &'a str, String)>,
165         }
166         let mut expected = source.descr_expected();
167         let path_str = Segment::names_to_string(path);
168         let item_str = path.last().unwrap().ident;
169         let base_error = if let Some(res) = res {
170             BaseError {
171                 msg: format!("expected {}, found {} `{}`", expected, res.descr(), path_str),
172                 fallback_label: format!("not a {expected}"),
173                 span,
174                 span_label: match res {
175                     Res::Def(kind, def_id) if kind == DefKind::TyParam => {
176                         self.def_span(def_id).map(|span| (span, "found this type pararmeter"))
177                     }
178                     _ => None,
179                 },
180                 could_be_expr: match res {
181                     Res::Def(DefKind::Fn, _) => {
182                         // Verify whether this is a fn call or an Fn used as a type.
183                         self.r
184                             .session
185                             .source_map()
186                             .span_to_snippet(span)
187                             .map(|snippet| snippet.ends_with(')'))
188                             .unwrap_or(false)
189                     }
190                     Res::Def(
191                         DefKind::Ctor(..) | DefKind::AssocFn | DefKind::Const | DefKind::AssocConst,
192                         _,
193                     )
194                     | Res::SelfCtor(_)
195                     | Res::PrimTy(_)
196                     | Res::Local(_) => true,
197                     _ => false,
198                 },
199                 suggestion: None,
200             }
201         } else {
202             let item_span = path.last().unwrap().ident.span;
203             let (mod_prefix, mod_str, suggestion) = if path.len() == 1 {
204                 debug!(?self.diagnostic_metadata.current_impl_items);
205                 debug!(?self.diagnostic_metadata.current_function);
206                 let suggestion = if let Some(items) = self.diagnostic_metadata.current_impl_items
207                     && let Some((fn_kind, _)) = self.diagnostic_metadata.current_function
208                     && self.current_trait_ref.is_none()
209                     && let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
210                     && let Some(item) = items.iter().find(|i| {
211                         if let AssocItemKind::Fn(fn_) = &i.kind
212                             && !fn_.sig.decl.has_self()
213                             && i.ident.name == item_str.name
214                         {
215                             debug!(?item_str.name);
216                             debug!(?fn_.sig.decl.inputs);
217                             return true
218                         }
219                         false
220                     })
221                 {
222                     Some((
223                         item_span,
224                         "consider using the associated function",
225                         format!("Self::{}", item.ident)
226                     ))
227                 } else {
228                     None
229                 };
230                 (String::new(), "this scope".to_string(), suggestion)
231             } else if path.len() == 2 && path[0].ident.name == kw::PathRoot {
232                 if self.r.session.edition() > Edition::Edition2015 {
233                     // In edition 2018 onwards, the `::foo` syntax may only pull from the extern prelude
234                     // which overrides all other expectations of item type
235                     expected = "crate";
236                     (String::new(), "the list of imported crates".to_string(), None)
237                 } else {
238                     (String::new(), "the crate root".to_string(), None)
239                 }
240             } else if path.len() == 2 && path[0].ident.name == kw::Crate {
241                 (String::new(), "the crate root".to_string(), None)
242             } else {
243                 let mod_path = &path[..path.len() - 1];
244                 let mod_prefix = match self.resolve_path(mod_path, Some(TypeNS), None) {
245                     PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(),
246                     _ => None,
247                 }
248                 .map_or_else(String::new, |res| format!("{} ", res.descr()));
249                 (mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)), None)
250             };
251
252             let (fallback_label, suggestion) = if path_str == "async"
253                 && expected.starts_with("struct")
254             {
255                 ("`async` blocks are only allowed in Rust 2018 or later".to_string(), suggestion)
256             } else {
257                 // check if we are in situation of typo like `True` instead of `true`.
258                 let override_suggestion =
259                     if ["true", "false"].contains(&item_str.to_string().to_lowercase().as_str()) {
260                         let item_typo = item_str.to_string().to_lowercase();
261                         Some((
262                             item_span,
263                             "you may want to use a bool value instead",
264                             format!("{}", item_typo),
265                         ))
266                     } else {
267                         suggestion
268                     };
269                 (format!("not found in {mod_str}"), override_suggestion)
270             };
271
272             BaseError {
273                 msg: format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
274                 fallback_label,
275                 span: item_span,
276                 span_label: None,
277                 could_be_expr: false,
278                 suggestion,
279             }
280         };
281
282         let code = source.error_code(res.is_some());
283         let mut err =
284             self.r.session.struct_span_err_with_code(base_error.span, &base_error.msg, code);
285
286         self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span);
287
288         if let Some((span, label)) = base_error.span_label {
289             err.span_label(span, label);
290         }
291
292         if let Some(sugg) = base_error.suggestion {
293             err.span_suggestion_verbose(sugg.0, sugg.1, sugg.2, Applicability::MaybeIncorrect);
294         }
295
296         if let Some(span) = self.diagnostic_metadata.current_block_could_be_bare_struct_literal {
297             err.multipart_suggestion(
298                 "you might have meant to write a `struct` literal",
299                 vec![
300                     (span.shrink_to_lo(), "{ SomeStruct ".to_string()),
301                     (span.shrink_to_hi(), "}".to_string()),
302                 ],
303                 Applicability::HasPlaceholders,
304             );
305         }
306         match (source, self.diagnostic_metadata.in_if_condition) {
307             (
308                 PathSource::Expr(_),
309                 Some(Expr { span: expr_span, kind: ExprKind::Assign(lhs, _, _), .. }),
310             ) => {
311                 // Icky heuristic so we don't suggest:
312                 // `if (i + 2) = 2` => `if let (i + 2) = 2` (approximately pattern)
313                 // `if 2 = i` => `if let 2 = i` (lhs needs to contain error span)
314                 if lhs.is_approximately_pattern() && lhs.span.contains(span) {
315                     err.span_suggestion_verbose(
316                         expr_span.shrink_to_lo(),
317                         "you might have meant to use pattern matching",
318                         "let ",
319                         Applicability::MaybeIncorrect,
320                     );
321                 }
322             }
323             _ => {}
324         }
325
326         let is_assoc_fn = self.self_type_is_available();
327         // Emit help message for fake-self from other languages (e.g., `this` in Javascript).
328         if ["this", "my"].contains(&item_str.as_str()) && is_assoc_fn {
329             err.span_suggestion_short(
330                 span,
331                 "you might have meant to use `self` here instead",
332                 "self",
333                 Applicability::MaybeIncorrect,
334             );
335             if !self.self_value_is_available(path[0].ident.span) {
336                 if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) =
337                     &self.diagnostic_metadata.current_function
338                 {
339                     let (span, sugg) = if let Some(param) = sig.decl.inputs.get(0) {
340                         (param.span.shrink_to_lo(), "&self, ")
341                     } else {
342                         (
343                             self.r
344                                 .session
345                                 .source_map()
346                                 .span_through_char(*fn_span, '(')
347                                 .shrink_to_hi(),
348                             "&self",
349                         )
350                     };
351                     err.span_suggestion_verbose(
352                         span,
353                         "if you meant to use `self`, you are also missing a `self` receiver \
354                          argument",
355                         sugg,
356                         Applicability::MaybeIncorrect,
357                     );
358                 }
359             }
360         }
361
362         self.detect_assoct_type_constraint_meant_as_path(base_error.span, &mut err);
363
364         // Emit special messages for unresolved `Self` and `self`.
365         if is_self_type(path, ns) {
366             err.code(rustc_errors::error_code!(E0411));
367             err.span_label(
368                 span,
369                 "`Self` is only available in impls, traits, and type definitions".to_string(),
370             );
371             if let Some(item_kind) = self.diagnostic_metadata.current_item {
372                 err.span_label(
373                     item_kind.ident.span,
374                     format!(
375                         "`Self` not allowed in {} {}",
376                         item_kind.kind.article(),
377                         item_kind.kind.descr()
378                     ),
379                 );
380             }
381             return (err, Vec::new());
382         }
383         if is_self_value(path, ns) {
384             debug!("smart_resolve_path_fragment: E0424, source={:?}", source);
385
386             err.code(rustc_errors::error_code!(E0424));
387             err.span_label(span, match source {
388                 PathSource::Pat => "`self` value is a keyword and may not be bound to variables or shadowed",
389                 _ => "`self` value is a keyword only available in methods with a `self` parameter",
390             });
391             if let Some((fn_kind, span)) = &self.diagnostic_metadata.current_function {
392                 // The current function has a `self' parameter, but we were unable to resolve
393                 // a reference to `self`. This can only happen if the `self` identifier we
394                 // are resolving came from a different hygiene context.
395                 if fn_kind.decl().inputs.get(0).map_or(false, |p| p.is_self()) {
396                     err.span_label(*span, "this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters");
397                 } else {
398                     let doesnt = if is_assoc_fn {
399                         let (span, sugg) = fn_kind
400                             .decl()
401                             .inputs
402                             .get(0)
403                             .map(|p| (p.span.shrink_to_lo(), "&self, "))
404                             .unwrap_or_else(|| {
405                                 // Try to look for the "(" after the function name, if possible.
406                                 // This avoids placing the suggestion into the visibility specifier.
407                                 let span = fn_kind
408                                     .ident()
409                                     .map_or(*span, |ident| span.with_lo(ident.span.hi()));
410                                 (
411                                     self.r
412                                         .session
413                                         .source_map()
414                                         .span_through_char(span, '(')
415                                         .shrink_to_hi(),
416                                     "&self",
417                                 )
418                             });
419                         err.span_suggestion_verbose(
420                             span,
421                             "add a `self` receiver parameter to make the associated `fn` a method",
422                             sugg,
423                             Applicability::MaybeIncorrect,
424                         );
425                         "doesn't"
426                     } else {
427                         "can't"
428                     };
429                     if let Some(ident) = fn_kind.ident() {
430                         err.span_label(
431                             ident.span,
432                             &format!("this function {} have a `self` parameter", doesnt),
433                         );
434                     }
435                 }
436             } else if let Some(item_kind) = self.diagnostic_metadata.current_item {
437                 err.span_label(
438                     item_kind.ident.span,
439                     format!(
440                         "`self` not allowed in {} {}",
441                         item_kind.kind.article(),
442                         item_kind.kind.descr()
443                     ),
444                 );
445             }
446             return (err, Vec::new());
447         }
448
449         // Try to lookup name in more relaxed fashion for better error reporting.
450         let ident = path.last().unwrap().ident;
451         let mut candidates = self
452             .r
453             .lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
454             .into_iter()
455             .filter(|ImportSuggestion { did, .. }| {
456                 match (did, res.and_then(|res| res.opt_def_id())) {
457                     (Some(suggestion_did), Some(actual_did)) => *suggestion_did != actual_did,
458                     _ => true,
459                 }
460             })
461             .collect::<Vec<_>>();
462         let crate_def_id = CRATE_DEF_ID.to_def_id();
463         // Try to filter out intrinsics candidates, as long as we have
464         // some other candidates to suggest.
465         let intrinsic_candidates: Vec<_> = candidates
466             .drain_filter(|sugg| {
467                 let path = path_names_to_string(&sugg.path);
468                 path.starts_with("core::intrinsics::") || path.starts_with("std::intrinsics::")
469             })
470             .collect();
471         if candidates.is_empty() {
472             // Put them back if we have no more candidates to suggest...
473             candidates.extend(intrinsic_candidates);
474         }
475         if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
476             let mut enum_candidates: Vec<_> = self
477                 .r
478                 .lookup_import_candidates(ident, ns, &self.parent_scope, is_enum_variant)
479                 .into_iter()
480                 .map(|suggestion| import_candidate_to_enum_paths(&suggestion))
481                 .filter(|(_, enum_ty_path)| !enum_ty_path.starts_with("std::prelude::"))
482                 .collect();
483             if !enum_candidates.is_empty() {
484                 if let (PathSource::Type, Some(span)) =
485                     (source, self.diagnostic_metadata.current_type_ascription.last())
486                 {
487                     if self
488                         .r
489                         .session
490                         .parse_sess
491                         .type_ascription_path_suggestions
492                         .borrow()
493                         .contains(span)
494                     {
495                         // Already reported this issue on the lhs of the type ascription.
496                         err.delay_as_bug();
497                         return (err, candidates);
498                     }
499                 }
500
501                 enum_candidates.sort();
502
503                 // Contextualize for E0412 "cannot find type", but don't belabor the point
504                 // (that it's a variant) for E0573 "expected type, found variant".
505                 let preamble = if res.is_none() {
506                     let others = match enum_candidates.len() {
507                         1 => String::new(),
508                         2 => " and 1 other".to_owned(),
509                         n => format!(" and {} others", n),
510                     };
511                     format!("there is an enum variant `{}`{}; ", enum_candidates[0].0, others)
512                 } else {
513                     String::new()
514                 };
515                 let msg = format!("{}try using the variant's enum", preamble);
516
517                 err.span_suggestions(
518                     span,
519                     &msg,
520                     enum_candidates.into_iter().map(|(_variant_path, enum_ty_path)| enum_ty_path),
521                     Applicability::MachineApplicable,
522                 );
523             }
524         }
525         // Try Levenshtein algorithm.
526         let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
527         if path.len() == 1 && self.self_type_is_available() {
528             if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
529                 let self_is_available = self.self_value_is_available(path[0].ident.span);
530                 match candidate {
531                     AssocSuggestion::Field => {
532                         if self_is_available {
533                             err.span_suggestion(
534                                 span,
535                                 "you might have meant to use the available field",
536                                 format!("self.{path_str}"),
537                                 Applicability::MachineApplicable,
538                             );
539                         } else {
540                             err.span_label(span, "a field by this name exists in `Self`");
541                         }
542                     }
543                     AssocSuggestion::MethodWithSelf if self_is_available => {
544                         err.span_suggestion(
545                             span,
546                             "you might have meant to call the method",
547                             format!("self.{path_str}"),
548                             Applicability::MachineApplicable,
549                         );
550                     }
551                     AssocSuggestion::MethodWithSelf
552                     | AssocSuggestion::AssocFn
553                     | AssocSuggestion::AssocConst
554                     | AssocSuggestion::AssocType => {
555                         err.span_suggestion(
556                             span,
557                             &format!("you might have meant to {}", candidate.action()),
558                             format!("Self::{path_str}"),
559                             Applicability::MachineApplicable,
560                         );
561                     }
562                 }
563                 self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
564                 return (err, candidates);
565             }
566
567             // If the first argument in call is `self` suggest calling a method.
568             if let Some((call_span, args_span)) = self.call_has_self_arg(source) {
569                 let mut args_snippet = String::new();
570                 if let Some(args_span) = args_span {
571                     if let Ok(snippet) = self.r.session.source_map().span_to_snippet(args_span) {
572                         args_snippet = snippet;
573                     }
574                 }
575
576                 err.span_suggestion(
577                     call_span,
578                     &format!("try calling `{ident}` as a method"),
579                     format!("self.{path_str}({args_snippet})"),
580                     Applicability::MachineApplicable,
581                 );
582                 return (err, candidates);
583             }
584         }
585
586         // Try context-dependent help if relaxed lookup didn't work.
587         if let Some(res) = res {
588             if self.smart_resolve_context_dependent_help(
589                 &mut err,
590                 span,
591                 source,
592                 res,
593                 &path_str,
594                 &base_error.fallback_label,
595             ) {
596                 // We do this to avoid losing a secondary span when we override the main error span.
597                 self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
598                 return (err, candidates);
599             }
600         }
601
602         let is_macro =
603             base_error.span.from_expansion() && base_error.span.desugaring_kind().is_none();
604         if !self.type_ascription_suggestion(&mut err, base_error.span) {
605             let mut fallback = false;
606             if let (
607                 PathSource::Trait(AliasPossibility::Maybe),
608                 Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)),
609                 false,
610             ) = (source, res, is_macro)
611             {
612                 if let Some(bounds @ [_, .., _]) = self.diagnostic_metadata.current_trait_object {
613                     fallback = true;
614                     let spans: Vec<Span> = bounds
615                         .iter()
616                         .map(|bound| bound.span())
617                         .filter(|&sp| sp != base_error.span)
618                         .collect();
619
620                     let start_span = bounds[0].span();
621                     // `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
622                     let end_span = bounds.last().unwrap().span();
623                     // `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
624                     let last_bound_span = spans.last().cloned().unwrap();
625                     let mut multi_span: MultiSpan = spans.clone().into();
626                     for sp in spans {
627                         let msg = if sp == last_bound_span {
628                             format!(
629                                 "...because of {these} bound{s}",
630                                 these = pluralize!("this", bounds.len() - 1),
631                                 s = pluralize!(bounds.len() - 1),
632                             )
633                         } else {
634                             String::new()
635                         };
636                         multi_span.push_span_label(sp, msg);
637                     }
638                     multi_span
639                         .push_span_label(base_error.span, "expected this type to be a trait...");
640                     err.span_help(
641                         multi_span,
642                         "`+` is used to constrain a \"trait object\" type with lifetimes or \
643                          auto-traits; structs and enums can't be bound in that way",
644                     );
645                     if bounds.iter().all(|bound| match bound {
646                         ast::GenericBound::Outlives(_) => true,
647                         ast::GenericBound::Trait(tr, _) => tr.span == base_error.span,
648                     }) {
649                         let mut sugg = vec![];
650                         if base_error.span != start_span {
651                             sugg.push((start_span.until(base_error.span), String::new()));
652                         }
653                         if base_error.span != end_span {
654                             sugg.push((base_error.span.shrink_to_hi().to(end_span), String::new()));
655                         }
656
657                         err.multipart_suggestion(
658                             "if you meant to use a type and not a trait here, remove the bounds",
659                             sugg,
660                             Applicability::MaybeIncorrect,
661                         );
662                     }
663                 }
664             }
665
666             fallback |= self.restrict_assoc_type_in_where_clause(span, &mut err);
667
668             if !self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span) {
669                 fallback = true;
670                 match self.diagnostic_metadata.current_let_binding {
671                     Some((pat_sp, Some(ty_sp), None))
672                         if ty_sp.contains(base_error.span) && base_error.could_be_expr =>
673                     {
674                         err.span_suggestion_short(
675                             pat_sp.between(ty_sp),
676                             "use `=` if you meant to assign",
677                             " = ",
678                             Applicability::MaybeIncorrect,
679                         );
680                     }
681                     _ => {}
682                 }
683
684                 // If the trait has a single item (which wasn't matched by Levenshtein), suggest it
685                 let suggestion = self.get_single_associated_item(&path, &source, is_expected);
686                 self.r.add_typo_suggestion(&mut err, suggestion, ident_span);
687             }
688             if fallback {
689                 // Fallback label.
690                 err.span_label(base_error.span, base_error.fallback_label);
691             }
692         }
693         if let Some(err_code) = &err.code {
694             if err_code == &rustc_errors::error_code!(E0425) {
695                 for label_rib in &self.label_ribs {
696                     for (label_ident, node_id) in &label_rib.bindings {
697                         if format!("'{}", ident) == label_ident.to_string() {
698                             err.span_label(label_ident.span, "a label with a similar name exists");
699                             if let PathSource::Expr(Some(Expr {
700                                 kind: ExprKind::Break(None, Some(_)),
701                                 ..
702                             })) = source
703                             {
704                                 err.span_suggestion(
705                                     span,
706                                     "use the similarly named label",
707                                     label_ident.name,
708                                     Applicability::MaybeIncorrect,
709                                 );
710                                 // Do not lint against unused label when we suggest them.
711                                 self.diagnostic_metadata.unused_labels.remove(node_id);
712                             }
713                         }
714                     }
715                 }
716             } else if err_code == &rustc_errors::error_code!(E0412) {
717                 if let Some(correct) = Self::likely_rust_type(path) {
718                     err.span_suggestion(
719                         span,
720                         "perhaps you intended to use this type",
721                         correct,
722                         Applicability::MaybeIncorrect,
723                     );
724                 }
725             }
726         }
727
728         (err, candidates)
729     }
730
731     fn detect_assoct_type_constraint_meant_as_path(&self, base_span: Span, err: &mut Diagnostic) {
732         let Some(ty) = self.diagnostic_metadata.current_type_path else { return; };
733         let TyKind::Path(_, path) = &ty.kind else { return; };
734         for segment in &path.segments {
735             let Some(params) = &segment.args else { continue; };
736             let ast::GenericArgs::AngleBracketed(ref params) = params.deref() else { continue; };
737             for param in &params.args {
738                 let ast::AngleBracketedArg::Constraint(constraint) = param else { continue; };
739                 let ast::AssocConstraintKind::Bound { bounds } = &constraint.kind else {
740                     continue;
741                 };
742                 for bound in bounds {
743                     let ast::GenericBound::Trait(trait_ref, ast::TraitBoundModifier::None)
744                         = bound else
745                     {
746                         continue;
747                     };
748                     if base_span == trait_ref.span {
749                         err.span_suggestion_verbose(
750                             constraint.ident.span.between(trait_ref.span),
751                             "you might have meant to write a path instead of an associated type bound",
752                             "::",
753                             Applicability::MachineApplicable,
754                         );
755                     }
756                 }
757             }
758         }
759     }
760
761     fn suggest_swapping_misplaced_self_ty_and_trait(
762         &mut self,
763         err: &mut Diagnostic,
764         source: PathSource<'_>,
765         res: Option<Res>,
766         span: Span,
767     ) {
768         if let Some((trait_ref, self_ty)) =
769             self.diagnostic_metadata.currently_processing_impl_trait.clone()
770             && let TyKind::Path(_, self_ty_path) = &self_ty.kind
771             && let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
772                 self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None)
773             && let ModuleKind::Def(DefKind::Trait, ..) = module.kind
774             && trait_ref.path.span == span
775             && let PathSource::Trait(_) = source
776             && let Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) = res
777             && let Ok(self_ty_str) =
778                 self.r.session.source_map().span_to_snippet(self_ty.span)
779             && let Ok(trait_ref_str) =
780                 self.r.session.source_map().span_to_snippet(trait_ref.path.span)
781         {
782                 err.multipart_suggestion(
783                     "`impl` items mention the trait being implemented first and the type it is being implemented for second",
784                     vec![(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)],
785                     Applicability::MaybeIncorrect,
786                 );
787         }
788     }
789
790     fn get_single_associated_item(
791         &mut self,
792         path: &[Segment],
793         source: &PathSource<'_>,
794         filter_fn: &impl Fn(Res) -> bool,
795     ) -> Option<TypoSuggestion> {
796         if let crate::PathSource::TraitItem(_) = source {
797             let mod_path = &path[..path.len() - 1];
798             if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
799                 self.resolve_path(mod_path, None, None)
800             {
801                 let resolutions = self.r.resolutions(module).borrow();
802                 let targets: Vec<_> =
803                     resolutions
804                         .iter()
805                         .filter_map(|(key, resolution)| {
806                             resolution.borrow().binding.map(|binding| binding.res()).and_then(
807                                 |res| if filter_fn(res) { Some((key, res)) } else { None },
808                             )
809                         })
810                         .collect();
811                 if targets.len() == 1 {
812                     let target = targets[0];
813                     return Some(TypoSuggestion::single_item_from_res(
814                         target.0.ident.name,
815                         target.1,
816                     ));
817                 }
818             }
819         }
820         None
821     }
822
823     /// Given `where <T as Bar>::Baz: String`, suggest `where T: Bar<Baz = String>`.
824     fn restrict_assoc_type_in_where_clause(&mut self, span: Span, err: &mut Diagnostic) -> bool {
825         // Detect that we are actually in a `where` predicate.
826         let (bounded_ty, bounds, where_span) =
827             if let Some(ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
828                 bounded_ty,
829                 bound_generic_params,
830                 bounds,
831                 span,
832             })) = self.diagnostic_metadata.current_where_predicate
833             {
834                 if !bound_generic_params.is_empty() {
835                     return false;
836                 }
837                 (bounded_ty, bounds, span)
838             } else {
839                 return false;
840             };
841
842         // Confirm that the target is an associated type.
843         let (ty, position, path) = if let ast::TyKind::Path(
844             Some(ast::QSelf { ty, position, .. }),
845             path,
846         ) = &bounded_ty.kind
847         {
848             // use this to verify that ident is a type param.
849             let Some(partial_res) = self.r.partial_res_map.get(&bounded_ty.id) else {
850                 return false;
851             };
852             if !(matches!(
853                 partial_res.base_res(),
854                 hir::def::Res::Def(hir::def::DefKind::AssocTy, _)
855             ) && partial_res.unresolved_segments() == 0)
856             {
857                 return false;
858             }
859             (ty, position, path)
860         } else {
861             return false;
862         };
863
864         let peeled_ty = ty.peel_refs();
865         if let ast::TyKind::Path(None, type_param_path) = &peeled_ty.kind {
866             // Confirm that the `SelfTy` is a type parameter.
867             let Some(partial_res) = self.r.partial_res_map.get(&peeled_ty.id) else {
868                 return false;
869             };
870             if !(matches!(
871                 partial_res.base_res(),
872                 hir::def::Res::Def(hir::def::DefKind::TyParam, _)
873             ) && partial_res.unresolved_segments() == 0)
874             {
875                 return false;
876             }
877             if let (
878                 [ast::PathSegment { ident: constrain_ident, args: None, .. }],
879                 [ast::GenericBound::Trait(poly_trait_ref, ast::TraitBoundModifier::None)],
880             ) = (&type_param_path.segments[..], &bounds[..])
881             {
882                 if let [ast::PathSegment { ident, args: None, .. }] =
883                     &poly_trait_ref.trait_ref.path.segments[..]
884                 {
885                     if ident.span == span {
886                         err.span_suggestion_verbose(
887                             *where_span,
888                             &format!("constrain the associated type to `{}`", ident),
889                             format!(
890                                 "{}: {}<{} = {}>",
891                                 self.r
892                                     .session
893                                     .source_map()
894                                     .span_to_snippet(ty.span) // Account for `<&'a T as Foo>::Bar`.
895                                     .unwrap_or_else(|_| constrain_ident.to_string()),
896                                 path.segments[..*position]
897                                     .iter()
898                                     .map(|segment| path_segment_to_string(segment))
899                                     .collect::<Vec<_>>()
900                                     .join("::"),
901                                 path.segments[*position..]
902                                     .iter()
903                                     .map(|segment| path_segment_to_string(segment))
904                                     .collect::<Vec<_>>()
905                                     .join("::"),
906                                 ident,
907                             ),
908                             Applicability::MaybeIncorrect,
909                         );
910                     }
911                     return true;
912                 }
913             }
914         }
915         false
916     }
917
918     /// Check if the source is call expression and the first argument is `self`. If true,
919     /// return the span of whole call and the span for all arguments expect the first one (`self`).
920     fn call_has_self_arg(&self, source: PathSource<'_>) -> Option<(Span, Option<Span>)> {
921         let mut has_self_arg = None;
922         if let PathSource::Expr(Some(parent)) = source {
923             match &parent.kind {
924                 ExprKind::Call(_, args) if !args.is_empty() => {
925                     let mut expr_kind = &args[0].kind;
926                     loop {
927                         match expr_kind {
928                             ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
929                                 if arg_name.segments[0].ident.name == kw::SelfLower {
930                                     let call_span = parent.span;
931                                     let tail_args_span = if args.len() > 1 {
932                                         Some(Span::new(
933                                             args[1].span.lo(),
934                                             args.last().unwrap().span.hi(),
935                                             call_span.ctxt(),
936                                             None,
937                                         ))
938                                     } else {
939                                         None
940                                     };
941                                     has_self_arg = Some((call_span, tail_args_span));
942                                 }
943                                 break;
944                             }
945                             ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
946                             _ => break,
947                         }
948                     }
949                 }
950                 _ => (),
951             }
952         };
953         has_self_arg
954     }
955
956     fn followed_by_brace(&self, span: Span) -> (bool, Option<Span>) {
957         // HACK(estebank): find a better way to figure out that this was a
958         // parser issue where a struct literal is being used on an expression
959         // where a brace being opened means a block is being started. Look
960         // ahead for the next text to see if `span` is followed by a `{`.
961         let sm = self.r.session.source_map();
962         let mut sp = span;
963         loop {
964             sp = sm.next_point(sp);
965             match sm.span_to_snippet(sp) {
966                 Ok(ref snippet) => {
967                     if snippet.chars().any(|c| !c.is_whitespace()) {
968                         break;
969                     }
970                 }
971                 _ => break,
972             }
973         }
974         let followed_by_brace = matches!(sm.span_to_snippet(sp), Ok(ref snippet) if snippet == "{");
975         // In case this could be a struct literal that needs to be surrounded
976         // by parentheses, find the appropriate span.
977         let mut i = 0;
978         let mut closing_brace = None;
979         loop {
980             sp = sm.next_point(sp);
981             match sm.span_to_snippet(sp) {
982                 Ok(ref snippet) => {
983                     if snippet == "}" {
984                         closing_brace = Some(span.to(sp));
985                         break;
986                     }
987                 }
988                 _ => break,
989             }
990             i += 1;
991             // The bigger the span, the more likely we're incorrect --
992             // bound it to 100 chars long.
993             if i > 100 {
994                 break;
995             }
996         }
997         (followed_by_brace, closing_brace)
998     }
999
1000     /// Provides context-dependent help for errors reported by the `smart_resolve_path_fragment`
1001     /// function.
1002     /// Returns `true` if able to provide context-dependent help.
1003     fn smart_resolve_context_dependent_help(
1004         &mut self,
1005         err: &mut Diagnostic,
1006         span: Span,
1007         source: PathSource<'_>,
1008         res: Res,
1009         path_str: &str,
1010         fallback_label: &str,
1011     ) -> bool {
1012         let ns = source.namespace();
1013         let is_expected = &|res| source.is_expected(res);
1014
1015         let path_sep = |err: &mut Diagnostic, expr: &Expr, kind: DefKind| {
1016             const MESSAGE: &str = "use the path separator to refer to an item";
1017
1018             let (lhs_span, rhs_span) = match &expr.kind {
1019                 ExprKind::Field(base, ident) => (base.span, ident.span),
1020                 ExprKind::MethodCall(_, receiver, _, span) => (receiver.span, *span),
1021                 _ => return false,
1022             };
1023
1024             if lhs_span.eq_ctxt(rhs_span) {
1025                 err.span_suggestion(
1026                     lhs_span.between(rhs_span),
1027                     MESSAGE,
1028                     "::",
1029                     Applicability::MaybeIncorrect,
1030                 );
1031                 true
1032             } else if kind == DefKind::Struct
1033             && let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
1034             && let Ok(snippet) = self.r.session.source_map().span_to_snippet(lhs_source_span)
1035             {
1036                 // The LHS is a type that originates from a macro call.
1037                 // We have to add angle brackets around it.
1038
1039                 err.span_suggestion_verbose(
1040                     lhs_source_span.until(rhs_span),
1041                     MESSAGE,
1042                     format!("<{snippet}>::"),
1043                     Applicability::MaybeIncorrect,
1044                 );
1045                 true
1046             } else {
1047                 // Either we were unable to obtain the source span / the snippet or
1048                 // the LHS originates from a macro call and it is not a type and thus
1049                 // there is no way to replace `.` with `::` and still somehow suggest
1050                 // valid Rust code.
1051
1052                 false
1053             }
1054         };
1055
1056         let find_span = |source: &PathSource<'_>, err: &mut Diagnostic| {
1057             match source {
1058                 PathSource::Expr(Some(Expr { span, kind: ExprKind::Call(_, _), .. }))
1059                 | PathSource::TupleStruct(span, _) => {
1060                     // We want the main underline to cover the suggested code as well for
1061                     // cleaner output.
1062                     err.set_span(*span);
1063                     *span
1064                 }
1065                 _ => span,
1066             }
1067         };
1068
1069         let mut bad_struct_syntax_suggestion = |def_id: DefId| {
1070             let (followed_by_brace, closing_brace) = self.followed_by_brace(span);
1071
1072             match source {
1073                 PathSource::Expr(Some(
1074                     parent @ Expr { kind: ExprKind::Field(..) | ExprKind::MethodCall(..), .. },
1075                 )) if path_sep(err, &parent, DefKind::Struct) => {}
1076                 PathSource::Expr(
1077                     None
1078                     | Some(Expr {
1079                         kind:
1080                             ExprKind::Path(..)
1081                             | ExprKind::Binary(..)
1082                             | ExprKind::Unary(..)
1083                             | ExprKind::If(..)
1084                             | ExprKind::While(..)
1085                             | ExprKind::ForLoop(..)
1086                             | ExprKind::Match(..),
1087                         ..
1088                     }),
1089                 ) if followed_by_brace => {
1090                     if let Some(sp) = closing_brace {
1091                         err.span_label(span, fallback_label);
1092                         err.multipart_suggestion(
1093                             "surround the struct literal with parentheses",
1094                             vec![
1095                                 (sp.shrink_to_lo(), "(".to_string()),
1096                                 (sp.shrink_to_hi(), ")".to_string()),
1097                             ],
1098                             Applicability::MaybeIncorrect,
1099                         );
1100                     } else {
1101                         err.span_label(
1102                             span, // Note the parentheses surrounding the suggestion below
1103                             format!(
1104                                 "you might want to surround a struct literal with parentheses: \
1105                                  `({} {{ /* fields */ }})`?",
1106                                 path_str
1107                             ),
1108                         );
1109                     }
1110                 }
1111                 PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
1112                     let span = find_span(&source, err);
1113                     if let Some(span) = self.def_span(def_id) {
1114                         err.span_label(span, &format!("`{}` defined here", path_str));
1115                     }
1116                     let (tail, descr, applicability) = match source {
1117                         PathSource::Pat | PathSource::TupleStruct(..) => {
1118                             ("", "pattern", Applicability::MachineApplicable)
1119                         }
1120                         _ => (": val", "literal", Applicability::HasPlaceholders),
1121                     };
1122                     let (fields, applicability) = match self.r.field_names.get(&def_id) {
1123                         Some(fields) => (
1124                             fields
1125                                 .iter()
1126                                 .map(|f| format!("{}{}", f.node, tail))
1127                                 .collect::<Vec<String>>()
1128                                 .join(", "),
1129                             applicability,
1130                         ),
1131                         None => ("/* fields */".to_string(), Applicability::HasPlaceholders),
1132                     };
1133                     let pad = match self.r.field_names.get(&def_id) {
1134                         Some(fields) if fields.is_empty() => "",
1135                         _ => " ",
1136                     };
1137                     err.span_suggestion(
1138                         span,
1139                         &format!("use struct {} syntax instead", descr),
1140                         format!("{path_str} {{{pad}{fields}{pad}}}"),
1141                         applicability,
1142                     );
1143                 }
1144                 _ => {
1145                     err.span_label(span, fallback_label);
1146                 }
1147             }
1148         };
1149
1150         match (res, source) {
1151             (
1152                 Res::Def(DefKind::Macro(MacroKind::Bang), _),
1153                 PathSource::Expr(Some(Expr {
1154                     kind: ExprKind::Index(..) | ExprKind::Call(..), ..
1155                 }))
1156                 | PathSource::Struct,
1157             ) => {
1158                 err.span_label(span, fallback_label);
1159                 err.span_suggestion_verbose(
1160                     span.shrink_to_hi(),
1161                     "use `!` to invoke the macro",
1162                     "!",
1163                     Applicability::MaybeIncorrect,
1164                 );
1165                 if path_str == "try" && span.rust_2015() {
1166                     err.note("if you want the `try` keyword, you need Rust 2018 or later");
1167                 }
1168             }
1169             (Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
1170                 err.span_label(span, fallback_label);
1171             }
1172             (Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {
1173                 err.span_label(span, "type aliases cannot be used as traits");
1174                 if self.r.session.is_nightly_build() {
1175                     let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
1176                                `type` alias";
1177                     if let Some(span) = self.def_span(def_id) {
1178                         if let Ok(snip) = self.r.session.source_map().span_to_snippet(span) {
1179                             // The span contains a type alias so we should be able to
1180                             // replace `type` with `trait`.
1181                             let snip = snip.replacen("type", "trait", 1);
1182                             err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect);
1183                         } else {
1184                             err.span_help(span, msg);
1185                         }
1186                     } else {
1187                         err.help(msg);
1188                     }
1189                 }
1190             }
1191             (
1192                 Res::Def(kind @ (DefKind::Mod | DefKind::Trait), _),
1193                 PathSource::Expr(Some(parent)),
1194             ) => {
1195                 if !path_sep(err, &parent, kind) {
1196                     return false;
1197                 }
1198             }
1199             (
1200                 Res::Def(DefKind::Enum, def_id),
1201                 PathSource::TupleStruct(..) | PathSource::Expr(..),
1202             ) => {
1203                 if self
1204                     .diagnostic_metadata
1205                     .current_type_ascription
1206                     .last()
1207                     .map(|sp| {
1208                         self.r
1209                             .session
1210                             .parse_sess
1211                             .type_ascription_path_suggestions
1212                             .borrow()
1213                             .contains(&sp)
1214                     })
1215                     .unwrap_or(false)
1216                 {
1217                     err.downgrade_to_delayed_bug();
1218                     // We already suggested changing `:` into `::` during parsing.
1219                     return false;
1220                 }
1221
1222                 self.suggest_using_enum_variant(err, source, def_id, span);
1223             }
1224             (Res::Def(DefKind::Struct, def_id), source) if ns == ValueNS => {
1225                 let (ctor_def, ctor_vis, fields) =
1226                     if let Some(struct_ctor) = self.r.struct_constructors.get(&def_id).cloned() {
1227                         if let PathSource::Expr(Some(parent)) = source {
1228                             if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind {
1229                                 bad_struct_syntax_suggestion(def_id);
1230                                 return true;
1231                             }
1232                         }
1233                         struct_ctor
1234                     } else {
1235                         bad_struct_syntax_suggestion(def_id);
1236                         return true;
1237                     };
1238
1239                 let is_accessible = self.r.is_accessible_from(ctor_vis, self.parent_scope.module);
1240                 if !is_expected(ctor_def) || is_accessible {
1241                     return true;
1242                 }
1243
1244                 let field_spans = match source {
1245                     // e.g. `if let Enum::TupleVariant(field1, field2) = _`
1246                     PathSource::TupleStruct(_, pattern_spans) => {
1247                         err.set_primary_message(
1248                             "cannot match against a tuple struct which contains private fields",
1249                         );
1250
1251                         // Use spans of the tuple struct pattern.
1252                         Some(Vec::from(pattern_spans))
1253                     }
1254                     // e.g. `let _ = Enum::TupleVariant(field1, field2);`
1255                     _ if source.is_call() => {
1256                         err.set_primary_message(
1257                             "cannot initialize a tuple struct which contains private fields",
1258                         );
1259
1260                         // Use spans of the tuple struct definition.
1261                         self.r
1262                             .field_names
1263                             .get(&def_id)
1264                             .map(|fields| fields.iter().map(|f| f.span).collect::<Vec<_>>())
1265                     }
1266                     _ => None,
1267                 };
1268
1269                 if let Some(spans) =
1270                     field_spans.filter(|spans| spans.len() > 0 && fields.len() == spans.len())
1271                 {
1272                     let non_visible_spans: Vec<Span> = iter::zip(&fields, &spans)
1273                         .filter(|(vis, _)| {
1274                             !self.r.is_accessible_from(**vis, self.parent_scope.module)
1275                         })
1276                         .map(|(_, span)| *span)
1277                         .collect();
1278
1279                     if non_visible_spans.len() > 0 {
1280                         let mut m: MultiSpan = non_visible_spans.clone().into();
1281                         non_visible_spans
1282                             .into_iter()
1283                             .for_each(|s| m.push_span_label(s, "private field"));
1284                         err.span_note(m, "constructor is not visible here due to private fields");
1285                     }
1286
1287                     return true;
1288                 }
1289
1290                 err.span_label(span, "constructor is not visible here due to private fields");
1291             }
1292             (
1293                 Res::Def(
1294                     DefKind::Union | DefKind::Variant | DefKind::Ctor(_, CtorKind::Fictive),
1295                     def_id,
1296                 ),
1297                 _,
1298             ) if ns == ValueNS => {
1299                 bad_struct_syntax_suggestion(def_id);
1300             }
1301             (Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id), _) if ns == ValueNS => {
1302                 match source {
1303                     PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
1304                         let span = find_span(&source, err);
1305                         if let Some(span) = self.def_span(def_id) {
1306                             err.span_label(span, &format!("`{}` defined here", path_str));
1307                         }
1308                         err.span_suggestion(
1309                             span,
1310                             "use this syntax instead",
1311                             path_str,
1312                             Applicability::MaybeIncorrect,
1313                         );
1314                     }
1315                     _ => return false,
1316                 }
1317             }
1318             (Res::Def(DefKind::Ctor(_, CtorKind::Fn), def_id), _) if ns == ValueNS => {
1319                 if let Some(span) = self.def_span(def_id) {
1320                     err.span_label(span, &format!("`{}` defined here", path_str));
1321                 }
1322                 let fields = self.r.field_names.get(&def_id).map_or_else(
1323                     || "/* fields */".to_string(),
1324                     |fields| vec!["_"; fields.len()].join(", "),
1325                 );
1326                 err.span_suggestion(
1327                     span,
1328                     "use the tuple variant pattern syntax instead",
1329                     format!("{}({})", path_str, fields),
1330                     Applicability::HasPlaceholders,
1331                 );
1332             }
1333             (Res::SelfTy { .. }, _) if ns == ValueNS => {
1334                 err.span_label(span, fallback_label);
1335                 err.note("can't use `Self` as a constructor, you must use the implemented struct");
1336             }
1337             (Res::Def(DefKind::TyAlias | DefKind::AssocTy, _), _) if ns == ValueNS => {
1338                 err.note("can't use a type alias as a constructor");
1339             }
1340             _ => return false,
1341         }
1342         true
1343     }
1344
1345     /// Given the target `ident` and `kind`, search for the similarly named associated item
1346     /// in `self.current_trait_ref`.
1347     pub(crate) fn find_similarly_named_assoc_item(
1348         &mut self,
1349         ident: Symbol,
1350         kind: &AssocItemKind,
1351     ) -> Option<Symbol> {
1352         let (module, _) = self.current_trait_ref.as_ref()?;
1353         if ident == kw::Underscore {
1354             // We do nothing for `_`.
1355             return None;
1356         }
1357
1358         let resolutions = self.r.resolutions(module);
1359         let targets = resolutions
1360             .borrow()
1361             .iter()
1362             .filter_map(|(key, res)| res.borrow().binding.map(|binding| (key, binding.res())))
1363             .filter(|(_, res)| match (kind, res) {
1364                 (AssocItemKind::Const(..), Res::Def(DefKind::AssocConst, _)) => true,
1365                 (AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true,
1366                 (AssocItemKind::TyAlias(..), Res::Def(DefKind::AssocTy, _)) => true,
1367                 _ => false,
1368             })
1369             .map(|(key, _)| key.ident.name)
1370             .collect::<Vec<_>>();
1371
1372         find_best_match_for_name(&targets, ident, None)
1373     }
1374
1375     fn lookup_assoc_candidate<FilterFn>(
1376         &mut self,
1377         ident: Ident,
1378         ns: Namespace,
1379         filter_fn: FilterFn,
1380     ) -> Option<AssocSuggestion>
1381     where
1382         FilterFn: Fn(Res) -> bool,
1383     {
1384         fn extract_node_id(t: &Ty) -> Option<NodeId> {
1385             match t.kind {
1386                 TyKind::Path(None, _) => Some(t.id),
1387                 TyKind::Rptr(_, ref mut_ty) => extract_node_id(&mut_ty.ty),
1388                 // This doesn't handle the remaining `Ty` variants as they are not
1389                 // that commonly the self_type, it might be interesting to provide
1390                 // support for those in future.
1391                 _ => None,
1392             }
1393         }
1394
1395         // Fields are generally expected in the same contexts as locals.
1396         if filter_fn(Res::Local(ast::DUMMY_NODE_ID)) {
1397             if let Some(node_id) =
1398                 self.diagnostic_metadata.current_self_type.as_ref().and_then(extract_node_id)
1399             {
1400                 // Look for a field with the same name in the current self_type.
1401                 if let Some(resolution) = self.r.partial_res_map.get(&node_id) {
1402                     match resolution.base_res() {
1403                         Res::Def(DefKind::Struct | DefKind::Union, did)
1404                             if resolution.unresolved_segments() == 0 =>
1405                         {
1406                             if let Some(field_names) = self.r.field_names.get(&did) {
1407                                 if field_names
1408                                     .iter()
1409                                     .any(|&field_name| ident.name == field_name.node)
1410                                 {
1411                                     return Some(AssocSuggestion::Field);
1412                                 }
1413                             }
1414                         }
1415                         _ => {}
1416                     }
1417                 }
1418             }
1419         }
1420
1421         if let Some(items) = self.diagnostic_metadata.current_trait_assoc_items {
1422             for assoc_item in items {
1423                 if assoc_item.ident == ident {
1424                     return Some(match &assoc_item.kind {
1425                         ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
1426                         ast::AssocItemKind::Fn(box ast::Fn { sig, .. }) if sig.decl.has_self() => {
1427                             AssocSuggestion::MethodWithSelf
1428                         }
1429                         ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn,
1430                         ast::AssocItemKind::TyAlias(..) => AssocSuggestion::AssocType,
1431                         ast::AssocItemKind::MacCall(_) => continue,
1432                     });
1433                 }
1434             }
1435         }
1436
1437         // Look for associated items in the current trait.
1438         if let Some((module, _)) = self.current_trait_ref {
1439             if let Ok(binding) = self.r.maybe_resolve_ident_in_module(
1440                 ModuleOrUniformRoot::Module(module),
1441                 ident,
1442                 ns,
1443                 &self.parent_scope,
1444             ) {
1445                 let res = binding.res();
1446                 if filter_fn(res) {
1447                     if self.r.has_self.contains(&res.def_id()) {
1448                         return Some(AssocSuggestion::MethodWithSelf);
1449                     } else {
1450                         match res {
1451                             Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn),
1452                             Res::Def(DefKind::AssocConst, _) => {
1453                                 return Some(AssocSuggestion::AssocConst);
1454                             }
1455                             Res::Def(DefKind::AssocTy, _) => {
1456                                 return Some(AssocSuggestion::AssocType);
1457                             }
1458                             _ => {}
1459                         }
1460                     }
1461                 }
1462             }
1463         }
1464
1465         None
1466     }
1467
1468     fn lookup_typo_candidate(
1469         &mut self,
1470         path: &[Segment],
1471         ns: Namespace,
1472         filter_fn: &impl Fn(Res) -> bool,
1473     ) -> Option<TypoSuggestion> {
1474         let mut names = Vec::new();
1475         if path.len() == 1 {
1476             // Search in lexical scope.
1477             // Walk backwards up the ribs in scope and collect candidates.
1478             for rib in self.ribs[ns].iter().rev() {
1479                 // Locals and type parameters
1480                 for (ident, &res) in &rib.bindings {
1481                     if filter_fn(res) {
1482                         names.push(TypoSuggestion::typo_from_res(ident.name, res));
1483                     }
1484                 }
1485                 // Items in scope
1486                 if let RibKind::ModuleRibKind(module) = rib.kind {
1487                     // Items from this module
1488                     self.r.add_module_candidates(module, &mut names, &filter_fn);
1489
1490                     if let ModuleKind::Block = module.kind {
1491                         // We can see through blocks
1492                     } else {
1493                         // Items from the prelude
1494                         if !module.no_implicit_prelude {
1495                             let extern_prelude = self.r.extern_prelude.clone();
1496                             names.extend(extern_prelude.iter().flat_map(|(ident, _)| {
1497                                 self.r.crate_loader.maybe_process_path_extern(ident.name).and_then(
1498                                     |crate_id| {
1499                                         let crate_mod =
1500                                             Res::Def(DefKind::Mod, crate_id.as_def_id());
1501
1502                                         if filter_fn(crate_mod) {
1503                                             Some(TypoSuggestion::typo_from_res(
1504                                                 ident.name, crate_mod,
1505                                             ))
1506                                         } else {
1507                                             None
1508                                         }
1509                                     },
1510                                 )
1511                             }));
1512
1513                             if let Some(prelude) = self.r.prelude {
1514                                 self.r.add_module_candidates(prelude, &mut names, &filter_fn);
1515                             }
1516                         }
1517                         break;
1518                     }
1519                 }
1520             }
1521             // Add primitive types to the mix
1522             if filter_fn(Res::PrimTy(PrimTy::Bool)) {
1523                 names.extend(PrimTy::ALL.iter().map(|prim_ty| {
1524                     TypoSuggestion::typo_from_res(prim_ty.name(), Res::PrimTy(*prim_ty))
1525                 }))
1526             }
1527         } else {
1528             // Search in module.
1529             let mod_path = &path[..path.len() - 1];
1530             if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
1531                 self.resolve_path(mod_path, Some(TypeNS), None)
1532             {
1533                 self.r.add_module_candidates(module, &mut names, &filter_fn);
1534             }
1535         }
1536
1537         let name = path[path.len() - 1].ident.name;
1538         // Make sure error reporting is deterministic.
1539         names.sort_by(|a, b| a.candidate.as_str().partial_cmp(b.candidate.as_str()).unwrap());
1540
1541         match find_best_match_for_name(
1542             &names.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),
1543             name,
1544             None,
1545         ) {
1546             Some(found) if found != name => {
1547                 names.into_iter().find(|suggestion| suggestion.candidate == found)
1548             }
1549             _ => None,
1550         }
1551     }
1552
1553     // Returns the name of the Rust type approximately corresponding to
1554     // a type name in another programming language.
1555     fn likely_rust_type(path: &[Segment]) -> Option<Symbol> {
1556         let name = path[path.len() - 1].ident.as_str();
1557         // Common Java types
1558         Some(match name {
1559             "byte" => sym::u8, // In Java, bytes are signed, but in practice one almost always wants unsigned bytes.
1560             "short" => sym::i16,
1561             "Bool" => sym::bool,
1562             "Boolean" => sym::bool,
1563             "boolean" => sym::bool,
1564             "int" => sym::i32,
1565             "long" => sym::i64,
1566             "float" => sym::f32,
1567             "double" => sym::f64,
1568             _ => return None,
1569         })
1570     }
1571
1572     /// Only used in a specific case of type ascription suggestions
1573     fn get_colon_suggestion_span(&self, start: Span) -> Span {
1574         let sm = self.r.session.source_map();
1575         start.to(sm.next_point(start))
1576     }
1577
1578     fn type_ascription_suggestion(&self, err: &mut Diagnostic, base_span: Span) -> bool {
1579         let sm = self.r.session.source_map();
1580         let base_snippet = sm.span_to_snippet(base_span);
1581         if let Some(&sp) = self.diagnostic_metadata.current_type_ascription.last() {
1582             if let Ok(snippet) = sm.span_to_snippet(sp) {
1583                 let len = snippet.trim_end().len() as u32;
1584                 if snippet.trim() == ":" {
1585                     let colon_sp =
1586                         sp.with_lo(sp.lo() + BytePos(len - 1)).with_hi(sp.lo() + BytePos(len));
1587                     let mut show_label = true;
1588                     if sm.is_multiline(sp) {
1589                         err.span_suggestion_short(
1590                             colon_sp,
1591                             "maybe you meant to write `;` here",
1592                             ";",
1593                             Applicability::MaybeIncorrect,
1594                         );
1595                     } else {
1596                         let after_colon_sp =
1597                             self.get_colon_suggestion_span(colon_sp.shrink_to_hi());
1598                         if snippet.len() == 1 {
1599                             // `foo:bar`
1600                             err.span_suggestion(
1601                                 colon_sp,
1602                                 "maybe you meant to write a path separator here",
1603                                 "::",
1604                                 Applicability::MaybeIncorrect,
1605                             );
1606                             show_label = false;
1607                             if !self
1608                                 .r
1609                                 .session
1610                                 .parse_sess
1611                                 .type_ascription_path_suggestions
1612                                 .borrow_mut()
1613                                 .insert(colon_sp)
1614                             {
1615                                 err.downgrade_to_delayed_bug();
1616                             }
1617                         }
1618                         if let Ok(base_snippet) = base_snippet {
1619                             let mut sp = after_colon_sp;
1620                             for _ in 0..100 {
1621                                 // Try to find an assignment
1622                                 sp = sm.next_point(sp);
1623                                 let snippet = sm.span_to_snippet(sp.to(sm.next_point(sp)));
1624                                 match snippet {
1625                                     Ok(ref x) if x.as_str() == "=" => {
1626                                         err.span_suggestion(
1627                                             base_span,
1628                                             "maybe you meant to write an assignment here",
1629                                             format!("let {}", base_snippet),
1630                                             Applicability::MaybeIncorrect,
1631                                         );
1632                                         show_label = false;
1633                                         break;
1634                                     }
1635                                     Ok(ref x) if x.as_str() == "\n" => break,
1636                                     Err(_) => break,
1637                                     Ok(_) => {}
1638                                 }
1639                             }
1640                         }
1641                     }
1642                     if show_label {
1643                         err.span_label(
1644                             base_span,
1645                             "expecting a type here because of type ascription",
1646                         );
1647                     }
1648                     return show_label;
1649                 }
1650             }
1651         }
1652         false
1653     }
1654
1655     fn find_module(&mut self, def_id: DefId) -> Option<(Module<'a>, ImportSuggestion)> {
1656         let mut result = None;
1657         let mut seen_modules = FxHashSet::default();
1658         let mut worklist = vec![(self.r.graph_root, Vec::new())];
1659
1660         while let Some((in_module, path_segments)) = worklist.pop() {
1661             // abort if the module is already found
1662             if result.is_some() {
1663                 break;
1664             }
1665
1666             in_module.for_each_child(self.r, |_, ident, _, name_binding| {
1667                 // abort if the module is already found or if name_binding is private external
1668                 if result.is_some() || !name_binding.vis.is_visible_locally() {
1669                     return;
1670                 }
1671                 if let Some(module) = name_binding.module() {
1672                     // form the path
1673                     let mut path_segments = path_segments.clone();
1674                     path_segments.push(ast::PathSegment::from_ident(ident));
1675                     let module_def_id = module.def_id();
1676                     if module_def_id == def_id {
1677                         let path =
1678                             Path { span: name_binding.span, segments: path_segments, tokens: None };
1679                         result = Some((
1680                             module,
1681                             ImportSuggestion {
1682                                 did: Some(def_id),
1683                                 descr: "module",
1684                                 path,
1685                                 accessible: true,
1686                                 note: None,
1687                             },
1688                         ));
1689                     } else {
1690                         // add the module to the lookup
1691                         if seen_modules.insert(module_def_id) {
1692                             worklist.push((module, path_segments));
1693                         }
1694                     }
1695                 }
1696             });
1697         }
1698
1699         result
1700     }
1701
1702     fn collect_enum_ctors(&mut self, def_id: DefId) -> Option<Vec<(Path, DefId, CtorKind)>> {
1703         self.find_module(def_id).map(|(enum_module, enum_import_suggestion)| {
1704             let mut variants = Vec::new();
1705             enum_module.for_each_child(self.r, |_, ident, _, name_binding| {
1706                 if let Res::Def(DefKind::Ctor(CtorOf::Variant, kind), def_id) = name_binding.res() {
1707                     let mut segms = enum_import_suggestion.path.segments.clone();
1708                     segms.push(ast::PathSegment::from_ident(ident));
1709                     let path = Path { span: name_binding.span, segments: segms, tokens: None };
1710                     variants.push((path, def_id, kind));
1711                 }
1712             });
1713             variants
1714         })
1715     }
1716
1717     /// Adds a suggestion for using an enum's variant when an enum is used instead.
1718     fn suggest_using_enum_variant(
1719         &mut self,
1720         err: &mut Diagnostic,
1721         source: PathSource<'_>,
1722         def_id: DefId,
1723         span: Span,
1724     ) {
1725         let Some(variants) = self.collect_enum_ctors(def_id) else {
1726             err.note("you might have meant to use one of the enum's variants");
1727             return;
1728         };
1729
1730         let suggest_only_tuple_variants =
1731             matches!(source, PathSource::TupleStruct(..)) || source.is_call();
1732         if suggest_only_tuple_variants {
1733             // Suggest only tuple variants regardless of whether they have fields and do not
1734             // suggest path with added parentheses.
1735             let suggestable_variants = variants
1736                 .iter()
1737                 .filter(|(.., kind)| *kind == CtorKind::Fn)
1738                 .map(|(variant, ..)| path_names_to_string(variant))
1739                 .collect::<Vec<_>>();
1740
1741             let non_suggestable_variant_count = variants.len() - suggestable_variants.len();
1742
1743             let source_msg = if source.is_call() {
1744                 "to construct"
1745             } else if matches!(source, PathSource::TupleStruct(..)) {
1746                 "to match against"
1747             } else {
1748                 unreachable!()
1749             };
1750
1751             if !suggestable_variants.is_empty() {
1752                 let msg = if non_suggestable_variant_count == 0 && suggestable_variants.len() == 1 {
1753                     format!("try {} the enum's variant", source_msg)
1754                 } else {
1755                     format!("try {} one of the enum's variants", source_msg)
1756                 };
1757
1758                 err.span_suggestions(
1759                     span,
1760                     &msg,
1761                     suggestable_variants.into_iter(),
1762                     Applicability::MaybeIncorrect,
1763                 );
1764             }
1765
1766             // If the enum has no tuple variants..
1767             if non_suggestable_variant_count == variants.len() {
1768                 err.help(&format!("the enum has no tuple variants {}", source_msg));
1769             }
1770
1771             // If there are also non-tuple variants..
1772             if non_suggestable_variant_count == 1 {
1773                 err.help(&format!(
1774                     "you might have meant {} the enum's non-tuple variant",
1775                     source_msg
1776                 ));
1777             } else if non_suggestable_variant_count >= 1 {
1778                 err.help(&format!(
1779                     "you might have meant {} one of the enum's non-tuple variants",
1780                     source_msg
1781                 ));
1782             }
1783         } else {
1784             let needs_placeholder = |def_id: DefId, kind: CtorKind| {
1785                 let has_no_fields = self.r.field_names.get(&def_id).map_or(false, |f| f.is_empty());
1786                 match kind {
1787                     CtorKind::Const => false,
1788                     CtorKind::Fn | CtorKind::Fictive if has_no_fields => false,
1789                     _ => true,
1790                 }
1791             };
1792
1793             let suggestable_variants = variants
1794                 .iter()
1795                 .filter(|(_, def_id, kind)| !needs_placeholder(*def_id, *kind))
1796                 .map(|(variant, _, kind)| (path_names_to_string(variant), kind))
1797                 .map(|(variant, kind)| match kind {
1798                     CtorKind::Const => variant,
1799                     CtorKind::Fn => format!("({}())", variant),
1800                     CtorKind::Fictive => format!("({} {{}})", variant),
1801                 })
1802                 .collect::<Vec<_>>();
1803             let no_suggestable_variant = suggestable_variants.is_empty();
1804
1805             if !no_suggestable_variant {
1806                 let msg = if suggestable_variants.len() == 1 {
1807                     "you might have meant to use the following enum variant"
1808                 } else {
1809                     "you might have meant to use one of the following enum variants"
1810                 };
1811
1812                 err.span_suggestions(
1813                     span,
1814                     msg,
1815                     suggestable_variants.into_iter(),
1816                     Applicability::MaybeIncorrect,
1817                 );
1818             }
1819
1820             let suggestable_variants_with_placeholders = variants
1821                 .iter()
1822                 .filter(|(_, def_id, kind)| needs_placeholder(*def_id, *kind))
1823                 .map(|(variant, _, kind)| (path_names_to_string(variant), kind))
1824                 .filter_map(|(variant, kind)| match kind {
1825                     CtorKind::Fn => Some(format!("({}(/* fields */))", variant)),
1826                     CtorKind::Fictive => Some(format!("({} {{ /* fields */ }})", variant)),
1827                     _ => None,
1828                 })
1829                 .collect::<Vec<_>>();
1830
1831             if !suggestable_variants_with_placeholders.is_empty() {
1832                 let msg =
1833                     match (no_suggestable_variant, suggestable_variants_with_placeholders.len()) {
1834                         (true, 1) => "the following enum variant is available",
1835                         (true, _) => "the following enum variants are available",
1836                         (false, 1) => "alternatively, the following enum variant is available",
1837                         (false, _) => {
1838                             "alternatively, the following enum variants are also available"
1839                         }
1840                     };
1841
1842                 err.span_suggestions(
1843                     span,
1844                     msg,
1845                     suggestable_variants_with_placeholders.into_iter(),
1846                     Applicability::HasPlaceholders,
1847                 );
1848             }
1849         };
1850
1851         if def_id.is_local() {
1852             if let Some(span) = self.def_span(def_id) {
1853                 err.span_note(span, "the enum is defined here");
1854             }
1855         }
1856     }
1857
1858     pub(crate) fn report_missing_type_error(
1859         &self,
1860         path: &[Segment],
1861     ) -> Option<(Span, &'static str, String, Applicability)> {
1862         let (ident, span) = match path {
1863             [segment] if !segment.has_generic_args && segment.ident.name != kw::SelfUpper => {
1864                 (segment.ident.to_string(), segment.ident.span)
1865             }
1866             _ => return None,
1867         };
1868         let mut iter = ident.chars().map(|c| c.is_uppercase());
1869         let single_uppercase_char =
1870             matches!(iter.next(), Some(true)) && matches!(iter.next(), None);
1871         if !self.diagnostic_metadata.currently_processing_generics && !single_uppercase_char {
1872             return None;
1873         }
1874         match (self.diagnostic_metadata.current_item, single_uppercase_char, self.diagnostic_metadata.currently_processing_generics) {
1875             (Some(Item { kind: ItemKind::Fn(..), ident, .. }), _, _) if ident.name == sym::main => {
1876                 // Ignore `fn main()` as we don't want to suggest `fn main<T>()`
1877             }
1878             (
1879                 Some(Item {
1880                     kind:
1881                         kind @ ItemKind::Fn(..)
1882                         | kind @ ItemKind::Enum(..)
1883                         | kind @ ItemKind::Struct(..)
1884                         | kind @ ItemKind::Union(..),
1885                     ..
1886                 }),
1887                 true, _
1888             )
1889             // Without the 2nd `true`, we'd suggest `impl <T>` for `impl T` when a type `T` isn't found
1890             | (Some(Item { kind: kind @ ItemKind::Impl(..), .. }), true, true)
1891             | (Some(Item { kind, .. }), false, _) => {
1892                 // Likely missing type parameter.
1893                 if let Some(generics) = kind.generics() {
1894                     if span.overlaps(generics.span) {
1895                         // Avoid the following:
1896                         // error[E0405]: cannot find trait `A` in this scope
1897                         //  --> $DIR/typo-suggestion-named-underscore.rs:CC:LL
1898                         //   |
1899                         // L | fn foo<T: A>(x: T) {} // Shouldn't suggest underscore
1900                         //   |           ^- help: you might be missing a type parameter: `, A`
1901                         //   |           |
1902                         //   |           not found in this scope
1903                         return None;
1904                     }
1905                     let msg = "you might be missing a type parameter";
1906                     let (span, sugg) = if let [.., param] = &generics.params[..] {
1907                         let span = if let [.., bound] = &param.bounds[..] {
1908                             bound.span()
1909                         } else if let GenericParam {
1910                             kind: GenericParamKind::Const { ty, kw_span: _, default  }, ..
1911                         } = param {
1912                             default.as_ref().map(|def| def.value.span).unwrap_or(ty.span)
1913                         } else {
1914                             param.ident.span
1915                         };
1916                         (span, format!(", {}", ident))
1917                     } else {
1918                         (generics.span, format!("<{}>", ident))
1919                     };
1920                     // Do not suggest if this is coming from macro expansion.
1921                     if span.can_be_used_for_suggestions() {
1922                         return Some((
1923                             span.shrink_to_hi(),
1924                             msg,
1925                             sugg,
1926                             Applicability::MaybeIncorrect,
1927                         ));
1928                     }
1929                 }
1930             }
1931             _ => {}
1932         }
1933         None
1934     }
1935
1936     /// Given the target `label`, search the `rib_index`th label rib for similarly named labels,
1937     /// optionally returning the closest match and whether it is reachable.
1938     pub(crate) fn suggestion_for_label_in_rib(
1939         &self,
1940         rib_index: usize,
1941         label: Ident,
1942     ) -> Option<LabelSuggestion> {
1943         // Are ribs from this `rib_index` within scope?
1944         let within_scope = self.is_label_valid_from_rib(rib_index);
1945
1946         let rib = &self.label_ribs[rib_index];
1947         let names = rib
1948             .bindings
1949             .iter()
1950             .filter(|(id, _)| id.span.eq_ctxt(label.span))
1951             .map(|(id, _)| id.name)
1952             .collect::<Vec<Symbol>>();
1953
1954         find_best_match_for_name(&names, label.name, None).map(|symbol| {
1955             // Upon finding a similar name, get the ident that it was from - the span
1956             // contained within helps make a useful diagnostic. In addition, determine
1957             // whether this candidate is within scope.
1958             let (ident, _) = rib.bindings.iter().find(|(ident, _)| ident.name == symbol).unwrap();
1959             (*ident, within_scope)
1960         })
1961     }
1962
1963     pub(crate) fn maybe_report_lifetime_uses(
1964         &mut self,
1965         generics_span: Span,
1966         params: &[ast::GenericParam],
1967     ) {
1968         for (param_index, param) in params.iter().enumerate() {
1969             let GenericParamKind::Lifetime = param.kind else { continue };
1970
1971             let def_id = self.r.local_def_id(param.id);
1972
1973             let use_set = self.lifetime_uses.remove(&def_id);
1974             debug!(
1975                 "Use set for {:?}({:?} at {:?}) is {:?}",
1976                 def_id, param.ident, param.ident.span, use_set
1977             );
1978
1979             let deletion_span = || {
1980                 if params.len() == 1 {
1981                     // if sole lifetime, remove the entire `<>` brackets
1982                     generics_span
1983                 } else if param_index == 0 {
1984                     // if removing within `<>` brackets, we also want to
1985                     // delete a leading or trailing comma as appropriate
1986                     param.span().to(params[param_index + 1].span().shrink_to_lo())
1987                 } else {
1988                     // if removing within `<>` brackets, we also want to
1989                     // delete a leading or trailing comma as appropriate
1990                     params[param_index - 1].span().shrink_to_hi().to(param.span())
1991                 }
1992             };
1993             match use_set {
1994                 Some(LifetimeUseSet::Many) => {}
1995                 Some(LifetimeUseSet::One { use_span, use_ctxt }) => {
1996                     debug!(?param.ident, ?param.ident.span, ?use_span);
1997
1998                     let elidable = matches!(use_ctxt, LifetimeCtxt::Rptr);
1999
2000                     let deletion_span = deletion_span();
2001                     self.r.lint_buffer.buffer_lint_with_diagnostic(
2002                         lint::builtin::SINGLE_USE_LIFETIMES,
2003                         param.id,
2004                         param.ident.span,
2005                         &format!("lifetime parameter `{}` only used once", param.ident),
2006                         lint::BuiltinLintDiagnostics::SingleUseLifetime {
2007                             param_span: param.ident.span,
2008                             use_span: Some((use_span, elidable)),
2009                             deletion_span,
2010                         },
2011                     );
2012                 }
2013                 None => {
2014                     debug!(?param.ident, ?param.ident.span);
2015
2016                     let deletion_span = deletion_span();
2017                     self.r.lint_buffer.buffer_lint_with_diagnostic(
2018                         lint::builtin::UNUSED_LIFETIMES,
2019                         param.id,
2020                         param.ident.span,
2021                         &format!("lifetime parameter `{}` never used", param.ident),
2022                         lint::BuiltinLintDiagnostics::SingleUseLifetime {
2023                             param_span: param.ident.span,
2024                             use_span: None,
2025                             deletion_span,
2026                         },
2027                     );
2028                 }
2029             }
2030         }
2031     }
2032
2033     pub(crate) fn emit_undeclared_lifetime_error(
2034         &self,
2035         lifetime_ref: &ast::Lifetime,
2036         outer_lifetime_ref: Option<Ident>,
2037     ) {
2038         debug_assert_ne!(lifetime_ref.ident.name, kw::UnderscoreLifetime);
2039         let mut err = if let Some(outer) = outer_lifetime_ref {
2040             let mut err = struct_span_err!(
2041                 self.r.session,
2042                 lifetime_ref.ident.span,
2043                 E0401,
2044                 "can't use generic parameters from outer item",
2045             );
2046             err.span_label(lifetime_ref.ident.span, "use of generic parameter from outer item");
2047             err.span_label(outer.span, "lifetime parameter from outer item");
2048             err
2049         } else {
2050             let mut err = struct_span_err!(
2051                 self.r.session,
2052                 lifetime_ref.ident.span,
2053                 E0261,
2054                 "use of undeclared lifetime name `{}`",
2055                 lifetime_ref.ident
2056             );
2057             err.span_label(lifetime_ref.ident.span, "undeclared lifetime");
2058             err
2059         };
2060         self.suggest_introducing_lifetime(
2061             &mut err,
2062             Some(lifetime_ref.ident.name.as_str()),
2063             |err, _, span, message, suggestion| {
2064                 err.span_suggestion(span, message, suggestion, Applicability::MaybeIncorrect);
2065                 true
2066             },
2067         );
2068         err.emit();
2069     }
2070
2071     fn suggest_introducing_lifetime(
2072         &self,
2073         err: &mut Diagnostic,
2074         name: Option<&str>,
2075         suggest: impl Fn(&mut Diagnostic, bool, Span, &str, String) -> bool,
2076     ) {
2077         let mut suggest_note = true;
2078         for rib in self.lifetime_ribs.iter().rev() {
2079             let mut should_continue = true;
2080             match rib.kind {
2081                 LifetimeRibKind::Generics { binder: _, span, kind } => {
2082                     if !span.can_be_used_for_suggestions() && suggest_note && let Some(name) = name {
2083                         suggest_note = false; // Avoid displaying the same help multiple times.
2084                         err.span_label(
2085                             span,
2086                             &format!(
2087                                 "lifetime `{}` is missing in item created through this procedural macro",
2088                                 name,
2089                             ),
2090                         );
2091                         continue;
2092                     }
2093
2094                     let higher_ranked = matches!(
2095                         kind,
2096                         LifetimeBinderKind::BareFnType
2097                             | LifetimeBinderKind::PolyTrait
2098                             | LifetimeBinderKind::WhereBound
2099                     );
2100                     let (span, sugg) = if span.is_empty() {
2101                         let sugg = format!(
2102                             "{}<{}>{}",
2103                             if higher_ranked { "for" } else { "" },
2104                             name.unwrap_or("'a"),
2105                             if higher_ranked { " " } else { "" },
2106                         );
2107                         (span, sugg)
2108                     } else {
2109                         let span =
2110                             self.r.session.source_map().span_through_char(span, '<').shrink_to_hi();
2111                         let sugg = format!("{}, ", name.unwrap_or("'a"));
2112                         (span, sugg)
2113                     };
2114                     if higher_ranked {
2115                         let message = format!(
2116                             "consider making the {} lifetime-generic with a new `{}` lifetime",
2117                             kind.descr(),
2118                             name.unwrap_or("'a"),
2119                         );
2120                         should_continue = suggest(err, true, span, &message, sugg);
2121                         err.note_once(
2122                             "for more information on higher-ranked polymorphism, visit \
2123                              https://doc.rust-lang.org/nomicon/hrtb.html",
2124                         );
2125                     } else if let Some(name) = name {
2126                         let message = format!("consider introducing lifetime `{}` here", name);
2127                         should_continue = suggest(err, false, span, &message, sugg);
2128                     } else {
2129                         let message = format!("consider introducing a named lifetime parameter");
2130                         should_continue = suggest(err, false, span, &message, sugg);
2131                     }
2132                 }
2133                 LifetimeRibKind::Item => break,
2134                 _ => {}
2135             }
2136             if !should_continue {
2137                 break;
2138             }
2139         }
2140     }
2141
2142     pub(crate) fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &ast::Lifetime) {
2143         struct_span_err!(
2144             self.r.session,
2145             lifetime_ref.ident.span,
2146             E0771,
2147             "use of non-static lifetime `{}` in const generic",
2148             lifetime_ref.ident
2149         )
2150         .note(
2151             "for more information, see issue #74052 \
2152             <https://github.com/rust-lang/rust/issues/74052>",
2153         )
2154         .emit();
2155     }
2156
2157     /// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
2158     /// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
2159     /// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
2160     pub(crate) fn maybe_emit_forbidden_non_static_lifetime_error(
2161         &self,
2162         lifetime_ref: &ast::Lifetime,
2163     ) {
2164         let feature_active = self.r.session.features_untracked().generic_const_exprs;
2165         if !feature_active {
2166             feature_err(
2167                 &self.r.session.parse_sess,
2168                 sym::generic_const_exprs,
2169                 lifetime_ref.ident.span,
2170                 "a non-static lifetime is not allowed in a `const`",
2171             )
2172             .emit();
2173         }
2174     }
2175
2176     pub(crate) fn report_missing_lifetime_specifiers(
2177         &mut self,
2178         lifetime_refs: Vec<MissingLifetime>,
2179         function_param_lifetimes: Option<(Vec<MissingLifetime>, Vec<ElisionFnParameter>)>,
2180     ) -> ErrorGuaranteed {
2181         let num_lifetimes: usize = lifetime_refs.iter().map(|lt| lt.count).sum();
2182         let spans: Vec<_> = lifetime_refs.iter().map(|lt| lt.span).collect();
2183
2184         let mut err = struct_span_err!(
2185             self.r.session,
2186             spans,
2187             E0106,
2188             "missing lifetime specifier{}",
2189             pluralize!(num_lifetimes)
2190         );
2191         self.add_missing_lifetime_specifiers_label(
2192             &mut err,
2193             lifetime_refs,
2194             function_param_lifetimes,
2195         );
2196         err.emit()
2197     }
2198
2199     fn add_missing_lifetime_specifiers_label(
2200         &mut self,
2201         err: &mut Diagnostic,
2202         lifetime_refs: Vec<MissingLifetime>,
2203         function_param_lifetimes: Option<(Vec<MissingLifetime>, Vec<ElisionFnParameter>)>,
2204     ) {
2205         for &lt in &lifetime_refs {
2206             err.span_label(
2207                 lt.span,
2208                 format!(
2209                     "expected {} lifetime parameter{}",
2210                     if lt.count == 1 { "named".to_string() } else { lt.count.to_string() },
2211                     pluralize!(lt.count),
2212                 ),
2213             );
2214         }
2215
2216         let mut in_scope_lifetimes: Vec<_> = self
2217             .lifetime_ribs
2218             .iter()
2219             .rev()
2220             .take_while(|rib| !matches!(rib.kind, LifetimeRibKind::Item))
2221             .flat_map(|rib| rib.bindings.iter())
2222             .map(|(&ident, &res)| (ident, res))
2223             .filter(|(ident, _)| ident.name != kw::UnderscoreLifetime)
2224             .collect();
2225         debug!(?in_scope_lifetimes);
2226
2227         debug!(?function_param_lifetimes);
2228         if let Some((param_lifetimes, params)) = &function_param_lifetimes {
2229             let elided_len = param_lifetimes.len();
2230             let num_params = params.len();
2231
2232             let mut m = String::new();
2233
2234             for (i, info) in params.iter().enumerate() {
2235                 let ElisionFnParameter { ident, index, lifetime_count, span } = *info;
2236                 debug_assert_ne!(lifetime_count, 0);
2237
2238                 err.span_label(span, "");
2239
2240                 if i != 0 {
2241                     if i + 1 < num_params {
2242                         m.push_str(", ");
2243                     } else if num_params == 2 {
2244                         m.push_str(" or ");
2245                     } else {
2246                         m.push_str(", or ");
2247                     }
2248                 }
2249
2250                 let help_name = if let Some(ident) = ident {
2251                     format!("`{}`", ident)
2252                 } else {
2253                     format!("argument {}", index + 1)
2254                 };
2255
2256                 if lifetime_count == 1 {
2257                     m.push_str(&help_name[..])
2258                 } else {
2259                     m.push_str(&format!("one of {}'s {} lifetimes", help_name, lifetime_count)[..])
2260                 }
2261             }
2262
2263             if num_params == 0 {
2264                 err.help(
2265                     "this function's return type contains a borrowed value, \
2266                  but there is no value for it to be borrowed from",
2267                 );
2268                 if in_scope_lifetimes.is_empty() {
2269                     in_scope_lifetimes = vec![(
2270                         Ident::with_dummy_span(kw::StaticLifetime),
2271                         (DUMMY_NODE_ID, LifetimeRes::Static),
2272                     )];
2273                 }
2274             } else if elided_len == 0 {
2275                 err.help(
2276                     "this function's return type contains a borrowed value with \
2277                  an elided lifetime, but the lifetime cannot be derived from \
2278                  the arguments",
2279                 );
2280                 if in_scope_lifetimes.is_empty() {
2281                     in_scope_lifetimes = vec![(
2282                         Ident::with_dummy_span(kw::StaticLifetime),
2283                         (DUMMY_NODE_ID, LifetimeRes::Static),
2284                     )];
2285                 }
2286             } else if num_params == 1 {
2287                 err.help(&format!(
2288                     "this function's return type contains a borrowed value, \
2289                  but the signature does not say which {} it is borrowed from",
2290                     m
2291                 ));
2292             } else {
2293                 err.help(&format!(
2294                     "this function's return type contains a borrowed value, \
2295                  but the signature does not say whether it is borrowed from {}",
2296                     m
2297                 ));
2298             }
2299         }
2300
2301         let existing_name = match &in_scope_lifetimes[..] {
2302             [] => Symbol::intern("'a"),
2303             [(existing, _)] => existing.name,
2304             _ => Symbol::intern("'lifetime"),
2305         };
2306
2307         let mut spans_suggs: Vec<_> = Vec::new();
2308         let build_sugg = |lt: MissingLifetime| match lt.kind {
2309             MissingLifetimeKind::Underscore => {
2310                 debug_assert_eq!(lt.count, 1);
2311                 (lt.span, existing_name.to_string())
2312             }
2313             MissingLifetimeKind::Ampersand => {
2314                 debug_assert_eq!(lt.count, 1);
2315                 (lt.span.shrink_to_hi(), format!("{} ", existing_name))
2316             }
2317             MissingLifetimeKind::Comma => {
2318                 let sugg: String = std::iter::repeat([existing_name.as_str(), ", "])
2319                     .take(lt.count)
2320                     .flatten()
2321                     .collect();
2322                 (lt.span.shrink_to_hi(), sugg)
2323             }
2324             MissingLifetimeKind::Brackets => {
2325                 let sugg: String = std::iter::once("<")
2326                     .chain(
2327                         std::iter::repeat(existing_name.as_str()).take(lt.count).intersperse(", "),
2328                     )
2329                     .chain([">"])
2330                     .collect();
2331                 (lt.span.shrink_to_hi(), sugg)
2332             }
2333         };
2334         for &lt in &lifetime_refs {
2335             spans_suggs.push(build_sugg(lt));
2336         }
2337         debug!(?spans_suggs);
2338         match in_scope_lifetimes.len() {
2339             0 => {
2340                 if let Some((param_lifetimes, _)) = function_param_lifetimes {
2341                     for lt in param_lifetimes {
2342                         spans_suggs.push(build_sugg(lt))
2343                     }
2344                 }
2345                 self.suggest_introducing_lifetime(
2346                     err,
2347                     None,
2348                     |err, higher_ranked, span, message, intro_sugg| {
2349                         err.multipart_suggestion_verbose(
2350                             message,
2351                             std::iter::once((span, intro_sugg))
2352                                 .chain(spans_suggs.iter().cloned())
2353                                 .collect(),
2354                             Applicability::MaybeIncorrect,
2355                         );
2356                         higher_ranked
2357                     },
2358                 );
2359             }
2360             1 => {
2361                 err.multipart_suggestion_verbose(
2362                     &format!("consider using the `{}` lifetime", existing_name),
2363                     spans_suggs,
2364                     Applicability::MaybeIncorrect,
2365                 );
2366
2367                 // Record as using the suggested resolution.
2368                 let (_, (_, res)) = in_scope_lifetimes[0];
2369                 for &lt in &lifetime_refs {
2370                     self.r.lifetimes_res_map.insert(lt.id, res);
2371                 }
2372             }
2373             _ => {
2374                 let lifetime_spans: Vec<_> =
2375                     in_scope_lifetimes.iter().map(|(ident, _)| ident.span).collect();
2376                 err.span_note(lifetime_spans, "these named lifetimes are available to use");
2377
2378                 if spans_suggs.len() > 0 {
2379                     // This happens when we have `Foo<T>` where we point at the space before `T`,
2380                     // but this can be confusing so we give a suggestion with placeholders.
2381                     err.multipart_suggestion_verbose(
2382                         "consider using one of the available lifetimes here",
2383                         spans_suggs,
2384                         Applicability::HasPlaceholders,
2385                     );
2386                 }
2387             }
2388         }
2389     }
2390 }
2391
2392 /// Report lifetime/lifetime shadowing as an error.
2393 pub fn signal_lifetime_shadowing(sess: &Session, orig: Ident, shadower: Ident) {
2394     let mut err = struct_span_err!(
2395         sess,
2396         shadower.span,
2397         E0496,
2398         "lifetime name `{}` shadows a lifetime name that is already in scope",
2399         orig.name,
2400     );
2401     err.span_label(orig.span, "first declared here");
2402     err.span_label(shadower.span, format!("lifetime `{}` already in scope", orig.name));
2403     err.emit();
2404 }
2405
2406 /// Shadowing involving a label is only a warning for historical reasons.
2407 //FIXME: make this a proper lint.
2408 pub fn signal_label_shadowing(sess: &Session, orig: Span, shadower: Ident) {
2409     let name = shadower.name;
2410     let shadower = shadower.span;
2411     let mut err = sess.struct_span_warn(
2412         shadower,
2413         &format!("label name `{}` shadows a label name that is already in scope", name),
2414     );
2415     err.span_label(orig, "first declared here");
2416     err.span_label(shadower, format!("label `{}` already in scope", name));
2417     err.emit();
2418 }