4 use rustc_ast::visit::{self, Visitor};
5 use rustc_ast::{self as ast, Crate, ItemKind, ModKind, NodeId, Path, CRATE_NODE_ID};
6 use rustc_ast_pretty::pprust;
7 use rustc_data_structures::fx::FxHashSet;
8 use rustc_errors::struct_span_err;
9 use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
10 use rustc_feature::BUILTIN_ATTRIBUTES;
11 use rustc_hir::def::Namespace::{self, *};
12 use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
13 use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
14 use rustc_hir::PrimTy;
15 use rustc_index::vec::IndexVec;
16 use rustc_middle::bug;
17 use rustc_middle::ty::DefIdTree;
18 use rustc_session::lint::builtin::ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE;
19 use rustc_session::lint::builtin::MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS;
20 use rustc_session::lint::BuiltinLintDiagnostics;
21 use rustc_session::Session;
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::source_map::SourceMap;
26 use rustc_span::symbol::{kw, sym, Ident, Symbol};
27 use rustc_span::{BytePos, Span, SyntaxContext};
29 use crate::errors as errs;
30 use crate::imports::{Import, ImportKind, ImportResolver};
31 use crate::late::{PatternSource, Rib};
32 use crate::path_names_to_string;
33 use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize};
34 use crate::{HasGenericParams, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot};
35 use crate::{LexicalScopeBinding, NameBinding, NameBindingKind, PrivacyError, VisResolutionError};
36 use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet};
37 use crate::{Segment, UseError};
42 type Res = def::Res<ast::NodeId>;
44 /// A vector of spans and replacements, a message and applicability.
45 pub(crate) type Suggestion = (Vec<(Span, String)>, String, Applicability);
47 /// Potential candidate for an undeclared or out-of-scope label - contains the ident of a
48 /// similarly named label and whether or not it is reachable.
49 pub(crate) type LabelSuggestion = (Ident, bool);
52 pub(crate) enum SuggestionTarget {
53 /// The target has a similar name as the name used by the programmer (probably a typo)
55 /// The target is the only valid item that can be used in the corresponding context
60 pub(crate) struct TypoSuggestion {
61 pub candidate: Symbol,
62 /// The source location where the name is defined; None if the name is not defined
63 /// in source e.g. primitives
64 pub span: Option<Span>,
66 pub target: SuggestionTarget,
70 pub(crate) fn typo_from_ident(ident: Ident, res: Res) -> TypoSuggestion {
72 candidate: ident.name,
73 span: Some(ident.span),
75 target: SuggestionTarget::SimilarlyNamed,
78 pub(crate) fn typo_from_name(candidate: Symbol, res: Res) -> TypoSuggestion {
79 Self { candidate, span: None, res, target: SuggestionTarget::SimilarlyNamed }
81 pub(crate) fn single_item_from_ident(ident: Ident, res: Res) -> TypoSuggestion {
83 candidate: ident.name,
84 span: Some(ident.span),
86 target: SuggestionTarget::SingleItem,
91 /// A free importable items suggested in case of resolution failure.
92 #[derive(Debug, Clone)]
93 pub(crate) struct ImportSuggestion {
94 pub did: Option<DefId>,
95 pub descr: &'static str,
98 /// An extra note that should be issued if this item is suggested
99 pub note: Option<String>,
102 /// Adjust the impl span so that just the `impl` keyword is taken by removing
103 /// everything after `<` (`"impl<T> Iterator for A<T> {}" -> "impl"`) and
104 /// everything after the first whitespace (`"impl Iterator for A" -> "impl"`).
106 /// *Attention*: the method used is very fragile since it essentially duplicates the work of the
107 /// parser. If you need to use this function or something similar, please consider updating the
108 /// `source_map` functions and this function to something more robust.
109 fn reduce_impl_span_to_impl_keyword(sm: &SourceMap, impl_span: Span) -> Span {
110 let impl_span = sm.span_until_char(impl_span, '<');
111 sm.span_until_whitespace(impl_span)
114 impl<'a> Resolver<'a> {
115 pub(crate) fn report_errors(&mut self, krate: &Crate) {
116 self.report_with_use_injections(krate);
118 for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
119 let msg = "macro-expanded `macro_export` macros from the current crate \
120 cannot be referred to by absolute paths";
121 self.lint_buffer.buffer_lint_with_diagnostic(
122 MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
126 BuiltinLintDiagnostics::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def),
130 for ambiguity_error in &self.ambiguity_errors {
131 self.report_ambiguity_error(ambiguity_error);
134 let mut reported_spans = FxHashSet::default();
135 for error in &self.privacy_errors {
136 if reported_spans.insert(error.dedup_span) {
137 self.report_privacy_error(error);
142 fn report_with_use_injections(&mut self, krate: &Crate) {
143 for UseError { mut err, candidates, def_id, instead, suggestion, path, is_call } in
144 self.use_injections.drain(..)
146 let (span, found_use) = if let Some(def_id) = def_id.as_local() {
147 UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id])
152 if !candidates.is_empty() {
159 if instead { Instead::Yes } else { Instead::No },
161 DiagnosticMode::Normal,
165 } else if let Some((span, msg, sugg, appl)) = suggestion {
166 err.span_suggestion(span, msg, sugg, appl);
168 } else if let [segment] = path.as_slice() && is_call {
169 err.stash(segment.ident.span, rustc_errors::StashKey::CallIntoMethod);
176 pub(crate) fn report_conflict<'b>(
181 new_binding: &NameBinding<'b>,
182 old_binding: &NameBinding<'b>,
184 // Error on the second of two conflicting names
185 if old_binding.span.lo() > new_binding.span.lo() {
186 return self.report_conflict(parent, ident, ns, old_binding, new_binding);
189 let container = match parent.kind {
190 ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id()),
191 ModuleKind::Block => "block",
194 let old_noun = match old_binding.is_import_user_facing() {
196 false => "definition",
199 let new_participle = match new_binding.is_import_user_facing() {
205 (ident.name, self.session.source_map().guess_head_span(new_binding.span));
207 if let Some(s) = self.name_already_seen.get(&name) {
213 let old_kind = match (ns, old_binding.module()) {
214 (ValueNS, _) => "value",
215 (MacroNS, _) => "macro",
216 (TypeNS, _) if old_binding.is_extern_crate() => "extern crate",
217 (TypeNS, Some(module)) if module.is_normal() => "module",
218 (TypeNS, Some(module)) if module.is_trait() => "trait",
219 (TypeNS, _) => "type",
222 let msg = format!("the name `{}` is defined multiple times", name);
224 let mut err = match (old_binding.is_extern_crate(), new_binding.is_extern_crate()) {
225 (true, true) => struct_span_err!(self.session, span, E0259, "{}", msg),
226 (true, _) | (_, true) => match new_binding.is_import() && old_binding.is_import() {
227 true => struct_span_err!(self.session, span, E0254, "{}", msg),
228 false => struct_span_err!(self.session, span, E0260, "{}", msg),
230 _ => match (old_binding.is_import_user_facing(), new_binding.is_import_user_facing()) {
231 (false, false) => struct_span_err!(self.session, span, E0428, "{}", msg),
232 (true, true) => struct_span_err!(self.session, span, E0252, "{}", msg),
233 _ => struct_span_err!(self.session, span, E0255, "{}", msg),
238 "`{}` must be defined only once in the {} namespace of this {}",
244 err.span_label(span, format!("`{}` re{} here", name, new_participle));
245 if !old_binding.span.is_dummy() && old_binding.span != span {
247 self.session.source_map().guess_head_span(old_binding.span),
248 format!("previous {} of the {} `{}` here", old_noun, old_kind, name),
252 // See https://github.com/rust-lang/rust/issues/32354
253 use NameBindingKind::Import;
254 let can_suggest = |binding: &NameBinding<'_>, import: &self::Import<'_>| {
255 !binding.span.is_dummy()
256 && !matches!(import.kind, ImportKind::MacroUse | ImportKind::MacroExport)
258 let import = match (&new_binding.kind, &old_binding.kind) {
259 // If there are two imports where one or both have attributes then prefer removing the
260 // import without attributes.
261 (Import { import: new, .. }, Import { import: old, .. })
263 (new.has_attributes || old.has_attributes)
264 && can_suggest(old_binding, old)
265 && can_suggest(new_binding, new)
268 if old.has_attributes {
269 Some((new, new_binding.span, true))
271 Some((old, old_binding.span, true))
274 // Otherwise prioritize the new binding.
275 (Import { import, .. }, other) if can_suggest(new_binding, import) => {
276 Some((import, new_binding.span, other.is_import()))
278 (other, Import { import, .. }) if can_suggest(old_binding, import) => {
279 Some((import, old_binding.span, other.is_import()))
284 // Check if the target of the use for both bindings is the same.
285 let duplicate = new_binding.res().opt_def_id() == old_binding.res().opt_def_id();
286 let has_dummy_span = new_binding.span.is_dummy() || old_binding.span.is_dummy();
288 self.extern_prelude.get(&ident).map_or(true, |entry| entry.introduced_by_item);
289 // Only suggest removing an import if both bindings are to the same def, if both spans
290 // aren't dummy spans. Further, if both bindings are imports, then the ident must have
291 // been introduced by an item.
292 let should_remove_import = duplicate
294 && ((new_binding.is_extern_crate() || old_binding.is_extern_crate()) || from_item);
297 Some((import, span, true)) if should_remove_import && import.is_nested() => {
298 self.add_suggestion_for_duplicate_nested_use(&mut err, import, span)
300 Some((import, _, true)) if should_remove_import && !import.is_glob() => {
301 // Simple case - remove the entire import. Due to the above match arm, this can
302 // only be a single use so just remove it entirely.
303 err.tool_only_span_suggestion(
304 import.use_span_with_attributes,
305 "remove unnecessary import",
307 Applicability::MaybeIncorrect,
310 Some((import, span, _)) => {
311 self.add_suggestion_for_rename_of_use(&mut err, name, import, span)
317 self.name_already_seen.insert(name, span);
320 /// This function adds a suggestion to change the binding name of a new import that conflicts
321 /// with an existing import.
323 /// ```text,ignore (diagnostic)
324 /// help: you can use `as` to change the binding name of the import
326 /// LL | use foo::bar as other_bar;
327 /// | ^^^^^^^^^^^^^^^^^^^^^
329 fn add_suggestion_for_rename_of_use(
331 err: &mut Diagnostic,
336 let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
337 format!("Other{}", name)
339 format!("other_{}", name)
342 let mut suggestion = None;
344 ImportKind::Single { type_ns_only: true, .. } => {
345 suggestion = Some(format!("self as {}", suggested_name))
347 ImportKind::Single { source, .. } => {
349 source.span.hi().0.checked_sub(binding_span.lo().0).map(|pos| pos as usize)
351 if let Ok(snippet) = self.session.source_map().span_to_snippet(binding_span) {
352 if pos <= snippet.len() {
353 suggestion = Some(format!(
357 if snippet.ends_with(';') { ";" } else { "" }
363 ImportKind::ExternCrate { source, target, .. } => {
364 suggestion = Some(format!(
365 "extern crate {} as {};",
366 source.unwrap_or(target.name),
373 let rename_msg = "you can use `as` to change the binding name of the import";
374 if let Some(suggestion) = suggestion {
379 Applicability::MaybeIncorrect,
382 err.span_label(binding_span, rename_msg);
386 /// This function adds a suggestion to remove an unnecessary binding from an import that is
387 /// nested. In the following example, this function will be invoked to remove the `a` binding
388 /// in the second use statement:
390 /// ```ignore (diagnostic)
391 /// use issue_52891::a;
392 /// use issue_52891::{d, a, e};
395 /// The following suggestion will be added:
397 /// ```ignore (diagnostic)
398 /// use issue_52891::{d, a, e};
399 /// ^-- help: remove unnecessary import
402 /// If the nested use contains only one import then the suggestion will remove the entire
405 /// It is expected that the provided import is nested - this isn't checked by the
406 /// function. If this invariant is not upheld, this function's behaviour will be unexpected
407 /// as characters expected by span manipulations won't be present.
408 fn add_suggestion_for_duplicate_nested_use(
410 err: &mut Diagnostic,
414 assert!(import.is_nested());
415 let message = "remove unnecessary import";
417 // Two examples will be used to illustrate the span manipulations we're doing:
419 // - Given `use issue_52891::{d, a, e};` where `a` is a duplicate then `binding_span` is
420 // `a` and `import.use_span` is `issue_52891::{d, a, e};`.
421 // - Given `use issue_52891::{d, e, a};` where `a` is a duplicate then `binding_span` is
422 // `a` and `import.use_span` is `issue_52891::{d, e, a};`.
424 let (found_closing_brace, span) =
425 find_span_of_binding_until_next_binding(self.session, binding_span, import.use_span);
427 // If there was a closing brace then identify the span to remove any trailing commas from
429 if found_closing_brace {
430 if let Some(span) = extend_span_to_previous_binding(self.session, span) {
431 err.tool_only_span_suggestion(span, message, "", Applicability::MaybeIncorrect);
433 // Remove the entire line if we cannot extend the span back, this indicates an
434 // `issue_52891::{self}` case.
436 import.use_span_with_attributes,
439 Applicability::MaybeIncorrect,
446 err.span_suggestion(span, message, "", Applicability::MachineApplicable);
449 pub(crate) fn lint_if_path_starts_with_module(
451 finalize: Option<Finalize>,
453 second_binding: Option<&NameBinding<'_>>,
455 let Some(Finalize { node_id, root_span, .. }) = finalize else {
459 let first_name = match path.get(0) {
460 // In the 2018 edition this lint is a hard error, so nothing to do
461 Some(seg) if seg.ident.span.rust_2015() && self.session.rust_2015() => seg.ident.name,
465 // We're only interested in `use` paths which should start with
466 // `{{root}}` currently.
467 if first_name != kw::PathRoot {
472 // If this import looks like `crate::...` it's already good
473 Some(Segment { ident, .. }) if ident.name == kw::Crate => return,
474 // Otherwise go below to see if it's an extern crate
476 // If the path has length one (and it's `PathRoot` most likely)
477 // then we don't know whether we're gonna be importing a crate or an
478 // item in our crate. Defer this lint to elsewhere
482 // If the first element of our path was actually resolved to an
483 // `ExternCrate` (also used for `crate::...`) then no need to issue a
484 // warning, this looks all good!
485 if let Some(binding) = second_binding {
486 if let NameBindingKind::Import { import, .. } = binding.kind {
487 // Careful: we still want to rewrite paths from renamed extern crates.
488 if let ImportKind::ExternCrate { source: None, .. } = import.kind {
494 let diag = BuiltinLintDiagnostics::AbsPathWithModule(root_span);
495 self.lint_buffer.buffer_lint_with_diagnostic(
496 ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
499 "absolute paths must start with `self`, `super`, \
500 `crate`, or an external crate name in the 2018 edition",
505 pub(crate) fn add_module_candidates(
508 names: &mut Vec<TypoSuggestion>,
509 filter_fn: &impl Fn(Res) -> bool,
510 ctxt: Option<SyntaxContext>,
512 for (key, resolution) in self.resolutions(module).borrow().iter() {
513 if let Some(binding) = resolution.borrow().binding {
514 let res = binding.res();
515 if filter_fn(res) && ctxt.map_or(true, |ctxt| ctxt == key.ident.span.ctxt()) {
516 names.push(TypoSuggestion::typo_from_ident(key.ident, res));
522 /// Combines an error with provided span and emits it.
524 /// This takes the error provided, combines it with the span and any additional spans inside the
525 /// error and emits it.
526 pub(crate) fn report_error(&mut self, span: Span, resolution_error: ResolutionError<'a>) {
527 self.into_struct_error(span, resolution_error).emit();
530 pub(crate) fn into_struct_error(
533 resolution_error: ResolutionError<'a>,
534 ) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
535 match resolution_error {
536 ResolutionError::GenericParamsFromOuterFunction(outer_res, has_generic_params) => {
537 let mut err = struct_span_err!(
541 "can't use generic parameters from outer function",
543 err.span_label(span, "use of generic parameter from outer function");
545 let sm = self.session.source_map();
546 let def_id = match outer_res {
547 Res::SelfTyParam { .. } => {
548 err.span_label(span, "can't use `Self` here");
551 Res::SelfTyAlias { alias_to: def_id, .. } => {
552 if let Some(impl_span) = self.opt_span(def_id) {
554 reduce_impl_span_to_impl_keyword(sm, impl_span),
555 "`Self` type implicitly declared here, by this `impl`",
558 err.span_label(span, "use a type here instead");
561 Res::Def(DefKind::TyParam, def_id) => {
562 if let Some(span) = self.opt_span(def_id) {
563 err.span_label(span, "type parameter from outer function");
567 Res::Def(DefKind::ConstParam, def_id) => {
568 if let Some(span) = self.opt_span(def_id) {
569 err.span_label(span, "const parameter from outer function");
575 "GenericParamsFromOuterFunction should only be used with \
576 Res::SelfTyParam, Res::SelfTyAlias, DefKind::TyParam or \
582 if let HasGenericParams::Yes(span) = has_generic_params {
583 // Try to retrieve the span of the function signature and generate a new
584 // message with a local type or const parameter.
585 let sugg_msg = "try using a local generic parameter instead";
586 let name = self.opt_name(def_id).unwrap_or(sym::T);
587 let (span, snippet) = if span.is_empty() {
588 let snippet = format!("<{}>", name);
591 let span = sm.span_through_char(span, '<').shrink_to_hi();
592 let snippet = format!("{}, ", name);
595 // Suggest the modification to the user
596 err.span_suggestion(span, sugg_msg, snippet, Applicability::MaybeIncorrect);
601 ResolutionError::NameAlreadyUsedInParameterList(name, first_use_span) => self
603 .create_err(errs::NameAlreadyUsedInParameterList { span, first_use_span, name }),
604 ResolutionError::MethodNotMemberOfTrait(method, trait_, candidate) => {
605 self.session.create_err(errs::MethodNotMemberOfTrait {
609 sub: candidate.map(|c| errs::AssociatedFnWithSimilarNameExists {
615 ResolutionError::TypeNotMemberOfTrait(type_, trait_, candidate) => {
616 self.session.create_err(errs::TypeNotMemberOfTrait {
620 sub: candidate.map(|c| errs::AssociatedTypeWithSimilarNameExists {
626 ResolutionError::ConstNotMemberOfTrait(const_, trait_, candidate) => {
627 self.session.create_err(errs::ConstNotMemberOfTrait {
631 sub: candidate.map(|c| errs::AssociatedConstWithSimilarNameExists {
637 ResolutionError::VariableNotBoundInPattern(binding_error, parent_scope) => {
638 let BindingError { name, target, origin, could_be_path } = binding_error;
640 let target_sp = target.iter().copied().collect::<Vec<_>>();
641 let origin_sp = origin.iter().copied().collect::<Vec<_>>();
643 let msp = MultiSpan::from_spans(target_sp.clone());
644 let mut err = struct_span_err!(
648 "variable `{}` is not bound in all patterns",
651 for sp in target_sp {
652 err.span_label(sp, format!("pattern doesn't bind `{}`", name));
654 for sp in origin_sp {
655 err.span_label(sp, "variable not in all patterns");
658 let import_suggestions = self.lookup_import_candidates(
659 Ident::with_dummy_span(name),
662 &|res: Res| match res {
664 DefKind::Ctor(CtorOf::Variant, CtorKind::Const)
665 | DefKind::Ctor(CtorOf::Struct, CtorKind::Const)
667 | DefKind::AssocConst,
674 if import_suggestions.is_empty() {
675 let help_msg = format!(
676 "if you meant to match on a variant or a `const` item, consider \
677 making the path in the pattern qualified: `path::to::ModOrType::{}`",
680 err.span_help(span, &help_msg);
690 DiagnosticMode::Pattern,
696 ResolutionError::VariableBoundWithDifferentMode(variable_name, first_binding_span) => {
697 self.session.create_err(errs::VariableBoundWithDifferentMode {
703 ResolutionError::IdentifierBoundMoreThanOnceInParameterList(identifier) => self
705 .create_err(errs::IdentifierBoundMoreThanOnceInParameterList { span, identifier }),
706 ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(identifier) => self
708 .create_err(errs::IdentifierBoundMoreThanOnceInSamePattern { span, identifier }),
709 ResolutionError::UndeclaredLabel { name, suggestion } => {
710 let ((sub_reachable, sub_reachable_suggestion), sub_unreachable) = match suggestion
712 // A reachable label with a similar name exists.
713 Some((ident, true)) => (
715 Some(errs::LabelWithSimilarNameReachable(ident.span)),
716 Some(errs::TryUsingSimilarlyNamedLabel {
718 ident_name: ident.name,
723 // An unreachable label with a similar name exists.
724 Some((ident, false)) => (
726 Some(errs::UnreachableLabelWithSimilarNameExists {
727 ident_span: ident.span,
730 // No similarly-named labels exist.
731 None => ((None, None), None),
733 self.session.create_err(errs::UndeclaredLabel {
737 sub_reachable_suggestion,
741 ResolutionError::SelfImportsOnlyAllowedWithin { root, span_with_rename } => {
742 // None of the suggestions below would help with a case like `use self`.
743 let (suggestion, mpart_suggestion) = if root {
746 // use foo::bar::self -> foo::bar
747 // use foo::bar::self as abc -> foo::bar as abc
748 let suggestion = errs::SelfImportsOnlyAllowedWithinSuggestion { span };
750 // use foo::bar::self -> foo::bar::{self}
751 // use foo::bar::self as abc -> foo::bar::{self as abc}
752 let mpart_suggestion = errs::SelfImportsOnlyAllowedWithinMultipartSuggestion {
753 multipart_start: span_with_rename.shrink_to_lo(),
754 multipart_end: span_with_rename.shrink_to_hi(),
756 (Some(suggestion), Some(mpart_suggestion))
758 self.session.create_err(errs::SelfImportsOnlyAllowedWithin {
764 ResolutionError::SelfImportCanOnlyAppearOnceInTheList => {
765 self.session.create_err(errs::SelfImportCanOnlyAppearOnceInTheList { span })
767 ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
768 self.session.create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
770 ResolutionError::FailedToResolve { label, suggestion } => {
772 struct_span_err!(self.session, span, E0433, "failed to resolve: {}", &label);
773 err.span_label(span, label);
775 if let Some((suggestions, msg, applicability)) = suggestion {
776 if suggestions.is_empty() {
780 err.multipart_suggestion(&msg, suggestions, applicability);
785 ResolutionError::CannotCaptureDynamicEnvironmentInFnItem => {
786 self.session.create_err(errs::CannotCaptureDynamicEnvironmentInFnItem { span })
788 ResolutionError::AttemptToUseNonConstantValueInConstant(ident, suggestion, current) => {
790 // ^^^ given this Span
791 // ------- get this Span to have an applicable suggestion
794 // only do this if the const and usage of the non-constant value are on the same line
795 // the further the two are apart, the higher the chance of the suggestion being wrong
800 .span_extend_to_prev_str(ident.span, current, true, false);
802 let ((with, with_label), without) = match sp {
803 Some(sp) if !self.session.source_map().is_multiline(sp) => {
804 let sp = sp.with_lo(BytePos(sp.lo().0 - (current.len() as u32)));
806 (Some(errs::AttemptToUseNonConstantValueInConstantWithSuggestion {
811 }), Some(errs::AttemptToUseNonConstantValueInConstantLabelWithSuggestion {span})),
817 Some(errs::AttemptToUseNonConstantValueInConstantWithoutSuggestion {
818 ident_span: ident.span,
824 self.session.create_err(errs::AttemptToUseNonConstantValueInConstant {
831 ResolutionError::BindingShadowsSomethingUnacceptable {
837 shadowed_binding_span,
838 } => self.session.create_err(errs::BindingShadowsSomethingUnacceptable {
843 sub_suggestion: match (shadowing_binding, shadowed_binding) {
845 PatternSource::Match,
846 Res::Def(DefKind::Ctor(CtorOf::Variant | CtorOf::Struct, CtorKind::Fn), _),
847 ) => Some(errs::BindingShadowsSomethingUnacceptableSuggestion { span, name }),
850 shadowed_binding_span,
854 ResolutionError::ForwardDeclaredGenericParam => {
855 self.session.create_err(errs::ForwardDeclaredGenericParam { span })
857 ResolutionError::ParamInTyOfConstParam(name) => {
858 self.session.create_err(errs::ParamInTyOfConstParam { span, name })
860 ResolutionError::ParamInNonTrivialAnonConst { name, is_type } => {
861 self.session.create_err(errs::ParamInNonTrivialAnonConst {
864 sub_is_type: if is_type {
865 errs::ParamInNonTrivialAnonConstIsType::AType
867 errs::ParamInNonTrivialAnonConstIsType::NotAType { name }
872 .then_some(errs::ParamInNonTrivialAnonConstHelp),
875 ResolutionError::SelfInGenericParamDefault => {
876 self.session.create_err(errs::SelfInGenericParamDefault { span })
878 ResolutionError::UnreachableLabel { name, definition_span, suggestion } => {
879 let ((sub_suggestion_label, sub_suggestion), sub_unreachable_label) =
881 // A reachable label with a similar name exists.
882 Some((ident, true)) => (
884 Some(errs::UnreachableLabelSubLabel { ident_span: ident.span }),
885 Some(errs::UnreachableLabelSubSuggestion {
887 // intentionally taking 'ident.name' instead of 'ident' itself, as this
888 // could be used in suggestion context
889 ident_name: ident.name,
894 // An unreachable label with a similar name exists.
895 Some((ident, false)) => (
897 Some(errs::UnreachableLabelSubLabelUnreachable {
898 ident_span: ident.span,
901 // No similarly-named labels exist.
902 None => ((None, None), None),
904 self.session.create_err(errs::UnreachableLabel {
909 sub_suggestion_label,
910 sub_unreachable_label,
913 ResolutionError::TraitImplMismatch {
920 let mut err = self.session.struct_span_err_with_code(
923 "item `{}` is an associated {}, which doesn't match its trait `{}`",
924 name, kind, trait_path,
928 err.span_label(span, "does not match trait");
929 err.span_label(trait_item_span, "item in trait");
932 ResolutionError::TraitImplDuplicate { name, trait_item_span, old_span } => self
934 .create_err(errs::TraitImplDuplicate { span, name, trait_item_span, old_span }),
935 ResolutionError::InvalidAsmSym => self.session.create_err(errs::InvalidAsmSym { span }),
939 pub(crate) fn report_vis_error(
941 vis_resolution_error: VisResolutionError<'_>,
942 ) -> ErrorGuaranteed {
943 match vis_resolution_error {
944 VisResolutionError::Relative2018(span, path) => {
945 self.session.create_err(errs::Relative2018 {
947 path_span: path.span,
948 // intentionally converting to String, as the text would also be used as
949 // in suggestion context
950 path_str: pprust::path_to_string(&path),
953 VisResolutionError::AncestorOnly(span) => {
954 self.session.create_err(errs::AncestorOnly(span))
956 VisResolutionError::FailedToResolve(span, label, suggestion) => {
957 self.into_struct_error(span, ResolutionError::FailedToResolve { label, suggestion })
959 VisResolutionError::ExpectedFound(span, path_str, res) => {
960 self.session.create_err(errs::ExpectedFound { span, res, path_str })
962 VisResolutionError::Indeterminate(span) => {
963 self.session.create_err(errs::Indeterminate(span))
965 VisResolutionError::ModuleOnly(span) => self.session.create_err(errs::ModuleOnly(span)),
970 /// Lookup typo candidate in scope for a macro or import.
971 fn early_lookup_typo_candidate(
973 scope_set: ScopeSet<'a>,
974 parent_scope: &ParentScope<'a>,
976 filter_fn: &impl Fn(Res) -> bool,
977 ) -> Option<TypoSuggestion> {
978 let mut suggestions = Vec::new();
979 let ctxt = ident.span.ctxt();
980 self.visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
982 Scope::DeriveHelpers(expn_id) => {
983 let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
990 .map(|ident| TypoSuggestion::typo_from_ident(*ident, res)),
994 Scope::DeriveHelpersCompat => {
995 let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
997 for derive in parent_scope.derives {
998 let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
999 if let Ok((Some(ext), _)) = this.resolve_macro_path(
1001 Some(MacroKind::Derive),
1009 .map(|name| TypoSuggestion::typo_from_name(*name, res)),
1015 Scope::MacroRules(macro_rules_scope) => {
1016 if let MacroRulesScope::Binding(macro_rules_binding) = macro_rules_scope.get() {
1017 let res = macro_rules_binding.binding.res();
1019 suggestions.push(TypoSuggestion::typo_from_ident(
1020 macro_rules_binding.ident,
1026 Scope::CrateRoot => {
1027 let root_ident = Ident::new(kw::PathRoot, ident.span);
1028 let root_module = this.resolve_crate_root(root_ident);
1029 this.add_module_candidates(root_module, &mut suggestions, filter_fn, None);
1031 Scope::Module(module) => {
1032 this.add_module_candidates(module, &mut suggestions, filter_fn, None);
1034 Scope::MacroUsePrelude => {
1035 suggestions.extend(this.macro_use_prelude.iter().filter_map(
1037 let res = binding.res();
1038 filter_fn(res).then_some(TypoSuggestion::typo_from_name(*name, res))
1042 Scope::BuiltinAttrs => {
1043 let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(kw::Empty));
1048 .map(|attr| TypoSuggestion::typo_from_name(attr.name, res)),
1052 Scope::ExternPrelude => {
1053 suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
1054 let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
1055 filter_fn(res).then_some(TypoSuggestion::typo_from_ident(*ident, res))
1058 Scope::ToolPrelude => {
1059 let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1061 this.registered_tools
1063 .map(|ident| TypoSuggestion::typo_from_ident(*ident, res)),
1066 Scope::StdLibPrelude => {
1067 if let Some(prelude) = this.prelude {
1068 let mut tmp_suggestions = Vec::new();
1069 this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn, None);
1073 .filter(|s| use_prelude || this.is_builtin_macro(s.res)),
1077 Scope::BuiltinTypes => {
1078 suggestions.extend(PrimTy::ALL.iter().filter_map(|prim_ty| {
1079 let res = Res::PrimTy(*prim_ty);
1081 .then_some(TypoSuggestion::typo_from_name(prim_ty.name(), res))
1089 // Make sure error reporting is deterministic.
1090 suggestions.sort_by(|a, b| a.candidate.as_str().partial_cmp(b.candidate.as_str()).unwrap());
1092 match find_best_match_for_name(
1093 &suggestions.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),
1097 Some(found) if found != ident.name => {
1098 suggestions.into_iter().find(|suggestion| suggestion.candidate == found)
1104 fn lookup_import_candidates_from_module<FilterFn>(
1106 lookup_ident: Ident,
1107 namespace: Namespace,
1108 parent_scope: &ParentScope<'a>,
1109 start_module: Module<'a>,
1111 filter_fn: FilterFn,
1112 ) -> Vec<ImportSuggestion>
1114 FilterFn: Fn(Res) -> bool,
1116 let mut candidates = Vec::new();
1117 let mut seen_modules = FxHashSet::default();
1118 let mut worklist = vec![(start_module, Vec::<ast::PathSegment>::new(), true)];
1119 let mut worklist_via_import = vec![];
1121 while let Some((in_module, path_segments, accessible)) = match worklist.pop() {
1122 None => worklist_via_import.pop(),
1125 let in_module_is_extern = !in_module.def_id().is_local();
1126 // We have to visit module children in deterministic order to avoid
1127 // instabilities in reported imports (#43552).
1128 in_module.for_each_child(self, |this, ident, ns, name_binding| {
1129 // avoid non-importable candidates
1130 if !name_binding.is_importable() {
1134 let child_accessible =
1135 accessible && this.is_accessible_from(name_binding.vis, parent_scope.module);
1137 // do not venture inside inaccessible items of other crates
1138 if in_module_is_extern && !child_accessible {
1142 let via_import = name_binding.is_import() && !name_binding.is_extern_crate();
1144 // There is an assumption elsewhere that paths of variants are in the enum's
1145 // declaration and not imported. With this assumption, the variant component is
1146 // chopped and the rest of the path is assumed to be the enum's own path. For
1147 // errors where a variant is used as the type instead of the enum, this causes
1148 // funny looking invalid suggestions, i.e `foo` instead of `foo::MyEnum`.
1149 if via_import && name_binding.is_possibly_imported_variant() {
1153 // #90113: Do not count an inaccessible reexported item as a candidate.
1154 if let NameBindingKind::Import { binding, .. } = name_binding.kind {
1155 if this.is_accessible_from(binding.vis, parent_scope.module)
1156 && !this.is_accessible_from(name_binding.vis, parent_scope.module)
1162 // collect results based on the filter function
1163 // avoid suggesting anything from the same module in which we are resolving
1164 // avoid suggesting anything with a hygienic name
1165 if ident.name == lookup_ident.name
1167 && !ptr::eq(in_module, parent_scope.module)
1168 && !ident.span.normalize_to_macros_2_0().from_expansion()
1170 let res = name_binding.res();
1173 let mut segms = path_segments.clone();
1174 if lookup_ident.span.rust_2018() {
1175 // crate-local absolute paths start with `crate::` in edition 2018
1176 // FIXME: may also be stabilized for Rust 2015 (Issues #45477, #44660)
1177 segms.insert(0, ast::PathSegment::from_ident(crate_name));
1180 segms.push(ast::PathSegment::from_ident(ident));
1181 let path = Path { span: name_binding.span, segments: segms, tokens: None };
1182 let did = match res {
1183 Res::Def(DefKind::Ctor(..), did) => this.opt_parent(did),
1184 _ => res.opt_def_id(),
1187 if child_accessible {
1188 // Remove invisible match if exists
1189 if let Some(idx) = candidates
1191 .position(|v: &ImportSuggestion| v.did == did && !v.accessible)
1193 candidates.remove(idx);
1197 if candidates.iter().all(|v: &ImportSuggestion| v.did != did) {
1198 // See if we're recommending TryFrom, TryInto, or FromIterator and add
1199 // a note about editions
1200 let note = if let Some(did) = did {
1201 let requires_note = !did.is_local()
1202 && this.cstore().item_attrs_untracked(did, this.session).any(
1204 if attr.has_name(sym::rustc_diagnostic_item) {
1205 [sym::TryInto, sym::TryFrom, sym::FromIterator]
1207 .contains(&attr.value_str())
1214 requires_note.then(|| {
1216 "'{}' is included in the prelude starting in Edition 2021",
1217 path_names_to_string(&path)
1224 candidates.push(ImportSuggestion {
1228 accessible: child_accessible,
1235 // collect submodules to explore
1236 if let Some(module) = name_binding.module() {
1238 let mut path_segments = path_segments.clone();
1239 path_segments.push(ast::PathSegment::from_ident(ident));
1241 let is_extern_crate_that_also_appears_in_prelude =
1242 name_binding.is_extern_crate() && lookup_ident.span.rust_2018();
1244 if !is_extern_crate_that_also_appears_in_prelude {
1245 // add the module to the lookup
1246 if seen_modules.insert(module.def_id()) {
1247 if via_import { &mut worklist_via_import } else { &mut worklist }
1248 .push((module, path_segments, child_accessible));
1255 // If only some candidates are accessible, take just them
1256 if !candidates.iter().all(|v: &ImportSuggestion| !v.accessible) {
1257 candidates.retain(|x| x.accessible)
1263 /// When name resolution fails, this method can be used to look up candidate
1264 /// entities with the expected name. It allows filtering them using the
1265 /// supplied predicate (which should be used to only accept the types of
1266 /// definitions expected, e.g., traits). The lookup spans across all crates.
1268 /// N.B., the method does not look into imports, but this is not a problem,
1269 /// since we report the definitions (thus, the de-aliased imports).
1270 pub(crate) fn lookup_import_candidates<FilterFn>(
1272 lookup_ident: Ident,
1273 namespace: Namespace,
1274 parent_scope: &ParentScope<'a>,
1275 filter_fn: FilterFn,
1276 ) -> Vec<ImportSuggestion>
1278 FilterFn: Fn(Res) -> bool,
1280 let mut suggestions = self.lookup_import_candidates_from_module(
1285 Ident::with_dummy_span(kw::Crate),
1289 if lookup_ident.span.rust_2018() {
1290 let extern_prelude_names = self.extern_prelude.clone();
1291 for (ident, _) in extern_prelude_names.into_iter() {
1292 if ident.span.from_expansion() {
1293 // Idents are adjusted to the root context before being
1294 // resolved in the extern prelude, so reporting this to the
1295 // user is no help. This skips the injected
1296 // `extern crate std` in the 2018 edition, which would
1297 // otherwise cause duplicate suggestions.
1300 if let Some(crate_id) = self.crate_loader.maybe_process_path_extern(ident.name) {
1301 let crate_root = self.expect_module(crate_id.as_def_id());
1302 suggestions.extend(self.lookup_import_candidates_from_module(
1317 pub(crate) fn unresolved_macro_suggestions(
1319 err: &mut Diagnostic,
1320 macro_kind: MacroKind,
1321 parent_scope: &ParentScope<'a>,
1324 let is_expected = &|res: Res| res.macro_kind() == Some(macro_kind);
1325 let suggestion = self.early_lookup_typo_candidate(
1326 ScopeSet::Macro(macro_kind),
1331 self.add_typo_suggestion(err, suggestion, ident.span);
1333 let import_suggestions =
1334 self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected);
1340 &import_suggestions,
1343 DiagnosticMode::Normal,
1347 if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
1348 let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
1349 err.span_note(ident.span, &msg);
1352 if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
1353 err.help("have you added the `#[macro_use]` on the module/import?");
1356 if ident.name == kw::Default
1357 && let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
1358 && let Some(span) = self.opt_span(def_id)
1360 let source_map = self.session.source_map();
1361 let head_span = source_map.guess_head_span(span);
1362 if let Ok(head) = source_map.span_to_snippet(head_span) {
1363 err.span_suggestion(head_span, "consider adding a derive", format!("#[derive(Default)]\n{head}"), Applicability::MaybeIncorrect);
1367 "consider adding `#[derive(Default)]` to this enum",
1371 for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
1372 if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
1374 ScopeSet::All(ns, false),
1380 let desc = match binding.res() {
1381 Res::Def(DefKind::Macro(MacroKind::Bang), _) => {
1382 "a function-like macro".to_string()
1384 Res::Def(DefKind::Macro(MacroKind::Attr), _) | Res::NonMacroAttr(..) => {
1385 format!("an attribute: `#[{}]`", ident)
1387 Res::Def(DefKind::Macro(MacroKind::Derive), _) => {
1388 format!("a derive macro: `#[derive({})]`", ident)
1391 // Don't confuse the user with tool modules.
1394 Res::Def(DefKind::Trait, _) if macro_kind == MacroKind::Derive => {
1395 "only a trait, without a derive macro".to_string()
1401 macro_kind.article(),
1402 macro_kind.descr_expected(),
1405 if let crate::NameBindingKind::Import { import, .. } = binding.kind {
1406 if !import.span.is_dummy() {
1409 &format!("`{}` is imported here, but it is {}", ident, desc),
1411 // Silence the 'unused import' warning we might get,
1412 // since this diagnostic already covers that import.
1413 self.record_use(ident, binding, false);
1417 err.note(&format!("`{}` is in scope, but it is {}", ident, desc));
1423 pub(crate) fn add_typo_suggestion(
1425 err: &mut Diagnostic,
1426 suggestion: Option<TypoSuggestion>,
1429 let suggestion = match suggestion {
1430 None => return false,
1431 // We shouldn't suggest underscore.
1432 Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
1433 Some(suggestion) => suggestion,
1435 let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
1436 LOCAL_CRATE => self.opt_span(def_id),
1437 _ => Some(self.cstore().get_span_untracked(def_id, self.session)),
1439 if let Some(def_span) = def_span {
1440 if span.overlaps(def_span) {
1441 // Don't suggest typo suggestion for itself like in the following:
1442 // error[E0423]: expected function, tuple struct or tuple variant, found struct `X`
1443 // --> $DIR/issue-64792-bad-unicode-ctor.rs:3:14
1446 // | ----------- `X` defined here
1448 // LL | const Y: X = X("ö");
1449 // | -------------^^^^^^- similarly named constant `Y` defined here
1451 // help: use struct literal syntax instead
1453 // LL | const Y: X = X {};
1455 // help: a constant with a similar name exists
1457 // LL | const Y: X = Y("ö");
1461 let prefix = match suggestion.target {
1462 SuggestionTarget::SimilarlyNamed => "similarly named ",
1463 SuggestionTarget::SingleItem => "",
1467 self.session.source_map().guess_head_span(def_span),
1469 "{}{} `{}` defined here",
1471 suggestion.res.descr(),
1472 suggestion.candidate,
1476 let msg = match suggestion.target {
1477 SuggestionTarget::SimilarlyNamed => format!(
1478 "{} {} with a similar name exists",
1479 suggestion.res.article(),
1480 suggestion.res.descr()
1482 SuggestionTarget::SingleItem => {
1483 format!("maybe you meant this {}", suggestion.res.descr())
1486 err.span_suggestion(span, &msg, suggestion.candidate, Applicability::MaybeIncorrect);
1490 fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
1492 if b.span.is_dummy() || !self.session.source_map().is_span_accessible(b.span) {
1493 // These already contain the "built-in" prefix or look bad with it.
1495 !matches!(b.res(), Res::NonMacroAttr(..) | Res::PrimTy(..) | Res::ToolMod);
1496 let (built_in, from) = if from_prelude {
1497 ("", " from prelude")
1498 } else if b.is_extern_crate()
1500 && self.session.opts.externs.get(ident.as_str()).is_some()
1502 ("", " passed with `--extern`")
1503 } else if add_built_in {
1509 let a = if built_in.is_empty() { res.article() } else { "a" };
1510 format!("{a}{built_in} {thing}{from}", thing = res.descr())
1512 let introduced = if b.is_import_user_facing() { "imported" } else { "defined" };
1513 format!("the {thing} {introduced} here", thing = res.descr())
1517 fn report_ambiguity_error(&self, ambiguity_error: &AmbiguityError<'_>) {
1518 let AmbiguityError { kind, ident, b1, b2, misc1, misc2 } = *ambiguity_error;
1519 let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() {
1520 // We have to print the span-less alternative first, otherwise formatting looks bad.
1521 (b2, b1, misc2, misc1, true)
1523 (b1, b2, misc1, misc2, false)
1526 let mut err = struct_span_err!(self.session, ident.span, E0659, "`{ident}` is ambiguous");
1527 err.span_label(ident.span, "ambiguous name");
1528 err.note(&format!("ambiguous because of {}", kind.descr()));
1530 let mut could_refer_to = |b: &NameBinding<'_>, misc: AmbiguityErrorMisc, also: &str| {
1531 let what = self.binding_description(b, ident, misc == AmbiguityErrorMisc::FromPrelude);
1532 let note_msg = format!("`{ident}` could{also} refer to {what}");
1534 let thing = b.res().descr();
1535 let mut help_msgs = Vec::new();
1536 if b.is_glob_import()
1537 && (kind == AmbiguityKind::GlobVsGlob
1538 || kind == AmbiguityKind::GlobVsExpanded
1539 || kind == AmbiguityKind::GlobVsOuter && swapped != also.is_empty())
1541 help_msgs.push(format!(
1542 "consider adding an explicit import of `{ident}` to disambiguate"
1545 if b.is_extern_crate() && ident.span.rust_2018() {
1546 help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously"))
1548 if misc == AmbiguityErrorMisc::SuggestCrate {
1550 .push(format!("use `crate::{ident}` to refer to this {thing} unambiguously"))
1551 } else if misc == AmbiguityErrorMisc::SuggestSelf {
1553 .push(format!("use `self::{ident}` to refer to this {thing} unambiguously"))
1556 err.span_note(b.span, ¬e_msg);
1557 for (i, help_msg) in help_msgs.iter().enumerate() {
1558 let or = if i == 0 { "" } else { "or " };
1559 err.help(&format!("{}{}", or, help_msg));
1563 could_refer_to(b1, misc1, "");
1564 could_refer_to(b2, misc2, " also");
1568 /// If the binding refers to a tuple struct constructor with fields,
1569 /// returns the span of its fields.
1570 fn ctor_fields_span(&self, binding: &NameBinding<'_>) -> Option<Span> {
1571 if let NameBindingKind::Res(Res::Def(
1572 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn),
1576 let def_id = self.parent(ctor_def_id);
1577 let fields = self.field_names.get(&def_id)?;
1578 return fields.iter().map(|name| name.span).reduce(Span::to); // None for `struct Foo()`
1583 fn report_privacy_error(&self, privacy_error: &PrivacyError<'_>) {
1584 let PrivacyError { ident, binding, .. } = *privacy_error;
1586 let res = binding.res();
1587 let ctor_fields_span = self.ctor_fields_span(binding);
1588 let plain_descr = res.descr().to_string();
1589 let nonimport_descr =
1590 if ctor_fields_span.is_some() { plain_descr + " constructor" } else { plain_descr };
1591 let import_descr = nonimport_descr.clone() + " import";
1593 |b: &NameBinding<'_>| if b.is_import() { &import_descr } else { &nonimport_descr };
1595 // Print the primary message.
1596 let descr = get_descr(binding);
1598 struct_span_err!(self.session, ident.span, E0603, "{} `{}` is private", descr, ident);
1599 err.span_label(ident.span, &format!("private {}", descr));
1600 if let Some(span) = ctor_fields_span {
1601 err.span_label(span, "a constructor is private if any of the fields is private");
1604 // Print the whole import chain to make it easier to see what happens.
1605 let first_binding = binding;
1606 let mut next_binding = Some(binding);
1607 let mut next_ident = ident;
1608 while let Some(binding) = next_binding {
1609 let name = next_ident;
1610 next_binding = match binding.kind {
1611 _ if res == Res::Err => None,
1612 NameBindingKind::Import { binding, import, .. } => match import.kind {
1613 _ if binding.span.is_dummy() => None,
1614 ImportKind::Single { source, .. } => {
1615 next_ident = source;
1618 ImportKind::Glob { .. } | ImportKind::MacroUse | ImportKind::MacroExport => {
1621 ImportKind::ExternCrate { .. } => None,
1626 let first = ptr::eq(binding, first_binding);
1628 "{and_refers_to}the {item} `{name}`{which} is defined here{dots}",
1629 and_refers_to = if first { "" } else { "...and refers to " },
1630 item = get_descr(binding),
1631 which = if first { "" } else { " which" },
1632 dots = if next_binding.is_some() { "..." } else { "" },
1634 let def_span = self.session.source_map().guess_head_span(binding.span);
1635 let mut note_span = MultiSpan::from_span(def_span);
1636 if !first && binding.vis.is_public() {
1637 note_span.push_span_label(def_span, "consider importing it directly");
1639 err.span_note(note_span, &msg);
1645 pub(crate) fn find_similarly_named_module_or_crate(
1648 current_module: &Module<'a>,
1649 ) -> Option<Symbol> {
1650 let mut candidates = self
1653 .map(|(ident, _)| ident.name)
1657 .filter(|(_, module)| {
1658 current_module.is_ancestor_of(module) && !ptr::eq(current_module, *module)
1660 .flat_map(|(_, module)| module.kind.name()),
1662 .filter(|c| !c.to_string().is_empty())
1663 .collect::<Vec<_>>();
1666 match find_best_match_for_name(&candidates, ident, None) {
1667 Some(sugg) if sugg == ident => None,
1672 pub(crate) fn report_path_resolution_error(
1675 opt_ns: Option<Namespace>, // `None` indicates a module path in import
1676 parent_scope: &ParentScope<'a>,
1677 ribs: Option<&PerNS<Vec<Rib<'a>>>>,
1678 ignore_binding: Option<&'a NameBinding<'a>>,
1679 module: Option<ModuleOrUniformRoot<'a>>,
1682 ) -> (String, Option<Suggestion>) {
1683 let is_last = i == path.len() - 1;
1684 let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
1685 let module_res = match module {
1686 Some(ModuleOrUniformRoot::Module(module)) => module.res(),
1689 if module_res == self.graph_root.res() {
1690 let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
1691 let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
1693 .sort_by_cached_key(|c| (c.path.segments.len(), pprust::path_to_string(&c.path)));
1694 if let Some(candidate) = candidates.get(0) {
1696 String::from("unresolved import"),
1698 vec![(ident.span, pprust::path_to_string(&candidate.path))],
1699 String::from("a similar path exists"),
1700 Applicability::MaybeIncorrect,
1703 } else if self.session.edition() == Edition::Edition2015 {
1705 format!("maybe a missing crate `{ident}`?"),
1709 "consider adding `extern crate {ident}` to use the `{ident}` crate"
1711 Applicability::MaybeIncorrect,
1715 (format!("could not find `{ident}` in the crate root"), None)
1718 let parent = path[i - 1].ident.name;
1719 let parent = match parent {
1720 // ::foo is mounted at the crate root for 2015, and is the extern
1721 // prelude for 2018+
1722 kw::PathRoot if self.session.edition() > Edition::Edition2015 => {
1723 "the list of imported crates".to_owned()
1725 kw::PathRoot | kw::Crate => "the crate root".to_owned(),
1726 _ => format!("`{parent}`"),
1729 let mut msg = format!("could not find `{}` in {}", ident, parent);
1730 if ns == TypeNS || ns == ValueNS {
1731 let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
1732 let binding = if let Some(module) = module {
1733 self.resolve_ident_in_module(
1741 } else if let Some(ribs) = ribs
1742 && let Some(TypeNS | ValueNS) = opt_ns
1744 match self.resolve_ident_in_lexical_scope(
1752 // we found a locally-imported or available item/module
1753 Some(LexicalScopeBinding::Item(binding)) => Some(binding),
1757 let scopes = ScopeSet::All(ns_to_try, opt_ns.is_none());
1758 self.early_resolve_ident_in_lexical_scope(
1767 if let Some(binding) = binding {
1768 let mut found = |what| {
1770 "expected {}, found {} `{}` in {}",
1777 if binding.module().is_some() {
1780 match binding.res() {
1781 Res::Def(kind, id) => found(kind.descr(id)),
1782 _ => found(ns_to_try.descr()),
1788 } else if ident.name == kw::SelfUpper {
1789 ("`Self` is only available in impls, traits, and type definitions".to_string(), None)
1790 } else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
1791 // Check whether the name refers to an item in the value namespace.
1792 let binding = if let Some(ribs) = ribs {
1793 self.resolve_ident_in_lexical_scope(
1804 let match_span = match binding {
1805 // Name matches a local variable. For example:
1808 // let Foo: &str = "";
1809 // println!("{}", Foo::Bar); // Name refers to local
1810 // // variable `Foo`.
1813 Some(LexicalScopeBinding::Res(Res::Local(id))) => {
1814 Some(*self.pat_span_map.get(&id).unwrap())
1816 // Name matches item from a local name binding
1817 // created by `use` declaration. For example:
1819 // pub Foo: &str = "";
1823 // println!("{}", Foo::Bar); // Name refers to local
1824 // // binding `Foo`.
1827 Some(LexicalScopeBinding::Item(name_binding)) => Some(name_binding.span),
1830 let suggestion = if let Some(span) = match_span {
1832 vec![(span, String::from(""))],
1833 format!("`{}` is defined here, but is not a type", ident),
1834 Applicability::MaybeIncorrect,
1840 (format!("use of undeclared type `{}`", ident), suggestion)
1842 let suggestion = if ident.name == sym::alloc {
1845 String::from("add `extern crate alloc` to use the `alloc` crate"),
1846 Applicability::MaybeIncorrect,
1849 self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module).map(
1852 vec![(ident.span, sugg.to_string())],
1853 String::from("there is a crate or module with a similar name"),
1854 Applicability::MaybeIncorrect,
1859 (format!("use of undeclared crate or module `{}`", ident), suggestion)
1864 impl<'a, 'b> ImportResolver<'a, 'b> {
1865 /// Adds suggestions for a path that cannot be resolved.
1866 pub(crate) fn make_path_suggestion(
1869 mut path: Vec<Segment>,
1870 parent_scope: &ParentScope<'b>,
1871 ) -> Option<(Vec<Segment>, Option<String>)> {
1872 debug!("make_path_suggestion: span={:?} path={:?}", span, path);
1874 match (path.get(0), path.get(1)) {
1875 // `{{root}}::ident::...` on both editions.
1876 // On 2015 `{{root}}` is usually added implicitly.
1877 (Some(fst), Some(snd))
1878 if fst.ident.name == kw::PathRoot && !snd.ident.is_path_segment_keyword() => {}
1879 // `ident::...` on 2018.
1881 if fst.ident.span.rust_2018() && !fst.ident.is_path_segment_keyword() =>
1883 // Insert a placeholder that's later replaced by `self`/`super`/etc.
1884 path.insert(0, Segment::from_ident(Ident::empty()));
1889 self.make_missing_self_suggestion(path.clone(), parent_scope)
1890 .or_else(|| self.make_missing_crate_suggestion(path.clone(), parent_scope))
1891 .or_else(|| self.make_missing_super_suggestion(path.clone(), parent_scope))
1892 .or_else(|| self.make_external_crate_suggestion(path, parent_scope))
1895 /// Suggest a missing `self::` if that resolves to an correct module.
1899 /// LL | use foo::Bar;
1900 /// | ^^^ did you mean `self::foo`?
1902 fn make_missing_self_suggestion(
1904 mut path: Vec<Segment>,
1905 parent_scope: &ParentScope<'b>,
1906 ) -> Option<(Vec<Segment>, Option<String>)> {
1907 // Replace first ident with `self` and check if that is valid.
1908 path[0].ident.name = kw::SelfLower;
1909 let result = self.r.maybe_resolve_path(&path, None, parent_scope);
1910 debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result);
1911 if let PathResult::Module(..) = result { Some((path, None)) } else { None }
1914 /// Suggests a missing `crate::` if that resolves to an correct module.
1918 /// LL | use foo::Bar;
1919 /// | ^^^ did you mean `crate::foo`?
1921 fn make_missing_crate_suggestion(
1923 mut path: Vec<Segment>,
1924 parent_scope: &ParentScope<'b>,
1925 ) -> Option<(Vec<Segment>, Option<String>)> {
1926 // Replace first ident with `crate` and check if that is valid.
1927 path[0].ident.name = kw::Crate;
1928 let result = self.r.maybe_resolve_path(&path, None, parent_scope);
1929 debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result);
1930 if let PathResult::Module(..) = result {
1934 "`use` statements changed in Rust 2018; read more at \
1935 <https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-\
1945 /// Suggests a missing `super::` if that resolves to an correct module.
1949 /// LL | use foo::Bar;
1950 /// | ^^^ did you mean `super::foo`?
1952 fn make_missing_super_suggestion(
1954 mut path: Vec<Segment>,
1955 parent_scope: &ParentScope<'b>,
1956 ) -> Option<(Vec<Segment>, Option<String>)> {
1957 // Replace first ident with `crate` and check if that is valid.
1958 path[0].ident.name = kw::Super;
1959 let result = self.r.maybe_resolve_path(&path, None, parent_scope);
1960 debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result);
1961 if let PathResult::Module(..) = result { Some((path, None)) } else { None }
1964 /// Suggests a missing external crate name if that resolves to an correct module.
1968 /// LL | use foobar::Baz;
1969 /// | ^^^^^^ did you mean `baz::foobar`?
1972 /// Used when importing a submodule of an external crate but missing that crate's
1973 /// name as the first part of path.
1974 fn make_external_crate_suggestion(
1976 mut path: Vec<Segment>,
1977 parent_scope: &ParentScope<'b>,
1978 ) -> Option<(Vec<Segment>, Option<String>)> {
1979 if path[1].ident.span.rust_2015() {
1983 // Sort extern crate names in *reverse* order to get
1984 // 1) some consistent ordering for emitted diagnostics, and
1985 // 2) `std` suggestions before `core` suggestions.
1986 let mut extern_crate_names =
1987 self.r.extern_prelude.iter().map(|(ident, _)| ident.name).collect::<Vec<_>>();
1988 extern_crate_names.sort_by(|a, b| b.as_str().partial_cmp(a.as_str()).unwrap());
1990 for name in extern_crate_names.into_iter() {
1991 // Replace first ident with a crate name and check if that is valid.
1992 path[0].ident.name = name;
1993 let result = self.r.maybe_resolve_path(&path, None, parent_scope);
1995 "make_external_crate_suggestion: name={:?} path={:?} result={:?}",
1998 if let PathResult::Module(..) = result {
1999 return Some((path, None));
2006 /// Suggests importing a macro from the root of the crate rather than a module within
2010 /// help: a macro with this name exists at the root of the crate
2012 /// LL | use issue_59764::makro;
2013 /// | ^^^^^^^^^^^^^^^^^^
2015 /// = note: this could be because a macro annotated with `#[macro_export]` will be exported
2016 /// at the root of the crate instead of the module where it is defined
2018 pub(crate) fn check_for_module_export_macro(
2020 import: &'b Import<'b>,
2021 module: ModuleOrUniformRoot<'b>,
2023 ) -> Option<(Option<Suggestion>, Option<String>)> {
2024 let ModuleOrUniformRoot::Module(mut crate_module) = module else {
2028 while let Some(parent) = crate_module.parent {
2029 crate_module = parent;
2032 if ModuleOrUniformRoot::same_def(ModuleOrUniformRoot::Module(crate_module), module) {
2033 // Don't make a suggestion if the import was already from the root of the
2038 let resolutions = self.r.resolutions(crate_module).borrow();
2039 let resolution = resolutions.get(&self.r.new_key(ident, MacroNS))?;
2040 let binding = resolution.borrow().binding()?;
2041 if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() {
2042 let module_name = crate_module.kind.name().unwrap();
2043 let import_snippet = match import.kind {
2044 ImportKind::Single { source, target, .. } if source != target => {
2045 format!("{} as {}", source, target)
2047 _ => format!("{}", ident),
2050 let mut corrections: Vec<(Span, String)> = Vec::new();
2051 if !import.is_nested() {
2052 // Assume this is the easy case of `use issue_59764::foo::makro;` and just remove
2053 // intermediate segments.
2054 corrections.push((import.span, format!("{}::{}", module_name, import_snippet)));
2056 // Find the binding span (and any trailing commas and spaces).
2057 // ie. `use a::b::{c, d, e};`
2059 let (found_closing_brace, binding_span) = find_span_of_binding_until_next_binding(
2065 "check_for_module_export_macro: found_closing_brace={:?} binding_span={:?}",
2066 found_closing_brace, binding_span
2069 let mut removal_span = binding_span;
2070 if found_closing_brace {
2071 // If the binding span ended with a closing brace, as in the below example:
2072 // ie. `use a::b::{c, d};`
2074 // Then expand the span of characters to remove to include the previous
2075 // binding's trailing comma.
2076 // ie. `use a::b::{c, d};`
2078 if let Some(previous_span) =
2079 extend_span_to_previous_binding(self.r.session, binding_span)
2081 debug!("check_for_module_export_macro: previous_span={:?}", previous_span);
2082 removal_span = removal_span.with_lo(previous_span.lo());
2085 debug!("check_for_module_export_macro: removal_span={:?}", removal_span);
2087 // Remove the `removal_span`.
2088 corrections.push((removal_span, "".to_string()));
2090 // Find the span after the crate name and if it has nested imports immediately
2091 // after the crate name already.
2092 // ie. `use a::b::{c, d};`
2094 // or `use a::{b, c, d}};`
2096 let (has_nested, after_crate_name) = find_span_immediately_after_crate_name(
2102 "check_for_module_export_macro: has_nested={:?} after_crate_name={:?}",
2103 has_nested, after_crate_name
2106 let source_map = self.r.session.source_map();
2108 // Add the import to the start, with a `{` if required.
2109 let start_point = source_map.start_point(after_crate_name);
2110 if let Ok(start_snippet) = source_map.span_to_snippet(start_point) {
2114 // In this case, `start_snippet` must equal '{'.
2115 format!("{}{}, ", start_snippet, import_snippet)
2117 // In this case, add a `{`, then the moved import, then whatever
2118 // was there before.
2119 format!("{{{}, {}", import_snippet, start_snippet)
2124 // Add a `};` to the end if nested, matching the `{` added at the start.
2126 corrections.push((source_map.end_point(after_crate_name), "};".to_string()));
2130 let suggestion = Some((
2132 String::from("a macro with this name exists at the root of the crate"),
2133 Applicability::MaybeIncorrect,
2135 Some((suggestion, Some("this could be because a macro annotated with `#[macro_export]` will be exported \
2136 at the root of the crate instead of the module where it is defined"
2144 /// Given a `binding_span` of a binding within a use statement:
2146 /// ```ignore (illustrative)
2147 /// use foo::{a, b, c};
2151 /// then return the span until the next binding or the end of the statement:
2153 /// ```ignore (illustrative)
2154 /// use foo::{a, b, c};
2157 fn find_span_of_binding_until_next_binding(
2162 let source_map = sess.source_map();
2164 // Find the span of everything after the binding.
2165 // ie. `a, e};` or `a};`
2166 let binding_until_end = binding_span.with_hi(use_span.hi());
2168 // Find everything after the binding but not including the binding.
2169 // ie. `, e};` or `};`
2170 let after_binding_until_end = binding_until_end.with_lo(binding_span.hi());
2172 // Keep characters in the span until we encounter something that isn't a comma or
2176 // Also note whether a closing brace character was encountered. If there
2177 // was, then later go backwards to remove any trailing commas that are left.
2178 let mut found_closing_brace = false;
2179 let after_binding_until_next_binding =
2180 source_map.span_take_while(after_binding_until_end, |&ch| {
2182 found_closing_brace = true;
2184 ch == ' ' || ch == ','
2187 // Combine the two spans.
2188 // ie. `a, ` or `a`.
2190 // Removing these would leave `issue_52891::{d, e};` or `issue_52891::{d, e, };`
2191 let span = binding_span.with_hi(after_binding_until_next_binding.hi());
2193 (found_closing_brace, span)
2196 /// Given a `binding_span`, return the span through to the comma or opening brace of the previous
2199 /// ```ignore (illustrative)
2200 /// use foo::a::{a, b, c};
2201 /// // ^^--- binding span
2203 /// // returned span
2205 /// use foo::{a, b, c};
2206 /// // --- binding span
2208 fn extend_span_to_previous_binding(sess: &Session, binding_span: Span) -> Option<Span> {
2209 let source_map = sess.source_map();
2211 // `prev_source` will contain all of the source that came before the span.
2212 // Then split based on a command and take the first (ie. closest to our span)
2213 // snippet. In the example, this is a space.
2214 let prev_source = source_map.span_to_prev_source(binding_span).ok()?;
2216 let prev_comma = prev_source.rsplit(',').collect::<Vec<_>>();
2217 let prev_starting_brace = prev_source.rsplit('{').collect::<Vec<_>>();
2218 if prev_comma.len() <= 1 || prev_starting_brace.len() <= 1 {
2222 let prev_comma = prev_comma.first().unwrap();
2223 let prev_starting_brace = prev_starting_brace.first().unwrap();
2225 // If the amount of source code before the comma is greater than
2226 // the amount of source code before the starting brace then we've only
2227 // got one item in the nested item (eg. `issue_52891::{self}`).
2228 if prev_comma.len() > prev_starting_brace.len() {
2232 Some(binding_span.with_lo(BytePos(
2233 // Take away the number of bytes for the characters we've found and an
2234 // extra for the comma.
2235 binding_span.lo().0 - (prev_comma.as_bytes().len() as u32) - 1,
2239 /// Given a `use_span` of a binding within a use statement, returns the highlighted span and if
2240 /// it is a nested use tree.
2242 /// ```ignore (illustrative)
2243 /// use foo::a::{b, c};
2244 /// // ^^^^^^^^^^ -- false
2246 /// use foo::{a, b, c};
2247 /// // ^^^^^^^^^^ -- true
2249 /// use foo::{a, b::{c, d}};
2250 /// // ^^^^^^^^^^^^^^^ -- true
2252 fn find_span_immediately_after_crate_name(
2254 module_name: Symbol,
2258 "find_span_immediately_after_crate_name: module_name={:?} use_span={:?}",
2259 module_name, use_span
2261 let source_map = sess.source_map();
2263 // Using `use issue_59764::foo::{baz, makro};` as an example throughout..
2264 let mut num_colons = 0;
2265 // Find second colon.. `use issue_59764:`
2266 let until_second_colon = source_map.span_take_while(use_span, |c| {
2270 !matches!(c, ':' if num_colons == 2)
2272 // Find everything after the second colon.. `foo::{baz, makro};`
2273 let from_second_colon = use_span.with_lo(until_second_colon.hi() + BytePos(1));
2275 let mut found_a_non_whitespace_character = false;
2276 // Find the first non-whitespace character in `from_second_colon`.. `f`
2277 let after_second_colon = source_map.span_take_while(from_second_colon, |c| {
2278 if found_a_non_whitespace_character {
2281 if !c.is_whitespace() {
2282 found_a_non_whitespace_character = true;
2287 // Find the first `{` in from_second_colon.. `foo::{`
2288 let next_left_bracket = source_map.span_through_char(from_second_colon, '{');
2290 (next_left_bracket == after_second_colon, from_second_colon)
2293 /// A suggestion has already been emitted, change the wording slightly to clarify that both are
2294 /// independent options.
2300 /// Whether an existing place with an `use` item was found.
2306 /// Whether a binding is part of a pattern or a use statement. Used for diagnostics.
2307 enum DiagnosticMode {
2309 /// The binding is part of a pattern
2311 /// The binding is part of a use statement
2315 pub(crate) fn import_candidates(
2317 source_span: &IndexVec<LocalDefId, Span>,
2318 err: &mut Diagnostic,
2319 // This is `None` if all placement locations are inside expansions
2320 use_placement_span: Option<Span>,
2321 candidates: &[ImportSuggestion],
2331 DiagnosticMode::Import,
2336 /// When an entity with a given name is not available in scope, we search for
2337 /// entities with that name in all crates. This method allows outputting the
2338 /// results of this search in a programmer-friendly way
2341 source_span: &IndexVec<LocalDefId, Span>,
2342 err: &mut Diagnostic,
2343 // This is `None` if all placement locations are inside expansions
2344 use_placement_span: Option<Span>,
2345 candidates: &[ImportSuggestion],
2347 found_use: FoundUse,
2348 mode: DiagnosticMode,
2351 if candidates.is_empty() {
2355 let mut accessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>)> =
2357 let mut inaccessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>)> =
2360 candidates.iter().for_each(|c| {
2361 (if c.accessible { &mut accessible_path_strings } else { &mut inaccessible_path_strings })
2362 .push((path_names_to_string(&c.path), c.descr, c.did, &c.note))
2365 // we want consistent results across executions, but candidates are produced
2366 // by iterating through a hash map, so make sure they are ordered:
2367 for path_strings in [&mut accessible_path_strings, &mut inaccessible_path_strings] {
2368 path_strings.sort_by(|a, b| a.0.cmp(&b.0));
2369 let core_path_strings =
2370 path_strings.drain_filter(|p| p.0.starts_with("core::")).collect::<Vec<_>>();
2371 path_strings.extend(core_path_strings);
2372 path_strings.dedup_by(|a, b| a.0 == b.0);
2375 if !accessible_path_strings.is_empty() {
2376 let (determiner, kind, name) = if accessible_path_strings.len() == 1 {
2377 ("this", accessible_path_strings[0].1, format!(" `{}`", accessible_path_strings[0].0))
2379 ("one of these", "items", String::new())
2382 let instead = if let Instead::Yes = instead { " instead" } else { "" };
2383 let mut msg = if let DiagnosticMode::Pattern = mode {
2385 "if you meant to match on {}{}{}, use the full path in the pattern",
2389 format!("consider importing {} {}{}", determiner, kind, instead)
2392 for note in accessible_path_strings.iter().flat_map(|cand| cand.3.as_ref()) {
2396 if let Some(span) = use_placement_span {
2397 let add_use = match mode {
2398 DiagnosticMode::Pattern => {
2399 err.span_suggestions(
2402 accessible_path_strings.into_iter().map(|a| a.0),
2403 Applicability::MaybeIncorrect,
2407 DiagnosticMode::Import => "",
2408 DiagnosticMode::Normal => "use ",
2410 for candidate in &mut accessible_path_strings {
2411 // produce an additional newline to separate the new use statement
2412 // from the directly following item.
2413 let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
2414 candidate.0 = format!("{}{};\n{}", add_use, &candidate.0, additional_newline);
2417 err.span_suggestions(
2420 accessible_path_strings.into_iter().map(|a| a.0),
2421 Applicability::MaybeIncorrect,
2423 if let [first, .., last] = &path[..] {
2424 let sp = first.ident.span.until(last.ident.span);
2425 if sp.can_be_used_for_suggestions() {
2426 err.span_suggestion_verbose(
2428 &format!("if you import `{}`, refer to it directly", last.ident),
2430 Applicability::Unspecified,
2437 for candidate in accessible_path_strings {
2439 msg.push_str(&candidate.0);
2444 } else if !matches!(mode, DiagnosticMode::Import) {
2445 assert!(!inaccessible_path_strings.is_empty());
2447 let prefix = if let DiagnosticMode::Pattern = mode {
2448 "you might have meant to match on "
2452 if inaccessible_path_strings.len() == 1 {
2453 let (name, descr, def_id, note) = &inaccessible_path_strings[0];
2455 "{}{} `{}`{} exists but is inaccessible",
2459 if let DiagnosticMode::Pattern = mode { ", which" } else { "" }
2462 if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
2463 let span = source_span[local_def_id];
2464 let span = session.source_map().guess_head_span(span);
2465 let mut multi_span = MultiSpan::from_span(span);
2466 multi_span.push_span_label(span, "not accessible");
2467 err.span_note(multi_span, &msg);
2471 if let Some(note) = (*note).as_deref() {
2475 let (_, descr_first, _, _) = &inaccessible_path_strings[0];
2476 let descr = if inaccessible_path_strings
2479 .all(|(_, descr, _, _)| descr == descr_first)
2486 if descr.ends_with('s') { format!("{}es", descr) } else { format!("{}s", descr) };
2488 let mut msg = format!("{}these {} exist but are inaccessible", prefix, plural_descr);
2489 let mut has_colon = false;
2491 let mut spans = Vec::new();
2492 for (name, _, def_id, _) in &inaccessible_path_strings {
2493 if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
2494 let span = source_span[local_def_id];
2495 let span = session.source_map().guess_head_span(span);
2496 spans.push((name, span));
2507 let mut multi_span = MultiSpan::from_spans(spans.iter().map(|(_, sp)| *sp).collect());
2508 for (name, span) in spans {
2509 multi_span.push_span_label(span, format!("`{}`: not accessible", name));
2512 for note in inaccessible_path_strings.iter().flat_map(|cand| cand.3.as_ref()) {
2516 err.span_note(multi_span, &msg);
2522 struct UsePlacementFinder {
2523 target_module: NodeId,
2524 first_legal_span: Option<Span>,
2525 first_use_span: Option<Span>,
2528 impl UsePlacementFinder {
2529 fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, FoundUse) {
2531 UsePlacementFinder { target_module, first_legal_span: None, first_use_span: None };
2532 finder.visit_crate(krate);
2533 if let Some(use_span) = finder.first_use_span {
2534 (Some(use_span), FoundUse::Yes)
2536 (finder.first_legal_span, FoundUse::No)
2541 impl<'tcx> visit::Visitor<'tcx> for UsePlacementFinder {
2542 fn visit_crate(&mut self, c: &Crate) {
2543 if self.target_module == CRATE_NODE_ID {
2544 let inject = c.spans.inject_use_span;
2545 if is_span_suitable_for_use_injection(inject) {
2546 self.first_legal_span = Some(inject);
2548 self.first_use_span = search_for_any_use_in_items(&c.items);
2551 visit::walk_crate(self, c);
2555 fn visit_item(&mut self, item: &'tcx ast::Item) {
2556 if self.target_module == item.id {
2557 if let ItemKind::Mod(_, ModKind::Loaded(items, _inline, mod_spans)) = &item.kind {
2558 let inject = mod_spans.inject_use_span;
2559 if is_span_suitable_for_use_injection(inject) {
2560 self.first_legal_span = Some(inject);
2562 self.first_use_span = search_for_any_use_in_items(items);
2566 visit::walk_item(self, item);
2571 fn search_for_any_use_in_items(items: &[P<ast::Item>]) -> Option<Span> {
2573 if let ItemKind::Use(..) = item.kind {
2574 if is_span_suitable_for_use_injection(item.span) {
2575 return Some(item.span.shrink_to_lo());
2582 fn is_span_suitable_for_use_injection(s: Span) -> bool {
2583 // don't suggest placing a use before the prelude
2584 // import or other generated ones
2588 /// Convert the given number into the corresponding ordinal
2589 pub(crate) fn ordinalize(v: usize) -> String {
2590 let suffix = match ((11..=13).contains(&(v % 100)), v % 10) {
2596 format!("{v}{suffix}")