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::{CrateLint, Module, ModuleKind, ModuleOrUniformRoot}
;
6 use crate::{PathResult, PathSource, Segment}
;
8 use rustc_ast
::visit
::FnKind
;
10 self as ast
, Expr
, ExprKind
, GenericParam
, GenericParamKind
, Item
, ItemKind
, NodeId
, Path
, Ty
,
13 use rustc_ast_pretty
::pprust
::path_segment_to_string
;
14 use rustc_data_structures
::fx
::FxHashSet
;
15 use rustc_errors
::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, SuggestionStyle}
;
17 use rustc_hir
::def
::Namespace
::{self, *}
;
18 use rustc_hir
::def
::{self, CtorKind, CtorOf, DefKind}
;
19 use rustc_hir
::def_id
::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}
;
20 use rustc_hir
::PrimTy
;
21 use rustc_session
::parse
::feature_err
;
22 use rustc_span
::edition
::Edition
;
23 use rustc_span
::hygiene
::MacroKind
;
24 use rustc_span
::lev_distance
::find_best_match_for_name
;
25 use rustc_span
::symbol
::{kw, sym, Ident, Symbol}
;
26 use rustc_span
::{BytePos, MultiSpan, Span, DUMMY_SP}
;
32 type Res
= def
::Res
<ast
::NodeId
>;
34 /// A field or associated item from self type suggested in case of resolution failure.
35 enum AssocSuggestion
{
43 impl AssocSuggestion
{
44 fn action(&self) -> &'
static str {
46 AssocSuggestion
::Field
=> "use the available field",
47 AssocSuggestion
::MethodWithSelf
=> "call the method with the fully-qualified path",
48 AssocSuggestion
::AssocFn
=> "call the associated function",
49 AssocSuggestion
::AssocConst
=> "use the associated `const`",
50 AssocSuggestion
::AssocType
=> "use the associated type",
55 crate enum MissingLifetimeSpot
<'tcx
> {
56 Generics(&'tcx hir
::Generics
<'tcx
>),
57 HigherRanked { span: Span, span_type: ForLifetimeSpanType }
,
61 crate enum ForLifetimeSpanType
{
68 impl ForLifetimeSpanType
{
69 crate fn descr(&self) -> &'
static str {
71 Self::BoundEmpty
| Self::BoundTail
=> "bound",
72 Self::TypeEmpty
| Self::TypeTail
=> "type",
76 crate fn suggestion(&self, sugg
: &str) -> String
{
78 Self::BoundEmpty
| Self::TypeEmpty
=> format
!("for<{}> ", sugg
),
79 Self::BoundTail
| Self::TypeTail
=> format
!(", {}", sugg
),
84 impl<'tcx
> Into
<MissingLifetimeSpot
<'tcx
>> for &'tcx hir
::Generics
<'tcx
> {
85 fn into(self) -> MissingLifetimeSpot
<'tcx
> {
86 MissingLifetimeSpot
::Generics(self)
90 fn is_self_type(path
: &[Segment
], namespace
: Namespace
) -> bool
{
91 namespace
== TypeNS
&& path
.len() == 1 && path
[0].ident
.name
== kw
::SelfUpper
94 fn is_self_value(path
: &[Segment
], namespace
: Namespace
) -> bool
{
95 namespace
== ValueNS
&& path
.len() == 1 && path
[0].ident
.name
== kw
::SelfLower
98 /// Gets the stringified path for an enum from an `ImportSuggestion` for an enum variant.
99 fn import_candidate_to_enum_paths(suggestion
: &ImportSuggestion
) -> (String
, String
) {
100 let variant_path
= &suggestion
.path
;
101 let variant_path_string
= path_names_to_string(variant_path
);
103 let path_len
= suggestion
.path
.segments
.len();
104 let enum_path
= ast
::Path
{
105 span
: suggestion
.path
.span
,
106 segments
: suggestion
.path
.segments
[0..path_len
- 1].to_vec(),
109 let enum_path_string
= path_names_to_string(&enum_path
);
111 (variant_path_string
, enum_path_string
)
114 impl<'a
: 'ast
, 'ast
> LateResolutionVisitor
<'a
, '_
, 'ast
> {
115 fn def_span(&self, def_id
: DefId
) -> Option
<Span
> {
117 LOCAL_CRATE
=> self.r
.opt_span(def_id
),
122 .guess_head_span(self.r
.cstore().get_span_untracked(def_id
, self.r
.session
)),
127 /// Handles error reporting for `smart_resolve_path_fragment` function.
128 /// Creates base error and amends it with one short label and possibly some longer helps/notes.
129 pub(crate) fn smart_resolve_report_errors(
133 source
: PathSource
<'_
>,
135 ) -> (DiagnosticBuilder
<'a
>, Vec
<ImportSuggestion
>) {
136 let ident_span
= path
.last().map_or(span
, |ident
| ident
.ident
.span
);
137 let ns
= source
.namespace();
138 let is_expected
= &|res
| source
.is_expected(res
);
139 let is_enum_variant
= &|res
| matches
!(res
, Res
::Def(DefKind
::Variant
, _
));
141 // Make the base error.
142 let mut expected
= source
.descr_expected();
143 let path_str
= Segment
::names_to_string(path
);
144 let item_str
= path
.last().unwrap().ident
;
145 let (base_msg
, fallback_label
, base_span
, could_be_expr
) = if let Some(res
) = res
{
147 format
!("expected {}, found {} `{}`", expected
, res
.descr(), path_str
),
148 format
!("not a {}", expected
),
151 Res
::Def(DefKind
::Fn
, _
) => {
152 // Verify whether this is a fn call or an Fn used as a type.
156 .span_to_snippet(span
)
157 .map(|snippet
| snippet
.ends_with('
)'
))
161 DefKind
::Ctor(..) | DefKind
::AssocFn
| DefKind
::Const
| DefKind
::AssocConst
,
166 | Res
::Local(_
) => true,
171 let item_span
= path
.last().unwrap().ident
.span
;
172 let (mod_prefix
, mod_str
) = if path
.len() == 1 {
173 (String
::new(), "this scope".to_string())
174 } else if path
.len() == 2 && path
[0].ident
.name
== kw
::PathRoot
{
175 if self.r
.session
.edition() > Edition
::Edition2015
{
176 // In edition 2018 onwards, the `::foo` syntax may only pull from the extern prelude
177 // which overrides all other expectations of item type
179 (String
::new(), "the list of imported crates".to_string())
181 (String
::new(), "the crate root".to_string())
183 } else if path
.len() == 2 && path
[0].ident
.name
== kw
::Crate
{
184 (String
::new(), "the crate root".to_string())
186 let mod_path
= &path
[..path
.len() - 1];
188 match self.resolve_path(mod_path
, Some(TypeNS
), false, span
, CrateLint
::No
) {
189 PathResult
::Module(ModuleOrUniformRoot
::Module(module
)) => module
.res(),
192 .map_or_else(String
::new
, |res
| format
!("{} ", res
.descr()));
193 (mod_prefix
, format
!("`{}`", Segment
::names_to_string(mod_path
)))
196 format
!("cannot find {} `{}` in {}{}", expected
, item_str
, mod_prefix
, mod_str
),
197 if path_str
== "async" && expected
.starts_with("struct") {
198 "`async` blocks are only allowed in Rust 2018 or later".to_string()
200 format
!("not found in {}", mod_str
)
207 let code
= source
.error_code(res
.is_some());
208 let mut err
= self.r
.session
.struct_span_err_with_code(base_span
, &base_msg
, code
);
210 match (source
, self.diagnostic_metadata
.in_if_condition
) {
211 (PathSource
::Expr(_
), Some(Expr { span, kind: ExprKind::Assign(..), .. }
)) => {
212 err
.span_suggestion_verbose(
214 "you might have meant to use pattern matching",
216 Applicability
::MaybeIncorrect
,
218 self.r
.session
.if_let_suggestions
.borrow_mut().insert(*span
);
223 let is_assoc_fn
= self.self_type_is_available(span
);
224 // Emit help message for fake-self from other languages (e.g., `this` in Javascript).
225 if ["this", "my"].contains(&&*item_str
.as_str()) && is_assoc_fn
{
226 err
.span_suggestion_short(
228 "you might have meant to use `self` here instead",
230 Applicability
::MaybeIncorrect
,
232 if !self.self_value_is_available(path
[0].ident
.span
, span
) {
233 if let Some((FnKind
::Fn(_
, _
, sig
, ..), fn_span
)) =
234 &self.diagnostic_metadata
.current_function
236 let (span
, sugg
) = if let Some(param
) = sig
.decl
.inputs
.get(0) {
237 (param
.span
.shrink_to_lo(), "&self, ")
243 .span_through_char(*fn_span
, '
('
)
248 err
.span_suggestion_verbose(
250 "if you meant to use `self`, you are also missing a `self` receiver \
253 Applicability
::MaybeIncorrect
,
259 // Emit special messages for unresolved `Self` and `self`.
260 if is_self_type(path
, ns
) {
261 err
.code(rustc_errors
::error_code
!(E0411
));
264 "`Self` is only available in impls, traits, and type definitions".to_string(),
266 return (err
, Vec
::new());
268 if is_self_value(path
, ns
) {
269 debug
!("smart_resolve_path_fragment: E0424, source={:?}", source
);
271 err
.code(rustc_errors
::error_code
!(E0424
));
272 err
.span_label(span
, match source
{
273 PathSource
::Pat
=> "`self` value is a keyword and may not be bound to variables or shadowed"
275 _
=> "`self` value is a keyword only available in methods with a `self` parameter"
278 if let Some((fn_kind
, span
)) = &self.diagnostic_metadata
.current_function
{
279 // The current function has a `self' parameter, but we were unable to resolve
280 // a reference to `self`. This can only happen if the `self` identifier we
281 // are resolving came from a different hygiene context.
282 if fn_kind
.decl().inputs
.get(0).map_or(false, |p
| p
.is_self()) {
283 err
.span_label(*span
, "this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters");
285 let doesnt
= if is_assoc_fn
{
286 let (span
, sugg
) = fn_kind
290 .map(|p
| (p
.span
.shrink_to_lo(), "&self, "))
296 .span_through_char(*span
, '
('
)
301 err
.span_suggestion_verbose(
303 "add a `self` receiver parameter to make the associated `fn` a method",
305 Applicability
::MaybeIncorrect
,
311 if let Some(ident
) = fn_kind
.ident() {
314 &format
!("this function {} have a `self` parameter", doesnt
),
319 return (err
, Vec
::new());
322 // Try to lookup name in more relaxed fashion for better error reporting.
323 let ident
= path
.last().unwrap().ident
;
324 let candidates
= self
326 .lookup_import_candidates(ident
, ns
, &self.parent_scope
, is_expected
)
328 .filter(|ImportSuggestion { did, .. }
| {
329 match (did
, res
.and_then(|res
| res
.opt_def_id())) {
330 (Some(suggestion_did
), Some(actual_did
)) => *suggestion_did
!= actual_did
,
334 .collect
::<Vec
<_
>>();
335 let crate_def_id
= DefId
::local(CRATE_DEF_INDEX
);
336 if candidates
.is_empty() && is_expected(Res
::Def(DefKind
::Enum
, crate_def_id
)) {
337 let mut enum_candidates
: Vec
<_
> = self
339 .lookup_import_candidates(ident
, ns
, &self.parent_scope
, is_enum_variant
)
341 .map(|suggestion
| import_candidate_to_enum_paths(&suggestion
))
342 .filter(|(_
, enum_ty_path
)| !enum_ty_path
.starts_with("std::prelude::"))
344 if !enum_candidates
.is_empty() {
345 if let (PathSource
::Type
, Some(span
)) =
346 (source
, self.diagnostic_metadata
.current_type_ascription
.last())
352 .type_ascription_path_suggestions
356 // Already reported this issue on the lhs of the type ascription.
358 return (err
, candidates
);
362 enum_candidates
.sort();
364 // Contextualize for E0412 "cannot find type", but don't belabor the point
365 // (that it's a variant) for E0573 "expected type, found variant".
366 let preamble
= if res
.is_none() {
367 let others
= match enum_candidates
.len() {
369 2 => " and 1 other".to_owned(),
370 n
=> format
!(" and {} others", n
),
372 format
!("there is an enum variant `{}`{}; ", enum_candidates
[0].0, others
)
376 let msg
= format
!("{}try using the variant's enum", preamble
);
378 err
.span_suggestions(
381 enum_candidates
.into_iter().map(|(_variant_path
, enum_ty_path
)| enum_ty_path
),
382 Applicability
::MachineApplicable
,
386 if path
.len() == 1 && self.self_type_is_available(span
) {
387 if let Some(candidate
) = self.lookup_assoc_candidate(ident
, ns
, is_expected
) {
388 let self_is_available
= self.self_value_is_available(path
[0].ident
.span
, span
);
390 AssocSuggestion
::Field
=> {
391 if self_is_available
{
394 "you might have meant to use the available field",
395 format
!("self.{}", path_str
),
396 Applicability
::MachineApplicable
,
399 err
.span_label(span
, "a field by this name exists in `Self`");
402 AssocSuggestion
::MethodWithSelf
if self_is_available
=> {
405 "you might have meant to call the method",
406 format
!("self.{}", path_str
),
407 Applicability
::MachineApplicable
,
410 AssocSuggestion
::MethodWithSelf
411 | AssocSuggestion
::AssocFn
412 | AssocSuggestion
::AssocConst
413 | AssocSuggestion
::AssocType
=> {
416 &format
!("you might have meant to {}", candidate
.action()),
417 format
!("Self::{}", path_str
),
418 Applicability
::MachineApplicable
,
422 return (err
, candidates
);
425 // If the first argument in call is `self` suggest calling a method.
426 if let Some((call_span
, args_span
)) = self.call_has_self_arg(source
) {
427 let mut args_snippet
= String
::new();
428 if let Some(args_span
) = args_span
{
429 if let Ok(snippet
) = self.r
.session
.source_map().span_to_snippet(args_span
) {
430 args_snippet
= snippet
;
436 &format
!("try calling `{}` as a method", ident
),
437 format
!("self.{}({})", path_str
, args_snippet
),
438 Applicability
::MachineApplicable
,
440 return (err
, candidates
);
444 // Try Levenshtein algorithm.
445 let typo_sugg
= self.lookup_typo_candidate(path
, ns
, is_expected
, span
);
446 // Try context-dependent help if relaxed lookup didn't work.
447 if let Some(res
) = res
{
448 if self.smart_resolve_context_dependent_help(
456 // We do this to avoid losing a secondary span when we override the main error span.
457 self.r
.add_typo_suggestion(&mut err
, typo_sugg
, ident_span
);
458 return (err
, candidates
);
462 let is_macro
= base_span
.from_expansion() && base_span
.desugaring_kind().is_none();
463 if !self.type_ascription_suggestion(&mut err
, base_span
) {
464 let mut fallback
= false;
466 PathSource
::Trait(AliasPossibility
::Maybe
),
467 Some(Res
::Def(DefKind
::Struct
| DefKind
::Enum
| DefKind
::Union
, _
)),
469 ) = (source
, res
, is_macro
)
471 if let Some(bounds @
[_
, .., _
]) = self.diagnostic_metadata
.current_trait_object
{
473 let spans
: Vec
<Span
> = bounds
475 .map(|bound
| bound
.span())
476 .filter(|&sp
| sp
!= base_span
)
479 let start_span
= bounds
.iter().map(|bound
| bound
.span()).next().unwrap();
480 // `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
481 let end_span
= bounds
.iter().map(|bound
| bound
.span()).last().unwrap();
482 // `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
483 let last_bound_span
= spans
.last().cloned().unwrap();
484 let mut multi_span
: MultiSpan
= spans
.clone().into();
486 let msg
= if sp
== last_bound_span
{
488 "...because of {} bound{}",
489 if bounds
.len() <= 2 { "this" }
else { "these" }
,
490 if bounds
.len() <= 2 { "" }
else { "s" }
,
495 multi_span
.push_span_label(sp
, msg
);
497 multi_span
.push_span_label(
499 "expected this type to be a trait...".to_string(),
503 "`+` is used to constrain a \"trait object\" type with lifetimes or \
504 auto-traits; structs and enums can't be bound in that way",
506 if bounds
.iter().all(|bound
| match bound
{
507 ast
::GenericBound
::Outlives(_
) => true,
508 ast
::GenericBound
::Trait(tr
, _
) => tr
.span
== base_span
,
510 let mut sugg
= vec
![];
511 if base_span
!= start_span
{
512 sugg
.push((start_span
.until(base_span
), String
::new()));
514 if base_span
!= end_span
{
515 sugg
.push((base_span
.shrink_to_hi().to(end_span
), String
::new()));
518 err
.multipart_suggestion(
519 "if you meant to use a type and not a trait here, remove the bounds",
521 Applicability
::MaybeIncorrect
,
527 fallback
|= self.restrict_assoc_type_in_where_clause(span
, &mut err
);
529 if !self.r
.add_typo_suggestion(&mut err
, typo_sugg
, ident_span
) {
531 match self.diagnostic_metadata
.current_let_binding
{
532 Some((pat_sp
, Some(ty_sp
), None
))
533 if ty_sp
.contains(base_span
) && could_be_expr
=>
535 err
.span_suggestion_short(
536 pat_sp
.between(ty_sp
),
537 "use `=` if you meant to assign",
539 Applicability
::MaybeIncorrect
,
547 err
.span_label(base_span
, fallback_label
);
550 if let Some(err_code
) = &err
.code
{
551 if err_code
== &rustc_errors
::error_code
!(E0425
) {
552 for label_rib
in &self.label_ribs
{
553 for (label_ident
, node_id
) in &label_rib
.bindings
{
554 if format
!("'{}", ident
) == label_ident
.to_string() {
555 err
.span_label(label_ident
.span
, "a label with a similar name exists");
556 if let PathSource
::Expr(Some(Expr
{
557 kind
: ExprKind
::Break(None
, Some(_
)),
563 "use the similarly named label",
564 label_ident
.name
.to_string(),
565 Applicability
::MaybeIncorrect
,
567 // Do not lint against unused label when we suggest them.
568 self.diagnostic_metadata
.unused_labels
.remove(node_id
);
573 } else if err_code
== &rustc_errors
::error_code
!(E0412
) {
574 if let Some(correct
) = Self::likely_rust_type(path
) {
577 "perhaps you intended to use this type",
579 Applicability
::MaybeIncorrect
,
588 /// Given `where <T as Bar>::Baz: String`, suggest `where T: Bar<Baz = String>`.
589 fn restrict_assoc_type_in_where_clause(
592 err
: &mut DiagnosticBuilder
<'_
>,
594 // Detect that we are actually in a `where` predicate.
595 let (bounded_ty
, bounds
, where_span
) =
596 if let Some(ast
::WherePredicate
::BoundPredicate(ast
::WhereBoundPredicate
{
598 bound_generic_params
,
601 })) = self.diagnostic_metadata
.current_where_predicate
603 if !bound_generic_params
.is_empty() {
606 (bounded_ty
, bounds
, span
)
611 // Confirm that the target is an associated type.
612 let (ty
, position
, path
) = if let ast
::TyKind
::Path(
613 Some(ast
::QSelf { ty, position, .. }
),
617 // use this to verify that ident is a type param.
618 let partial_res
= if let Ok(Some(partial_res
)) = self.resolve_qpath_anywhere(
621 &Segment
::from_path(path
),
632 partial_res
.base_res(),
633 hir
::def
::Res
::Def(hir
::def
::DefKind
::AssocTy
, _
)
634 ) && partial_res
.unresolved_segments() == 0)
643 if let ast
::TyKind
::Path(None
, type_param_path
) = &ty
.peel_refs().kind
{
644 // Confirm that the `SelfTy` is a type parameter.
645 let partial_res
= if let Ok(Some(partial_res
)) = self.resolve_qpath_anywhere(
648 &Segment
::from_path(type_param_path
),
659 partial_res
.base_res(),
660 hir
::def
::Res
::Def(hir
::def
::DefKind
::TyParam
, _
)
661 ) && partial_res
.unresolved_segments() == 0)
666 [ast
::PathSegment { ident: constrain_ident, args: None, .. }
],
667 [ast
::GenericBound
::Trait(poly_trait_ref
, ast
::TraitBoundModifier
::None
)],
668 ) = (&type_param_path
.segments
[..], &bounds
[..])
670 if let [ast
::PathSegment { ident, args: None, .. }
] =
671 &poly_trait_ref
.trait_ref
.path
.segments
[..]
673 if ident
.span
== span
{
674 err
.span_suggestion_verbose(
676 &format
!("constrain the associated type to `{}`", ident
),
682 .span_to_snippet(ty
.span
) // Account for `<&'a T as Foo>::Bar`.
683 .unwrap_or_else(|_
| constrain_ident
.to_string()),
684 path
.segments
[..*position
]
686 .map(|segment
| path_segment_to_string(segment
))
689 path
.segments
[*position
..]
691 .map(|segment
| path_segment_to_string(segment
))
696 Applicability
::MaybeIncorrect
,
706 /// Check if the source is call expression and the first argument is `self`. If true,
707 /// return the span of whole call and the span for all arguments expect the first one (`self`).
708 fn call_has_self_arg(&self, source
: PathSource
<'_
>) -> Option
<(Span
, Option
<Span
>)> {
709 let mut has_self_arg
= None
;
710 if let PathSource
::Expr(Some(parent
)) = source
{
712 ExprKind
::Call(_
, args
) if !args
.is_empty() => {
713 let mut expr_kind
= &args
[0].kind
;
716 ExprKind
::Path(_
, arg_name
) if arg_name
.segments
.len() == 1 => {
717 if arg_name
.segments
[0].ident
.name
== kw
::SelfLower
{
718 let call_span
= parent
.span
;
719 let tail_args_span
= if args
.len() > 1 {
722 args
.last().unwrap().span
.hi(),
728 has_self_arg
= Some((call_span
, tail_args_span
));
732 ExprKind
::AddrOf(_
, _
, expr
) => expr_kind
= &expr
.kind
,
743 fn followed_by_brace(&self, span
: Span
) -> (bool
, Option
<Span
>) {
744 // HACK(estebank): find a better way to figure out that this was a
745 // parser issue where a struct literal is being used on an expression
746 // where a brace being opened means a block is being started. Look
747 // ahead for the next text to see if `span` is followed by a `{`.
748 let sm
= self.r
.session
.source_map();
751 sp
= sm
.next_point(sp
);
752 match sm
.span_to_snippet(sp
) {
754 if snippet
.chars().any(|c
| !c
.is_whitespace()) {
761 let followed_by_brace
= matches
!(sm
.span_to_snippet(sp
), Ok(ref snippet
) if snippet
== "{");
762 // In case this could be a struct literal that needs to be surrounded
763 // by parentheses, find the appropriate span.
765 let mut closing_brace
= None
;
767 sp
= sm
.next_point(sp
);
768 match sm
.span_to_snippet(sp
) {
771 closing_brace
= Some(span
.to(sp
));
778 // The bigger the span, the more likely we're incorrect --
779 // bound it to 100 chars long.
784 (followed_by_brace
, closing_brace
)
787 /// Provides context-dependent help for errors reported by the `smart_resolve_path_fragment`
789 /// Returns `true` if able to provide context-dependent help.
790 fn smart_resolve_context_dependent_help(
792 err
: &mut DiagnosticBuilder
<'a
>,
794 source
: PathSource
<'_
>,
797 fallback_label
: &str,
799 let ns
= source
.namespace();
800 let is_expected
= &|res
| source
.is_expected(res
);
802 let path_sep
= |err
: &mut DiagnosticBuilder
<'_
>, expr
: &Expr
| match expr
.kind
{
803 ExprKind
::Field(_
, ident
) => {
806 "use the path separator to refer to an item",
807 format
!("{}::{}", path_str
, ident
),
808 Applicability
::MaybeIncorrect
,
812 ExprKind
::MethodCall(ref segment
, ..) => {
813 let span
= expr
.span
.with_hi(segment
.ident
.span
.hi());
816 "use the path separator to refer to an item",
817 format
!("{}::{}", path_str
, segment
.ident
),
818 Applicability
::MaybeIncorrect
,
825 let find_span
= |source
: &PathSource
<'_
>, err
: &mut DiagnosticBuilder
<'_
>| {
827 PathSource
::Expr(Some(Expr { span, kind: ExprKind::Call(_, _), .. }
))
828 | PathSource
::TupleStruct(span
, _
) => {
829 // We want the main underline to cover the suggested code as well for
838 let mut bad_struct_syntax_suggestion
= |def_id
: DefId
| {
839 let (followed_by_brace
, closing_brace
) = self.followed_by_brace(span
);
842 PathSource
::Expr(Some(
843 parent @ Expr { kind: ExprKind::Field(..) | ExprKind::MethodCall(..), .. }
,
844 )) if path_sep(err
, &parent
) => {}
850 | ExprKind
::Binary(..)
851 | ExprKind
::Unary(..)
853 | ExprKind
::While(..)
854 | ExprKind
::ForLoop(..)
855 | ExprKind
::Match(..),
858 ) if followed_by_brace
=> {
859 if let Some(sp
) = closing_brace
{
860 err
.span_label(span
, fallback_label
);
861 err
.multipart_suggestion(
862 "surround the struct literal with parentheses",
864 (sp
.shrink_to_lo(), "(".to_string()),
865 (sp
.shrink_to_hi(), ")".to_string()),
867 Applicability
::MaybeIncorrect
,
871 span
, // Note the parentheses surrounding the suggestion below
873 "you might want to surround a struct literal with parentheses: \
874 `({} {{ /* fields */ }})`?",
880 PathSource
::Expr(_
) | PathSource
::TupleStruct(..) | PathSource
::Pat
=> {
881 let span
= find_span(&source
, err
);
882 if let Some(span
) = self.def_span(def_id
) {
883 err
.span_label(span
, &format
!("`{}` defined here", path_str
));
885 let (tail
, descr
, applicability
) = match source
{
886 PathSource
::Pat
| PathSource
::TupleStruct(..) => {
887 ("", "pattern", Applicability
::MachineApplicable
)
889 _
=> (": val", "literal", Applicability
::HasPlaceholders
),
891 let (fields
, applicability
) = match self.r
.field_names
.get(&def_id
) {
895 .map(|f
| format
!("{}{}", f
.node
, tail
))
896 .collect
::<Vec
<String
>>()
900 None
=> ("/* fields */".to_string(), Applicability
::HasPlaceholders
),
902 let pad
= match self.r
.field_names
.get(&def_id
) {
903 Some(fields
) if fields
.is_empty() => "",
908 &format
!("use struct {} syntax instead", descr
),
909 format
!("{path_str} {{{pad}{fields}{pad}}}"),
914 err
.span_label(span
, fallback_label
);
919 match (res
, source
) {
920 (Res
::Def(DefKind
::Macro(MacroKind
::Bang
), _
), _
) => {
921 err
.span_label(span
, fallback_label
);
922 err
.span_suggestion_verbose(
924 "use `!` to invoke the macro",
926 Applicability
::MaybeIncorrect
,
928 if path_str
== "try" && span
.rust_2015() {
929 err
.note("if you want the `try` keyword, you need Rust 2018 or later");
932 (Res
::Def(DefKind
::TyAlias
, def_id
), PathSource
::Trait(_
)) => {
933 err
.span_label(span
, "type aliases cannot be used as traits");
934 if self.r
.session
.is_nightly_build() {
935 let msg
= "you might have meant to use `#![feature(trait_alias)]` instead of a \
937 if let Some(span
) = self.def_span(def_id
) {
938 if let Ok(snip
) = self.r
.session
.source_map().span_to_snippet(span
) {
939 // The span contains a type alias so we should be able to
940 // replace `type` with `trait`.
941 let snip
= snip
.replacen("type", "trait", 1);
942 err
.span_suggestion(span
, msg
, snip
, Applicability
::MaybeIncorrect
);
944 err
.span_help(span
, msg
);
951 (Res
::Def(DefKind
::Mod
, _
), PathSource
::Expr(Some(parent
))) => {
952 if !path_sep(err
, &parent
) {
957 Res
::Def(DefKind
::Enum
, def_id
),
958 PathSource
::TupleStruct(..) | PathSource
::Expr(..),
962 .current_type_ascription
968 .type_ascription_path_suggestions
975 // We already suggested changing `:` into `::` during parsing.
979 self.suggest_using_enum_variant(err
, source
, def_id
, span
);
981 (Res
::Def(DefKind
::Struct
, def_id
), _
) if ns
== ValueNS
=> {
982 let (ctor_def
, ctor_vis
, fields
) =
983 if let Some(struct_ctor
) = self.r
.struct_constructors
.get(&def_id
).cloned() {
986 bad_struct_syntax_suggestion(def_id
);
990 let is_accessible
= self.r
.is_accessible_from(ctor_vis
, self.parent_scope
.module
);
991 if !is_expected(ctor_def
) || is_accessible
{
995 let field_spans
= match source
{
996 // e.g. `if let Enum::TupleVariant(field1, field2) = _`
997 PathSource
::TupleStruct(_
, pattern_spans
) => {
998 err
.set_primary_message(
999 "cannot match against a tuple struct which contains private fields",
1002 // Use spans of the tuple struct pattern.
1003 Some(Vec
::from(pattern_spans
))
1005 // e.g. `let _ = Enum::TupleVariant(field1, field2);`
1006 _
if source
.is_call() => {
1007 err
.set_primary_message(
1008 "cannot initialize a tuple struct which contains private fields",
1011 // Use spans of the tuple struct definition.
1015 .map(|fields
| fields
.iter().map(|f
| f
.span
).collect
::<Vec
<_
>>())
1020 if let Some(spans
) =
1021 field_spans
.filter(|spans
| spans
.len() > 0 && fields
.len() == spans
.len())
1023 let non_visible_spans
: Vec
<Span
> = iter
::zip(&fields
, &spans
)
1024 .filter(|(vis
, _
)| {
1025 !self.r
.is_accessible_from(**vis
, self.parent_scope
.module
)
1027 .map(|(_
, span
)| *span
)
1030 if non_visible_spans
.len() > 0 {
1031 let mut m
: rustc_span
::MultiSpan
= non_visible_spans
.clone().into();
1034 .for_each(|s
| m
.push_span_label(s
, "private field".to_string()));
1035 err
.span_note(m
, "constructor is not visible here due to private fields");
1043 "constructor is not visible here due to private fields".to_string(),
1048 DefKind
::Union
| DefKind
::Variant
| DefKind
::Ctor(_
, CtorKind
::Fictive
),
1052 ) if ns
== ValueNS
=> {
1053 bad_struct_syntax_suggestion(def_id
);
1055 (Res
::Def(DefKind
::Ctor(_
, CtorKind
::Const
), def_id
), _
) if ns
== ValueNS
=> {
1057 PathSource
::Expr(_
) | PathSource
::TupleStruct(..) | PathSource
::Pat
=> {
1058 let span
= find_span(&source
, err
);
1059 if let Some(span
) = self.def_span(def_id
) {
1060 err
.span_label(span
, &format
!("`{}` defined here", path_str
));
1062 err
.span_suggestion(
1064 &format
!("use this syntax instead"),
1065 format
!("{path_str}"),
1066 Applicability
::MaybeIncorrect
,
1072 (Res
::Def(DefKind
::Ctor(_
, CtorKind
::Fn
), def_id
), _
) if ns
== ValueNS
=> {
1073 if let Some(span
) = self.def_span(def_id
) {
1074 err
.span_label(span
, &format
!("`{}` defined here", path_str
));
1076 let fields
= self.r
.field_names
.get(&def_id
).map_or_else(
1077 || "/* fields */".to_string(),
1078 |fields
| vec
!["_"; fields
.len()].join(", "),
1080 err
.span_suggestion(
1082 "use the tuple variant pattern syntax instead",
1083 format
!("{}({})", path_str
, fields
),
1084 Applicability
::HasPlaceholders
,
1087 (Res
::SelfTy(..), _
) if ns
== ValueNS
=> {
1088 err
.span_label(span
, fallback_label
);
1089 err
.note("can't use `Self` as a constructor, you must use the implemented struct");
1091 (Res
::Def(DefKind
::TyAlias
| DefKind
::AssocTy
, _
), _
) if ns
== ValueNS
=> {
1092 err
.note("can't use a type alias as a constructor");
1099 fn lookup_assoc_candidate
<FilterFn
>(
1103 filter_fn
: FilterFn
,
1104 ) -> Option
<AssocSuggestion
>
1106 FilterFn
: Fn(Res
) -> bool
,
1108 fn extract_node_id(t
: &Ty
) -> Option
<NodeId
> {
1110 TyKind
::Path(None
, _
) => Some(t
.id
),
1111 TyKind
::Rptr(_
, ref mut_ty
) => extract_node_id(&mut_ty
.ty
),
1112 // This doesn't handle the remaining `Ty` variants as they are not
1113 // that commonly the self_type, it might be interesting to provide
1114 // support for those in future.
1119 // Fields are generally expected in the same contexts as locals.
1120 if filter_fn(Res
::Local(ast
::DUMMY_NODE_ID
)) {
1121 if let Some(node_id
) =
1122 self.diagnostic_metadata
.current_self_type
.as_ref().and_then(extract_node_id
)
1124 // Look for a field with the same name in the current self_type.
1125 if let Some(resolution
) = self.r
.partial_res_map
.get(&node_id
) {
1126 match resolution
.base_res() {
1127 Res
::Def(DefKind
::Struct
| DefKind
::Union
, did
)
1128 if resolution
.unresolved_segments() == 0 =>
1130 if let Some(field_names
) = self.r
.field_names
.get(&did
) {
1133 .any(|&field_name
| ident
.name
== field_name
.node
)
1135 return Some(AssocSuggestion
::Field
);
1145 if let Some(items
) = self.diagnostic_metadata
.current_trait_assoc_items
{
1146 for assoc_item
in items
{
1147 if assoc_item
.ident
== ident
{
1148 return Some(match &assoc_item
.kind
{
1149 ast
::AssocItemKind
::Const(..) => AssocSuggestion
::AssocConst
,
1150 ast
::AssocItemKind
::Fn(box ast
::FnKind(_
, sig
, ..))
1151 if sig
.decl
.has_self() =>
1153 AssocSuggestion
::MethodWithSelf
1155 ast
::AssocItemKind
::Fn(..) => AssocSuggestion
::AssocFn
,
1156 ast
::AssocItemKind
::TyAlias(..) => AssocSuggestion
::AssocType
,
1157 ast
::AssocItemKind
::MacCall(_
) => continue,
1163 // Look for associated items in the current trait.
1164 if let Some((module
, _
)) = self.current_trait_ref
{
1165 if let Ok(binding
) = self.r
.resolve_ident_in_module(
1166 ModuleOrUniformRoot
::Module(module
),
1173 let res
= binding
.res();
1175 if self.r
.has_self
.contains(&res
.def_id()) {
1176 return Some(AssocSuggestion
::MethodWithSelf
);
1179 Res
::Def(DefKind
::AssocFn
, _
) => return Some(AssocSuggestion
::AssocFn
),
1180 Res
::Def(DefKind
::AssocConst
, _
) => {
1181 return Some(AssocSuggestion
::AssocConst
);
1183 Res
::Def(DefKind
::AssocTy
, _
) => {
1184 return Some(AssocSuggestion
::AssocType
);
1196 fn lookup_typo_candidate(
1200 filter_fn
: &impl Fn(Res
) -> bool
,
1202 ) -> Option
<TypoSuggestion
> {
1203 let mut names
= Vec
::new();
1204 if path
.len() == 1 {
1205 // Search in lexical scope.
1206 // Walk backwards up the ribs in scope and collect candidates.
1207 for rib
in self.ribs
[ns
].iter().rev() {
1208 // Locals and type parameters
1209 for (ident
, &res
) in &rib
.bindings
{
1211 names
.push(TypoSuggestion
::from_res(ident
.name
, res
));
1215 if let RibKind
::ModuleRibKind(module
) = rib
.kind
{
1216 // Items from this module
1217 self.r
.add_module_candidates(module
, &mut names
, &filter_fn
);
1219 if let ModuleKind
::Block(..) = module
.kind
{
1220 // We can see through blocks
1222 // Items from the prelude
1223 if !module
.no_implicit_prelude
{
1224 let extern_prelude
= self.r
.extern_prelude
.clone();
1225 names
.extend(extern_prelude
.iter().flat_map(|(ident
, _
)| {
1226 self.r
.crate_loader
.maybe_process_path_extern(ident
.name
).and_then(
1228 let crate_mod
= Res
::Def(
1230 DefId { krate: crate_id, index: CRATE_DEF_INDEX }
,
1233 if filter_fn(crate_mod
) {
1234 Some(TypoSuggestion
::from_res(ident
.name
, crate_mod
))
1242 if let Some(prelude
) = self.r
.prelude
{
1243 self.r
.add_module_candidates(prelude
, &mut names
, &filter_fn
);
1250 // Add primitive types to the mix
1251 if filter_fn(Res
::PrimTy(PrimTy
::Bool
)) {
1253 PrimTy
::ALL
.iter().map(|prim_ty
| {
1254 TypoSuggestion
::from_res(prim_ty
.name(), Res
::PrimTy(*prim_ty
))
1259 // Search in module.
1260 let mod_path
= &path
[..path
.len() - 1];
1261 if let PathResult
::Module(module
) =
1262 self.resolve_path(mod_path
, Some(TypeNS
), false, span
, CrateLint
::No
)
1264 if let ModuleOrUniformRoot
::Module(module
) = module
{
1265 self.r
.add_module_candidates(module
, &mut names
, &filter_fn
);
1270 let name
= path
[path
.len() - 1].ident
.name
;
1271 // Make sure error reporting is deterministic.
1272 names
.sort_by_cached_key(|suggestion
| suggestion
.candidate
.as_str());
1274 match find_best_match_for_name(
1275 &names
.iter().map(|suggestion
| suggestion
.candidate
).collect
::<Vec
<Symbol
>>(),
1279 Some(found
) if found
!= name
=> {
1280 names
.into_iter().find(|suggestion
| suggestion
.candidate
== found
)
1286 // Returns the name of the Rust type approximately corresponding to
1287 // a type name in another programming language.
1288 fn likely_rust_type(path
: &[Segment
]) -> Option
<Symbol
> {
1289 let name
= path
[path
.len() - 1].ident
.as_str();
1290 // Common Java types
1292 "byte" => sym
::u8, // In Java, bytes are signed, but in practice one almost always wants unsigned bytes.
1293 "short" => sym
::i16,
1294 "boolean" => sym
::bool
,
1297 "float" => sym
::f32,
1298 "double" => sym
::f64,
1303 /// Only used in a specific case of type ascription suggestions
1304 fn get_colon_suggestion_span(&self, start
: Span
) -> Span
{
1305 let sm
= self.r
.session
.source_map();
1306 start
.to(sm
.next_point(start
))
1309 fn type_ascription_suggestion(&self, err
: &mut DiagnosticBuilder
<'_
>, base_span
: Span
) -> bool
{
1310 let sm
= self.r
.session
.source_map();
1311 let base_snippet
= sm
.span_to_snippet(base_span
);
1312 if let Some(&sp
) = self.diagnostic_metadata
.current_type_ascription
.last() {
1313 if let Ok(snippet
) = sm
.span_to_snippet(sp
) {
1314 let len
= snippet
.trim_end().len() as u32;
1315 if snippet
.trim() == ":" {
1317 sp
.with_lo(sp
.lo() + BytePos(len
- 1)).with_hi(sp
.lo() + BytePos(len
));
1318 let mut show_label
= true;
1319 if sm
.is_multiline(sp
) {
1320 err
.span_suggestion_short(
1322 "maybe you meant to write `;` here",
1324 Applicability
::MaybeIncorrect
,
1327 let after_colon_sp
=
1328 self.get_colon_suggestion_span(colon_sp
.shrink_to_hi());
1329 if snippet
.len() == 1 {
1331 err
.span_suggestion(
1333 "maybe you meant to write a path separator here",
1335 Applicability
::MaybeIncorrect
,
1342 .type_ascription_path_suggestions
1349 if let Ok(base_snippet
) = base_snippet
{
1350 let mut sp
= after_colon_sp
;
1352 // Try to find an assignment
1353 sp
= sm
.next_point(sp
);
1354 let snippet
= sm
.span_to_snippet(sp
.to(sm
.next_point(sp
)));
1356 Ok(ref x
) if x
.as_str() == "=" => {
1357 err
.span_suggestion(
1359 "maybe you meant to write an assignment here",
1360 format
!("let {}", base_snippet
),
1361 Applicability
::MaybeIncorrect
,
1366 Ok(ref x
) if x
.as_str() == "\n" => break,
1376 "expecting a type here because of type ascription",
1386 fn find_module(&mut self, def_id
: DefId
) -> Option
<(Module
<'a
>, ImportSuggestion
)> {
1387 let mut result
= None
;
1388 let mut seen_modules
= FxHashSet
::default();
1389 let mut worklist
= vec
![(self.r
.graph_root
, Vec
::new())];
1391 while let Some((in_module
, path_segments
)) = worklist
.pop() {
1392 // abort if the module is already found
1393 if result
.is_some() {
1397 in_module
.for_each_child(self.r
, |_
, ident
, _
, name_binding
| {
1398 // abort if the module is already found or if name_binding is private external
1399 if result
.is_some() || !name_binding
.vis
.is_visible_locally() {
1402 if let Some(module
) = name_binding
.module() {
1404 let mut path_segments
= path_segments
.clone();
1405 path_segments
.push(ast
::PathSegment
::from_ident(ident
));
1406 let module_def_id
= module
.def_id().unwrap();
1407 if module_def_id
== def_id
{
1409 Path { span: name_binding.span, segments: path_segments, tokens: None }
;
1420 // add the module to the lookup
1421 if seen_modules
.insert(module_def_id
) {
1422 worklist
.push((module
, path_segments
));
1432 fn collect_enum_ctors(&mut self, def_id
: DefId
) -> Option
<Vec
<(Path
, DefId
, CtorKind
)>> {
1433 self.find_module(def_id
).map(|(enum_module
, enum_import_suggestion
)| {
1434 let mut variants
= Vec
::new();
1435 enum_module
.for_each_child(self.r
, |_
, ident
, _
, name_binding
| {
1436 if let Res
::Def(DefKind
::Ctor(CtorOf
::Variant
, kind
), def_id
) = name_binding
.res() {
1437 let mut segms
= enum_import_suggestion
.path
.segments
.clone();
1438 segms
.push(ast
::PathSegment
::from_ident(ident
));
1439 let path
= Path { span: name_binding.span, segments: segms, tokens: None }
;
1440 variants
.push((path
, def_id
, kind
));
1447 /// Adds a suggestion for using an enum's variant when an enum is used instead.
1448 fn suggest_using_enum_variant(
1450 err
: &mut DiagnosticBuilder
<'a
>,
1451 source
: PathSource
<'_
>,
1455 let variants
= match self.collect_enum_ctors(def_id
) {
1456 Some(variants
) => variants
,
1458 err
.note("you might have meant to use one of the enum's variants");
1463 let suggest_only_tuple_variants
=
1464 matches
!(source
, PathSource
::TupleStruct(..)) || source
.is_call();
1465 if suggest_only_tuple_variants
{
1466 // Suggest only tuple variants regardless of whether they have fields and do not
1467 // suggest path with added parenthesis.
1468 let mut suggestable_variants
= variants
1470 .filter(|(.., kind
)| *kind
== CtorKind
::Fn
)
1471 .map(|(variant
, ..)| path_names_to_string(variant
))
1472 .collect
::<Vec
<_
>>();
1474 let non_suggestable_variant_count
= variants
.len() - suggestable_variants
.len();
1476 let source_msg
= if source
.is_call() {
1478 } else if matches
!(source
, PathSource
::TupleStruct(..)) {
1484 if !suggestable_variants
.is_empty() {
1485 let msg
= if non_suggestable_variant_count
== 0 && suggestable_variants
.len() == 1 {
1486 format
!("try {} the enum's variant", source_msg
)
1488 format
!("try {} one of the enum's variants", source_msg
)
1491 err
.span_suggestions(
1494 suggestable_variants
.drain(..),
1495 Applicability
::MaybeIncorrect
,
1499 // If the enum has no tuple variants..
1500 if non_suggestable_variant_count
== variants
.len() {
1501 err
.help(&format
!("the enum has no tuple variants {}", source_msg
));
1504 // If there are also non-tuple variants..
1505 if non_suggestable_variant_count
== 1 {
1507 "you might have meant {} the enum's non-tuple variant",
1510 } else if non_suggestable_variant_count
>= 1 {
1512 "you might have meant {} one of the enum's non-tuple variants",
1517 let needs_placeholder
= |def_id
: DefId
, kind
: CtorKind
| {
1518 let has_no_fields
= self.r
.field_names
.get(&def_id
).map_or(false, |f
| f
.is_empty());
1520 CtorKind
::Const
=> false,
1521 CtorKind
::Fn
| CtorKind
::Fictive
if has_no_fields
=> false,
1526 let mut suggestable_variants
= variants
1528 .filter(|(_
, def_id
, kind
)| !needs_placeholder(*def_id
, *kind
))
1529 .map(|(variant
, _
, kind
)| (path_names_to_string(variant
), kind
))
1530 .map(|(variant
, kind
)| match kind
{
1531 CtorKind
::Const
=> variant
,
1532 CtorKind
::Fn
=> format
!("({}())", variant
),
1533 CtorKind
::Fictive
=> format
!("({} {{}})", variant
),
1535 .collect
::<Vec
<_
>>();
1537 if !suggestable_variants
.is_empty() {
1538 let msg
= if suggestable_variants
.len() == 1 {
1539 "you might have meant to use the following enum variant"
1541 "you might have meant to use one of the following enum variants"
1544 err
.span_suggestions(
1547 suggestable_variants
.drain(..),
1548 Applicability
::MaybeIncorrect
,
1552 let mut suggestable_variants_with_placeholders
= variants
1554 .filter(|(_
, def_id
, kind
)| needs_placeholder(*def_id
, *kind
))
1555 .map(|(variant
, _
, kind
)| (path_names_to_string(variant
), kind
))
1556 .filter_map(|(variant
, kind
)| match kind
{
1557 CtorKind
::Fn
=> Some(format
!("({}(/* fields */))", variant
)),
1558 CtorKind
::Fictive
=> Some(format
!("({} {{ /* fields */ }})", variant
)),
1561 .collect
::<Vec
<_
>>();
1563 if !suggestable_variants_with_placeholders
.is_empty() {
1565 suggestable_variants
.is_empty(),
1566 suggestable_variants_with_placeholders
.len(),
1568 (true, 1) => "the following enum variant is available",
1569 (true, _
) => "the following enum variants are available",
1570 (false, 1) => "alternatively, the following enum variant is available",
1571 (false, _
) => "alternatively, the following enum variants are also available",
1574 err
.span_suggestions(
1577 suggestable_variants_with_placeholders
.drain(..),
1578 Applicability
::HasPlaceholders
,
1583 if def_id
.is_local() {
1584 if let Some(span
) = self.def_span(def_id
) {
1585 err
.span_note(span
, "the enum is defined here");
1590 crate fn report_missing_type_error(
1593 ) -> Option
<(Span
, &'
static str, String
, Applicability
)> {
1594 let (ident
, span
) = match path
{
1595 [segment
] if !segment
.has_generic_args
=> {
1596 (segment
.ident
.to_string(), segment
.ident
.span
)
1600 let mut iter
= ident
.chars().map(|c
| c
.is_uppercase());
1601 let single_uppercase_char
=
1602 matches
!(iter
.next(), Some(true)) && matches
!(iter
.next(), None
);
1603 if !self.diagnostic_metadata
.currently_processing_generics
&& !single_uppercase_char
{
1606 match (self.diagnostic_metadata
.current_item
, single_uppercase_char
, self.diagnostic_metadata
.currently_processing_generics
) {
1607 (Some(Item { kind: ItemKind::Fn(..), ident, .. }
), _
, _
) if ident
.name
== sym
::main
=> {
1608 // Ignore `fn main()` as we don't want to suggest `fn main<T>()`
1613 kind @ ItemKind
::Fn(..)
1614 | kind @ ItemKind
::Enum(..)
1615 | kind @ ItemKind
::Struct(..)
1616 | kind @ ItemKind
::Union(..),
1621 // Without the 2nd `true`, we'd suggest `impl <T>` for `impl T` when a type `T` isn't found
1622 | (Some(Item { kind: kind @ ItemKind::Impl(..), .. }
), true, true)
1623 | (Some(Item { kind, .. }
), false, _
) => {
1624 // Likely missing type parameter.
1625 if let Some(generics
) = kind
.generics() {
1626 if span
.overlaps(generics
.span
) {
1627 // Avoid the following:
1628 // error[E0405]: cannot find trait `A` in this scope
1629 // --> $DIR/typo-suggestion-named-underscore.rs:CC:LL
1631 // L | fn foo<T: A>(x: T) {} // Shouldn't suggest underscore
1632 // | ^- help: you might be missing a type parameter: `, A`
1634 // | not found in this scope
1637 let msg
= "you might be missing a type parameter";
1638 let (span
, sugg
) = if let [.., param
] = &generics
.params
[..] {
1639 let span
= if let [.., bound
] = ¶m
.bounds
[..] {
1641 } else if let GenericParam
{
1642 kind
: GenericParamKind
::Const { ty, kw_span: _, default }
, ..
1644 default.as_ref().map(|def
| def
.value
.span
).unwrap_or(ty
.span
)
1648 (span
, format
!(", {}", ident
))
1650 (generics
.span
, format
!("<{}>", ident
))
1652 // Do not suggest if this is coming from macro expansion.
1653 if !span
.from_expansion() {
1655 span
.shrink_to_hi(),
1658 Applicability
::MaybeIncorrect
,
1668 /// Given the target `label`, search the `rib_index`th label rib for similarly named labels,
1669 /// optionally returning the closest match and whether it is reachable.
1670 crate fn suggestion_for_label_in_rib(
1674 ) -> Option
<LabelSuggestion
> {
1675 // Are ribs from this `rib_index` within scope?
1676 let within_scope
= self.is_label_valid_from_rib(rib_index
);
1678 let rib
= &self.label_ribs
[rib_index
];
1682 .filter(|(id
, _
)| id
.span
.ctxt() == label
.span
.ctxt())
1683 .map(|(id
, _
)| id
.name
)
1684 .collect
::<Vec
<Symbol
>>();
1686 find_best_match_for_name(&names
, label
.name
, None
).map(|symbol
| {
1687 // Upon finding a similar name, get the ident that it was from - the span
1688 // contained within helps make a useful diagnostic. In addition, determine
1689 // whether this candidate is within scope.
1690 let (ident
, _
) = rib
.bindings
.iter().find(|(ident
, _
)| ident
.name
== symbol
).unwrap();
1691 (*ident
, within_scope
)
1696 impl<'tcx
> LifetimeContext
<'_
, 'tcx
> {
1697 crate fn report_missing_lifetime_specifiers(
1701 ) -> DiagnosticBuilder
<'tcx
> {
1706 "missing lifetime specifier{}",
1711 crate fn emit_undeclared_lifetime_error(&self, lifetime_ref
: &hir
::Lifetime
) {
1712 let mut err
= struct_span_err
!(
1716 "use of undeclared lifetime name `{}`",
1719 err
.span_label(lifetime_ref
.span
, "undeclared lifetime");
1720 let mut suggests_in_band
= false;
1721 let mut suggest_note
= true;
1722 for missing
in &self.missing_named_lifetime_spots
{
1724 MissingLifetimeSpot
::Generics(generics
) => {
1725 let (span
, sugg
) = if let Some(param
) = generics
.params
.iter().find(|p
| {
1728 hir
::GenericParamKind
::Type
{
1729 synthetic
: Some(hir
::SyntheticTyParamKind
::ImplTrait
),
1731 } | hir
::GenericParamKind
::Lifetime
{
1732 kind
: hir
::LifetimeParamKind
::Elided
,
1736 (param
.span
.shrink_to_lo(), format
!("{}, ", lifetime_ref
))
1738 suggests_in_band
= true;
1739 (generics
.span
, format
!("<{}>", lifetime_ref
))
1741 if !span
.from_expansion() {
1742 err
.span_suggestion(
1744 &format
!("consider introducing lifetime `{}` here", lifetime_ref
),
1746 Applicability
::MaybeIncorrect
,
1748 } else if suggest_note
{
1749 suggest_note
= false; // Avoid displaying the same help multiple times.
1753 "lifetime `{}` is missing in item created through this procedural \
1760 MissingLifetimeSpot
::HigherRanked { span, span_type }
=> {
1761 err
.span_suggestion(
1764 "consider making the {} lifetime-generic with a new `{}` lifetime",
1768 span_type
.suggestion(&lifetime_ref
.to_string()),
1769 Applicability
::MaybeIncorrect
,
1772 "for more information on higher-ranked polymorphism, visit \
1773 https://doc.rust-lang.org/nomicon/hrtb.html",
1779 if self.tcx
.sess
.is_nightly_build()
1780 && !self.tcx
.features().in_band_lifetimes
1784 "if you want to experiment with in-band lifetime bindings, \
1785 add `#![feature(in_band_lifetimes)]` to the crate attributes",
1791 // FIXME(const_generics): This patches over a ICE caused by non-'static lifetimes in const
1792 // generics. We are disallowing this until we can decide on how we want to handle non-'static
1793 // lifetimes in const generics. See issue #74052 for discussion.
1794 crate fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref
: &hir
::Lifetime
) {
1795 let mut err
= struct_span_err
!(
1799 "use of non-static lifetime `{}` in const generic",
1803 "for more information, see issue #74052 \
1804 <https://github.com/rust-lang/rust/issues/74052>",
1809 crate fn is_trait_ref_fn_scope(&mut self, trait_ref
: &'tcx hir
::PolyTraitRef
<'tcx
>) -> bool
{
1810 if let def
::Res
::Def(_
, did
) = trait_ref
.trait_ref
.path
.res
{
1812 self.tcx
.lang_items().fn_once_trait(),
1813 self.tcx
.lang_items().fn_trait(),
1814 self.tcx
.lang_items().fn_mut_trait(),
1816 .contains(&Some(did
))
1818 let (span
, span_type
) = match &trait_ref
.bound_generic_params
{
1819 [] => (trait_ref
.span
.shrink_to_lo(), ForLifetimeSpanType
::BoundEmpty
),
1820 [.., bound
] => (bound
.span
.shrink_to_hi(), ForLifetimeSpanType
::BoundTail
),
1822 self.missing_named_lifetime_spots
1823 .push(MissingLifetimeSpot
::HigherRanked { span, span_type }
);
1830 crate fn add_missing_lifetime_specifiers_label(
1832 err
: &mut DiagnosticBuilder
<'_
>,
1833 mut spans_with_counts
: Vec
<(Span
, usize)>,
1834 lifetime_names
: &FxHashSet
<Symbol
>,
1835 lifetime_spans
: Vec
<Span
>,
1836 params
: &[ElisionFailureInfo
],
1838 let snippets
: Vec
<Option
<String
>> = spans_with_counts
1840 .map(|(span
, _
)| self.tcx
.sess
.source_map().span_to_snippet(*span
).ok())
1843 // Empty generics are marked with a span of "<", but since from now on
1844 // that information is in the snippets it can be removed from the spans.
1845 for ((span
, _
), snippet
) in spans_with_counts
.iter_mut().zip(&snippets
) {
1846 if snippet
.as_deref() == Some("<") {
1847 *span
= span
.shrink_to_hi();
1851 for &(span
, count
) in &spans_with_counts
{
1855 "expected {} lifetime parameter{}",
1856 if count
== 1 { "named".to_string() }
else { count.to_string() }
,
1862 let suggest_existing
=
1863 |err
: &mut DiagnosticBuilder
<'_
>,
1865 formatters
: Vec
<Option
<Box
<dyn Fn(&str) -> String
>>>| {
1866 if let Some(MissingLifetimeSpot
::HigherRanked { span: for_span, span_type }
) =
1867 self.missing_named_lifetime_spots
.iter().rev().next()
1869 // When we have `struct S<'a>(&'a dyn Fn(&X) -> &X);` we want to not only suggest
1870 // using `'a`, but also introduce the concept of HRLTs by suggesting
1871 // `struct S<'a>(&'a dyn for<'b> Fn(&X) -> &'b X);`. (#72404)
1872 let mut introduce_suggestion
= vec
![];
1874 let a_to_z_repeat_n
= |n
| {
1875 (b'a'
..=b'z'
).map(move |c
| {
1876 let mut s
= '
\''
.to_string();
1877 s
.extend(std
::iter
::repeat(char::from(c
)).take(n
));
1882 // If all single char lifetime names are present, we wrap around and double the chars.
1884 .flat_map(a_to_z_repeat_n
)
1885 .find(|lt
| !lifetime_names
.contains(&Symbol
::intern(<
)))
1888 "consider making the {} lifetime-generic with a new `{}` lifetime",
1893 "for more information on higher-ranked polymorphism, visit \
1894 https://doc.rust-lang.org/nomicon/hrtb.html",
1896 let for_sugg
= span_type
.suggestion(<_name
);
1897 for param
in params
{
1898 if let Ok(snippet
) = self.tcx
.sess
.source_map().span_to_snippet(param
.span
)
1900 if snippet
.starts_with('
&'
) && !snippet
.starts_with("&'") {
1901 introduce_suggestion
1902 .push((param
.span
, format
!("&{} {}", lt_name
, &snippet
[1..])));
1903 } else if let Some(stripped
) = snippet
.strip_prefix("&'_ ") {
1904 introduce_suggestion
1905 .push((param
.span
, format
!("&{} {}", lt_name
, stripped
)));
1909 introduce_suggestion
.push((*for_span
, for_sugg
));
1910 for ((span
, _
), formatter
) in spans_with_counts
.iter().zip(formatters
.iter()) {
1911 if let Some(formatter
) = formatter
{
1912 introduce_suggestion
.push((*span
, formatter(<_name
)));
1915 err
.multipart_suggestion_with_style(
1917 introduce_suggestion
,
1918 Applicability
::MaybeIncorrect
,
1919 SuggestionStyle
::ShowAlways
,
1923 let spans_suggs
: Vec
<_
> = formatters
1925 .zip(spans_with_counts
.iter())
1926 .filter_map(|(fmt
, (span
, _
))| {
1927 if let Some(formatter
) = fmt { Some((formatter, span)) }
else { None }
1929 .map(|(formatter
, span
)| (*span
, formatter(name
)))
1931 err
.multipart_suggestion_with_style(
1933 "consider using the `{}` lifetime",
1934 lifetime_names
.iter().next().unwrap()
1937 Applicability
::MaybeIncorrect
,
1938 SuggestionStyle
::ShowAlways
,
1941 let suggest_new
= |err
: &mut DiagnosticBuilder
<'_
>, suggs
: Vec
<Option
<String
>>| {
1942 for missing
in self.missing_named_lifetime_spots
.iter().rev() {
1943 let mut introduce_suggestion
= vec
![];
1946 introduce_suggestion
.push(match missing
{
1947 MissingLifetimeSpot
::Generics(generics
) => {
1948 if generics
.span
== DUMMY_SP
{
1949 // Account for malformed generics in the HIR. This shouldn't happen,
1950 // but if we make a mistake elsewhere, mainly by keeping something in
1951 // `missing_named_lifetime_spots` that we shouldn't, like associated
1952 // `const`s or making a mistake in the AST lowering we would provide
1953 // non-sensical suggestions. Guard against that by skipping these.
1957 msg
= "consider introducing a named lifetime parameter".to_string();
1958 should_break
= true;
1959 if let Some(param
) = generics
.params
.iter().find(|p
| {
1962 hir
::GenericParamKind
::Type
{
1963 synthetic
: Some(hir
::SyntheticTyParamKind
::ImplTrait
),
1965 } | hir
::GenericParamKind
::Lifetime
{
1966 kind
: hir
::LifetimeParamKind
::Elided
1970 (param
.span
.shrink_to_lo(), "'a, ".to_string())
1972 (generics
.span
, "<'a>".to_string())
1975 MissingLifetimeSpot
::HigherRanked { span, span_type }
=> {
1977 "consider making the {} lifetime-generic with a new `'a` lifetime",
1980 should_break
= false;
1982 "for more information on higher-ranked polymorphism, visit \
1983 https://doc.rust-lang.org/nomicon/hrtb.html",
1985 (*span
, span_type
.suggestion("'a"))
1987 MissingLifetimeSpot
::Static
=> {
1988 let mut spans_suggs
= Vec
::new();
1989 for ((span
, count
), snippet
) in
1990 spans_with_counts
.iter().copied().zip(snippets
.iter())
1992 let (span
, sugg
) = match snippet
.as_deref() {
1993 Some("&") => (span
.shrink_to_hi(), "'static ".to_owned()),
1994 Some("'_") => (span
, "'static".to_owned()),
1995 Some(snippet
) if !snippet
.ends_with('
>'
) => {
1999 std
::iter
::repeat("'static")
2001 .collect
::<Vec
<_
>>()
2004 } else if snippet
== "<" || snippet
== "(" {
2006 span
.shrink_to_hi(),
2007 std
::iter
::repeat("'static")
2009 .collect
::<Vec
<_
>>()
2014 span
.shrink_to_hi(),
2017 std
::iter
::repeat("'static")
2019 .collect
::<Vec
<_
>>()
2027 spans_suggs
.push((span
, sugg
.to_string()));
2029 err
.multipart_suggestion_with_style(
2030 "consider using the `'static` lifetime",
2032 Applicability
::MaybeIncorrect
,
2033 SuggestionStyle
::ShowAlways
,
2038 for param
in params
{
2039 if let Ok(snippet
) = self.tcx
.sess
.source_map().span_to_snippet(param
.span
) {
2040 if snippet
.starts_with('
&'
) && !snippet
.starts_with("&'") {
2041 introduce_suggestion
2042 .push((param
.span
, format
!("&'a {}", &snippet
[1..])));
2043 } else if let Some(stripped
) = snippet
.strip_prefix("&'_ ") {
2044 introduce_suggestion
.push((param
.span
, format
!("&'a {}", &stripped
)));
2048 for ((span
, _
), sugg
) in spans_with_counts
.iter().copied().zip(suggs
.iter()) {
2049 if let Some(sugg
) = sugg
{
2050 introduce_suggestion
.push((span
, sugg
.to_string()));
2053 err
.multipart_suggestion_with_style(
2055 introduce_suggestion
,
2056 Applicability
::MaybeIncorrect
,
2057 SuggestionStyle
::ShowAlways
,
2065 let lifetime_names
: Vec
<_
> = lifetime_names
.iter().collect();
2066 match &lifetime_names
[..] {
2068 let mut suggs
: Vec
<Option
<Box
<dyn Fn(&str) -> String
>>> = Vec
::new();
2069 for (snippet
, (_
, count
)) in snippets
.iter().zip(spans_with_counts
.iter().copied())
2071 suggs
.push(match snippet
.as_deref() {
2072 Some("&") => Some(Box
::new(|name
| format
!("&{} ", name
))),
2073 Some("'_") => Some(Box
::new(|n
| n
.to_string())),
2074 Some("") => Some(Box
::new(move |n
| format
!("{}, ", n
).repeat(count
))),
2075 Some("<") => Some(Box
::new(move |n
| {
2076 std
::iter
::repeat(n
).take(count
).collect
::<Vec
<_
>>().join(", ")
2078 Some(snippet
) if !snippet
.ends_with('
>'
) => Some(Box
::new(move |name
| {
2082 std
::iter
::repeat(name
.to_string())
2084 .collect
::<Vec
<_
>>()
2091 suggest_existing(err
, &name
.as_str()[..], suggs
);
2094 let mut suggs
= Vec
::new();
2095 for (snippet
, (_
, count
)) in
2096 snippets
.iter().cloned().zip(spans_with_counts
.iter().copied())
2098 suggs
.push(match snippet
.as_deref() {
2099 Some("&") => Some("&'a ".to_string()),
2100 Some("'_") => Some("'a".to_string()),
2102 Some(std
::iter
::repeat("'a, ").take(count
).collect
::<Vec
<_
>>().join(""))
2105 Some(std
::iter
::repeat("'a").take(count
).collect
::<Vec
<_
>>().join(", "))
2107 Some(snippet
) => Some(format
!(
2110 std
::iter
::repeat("'a").take(count
).collect
::<Vec
<_
>>().join(", "),
2115 suggest_new(err
, suggs
);
2117 lts
if lts
.len() > 1 => {
2118 err
.span_note(lifetime_spans
, "these named lifetimes are available to use");
2120 let mut spans_suggs
: Vec
<_
> = Vec
::new();
2121 for ((span
, _
), snippet
) in spans_with_counts
.iter().copied().zip(snippets
.iter()) {
2122 match snippet
.as_deref() {
2123 Some("") => spans_suggs
.push((span
, "'lifetime, ".to_string())),
2124 Some("&") => spans_suggs
.push((span
, "&'lifetime ".to_string())),
2129 if spans_suggs
.len() > 0 {
2130 // This happens when we have `Foo<T>` where we point at the space before `T`,
2131 // but this can be confusing so we give a suggestion with placeholders.
2132 err
.multipart_suggestion_with_style(
2133 "consider using one of the available lifetimes here",
2135 Applicability
::HasPlaceholders
,
2136 SuggestionStyle
::ShowAlways
,
2140 _
=> unreachable
!(),
2144 /// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
2145 /// This function will emit an error if `const_generics` is not enabled, the body identified by
2146 /// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
2147 crate fn maybe_emit_forbidden_non_static_lifetime_error(
2149 body_id
: hir
::BodyId
,
2150 lifetime_ref
: &'tcx hir
::Lifetime
,
2152 let is_anon_const
= matches
!(
2153 self.tcx
.def_kind(self.tcx
.hir().body_owner_def_id(body_id
)),
2154 hir
::def
::DefKind
::AnonConst
2156 let is_allowed_lifetime
= matches
!(
2158 hir
::LifetimeName
::Implicit
| hir
::LifetimeName
::Static
| hir
::LifetimeName
::Underscore
2161 if !self.tcx
.lazy_normalization() && is_anon_const
&& !is_allowed_lifetime
{
2163 &self.tcx
.sess
.parse_sess
,
2164 sym
::const_generics
,
2166 "a non-static lifetime is not allowed in a `const`",