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