1use rustc_ast::ptr::P;
2use rustc_ast::visit::{self, Visitor};
3use rustc_ast::{
4 self as ast, CRATE_NODE_ID, Crate, ItemKind, ModKind, NodeId, Path, join_path_idents,
5};
6use rustc_ast_pretty::pprust;
7use rustc_attr_data_structures::{
8 self as attr, AttributeKind, CfgEntry, Stability, StrippedCfgItem, find_attr,
9};
10use rustc_data_structures::fx::{FxHashMap, FxHashSet};
11use rustc_data_structures::unord::{UnordMap, UnordSet};
12use rustc_errors::codes::*;
13use rustc_errors::{
14 Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, MultiSpan, SuggestionStyle,
15 report_ambiguity_error, struct_span_code_err,
16};
17use rustc_feature::BUILTIN_ATTRIBUTES;
18use rustc_hir::PrimTy;
19use rustc_hir::def::Namespace::{self, *};
20use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
21use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
22use rustc_middle::bug;
23use rustc_middle::ty::TyCtxt;
24use rustc_session::Session;
25use rustc_session::lint::builtin::{
26 ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, AMBIGUOUS_GLOB_IMPORTS,
27 MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
28};
29use rustc_session::lint::{AmbiguityErrorDiag, BuiltinLintDiag};
30use rustc_session::utils::was_invoked_from_cargo;
31use rustc_span::edit_distance::find_best_match_for_name;
32use rustc_span::edition::Edition;
33use rustc_span::hygiene::MacroKind;
34use rustc_span::source_map::SourceMap;
35use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym};
36use thin_vec::{ThinVec, thin_vec};
37use tracing::{debug, instrument};
38
39use crate::errors::{
40 self, AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
41 ExplicitUnsafeTraits, MacroDefinedLater, MacroRulesNot, MacroSuggMovePosition,
42 MaybeMissingMacroRulesName,
43};
44use crate::imports::{Import, ImportKind};
45use crate::late::{PatternSource, Rib};
46use crate::{
47 AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, BindingKey, Finalize,
48 ForwardGenericParamBanReason, HasGenericParams, LexicalScopeBinding, MacroRulesScope, Module,
49 ModuleKind, ModuleOrUniformRoot, NameBinding, NameBindingKind, ParentScope, PathResult,
50 PrivacyError, ResolutionError, Resolver, Scope, ScopeSet, Segment, UseError, Used,
51 VisResolutionError, errors as errs, path_names_to_string,
52};
53
54type Res = def::Res<ast::NodeId>;
55
56pub(crate) type Suggestion = (Vec<(Span, String)>, String, Applicability);
58
59pub(crate) type LabelSuggestion = (Ident, bool);
62
63#[derive(Debug)]
64pub(crate) enum SuggestionTarget {
65 SimilarlyNamed,
67 SingleItem,
69}
70
71#[derive(Debug)]
72pub(crate) struct TypoSuggestion {
73 pub candidate: Symbol,
74 pub span: Option<Span>,
77 pub res: Res,
78 pub target: SuggestionTarget,
79}
80
81impl TypoSuggestion {
82 pub(crate) fn typo_from_ident(ident: Ident, res: Res) -> TypoSuggestion {
83 Self {
84 candidate: ident.name,
85 span: Some(ident.span),
86 res,
87 target: SuggestionTarget::SimilarlyNamed,
88 }
89 }
90 pub(crate) fn typo_from_name(candidate: Symbol, res: Res) -> TypoSuggestion {
91 Self { candidate, span: None, res, target: SuggestionTarget::SimilarlyNamed }
92 }
93 pub(crate) fn single_item_from_ident(ident: Ident, res: Res) -> TypoSuggestion {
94 Self {
95 candidate: ident.name,
96 span: Some(ident.span),
97 res,
98 target: SuggestionTarget::SingleItem,
99 }
100 }
101}
102
103#[derive(Debug, Clone)]
105pub(crate) struct ImportSuggestion {
106 pub did: Option<DefId>,
107 pub descr: &'static str,
108 pub path: Path,
109 pub accessible: bool,
110 pub doc_visible: bool,
112 pub via_import: bool,
113 pub note: Option<String>,
115 pub is_stable: bool,
116}
117
118fn reduce_impl_span_to_impl_keyword(sm: &SourceMap, impl_span: Span) -> Span {
126 let impl_span = sm.span_until_char(impl_span, '<');
127 sm.span_until_whitespace(impl_span)
128}
129
130impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
131 pub(crate) fn dcx(&self) -> DiagCtxtHandle<'tcx> {
132 self.tcx.dcx()
133 }
134
135 pub(crate) fn report_errors(&mut self, krate: &Crate) {
136 self.report_with_use_injections(krate);
137
138 for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
139 self.lint_buffer.buffer_lint(
140 MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
141 CRATE_NODE_ID,
142 span_use,
143 BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def),
144 );
145 }
146
147 for ambiguity_error in &self.ambiguity_errors {
148 let diag = self.ambiguity_diagnostics(ambiguity_error);
149 if ambiguity_error.warning {
150 let NameBindingKind::Import { import, .. } = ambiguity_error.b1.0.kind else {
151 unreachable!()
152 };
153 self.lint_buffer.buffer_lint(
154 AMBIGUOUS_GLOB_IMPORTS,
155 import.root_id,
156 ambiguity_error.ident.span,
157 BuiltinLintDiag::AmbiguousGlobImports { diag },
158 );
159 } else {
160 let mut err = struct_span_code_err!(self.dcx(), diag.span, E0659, "{}", diag.msg);
161 report_ambiguity_error(&mut err, diag);
162 err.emit();
163 }
164 }
165
166 let mut reported_spans = FxHashSet::default();
167 for error in std::mem::take(&mut self.privacy_errors) {
168 if reported_spans.insert(error.dedup_span) {
169 self.report_privacy_error(&error);
170 }
171 }
172 }
173
174 fn report_with_use_injections(&mut self, krate: &Crate) {
175 for UseError { mut err, candidates, def_id, instead, suggestion, path, is_call } in
176 std::mem::take(&mut self.use_injections)
177 {
178 let (span, found_use) = if let Some(def_id) = def_id.as_local() {
179 UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id))
180 } else {
181 (None, FoundUse::No)
182 };
183
184 if !candidates.is_empty() {
185 show_candidates(
186 self.tcx,
187 &mut err,
188 span,
189 &candidates,
190 if instead { Instead::Yes } else { Instead::No },
191 found_use,
192 DiagMode::Normal,
193 path,
194 "",
195 );
196 err.emit();
197 } else if let Some((span, msg, sugg, appl)) = suggestion {
198 err.span_suggestion_verbose(span, msg, sugg, appl);
199 err.emit();
200 } else if let [segment] = path.as_slice()
201 && is_call
202 {
203 err.stash(segment.ident.span, rustc_errors::StashKey::CallIntoMethod);
204 } else {
205 err.emit();
206 }
207 }
208 }
209
210 pub(crate) fn report_conflict(
211 &mut self,
212 parent: Module<'_>,
213 ident: Ident,
214 ns: Namespace,
215 new_binding: NameBinding<'ra>,
216 old_binding: NameBinding<'ra>,
217 ) {
218 if old_binding.span.lo() > new_binding.span.lo() {
220 return self.report_conflict(parent, ident, ns, old_binding, new_binding);
221 }
222
223 let container = match parent.kind {
224 ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id()),
227 ModuleKind::Block => "block",
228 };
229
230 let (name, span) =
231 (ident.name, self.tcx.sess.source_map().guess_head_span(new_binding.span));
232
233 if self.name_already_seen.get(&name) == Some(&span) {
234 return;
235 }
236
237 let old_kind = match (ns, old_binding.res()) {
238 (ValueNS, _) => "value",
239 (MacroNS, _) => "macro",
240 (TypeNS, _) if old_binding.is_extern_crate() => "extern crate",
241 (TypeNS, Res::Def(DefKind::Mod, _)) => "module",
242 (TypeNS, Res::Def(DefKind::Trait, _)) => "trait",
243 (TypeNS, _) => "type",
244 };
245
246 let code = match (old_binding.is_extern_crate(), new_binding.is_extern_crate()) {
247 (true, true) => E0259,
248 (true, _) | (_, true) => match new_binding.is_import() && old_binding.is_import() {
249 true => E0254,
250 false => E0260,
251 },
252 _ => match (old_binding.is_import_user_facing(), new_binding.is_import_user_facing()) {
253 (false, false) => E0428,
254 (true, true) => E0252,
255 _ => E0255,
256 },
257 };
258
259 let label = match new_binding.is_import_user_facing() {
260 true => errors::NameDefinedMultipleTimeLabel::Reimported { span },
261 false => errors::NameDefinedMultipleTimeLabel::Redefined { span },
262 };
263
264 let old_binding_label =
265 (!old_binding.span.is_dummy() && old_binding.span != span).then(|| {
266 let span = self.tcx.sess.source_map().guess_head_span(old_binding.span);
267 match old_binding.is_import_user_facing() {
268 true => {
269 errors::NameDefinedMultipleTimeOldBindingLabel::Import { span, old_kind }
270 }
271 false => errors::NameDefinedMultipleTimeOldBindingLabel::Definition {
272 span,
273 old_kind,
274 },
275 }
276 });
277
278 let mut err = self
279 .dcx()
280 .create_err(errors::NameDefinedMultipleTime {
281 span,
282 name,
283 descr: ns.descr(),
284 container,
285 label,
286 old_binding_label,
287 })
288 .with_code(code);
289
290 use NameBindingKind::Import;
292 let can_suggest = |binding: NameBinding<'_>, import: self::Import<'_>| {
293 !binding.span.is_dummy()
294 && !matches!(import.kind, ImportKind::MacroUse { .. } | ImportKind::MacroExport)
295 };
296 let import = match (&new_binding.kind, &old_binding.kind) {
297 (Import { import: new, .. }, Import { import: old, .. })
300 if {
301 (new.has_attributes || old.has_attributes)
302 && can_suggest(old_binding, *old)
303 && can_suggest(new_binding, *new)
304 } =>
305 {
306 if old.has_attributes {
307 Some((*new, new_binding.span, true))
308 } else {
309 Some((*old, old_binding.span, true))
310 }
311 }
312 (Import { import, .. }, other) if can_suggest(new_binding, *import) => {
314 Some((*import, new_binding.span, other.is_import()))
315 }
316 (other, Import { import, .. }) if can_suggest(old_binding, *import) => {
317 Some((*import, old_binding.span, other.is_import()))
318 }
319 _ => None,
320 };
321
322 let duplicate = new_binding.res().opt_def_id() == old_binding.res().opt_def_id();
324 let has_dummy_span = new_binding.span.is_dummy() || old_binding.span.is_dummy();
325 let from_item =
326 self.extern_prelude.get(&ident).is_none_or(|entry| entry.introduced_by_item);
327 let should_remove_import = duplicate
331 && !has_dummy_span
332 && ((new_binding.is_extern_crate() || old_binding.is_extern_crate()) || from_item);
333
334 match import {
335 Some((import, span, true)) if should_remove_import && import.is_nested() => {
336 self.add_suggestion_for_duplicate_nested_use(&mut err, import, span);
337 }
338 Some((import, _, true)) if should_remove_import && !import.is_glob() => {
339 err.subdiagnostic(errors::ToolOnlyRemoveUnnecessaryImport {
342 span: import.use_span_with_attributes,
343 });
344 }
345 Some((import, span, _)) => {
346 self.add_suggestion_for_rename_of_use(&mut err, name, import, span);
347 }
348 _ => {}
349 }
350
351 err.emit();
352 self.name_already_seen.insert(name, span);
353 }
354
355 fn add_suggestion_for_rename_of_use(
365 &self,
366 err: &mut Diag<'_>,
367 name: Symbol,
368 import: Import<'_>,
369 binding_span: Span,
370 ) {
371 let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
372 format!("Other{name}")
373 } else {
374 format!("other_{name}")
375 };
376
377 let mut suggestion = None;
378 let mut span = binding_span;
379 match import.kind {
380 ImportKind::Single { type_ns_only: true, .. } => {
381 suggestion = Some(format!("self as {suggested_name}"))
382 }
383 ImportKind::Single { source, .. } => {
384 if let Some(pos) = source.span.hi().0.checked_sub(binding_span.lo().0)
385 && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span)
386 && pos as usize <= snippet.len()
387 {
388 span = binding_span.with_lo(binding_span.lo() + BytePos(pos)).with_hi(
389 binding_span.hi() - BytePos(if snippet.ends_with(';') { 1 } else { 0 }),
390 );
391 suggestion = Some(format!(" as {suggested_name}"));
392 }
393 }
394 ImportKind::ExternCrate { source, target, .. } => {
395 suggestion = Some(format!(
396 "extern crate {} as {};",
397 source.unwrap_or(target.name),
398 suggested_name,
399 ))
400 }
401 _ => unreachable!(),
402 }
403
404 if let Some(suggestion) = suggestion {
405 err.subdiagnostic(ChangeImportBindingSuggestion { span, suggestion });
406 } else {
407 err.subdiagnostic(ChangeImportBinding { span });
408 }
409 }
410
411 fn add_suggestion_for_duplicate_nested_use(
434 &self,
435 err: &mut Diag<'_>,
436 import: Import<'_>,
437 binding_span: Span,
438 ) {
439 assert!(import.is_nested());
440
441 let (found_closing_brace, span) =
449 find_span_of_binding_until_next_binding(self.tcx.sess, binding_span, import.use_span);
450
451 if found_closing_brace {
454 if let Some(span) = extend_span_to_previous_binding(self.tcx.sess, span) {
455 err.subdiagnostic(errors::ToolOnlyRemoveUnnecessaryImport { span });
456 } else {
457 err.subdiagnostic(errors::RemoveUnnecessaryImport {
460 span: import.use_span_with_attributes,
461 });
462 }
463
464 return;
465 }
466
467 err.subdiagnostic(errors::RemoveUnnecessaryImport { span });
468 }
469
470 pub(crate) fn lint_if_path_starts_with_module(
471 &mut self,
472 finalize: Option<Finalize>,
473 path: &[Segment],
474 second_binding: Option<NameBinding<'_>>,
475 ) {
476 let Some(Finalize { node_id, root_span, .. }) = finalize else {
477 return;
478 };
479
480 let first_name = match path.get(0) {
481 Some(seg) if seg.ident.span.is_rust_2015() && self.tcx.sess.is_rust_2015() => {
483 seg.ident.name
484 }
485 _ => return,
486 };
487
488 if first_name != kw::PathRoot {
491 return;
492 }
493
494 match path.get(1) {
495 Some(Segment { ident, .. }) if ident.name == kw::Crate => return,
497 Some(_) => {}
499 None => return,
503 }
504
505 if let Some(binding) = second_binding
509 && let NameBindingKind::Import { import, .. } = binding.kind
510 && let ImportKind::ExternCrate { source: None, .. } = import.kind
512 {
513 return;
514 }
515
516 let diag = BuiltinLintDiag::AbsPathWithModule(root_span);
517 self.lint_buffer.buffer_lint(
518 ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
519 node_id,
520 root_span,
521 diag,
522 );
523 }
524
525 pub(crate) fn add_module_candidates(
526 &mut self,
527 module: Module<'ra>,
528 names: &mut Vec<TypoSuggestion>,
529 filter_fn: &impl Fn(Res) -> bool,
530 ctxt: Option<SyntaxContext>,
531 ) {
532 module.for_each_child(self, |_this, ident, _ns, binding| {
533 let res = binding.res();
534 if filter_fn(res) && ctxt.is_none_or(|ctxt| ctxt == ident.span.ctxt()) {
535 names.push(TypoSuggestion::typo_from_ident(ident, res));
536 }
537 });
538 }
539
540 pub(crate) fn report_error(
545 &mut self,
546 span: Span,
547 resolution_error: ResolutionError<'ra>,
548 ) -> ErrorGuaranteed {
549 self.into_struct_error(span, resolution_error).emit()
550 }
551
552 pub(crate) fn into_struct_error(
553 &mut self,
554 span: Span,
555 resolution_error: ResolutionError<'ra>,
556 ) -> Diag<'_> {
557 match resolution_error {
558 ResolutionError::GenericParamsFromOuterItem(
559 outer_res,
560 has_generic_params,
561 def_kind,
562 ) => {
563 use errs::GenericParamsFromOuterItemLabel as Label;
564 let static_or_const = match def_kind {
565 DefKind::Static { .. } => {
566 Some(errs::GenericParamsFromOuterItemStaticOrConst::Static)
567 }
568 DefKind::Const => Some(errs::GenericParamsFromOuterItemStaticOrConst::Const),
569 _ => None,
570 };
571 let is_self =
572 matches!(outer_res, Res::SelfTyParam { .. } | Res::SelfTyAlias { .. });
573 let mut err = errs::GenericParamsFromOuterItem {
574 span,
575 label: None,
576 refer_to_type_directly: None,
577 sugg: None,
578 static_or_const,
579 is_self,
580 };
581
582 let sm = self.tcx.sess.source_map();
583 let def_id = match outer_res {
584 Res::SelfTyParam { .. } => {
585 err.label = Some(Label::SelfTyParam(span));
586 return self.dcx().create_err(err);
587 }
588 Res::SelfTyAlias { alias_to: def_id, .. } => {
589 err.label = Some(Label::SelfTyAlias(reduce_impl_span_to_impl_keyword(
590 sm,
591 self.def_span(def_id),
592 )));
593 err.refer_to_type_directly = Some(span);
594 return self.dcx().create_err(err);
595 }
596 Res::Def(DefKind::TyParam, def_id) => {
597 err.label = Some(Label::TyParam(self.def_span(def_id)));
598 def_id
599 }
600 Res::Def(DefKind::ConstParam, def_id) => {
601 err.label = Some(Label::ConstParam(self.def_span(def_id)));
602 def_id
603 }
604 _ => {
605 bug!(
606 "GenericParamsFromOuterItem should only be used with \
607 Res::SelfTyParam, Res::SelfTyAlias, DefKind::TyParam or \
608 DefKind::ConstParam"
609 );
610 }
611 };
612
613 if let HasGenericParams::Yes(span) = has_generic_params {
614 let name = self.tcx.item_name(def_id);
615 let (span, snippet) = if span.is_empty() {
616 let snippet = format!("<{name}>");
617 (span, snippet)
618 } else {
619 let span = sm.span_through_char(span, '<').shrink_to_hi();
620 let snippet = format!("{name}, ");
621 (span, snippet)
622 };
623 err.sugg = Some(errs::GenericParamsFromOuterItemSugg { span, snippet });
624 }
625
626 self.dcx().create_err(err)
627 }
628 ResolutionError::NameAlreadyUsedInParameterList(name, first_use_span) => self
629 .dcx()
630 .create_err(errs::NameAlreadyUsedInParameterList { span, first_use_span, name }),
631 ResolutionError::MethodNotMemberOfTrait(method, trait_, candidate) => {
632 self.dcx().create_err(errs::MethodNotMemberOfTrait {
633 span,
634 method,
635 trait_,
636 sub: candidate.map(|c| errs::AssociatedFnWithSimilarNameExists {
637 span: method.span,
638 candidate: c,
639 }),
640 })
641 }
642 ResolutionError::TypeNotMemberOfTrait(type_, trait_, candidate) => {
643 self.dcx().create_err(errs::TypeNotMemberOfTrait {
644 span,
645 type_,
646 trait_,
647 sub: candidate.map(|c| errs::AssociatedTypeWithSimilarNameExists {
648 span: type_.span,
649 candidate: c,
650 }),
651 })
652 }
653 ResolutionError::ConstNotMemberOfTrait(const_, trait_, candidate) => {
654 self.dcx().create_err(errs::ConstNotMemberOfTrait {
655 span,
656 const_,
657 trait_,
658 sub: candidate.map(|c| errs::AssociatedConstWithSimilarNameExists {
659 span: const_.span,
660 candidate: c,
661 }),
662 })
663 }
664 ResolutionError::VariableNotBoundInPattern(binding_error, parent_scope) => {
665 let BindingError { name, target, origin, could_be_path } = binding_error;
666
667 let target_sp = target.iter().copied().collect::<Vec<_>>();
668 let origin_sp = origin.iter().copied().collect::<Vec<_>>();
669
670 let msp = MultiSpan::from_spans(target_sp.clone());
671 let mut err = self
672 .dcx()
673 .create_err(errors::VariableIsNotBoundInAllPatterns { multispan: msp, name });
674 for sp in target_sp {
675 err.subdiagnostic(errors::PatternDoesntBindName { span: sp, name });
676 }
677 for sp in origin_sp {
678 err.subdiagnostic(errors::VariableNotInAllPatterns { span: sp });
679 }
680 if could_be_path {
681 let import_suggestions = self.lookup_import_candidates(
682 name,
683 Namespace::ValueNS,
684 &parent_scope,
685 &|res: Res| {
686 matches!(
687 res,
688 Res::Def(
689 DefKind::Ctor(CtorOf::Variant, CtorKind::Const)
690 | DefKind::Ctor(CtorOf::Struct, CtorKind::Const)
691 | DefKind::Const
692 | DefKind::AssocConst,
693 _,
694 )
695 )
696 },
697 );
698
699 if import_suggestions.is_empty() {
700 let help_msg = format!(
701 "if you meant to match on a variant or a `const` item, consider \
702 making the path in the pattern qualified: `path::to::ModOrType::{name}`",
703 );
704 err.span_help(span, help_msg);
705 }
706 show_candidates(
707 self.tcx,
708 &mut err,
709 Some(span),
710 &import_suggestions,
711 Instead::No,
712 FoundUse::Yes,
713 DiagMode::Pattern,
714 vec![],
715 "",
716 );
717 }
718 err
719 }
720 ResolutionError::VariableBoundWithDifferentMode(variable_name, first_binding_span) => {
721 self.dcx().create_err(errs::VariableBoundWithDifferentMode {
722 span,
723 first_binding_span,
724 variable_name,
725 })
726 }
727 ResolutionError::IdentifierBoundMoreThanOnceInParameterList(identifier) => self
728 .dcx()
729 .create_err(errs::IdentifierBoundMoreThanOnceInParameterList { span, identifier }),
730 ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(identifier) => self
731 .dcx()
732 .create_err(errs::IdentifierBoundMoreThanOnceInSamePattern { span, identifier }),
733 ResolutionError::UndeclaredLabel { name, suggestion } => {
734 let ((sub_reachable, sub_reachable_suggestion), sub_unreachable) = match suggestion
735 {
736 Some((ident, true)) => (
738 (
739 Some(errs::LabelWithSimilarNameReachable(ident.span)),
740 Some(errs::TryUsingSimilarlyNamedLabel {
741 span,
742 ident_name: ident.name,
743 }),
744 ),
745 None,
746 ),
747 Some((ident, false)) => (
749 (None, None),
750 Some(errs::UnreachableLabelWithSimilarNameExists {
751 ident_span: ident.span,
752 }),
753 ),
754 None => ((None, None), None),
756 };
757 self.dcx().create_err(errs::UndeclaredLabel {
758 span,
759 name,
760 sub_reachable,
761 sub_reachable_suggestion,
762 sub_unreachable,
763 })
764 }
765 ResolutionError::SelfImportsOnlyAllowedWithin { root, span_with_rename } => {
766 let (suggestion, mpart_suggestion) = if root {
768 (None, None)
769 } else {
770 let suggestion = errs::SelfImportsOnlyAllowedWithinSuggestion { span };
773
774 let mpart_suggestion = errs::SelfImportsOnlyAllowedWithinMultipartSuggestion {
777 multipart_start: span_with_rename.shrink_to_lo(),
778 multipart_end: span_with_rename.shrink_to_hi(),
779 };
780 (Some(suggestion), Some(mpart_suggestion))
781 };
782 self.dcx().create_err(errs::SelfImportsOnlyAllowedWithin {
783 span,
784 suggestion,
785 mpart_suggestion,
786 })
787 }
788 ResolutionError::SelfImportCanOnlyAppearOnceInTheList => {
789 self.dcx().create_err(errs::SelfImportCanOnlyAppearOnceInTheList { span })
790 }
791 ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
792 self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
793 }
794 ResolutionError::FailedToResolve { segment, label, suggestion, module } => {
795 let mut err =
796 struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {label}");
797 err.span_label(span, label);
798
799 if let Some((suggestions, msg, applicability)) = suggestion {
800 if suggestions.is_empty() {
801 err.help(msg);
802 return err;
803 }
804 err.multipart_suggestion(msg, suggestions, applicability);
805 }
806 if let Some(ModuleOrUniformRoot::Module(module)) = module
807 && let Some(module) = module.opt_def_id()
808 && let Some(segment) = segment
809 {
810 self.find_cfg_stripped(&mut err, &segment, module);
811 }
812
813 err
814 }
815 ResolutionError::CannotCaptureDynamicEnvironmentInFnItem => {
816 self.dcx().create_err(errs::CannotCaptureDynamicEnvironmentInFnItem { span })
817 }
818 ResolutionError::AttemptToUseNonConstantValueInConstant {
819 ident,
820 suggestion,
821 current,
822 type_span,
823 } => {
824 let sp = self
833 .tcx
834 .sess
835 .source_map()
836 .span_extend_to_prev_str(ident.span, current, true, false);
837
838 let ((with, with_label), without) = match sp {
839 Some(sp) if !self.tcx.sess.source_map().is_multiline(sp) => {
840 let sp = sp
841 .with_lo(BytePos(sp.lo().0 - (current.len() as u32)))
842 .until(ident.span);
843 (
844 (Some(errs::AttemptToUseNonConstantValueInConstantWithSuggestion {
845 span: sp,
846 suggestion,
847 current,
848 type_span,
849 }), Some(errs::AttemptToUseNonConstantValueInConstantLabelWithSuggestion {span})),
850 None,
851 )
852 }
853 _ => (
854 (None, None),
855 Some(errs::AttemptToUseNonConstantValueInConstantWithoutSuggestion {
856 ident_span: ident.span,
857 suggestion,
858 }),
859 ),
860 };
861
862 self.dcx().create_err(errs::AttemptToUseNonConstantValueInConstant {
863 span,
864 with,
865 with_label,
866 without,
867 })
868 }
869 ResolutionError::BindingShadowsSomethingUnacceptable {
870 shadowing_binding,
871 name,
872 participle,
873 article,
874 shadowed_binding,
875 shadowed_binding_span,
876 } => self.dcx().create_err(errs::BindingShadowsSomethingUnacceptable {
877 span,
878 shadowing_binding,
879 shadowed_binding,
880 article,
881 sub_suggestion: match (shadowing_binding, shadowed_binding) {
882 (
883 PatternSource::Match,
884 Res::Def(DefKind::Ctor(CtorOf::Variant | CtorOf::Struct, CtorKind::Fn), _),
885 ) => Some(errs::BindingShadowsSomethingUnacceptableSuggestion { span, name }),
886 _ => None,
887 },
888 shadowed_binding_span,
889 participle,
890 name,
891 }),
892 ResolutionError::ForwardDeclaredGenericParam(param, reason) => match reason {
893 ForwardGenericParamBanReason::Default => {
894 self.dcx().create_err(errs::ForwardDeclaredGenericParam { param, span })
895 }
896 ForwardGenericParamBanReason::ConstParamTy => self
897 .dcx()
898 .create_err(errs::ForwardDeclaredGenericInConstParamTy { param, span }),
899 },
900 ResolutionError::ParamInTyOfConstParam { name } => {
901 self.dcx().create_err(errs::ParamInTyOfConstParam { span, name })
902 }
903 ResolutionError::ParamInNonTrivialAnonConst { name, param_kind: is_type } => {
904 self.dcx().create_err(errs::ParamInNonTrivialAnonConst {
905 span,
906 name,
907 param_kind: is_type,
908 help: self
909 .tcx
910 .sess
911 .is_nightly_build()
912 .then_some(errs::ParamInNonTrivialAnonConstHelp),
913 })
914 }
915 ResolutionError::ParamInEnumDiscriminant { name, param_kind: is_type } => self
916 .dcx()
917 .create_err(errs::ParamInEnumDiscriminant { span, name, param_kind: is_type }),
918 ResolutionError::ForwardDeclaredSelf(reason) => match reason {
919 ForwardGenericParamBanReason::Default => {
920 self.dcx().create_err(errs::SelfInGenericParamDefault { span })
921 }
922 ForwardGenericParamBanReason::ConstParamTy => {
923 self.dcx().create_err(errs::SelfInConstGenericTy { span })
924 }
925 },
926 ResolutionError::UnreachableLabel { name, definition_span, suggestion } => {
927 let ((sub_suggestion_label, sub_suggestion), sub_unreachable_label) =
928 match suggestion {
929 Some((ident, true)) => (
931 (
932 Some(errs::UnreachableLabelSubLabel { ident_span: ident.span }),
933 Some(errs::UnreachableLabelSubSuggestion {
934 span,
935 ident_name: ident.name,
938 }),
939 ),
940 None,
941 ),
942 Some((ident, false)) => (
944 (None, None),
945 Some(errs::UnreachableLabelSubLabelUnreachable {
946 ident_span: ident.span,
947 }),
948 ),
949 None => ((None, None), None),
951 };
952 self.dcx().create_err(errs::UnreachableLabel {
953 span,
954 name,
955 definition_span,
956 sub_suggestion,
957 sub_suggestion_label,
958 sub_unreachable_label,
959 })
960 }
961 ResolutionError::TraitImplMismatch {
962 name,
963 kind,
964 code,
965 trait_item_span,
966 trait_path,
967 } => self
968 .dcx()
969 .create_err(errors::TraitImplMismatch {
970 span,
971 name,
972 kind,
973 trait_path,
974 trait_item_span,
975 })
976 .with_code(code),
977 ResolutionError::TraitImplDuplicate { name, trait_item_span, old_span } => self
978 .dcx()
979 .create_err(errs::TraitImplDuplicate { span, name, trait_item_span, old_span }),
980 ResolutionError::InvalidAsmSym => self.dcx().create_err(errs::InvalidAsmSym { span }),
981 ResolutionError::LowercaseSelf => self.dcx().create_err(errs::LowercaseSelf { span }),
982 ResolutionError::BindingInNeverPattern => {
983 self.dcx().create_err(errs::BindingInNeverPattern { span })
984 }
985 }
986 }
987
988 pub(crate) fn report_vis_error(
989 &mut self,
990 vis_resolution_error: VisResolutionError<'_>,
991 ) -> ErrorGuaranteed {
992 match vis_resolution_error {
993 VisResolutionError::Relative2018(span, path) => {
994 self.dcx().create_err(errs::Relative2018 {
995 span,
996 path_span: path.span,
997 path_str: pprust::path_to_string(path),
1000 })
1001 }
1002 VisResolutionError::AncestorOnly(span) => {
1003 self.dcx().create_err(errs::AncestorOnly(span))
1004 }
1005 VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error(
1006 span,
1007 ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None },
1008 ),
1009 VisResolutionError::ExpectedFound(span, path_str, res) => {
1010 self.dcx().create_err(errs::ExpectedModuleFound { span, res, path_str })
1011 }
1012 VisResolutionError::Indeterminate(span) => {
1013 self.dcx().create_err(errs::Indeterminate(span))
1014 }
1015 VisResolutionError::ModuleOnly(span) => self.dcx().create_err(errs::ModuleOnly(span)),
1016 }
1017 .emit()
1018 }
1019
1020 fn early_lookup_typo_candidate(
1022 &mut self,
1023 scope_set: ScopeSet<'ra>,
1024 parent_scope: &ParentScope<'ra>,
1025 ident: Ident,
1026 filter_fn: &impl Fn(Res) -> bool,
1027 ) -> Option<TypoSuggestion> {
1028 let mut suggestions = Vec::new();
1029 let ctxt = ident.span.ctxt();
1030 self.visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
1031 match scope {
1032 Scope::DeriveHelpers(expn_id) => {
1033 let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
1034 if filter_fn(res) {
1035 suggestions.extend(
1036 this.helper_attrs
1037 .get(&expn_id)
1038 .into_iter()
1039 .flatten()
1040 .map(|(ident, _)| TypoSuggestion::typo_from_ident(*ident, res)),
1041 );
1042 }
1043 }
1044 Scope::DeriveHelpersCompat => {
1045 let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
1046 if filter_fn(res) {
1047 for derive in parent_scope.derives {
1048 let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
1049 let Ok((Some(ext), _)) = this.resolve_macro_path(
1050 derive,
1051 Some(MacroKind::Derive),
1052 parent_scope,
1053 false,
1054 false,
1055 None,
1056 None,
1057 ) else {
1058 continue;
1059 };
1060 suggestions.extend(
1061 ext.helper_attrs
1062 .iter()
1063 .map(|name| TypoSuggestion::typo_from_name(*name, res)),
1064 );
1065 }
1066 }
1067 }
1068 Scope::MacroRules(macro_rules_scope) => {
1069 if let MacroRulesScope::Binding(macro_rules_binding) = macro_rules_scope.get() {
1070 let res = macro_rules_binding.binding.res();
1071 if filter_fn(res) {
1072 suggestions.push(TypoSuggestion::typo_from_ident(
1073 macro_rules_binding.ident,
1074 res,
1075 ))
1076 }
1077 }
1078 }
1079 Scope::CrateRoot => {
1080 let root_ident = Ident::new(kw::PathRoot, ident.span);
1081 let root_module = this.resolve_crate_root(root_ident);
1082 this.add_module_candidates(root_module, &mut suggestions, filter_fn, None);
1083 }
1084 Scope::Module(module, _) => {
1085 this.add_module_candidates(module, &mut suggestions, filter_fn, None);
1086 }
1087 Scope::MacroUsePrelude => {
1088 suggestions.extend(this.macro_use_prelude.iter().filter_map(
1089 |(name, binding)| {
1090 let res = binding.res();
1091 filter_fn(res).then_some(TypoSuggestion::typo_from_name(*name, res))
1092 },
1093 ));
1094 }
1095 Scope::BuiltinAttrs => {
1096 let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(sym::dummy));
1097 if filter_fn(res) {
1098 suggestions.extend(
1099 BUILTIN_ATTRIBUTES
1100 .iter()
1101 .map(|attr| TypoSuggestion::typo_from_name(attr.name, res)),
1102 );
1103 }
1104 }
1105 Scope::ExternPrelude => {
1106 suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
1107 let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
1108 filter_fn(res).then_some(TypoSuggestion::typo_from_ident(*ident, res))
1109 }));
1110 }
1111 Scope::ToolPrelude => {
1112 let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1113 suggestions.extend(
1114 this.registered_tools
1115 .iter()
1116 .map(|ident| TypoSuggestion::typo_from_ident(*ident, res)),
1117 );
1118 }
1119 Scope::StdLibPrelude => {
1120 if let Some(prelude) = this.prelude {
1121 let mut tmp_suggestions = Vec::new();
1122 this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn, None);
1123 suggestions.extend(
1124 tmp_suggestions
1125 .into_iter()
1126 .filter(|s| use_prelude.into() || this.is_builtin_macro(s.res)),
1127 );
1128 }
1129 }
1130 Scope::BuiltinTypes => {
1131 suggestions.extend(PrimTy::ALL.iter().filter_map(|prim_ty| {
1132 let res = Res::PrimTy(*prim_ty);
1133 filter_fn(res)
1134 .then_some(TypoSuggestion::typo_from_name(prim_ty.name(), res))
1135 }))
1136 }
1137 }
1138
1139 None::<()>
1140 });
1141
1142 suggestions.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str()));
1144
1145 match find_best_match_for_name(
1146 &suggestions.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),
1147 ident.name,
1148 None,
1149 ) {
1150 Some(found) if found != ident.name => {
1151 suggestions.into_iter().find(|suggestion| suggestion.candidate == found)
1152 }
1153 _ => None,
1154 }
1155 }
1156
1157 fn lookup_import_candidates_from_module<FilterFn>(
1158 &mut self,
1159 lookup_ident: Ident,
1160 namespace: Namespace,
1161 parent_scope: &ParentScope<'ra>,
1162 start_module: Module<'ra>,
1163 crate_path: ThinVec<ast::PathSegment>,
1164 filter_fn: FilterFn,
1165 ) -> Vec<ImportSuggestion>
1166 where
1167 FilterFn: Fn(Res) -> bool,
1168 {
1169 let mut candidates = Vec::new();
1170 let mut seen_modules = FxHashSet::default();
1171 let start_did = start_module.def_id();
1172 let mut worklist = vec![(
1173 start_module,
1174 ThinVec::<ast::PathSegment>::new(),
1175 true,
1176 start_did.is_local() || !self.tcx.is_doc_hidden(start_did),
1177 true,
1178 )];
1179 let mut worklist_via_import = vec![];
1180
1181 while let Some((in_module, path_segments, accessible, doc_visible, is_stable)) =
1182 match worklist.pop() {
1183 None => worklist_via_import.pop(),
1184 Some(x) => Some(x),
1185 }
1186 {
1187 let in_module_is_extern = !in_module.def_id().is_local();
1188 in_module.for_each_child(self, |this, ident, ns, name_binding| {
1189 if name_binding.is_assoc_item()
1191 && !this.tcx.features().import_trait_associated_functions()
1192 {
1193 return;
1194 }
1195
1196 if ident.name == kw::Underscore {
1197 return;
1198 }
1199
1200 let child_accessible =
1201 accessible && this.is_accessible_from(name_binding.vis, parent_scope.module);
1202
1203 if in_module_is_extern && !child_accessible {
1205 return;
1206 }
1207
1208 let via_import = name_binding.is_import() && !name_binding.is_extern_crate();
1209
1210 if via_import && name_binding.is_possibly_imported_variant() {
1216 return;
1217 }
1218
1219 if let NameBindingKind::Import { binding, .. } = name_binding.kind
1221 && this.is_accessible_from(binding.vis, parent_scope.module)
1222 && !this.is_accessible_from(name_binding.vis, parent_scope.module)
1223 {
1224 return;
1225 }
1226
1227 let res = name_binding.res();
1228 let did = match res {
1229 Res::Def(DefKind::Ctor(..), did) => this.tcx.opt_parent(did),
1230 _ => res.opt_def_id(),
1231 };
1232 let child_doc_visible = doc_visible
1233 && did.is_none_or(|did| did.is_local() || !this.tcx.is_doc_hidden(did));
1234
1235 if ident.name == lookup_ident.name
1239 && ns == namespace
1240 && in_module != parent_scope.module
1241 && !ident.span.normalize_to_macros_2_0().from_expansion()
1242 && filter_fn(res)
1243 {
1244 let mut segms = if lookup_ident.span.at_least_rust_2018() {
1246 crate_path.clone()
1249 } else {
1250 ThinVec::new()
1251 };
1252 segms.append(&mut path_segments.clone());
1253
1254 segms.push(ast::PathSegment::from_ident(ident));
1255 let path = Path { span: name_binding.span, segments: segms, tokens: None };
1256
1257 if child_accessible
1258 && let Some(idx) = candidates
1260 .iter()
1261 .position(|v: &ImportSuggestion| v.did == did && !v.accessible)
1262 {
1263 candidates.remove(idx);
1264 }
1265
1266 let is_stable = if is_stable
1267 && let Some(did) = did
1268 && this.is_stable(did, path.span)
1269 {
1270 true
1271 } else {
1272 false
1273 };
1274
1275 if is_stable
1280 && let Some(idx) = candidates
1281 .iter()
1282 .position(|v: &ImportSuggestion| v.did == did && !v.is_stable)
1283 {
1284 candidates.remove(idx);
1285 }
1286
1287 if candidates.iter().all(|v: &ImportSuggestion| v.did != did) {
1288 let note = if let Some(did) = did {
1291 let requires_note = !did.is_local()
1292 && this.tcx.get_attrs(did, sym::rustc_diagnostic_item).any(
1293 |attr| {
1294 [sym::TryInto, sym::TryFrom, sym::FromIterator]
1295 .map(|x| Some(x))
1296 .contains(&attr.value_str())
1297 },
1298 );
1299
1300 requires_note.then(|| {
1301 format!(
1302 "'{}' is included in the prelude starting in Edition 2021",
1303 path_names_to_string(&path)
1304 )
1305 })
1306 } else {
1307 None
1308 };
1309
1310 candidates.push(ImportSuggestion {
1311 did,
1312 descr: res.descr(),
1313 path,
1314 accessible: child_accessible,
1315 doc_visible: child_doc_visible,
1316 note,
1317 via_import,
1318 is_stable,
1319 });
1320 }
1321 }
1322
1323 if let Some(def_id) = name_binding.res().module_like_def_id() {
1325 let mut path_segments = path_segments.clone();
1327 path_segments.push(ast::PathSegment::from_ident(ident));
1328
1329 let alias_import = if let NameBindingKind::Import { import, .. } =
1330 name_binding.kind
1331 && let ImportKind::ExternCrate { source: Some(_), .. } = import.kind
1332 && import.parent_scope.expansion == parent_scope.expansion
1333 {
1334 true
1335 } else {
1336 false
1337 };
1338
1339 let is_extern_crate_that_also_appears_in_prelude =
1340 name_binding.is_extern_crate() && lookup_ident.span.at_least_rust_2018();
1341
1342 if !is_extern_crate_that_also_appears_in_prelude || alias_import {
1343 if seen_modules.insert(def_id) {
1345 if via_import { &mut worklist_via_import } else { &mut worklist }.push(
1346 (
1347 this.expect_module(def_id),
1348 path_segments,
1349 child_accessible,
1350 child_doc_visible,
1351 is_stable && this.is_stable(def_id, name_binding.span),
1352 ),
1353 );
1354 }
1355 }
1356 }
1357 })
1358 }
1359
1360 candidates
1361 }
1362
1363 fn is_stable(&self, did: DefId, span: Span) -> bool {
1364 if did.is_local() {
1365 return true;
1366 }
1367
1368 match self.tcx.lookup_stability(did) {
1369 Some(Stability {
1370 level: attr::StabilityLevel::Unstable { implied_by, .. },
1371 feature,
1372 ..
1373 }) => {
1374 if span.allows_unstable(feature) {
1375 true
1376 } else if self.tcx.features().enabled(feature) {
1377 true
1378 } else if let Some(implied_by) = implied_by
1379 && self.tcx.features().enabled(implied_by)
1380 {
1381 true
1382 } else {
1383 false
1384 }
1385 }
1386 Some(_) => true,
1387 None => false,
1388 }
1389 }
1390
1391 pub(crate) fn lookup_import_candidates<FilterFn>(
1399 &mut self,
1400 lookup_ident: Ident,
1401 namespace: Namespace,
1402 parent_scope: &ParentScope<'ra>,
1403 filter_fn: FilterFn,
1404 ) -> Vec<ImportSuggestion>
1405 where
1406 FilterFn: Fn(Res) -> bool,
1407 {
1408 let crate_path = thin_vec![ast::PathSegment::from_ident(Ident::with_dummy_span(kw::Crate))];
1409 let mut suggestions = self.lookup_import_candidates_from_module(
1410 lookup_ident,
1411 namespace,
1412 parent_scope,
1413 self.graph_root,
1414 crate_path,
1415 &filter_fn,
1416 );
1417
1418 if lookup_ident.span.at_least_rust_2018() {
1419 for ident in self.extern_prelude.clone().into_keys() {
1420 if ident.span.from_expansion() {
1421 continue;
1427 }
1428 let Some(crate_id) =
1429 self.cstore_mut().maybe_process_path_extern(self.tcx, ident.name)
1430 else {
1431 continue;
1432 };
1433
1434 let crate_def_id = crate_id.as_def_id();
1435 let crate_root = self.expect_module(crate_def_id);
1436
1437 let needs_disambiguation =
1441 self.resolutions(parent_scope.module).borrow().iter().any(
1442 |(key, name_resolution)| {
1443 if key.ns == TypeNS
1444 && key.ident == ident
1445 && let Some(binding) = name_resolution.borrow().best_binding()
1446 {
1447 match binding.res() {
1448 Res::Def(_, def_id) => def_id != crate_def_id,
1451 Res::PrimTy(_) => true,
1452 _ => false,
1453 }
1454 } else {
1455 false
1456 }
1457 },
1458 );
1459 let mut crate_path = ThinVec::new();
1460 if needs_disambiguation {
1461 crate_path.push(ast::PathSegment::path_root(rustc_span::DUMMY_SP));
1462 }
1463 crate_path.push(ast::PathSegment::from_ident(ident));
1464
1465 suggestions.extend(self.lookup_import_candidates_from_module(
1466 lookup_ident,
1467 namespace,
1468 parent_scope,
1469 crate_root,
1470 crate_path,
1471 &filter_fn,
1472 ));
1473 }
1474 }
1475
1476 suggestions
1477 }
1478
1479 pub(crate) fn unresolved_macro_suggestions(
1480 &mut self,
1481 err: &mut Diag<'_>,
1482 macro_kind: MacroKind,
1483 parent_scope: &ParentScope<'ra>,
1484 ident: Ident,
1485 krate: &Crate,
1486 sugg_span: Option<Span>,
1487 ) {
1488 self.visit_scopes(
1491 ScopeSet::Macro(MacroKind::Derive),
1492 &parent_scope,
1493 ident.span.ctxt(),
1494 |this, scope, _use_prelude, _ctxt| {
1495 let Scope::Module(m, _) = scope else {
1496 return None;
1497 };
1498 for (_, resolution) in this.resolutions(m).borrow().iter() {
1499 let Some(binding) = resolution.borrow().best_binding() else {
1500 continue;
1501 };
1502 let Res::Def(DefKind::Macro(MacroKind::Derive | MacroKind::Attr), def_id) =
1503 binding.res()
1504 else {
1505 continue;
1506 };
1507 let _ = this.get_macro_by_def_id(def_id);
1510 }
1511 None::<()>
1512 },
1513 );
1514
1515 let is_expected = &|res: Res| res.macro_kind() == Some(macro_kind);
1516 let suggestion = self.early_lookup_typo_candidate(
1517 ScopeSet::Macro(macro_kind),
1518 parent_scope,
1519 ident,
1520 is_expected,
1521 );
1522 if !self.add_typo_suggestion(err, suggestion, ident.span) {
1523 self.detect_derive_attribute(err, ident, parent_scope, sugg_span);
1524 }
1525
1526 let import_suggestions =
1527 self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected);
1528 let (span, found_use) = match parent_scope.module.nearest_parent_mod().as_local() {
1529 Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id)),
1530 None => (None, FoundUse::No),
1531 };
1532 show_candidates(
1533 self.tcx,
1534 err,
1535 span,
1536 &import_suggestions,
1537 Instead::No,
1538 found_use,
1539 DiagMode::Normal,
1540 vec![],
1541 "",
1542 );
1543
1544 if macro_kind == MacroKind::Bang && ident.name == sym::macro_rules {
1545 let label_span = ident.span.shrink_to_hi();
1546 let mut spans = MultiSpan::from_span(label_span);
1547 spans.push_span_label(label_span, "put a macro name here");
1548 err.subdiagnostic(MaybeMissingMacroRulesName { spans });
1549 return;
1550 }
1551
1552 if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
1553 err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
1554 return;
1555 }
1556
1557 let unused_macro = self.unused_macros.iter().find_map(|(def_id, (_, unused_ident))| {
1558 if unused_ident.name == ident.name { Some((def_id, unused_ident)) } else { None }
1559 });
1560
1561 if let Some((def_id, unused_ident)) = unused_macro {
1562 let scope = self.local_macro_def_scopes[&def_id];
1563 let parent_nearest = parent_scope.module.nearest_parent_mod();
1564 if Some(parent_nearest) == scope.opt_def_id() {
1565 match macro_kind {
1566 MacroKind::Bang => {
1567 err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
1568 err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
1569 }
1570 MacroKind::Attr => {
1571 err.subdiagnostic(MacroRulesNot::Attr { span: unused_ident.span, ident });
1572 }
1573 MacroKind::Derive => {
1574 err.subdiagnostic(MacroRulesNot::Derive { span: unused_ident.span, ident });
1575 }
1576 }
1577
1578 return;
1579 }
1580 }
1581
1582 if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
1583 err.subdiagnostic(AddedMacroUse);
1584 return;
1585 }
1586
1587 if ident.name == kw::Default
1588 && let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
1589 {
1590 let span = self.def_span(def_id);
1591 let source_map = self.tcx.sess.source_map();
1592 let head_span = source_map.guess_head_span(span);
1593 err.subdiagnostic(ConsiderAddingADerive {
1594 span: head_span.shrink_to_lo(),
1595 suggestion: "#[derive(Default)]\n".to_string(),
1596 });
1597 }
1598 for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
1599 let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
1600 ident,
1601 ScopeSet::All(ns),
1602 parent_scope,
1603 None,
1604 false,
1605 None,
1606 None,
1607 ) else {
1608 continue;
1609 };
1610
1611 let desc = match binding.res() {
1612 Res::Def(DefKind::Macro(MacroKind::Bang), _) => "a function-like macro".to_string(),
1613 Res::Def(DefKind::Macro(MacroKind::Attr), _) | Res::NonMacroAttr(..) => {
1614 format!("an attribute: `#[{ident}]`")
1615 }
1616 Res::Def(DefKind::Macro(MacroKind::Derive), _) => {
1617 format!("a derive macro: `#[derive({ident})]`")
1618 }
1619 Res::ToolMod => {
1620 continue;
1622 }
1623 Res::Def(DefKind::Trait, _) if macro_kind == MacroKind::Derive => {
1624 "only a trait, without a derive macro".to_string()
1625 }
1626 res => format!(
1627 "{} {}, not {} {}",
1628 res.article(),
1629 res.descr(),
1630 macro_kind.article(),
1631 macro_kind.descr_expected(),
1632 ),
1633 };
1634 if let crate::NameBindingKind::Import { import, .. } = binding.kind
1635 && !import.span.is_dummy()
1636 {
1637 let note = errors::IdentImporterHereButItIsDesc {
1638 span: import.span,
1639 imported_ident: ident,
1640 imported_ident_desc: &desc,
1641 };
1642 err.subdiagnostic(note);
1643 self.record_use(ident, binding, Used::Other);
1646 return;
1647 }
1648 let note = errors::IdentInScopeButItIsDesc {
1649 imported_ident: ident,
1650 imported_ident_desc: &desc,
1651 };
1652 err.subdiagnostic(note);
1653 return;
1654 }
1655 }
1656
1657 fn detect_derive_attribute(
1660 &self,
1661 err: &mut Diag<'_>,
1662 ident: Ident,
1663 parent_scope: &ParentScope<'ra>,
1664 sugg_span: Option<Span>,
1665 ) {
1666 let mut derives = vec![];
1671 let mut all_attrs: UnordMap<Symbol, Vec<_>> = UnordMap::default();
1672 #[allow(rustc::potential_query_instability)]
1674 for (def_id, data) in self
1675 .local_macro_map
1676 .iter()
1677 .map(|(local_id, data)| (local_id.to_def_id(), data))
1678 .chain(self.extern_macro_map.borrow().iter().map(|(id, d)| (*id, d)))
1679 {
1680 for helper_attr in &data.ext.helper_attrs {
1681 let item_name = self.tcx.item_name(def_id);
1682 all_attrs.entry(*helper_attr).or_default().push(item_name);
1683 if helper_attr == &ident.name {
1684 derives.push(item_name);
1685 }
1686 }
1687 }
1688 let kind = MacroKind::Derive.descr();
1689 if !derives.is_empty() {
1690 let mut derives: Vec<String> = derives.into_iter().map(|d| d.to_string()).collect();
1692 derives.sort();
1693 derives.dedup();
1694 let msg = match &derives[..] {
1695 [derive] => format!(" `{derive}`"),
1696 [start @ .., last] => format!(
1697 "s {} and `{last}`",
1698 start.iter().map(|d| format!("`{d}`")).collect::<Vec<_>>().join(", ")
1699 ),
1700 [] => unreachable!("we checked for this to be non-empty 10 lines above!?"),
1701 };
1702 let msg = format!(
1703 "`{}` is an attribute that can be used by the {kind}{msg}, you might be \
1704 missing a `derive` attribute",
1705 ident.name,
1706 );
1707 let sugg_span = if let ModuleKind::Def(DefKind::Enum, id, _) = parent_scope.module.kind
1708 {
1709 let span = self.def_span(id);
1710 if span.from_expansion() {
1711 None
1712 } else {
1713 Some(span.shrink_to_lo())
1715 }
1716 } else {
1717 sugg_span
1719 };
1720 match sugg_span {
1721 Some(span) => {
1722 err.span_suggestion_verbose(
1723 span,
1724 msg,
1725 format!("#[derive({})]\n", derives.join(", ")),
1726 Applicability::MaybeIncorrect,
1727 );
1728 }
1729 None => {
1730 err.note(msg);
1731 }
1732 }
1733 } else {
1734 let all_attr_names = all_attrs.keys().map(|s| *s).into_sorted_stable_ord();
1736 if let Some(best_match) = find_best_match_for_name(&all_attr_names, ident.name, None)
1737 && let Some(macros) = all_attrs.get(&best_match)
1738 {
1739 let mut macros: Vec<String> = macros.into_iter().map(|d| d.to_string()).collect();
1740 macros.sort();
1741 macros.dedup();
1742 let msg = match ¯os[..] {
1743 [] => return,
1744 [name] => format!(" `{name}` accepts"),
1745 [start @ .., end] => format!(
1746 "s {} and `{end}` accept",
1747 start.iter().map(|m| format!("`{m}`")).collect::<Vec<_>>().join(", "),
1748 ),
1749 };
1750 let msg = format!("the {kind}{msg} the similarly named `{best_match}` attribute");
1751 err.span_suggestion_verbose(
1752 ident.span,
1753 msg,
1754 best_match,
1755 Applicability::MaybeIncorrect,
1756 );
1757 }
1758 }
1759 }
1760
1761 pub(crate) fn add_typo_suggestion(
1762 &self,
1763 err: &mut Diag<'_>,
1764 suggestion: Option<TypoSuggestion>,
1765 span: Span,
1766 ) -> bool {
1767 let suggestion = match suggestion {
1768 None => return false,
1769 Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
1771 Some(suggestion) => suggestion,
1772 };
1773
1774 let mut did_label_def_span = false;
1775
1776 if let Some(def_span) = suggestion.res.opt_def_id().map(|def_id| self.def_span(def_id)) {
1777 if span.overlaps(def_span) {
1778 return false;
1797 }
1798 let span = self.tcx.sess.source_map().guess_head_span(def_span);
1799 let candidate_descr = suggestion.res.descr();
1800 let candidate = suggestion.candidate;
1801 let label = match suggestion.target {
1802 SuggestionTarget::SimilarlyNamed => {
1803 errors::DefinedHere::SimilarlyNamed { span, candidate_descr, candidate }
1804 }
1805 SuggestionTarget::SingleItem => {
1806 errors::DefinedHere::SingleItem { span, candidate_descr, candidate }
1807 }
1808 };
1809 did_label_def_span = true;
1810 err.subdiagnostic(label);
1811 }
1812
1813 let (span, msg, sugg) = if let SuggestionTarget::SimilarlyNamed = suggestion.target
1814 && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
1815 && let Some(span) = suggestion.span
1816 && let Some(candidate) = suggestion.candidate.as_str().strip_prefix('_')
1817 && snippet == candidate
1818 {
1819 let candidate = suggestion.candidate;
1820 let msg = format!(
1823 "the leading underscore in `{candidate}` marks it as unused, consider renaming it to `{snippet}`"
1824 );
1825 if !did_label_def_span {
1826 err.span_label(span, format!("`{candidate}` defined here"));
1827 }
1828 (span, msg, snippet)
1829 } else {
1830 let msg = match suggestion.target {
1831 SuggestionTarget::SimilarlyNamed => format!(
1832 "{} {} with a similar name exists",
1833 suggestion.res.article(),
1834 suggestion.res.descr()
1835 ),
1836 SuggestionTarget::SingleItem => {
1837 format!("maybe you meant this {}", suggestion.res.descr())
1838 }
1839 };
1840 (span, msg, suggestion.candidate.to_ident_string())
1841 };
1842 err.span_suggestion(span, msg, sugg, Applicability::MaybeIncorrect);
1843 true
1844 }
1845
1846 fn binding_description(&self, b: NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
1847 let res = b.res();
1848 if b.span.is_dummy() || !self.tcx.sess.source_map().is_span_accessible(b.span) {
1849 let add_built_in =
1851 !matches!(b.res(), Res::NonMacroAttr(..) | Res::PrimTy(..) | Res::ToolMod);
1852 let (built_in, from) = if from_prelude {
1853 ("", " from prelude")
1854 } else if b.is_extern_crate()
1855 && !b.is_import()
1856 && self.tcx.sess.opts.externs.get(ident.as_str()).is_some()
1857 {
1858 ("", " passed with `--extern`")
1859 } else if add_built_in {
1860 (" built-in", "")
1861 } else {
1862 ("", "")
1863 };
1864
1865 let a = if built_in.is_empty() { res.article() } else { "a" };
1866 format!("{a}{built_in} {thing}{from}", thing = res.descr())
1867 } else {
1868 let introduced = if b.is_import_user_facing() { "imported" } else { "defined" };
1869 format!("the {thing} {introduced} here", thing = res.descr())
1870 }
1871 }
1872
1873 fn ambiguity_diagnostics(&self, ambiguity_error: &AmbiguityError<'_>) -> AmbiguityErrorDiag {
1874 let AmbiguityError { kind, ident, b1, b2, misc1, misc2, .. } = *ambiguity_error;
1875 let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() {
1876 (b2, b1, misc2, misc1, true)
1878 } else {
1879 (b1, b2, misc1, misc2, false)
1880 };
1881 let could_refer_to = |b: NameBinding<'_>, misc: AmbiguityErrorMisc, also: &str| {
1882 let what = self.binding_description(b, ident, misc == AmbiguityErrorMisc::FromPrelude);
1883 let note_msg = format!("`{ident}` could{also} refer to {what}");
1884
1885 let thing = b.res().descr();
1886 let mut help_msgs = Vec::new();
1887 if b.is_glob_import()
1888 && (kind == AmbiguityKind::GlobVsGlob
1889 || kind == AmbiguityKind::GlobVsExpanded
1890 || kind == AmbiguityKind::GlobVsOuter && swapped != also.is_empty())
1891 {
1892 help_msgs.push(format!(
1893 "consider adding an explicit import of `{ident}` to disambiguate"
1894 ))
1895 }
1896 if b.is_extern_crate() && ident.span.at_least_rust_2018() {
1897 help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously"))
1898 }
1899 match misc {
1900 AmbiguityErrorMisc::SuggestCrate => help_msgs
1901 .push(format!("use `crate::{ident}` to refer to this {thing} unambiguously")),
1902 AmbiguityErrorMisc::SuggestSelf => help_msgs
1903 .push(format!("use `self::{ident}` to refer to this {thing} unambiguously")),
1904 AmbiguityErrorMisc::FromPrelude | AmbiguityErrorMisc::None => {}
1905 }
1906
1907 (
1908 b.span,
1909 note_msg,
1910 help_msgs
1911 .iter()
1912 .enumerate()
1913 .map(|(i, help_msg)| {
1914 let or = if i == 0 { "" } else { "or " };
1915 format!("{or}{help_msg}")
1916 })
1917 .collect::<Vec<_>>(),
1918 )
1919 };
1920 let (b1_span, b1_note_msg, b1_help_msgs) = could_refer_to(b1, misc1, "");
1921 let (b2_span, b2_note_msg, b2_help_msgs) = could_refer_to(b2, misc2, " also");
1922
1923 AmbiguityErrorDiag {
1924 msg: format!("`{ident}` is ambiguous"),
1925 span: ident.span,
1926 label_span: ident.span,
1927 label_msg: "ambiguous name".to_string(),
1928 note_msg: format!("ambiguous because of {}", kind.descr()),
1929 b1_span,
1930 b1_note_msg,
1931 b1_help_msgs,
1932 b2_span,
1933 b2_note_msg,
1934 b2_help_msgs,
1935 }
1936 }
1937
1938 fn ctor_fields_span(&self, binding: NameBinding<'_>) -> Option<Span> {
1941 let NameBindingKind::Res(Res::Def(
1942 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn),
1943 ctor_def_id,
1944 )) = binding.kind
1945 else {
1946 return None;
1947 };
1948
1949 let def_id = self.tcx.parent(ctor_def_id);
1950 self.field_idents(def_id)?.iter().map(|&f| f.span).reduce(Span::to) }
1952
1953 fn report_privacy_error(&mut self, privacy_error: &PrivacyError<'ra>) {
1954 let PrivacyError { ident, binding, outermost_res, parent_scope, single_nested, dedup_span } =
1955 *privacy_error;
1956
1957 let res = binding.res();
1958 let ctor_fields_span = self.ctor_fields_span(binding);
1959 let plain_descr = res.descr().to_string();
1960 let nonimport_descr =
1961 if ctor_fields_span.is_some() { plain_descr + " constructor" } else { plain_descr };
1962 let import_descr = nonimport_descr.clone() + " import";
1963 let get_descr =
1964 |b: NameBinding<'_>| if b.is_import() { &import_descr } else { &nonimport_descr };
1965
1966 let ident_descr = get_descr(binding);
1968 let mut err =
1969 self.dcx().create_err(errors::IsPrivate { span: ident.span, ident_descr, ident });
1970
1971 let mut not_publicly_reexported = false;
1972 if let Some((this_res, outer_ident)) = outermost_res {
1973 let import_suggestions = self.lookup_import_candidates(
1974 outer_ident,
1975 this_res.ns().unwrap_or(Namespace::TypeNS),
1976 &parent_scope,
1977 &|res: Res| res == this_res,
1978 );
1979 let point_to_def = !show_candidates(
1980 self.tcx,
1981 &mut err,
1982 Some(dedup_span.until(outer_ident.span.shrink_to_hi())),
1983 &import_suggestions,
1984 Instead::Yes,
1985 FoundUse::Yes,
1986 DiagMode::Import { append: single_nested, unresolved_import: false },
1987 vec![],
1988 "",
1989 );
1990 if point_to_def && ident.span != outer_ident.span {
1992 not_publicly_reexported = true;
1993 let label = errors::OuterIdentIsNotPubliclyReexported {
1994 span: outer_ident.span,
1995 outer_ident_descr: this_res.descr(),
1996 outer_ident,
1997 };
1998 err.subdiagnostic(label);
1999 }
2000 }
2001
2002 let mut non_exhaustive = None;
2003 if let Some(def_id) = res.opt_def_id()
2007 && !def_id.is_local()
2008 && let Some(attr_span) = find_attr!(self.tcx.get_all_attrs(def_id), AttributeKind::NonExhaustive(span) => *span)
2009 {
2010 non_exhaustive = Some(attr_span);
2011 } else if let Some(span) = ctor_fields_span {
2012 let label = errors::ConstructorPrivateIfAnyFieldPrivate { span };
2013 err.subdiagnostic(label);
2014 if let Res::Def(_, d) = res
2015 && let Some(fields) = self.field_visibility_spans.get(&d)
2016 {
2017 let spans = fields.iter().map(|span| *span).collect();
2018 let sugg =
2019 errors::ConsiderMakingTheFieldPublic { spans, number_of_fields: fields.len() };
2020 err.subdiagnostic(sugg);
2021 }
2022 }
2023
2024 let mut sugg_paths: Vec<(Vec<Ident>, bool)> = vec![];
2025 if let Some(mut def_id) = res.opt_def_id() {
2026 let mut path = vec![def_id];
2028 while let Some(parent) = self.tcx.opt_parent(def_id) {
2029 def_id = parent;
2030 if !def_id.is_top_level_module() {
2031 path.push(def_id);
2032 } else {
2033 break;
2034 }
2035 }
2036 let path_names: Option<Vec<Ident>> = path
2038 .iter()
2039 .rev()
2040 .map(|def_id| {
2041 self.tcx.opt_item_name(*def_id).map(|name| {
2042 Ident::with_dummy_span(if def_id.is_top_level_module() {
2043 kw::Crate
2044 } else {
2045 name
2046 })
2047 })
2048 })
2049 .collect();
2050 if let Some(def_id) = path.get(0)
2051 && let Some(path) = path_names
2052 {
2053 if let Some(def_id) = def_id.as_local() {
2054 if self.effective_visibilities.is_directly_public(def_id) {
2055 sugg_paths.push((path, false));
2056 }
2057 } else if self.is_accessible_from(self.tcx.visibility(def_id), parent_scope.module)
2058 {
2059 sugg_paths.push((path, false));
2060 }
2061 }
2062 }
2063
2064 let first_binding = binding;
2066 let mut next_binding = Some(binding);
2067 let mut next_ident = ident;
2068 let mut path = vec![];
2069 while let Some(binding) = next_binding {
2070 let name = next_ident;
2071 next_binding = match binding.kind {
2072 _ if res == Res::Err => None,
2073 NameBindingKind::Import { binding, import, .. } => match import.kind {
2074 _ if binding.span.is_dummy() => None,
2075 ImportKind::Single { source, .. } => {
2076 next_ident = source;
2077 Some(binding)
2078 }
2079 ImportKind::Glob { .. }
2080 | ImportKind::MacroUse { .. }
2081 | ImportKind::MacroExport => Some(binding),
2082 ImportKind::ExternCrate { .. } => None,
2083 },
2084 _ => None,
2085 };
2086
2087 match binding.kind {
2088 NameBindingKind::Import { import, .. } => {
2089 for segment in import.module_path.iter().skip(1) {
2090 path.push(segment.ident);
2091 }
2092 sugg_paths.push((
2093 path.iter().cloned().chain(std::iter::once(ident)).collect::<Vec<_>>(),
2094 true, ));
2096 }
2097 NameBindingKind::Res(_) => {}
2098 }
2099 let first = binding == first_binding;
2100 let def_span = self.tcx.sess.source_map().guess_head_span(binding.span);
2101 let mut note_span = MultiSpan::from_span(def_span);
2102 if !first && binding.vis.is_public() {
2103 let desc = match binding.kind {
2104 NameBindingKind::Import { .. } => "re-export",
2105 _ => "directly",
2106 };
2107 note_span.push_span_label(def_span, format!("you could import this {desc}"));
2108 }
2109 if next_binding.is_none()
2112 && let Some(span) = non_exhaustive
2113 {
2114 note_span.push_span_label(
2115 span,
2116 "cannot be constructed because it is `#[non_exhaustive]`",
2117 );
2118 }
2119 let note = errors::NoteAndRefersToTheItemDefinedHere {
2120 span: note_span,
2121 binding_descr: get_descr(binding),
2122 binding_name: name,
2123 first,
2124 dots: next_binding.is_some(),
2125 };
2126 err.subdiagnostic(note);
2127 }
2128 sugg_paths.sort_by_key(|(p, reexport)| (p.len(), p[0].name == sym::core, *reexport));
2130 for (sugg, reexport) in sugg_paths {
2131 if not_publicly_reexported {
2132 break;
2133 }
2134 if sugg.len() <= 1 {
2135 continue;
2138 }
2139 let path = join_path_idents(sugg);
2140 let sugg = if reexport {
2141 errors::ImportIdent::ThroughReExport { span: dedup_span, ident, path }
2142 } else {
2143 errors::ImportIdent::Directly { span: dedup_span, ident, path }
2144 };
2145 err.subdiagnostic(sugg);
2146 break;
2147 }
2148
2149 err.emit();
2150 }
2151
2152 pub(crate) fn find_similarly_named_module_or_crate(
2153 &self,
2154 ident: Symbol,
2155 current_module: Module<'ra>,
2156 ) -> Option<Symbol> {
2157 let mut candidates = self
2158 .extern_prelude
2159 .keys()
2160 .map(|ident| ident.name)
2161 .chain(
2162 self.local_module_map
2163 .iter()
2164 .filter(|(_, module)| {
2165 current_module.is_ancestor_of(**module) && current_module != **module
2166 })
2167 .flat_map(|(_, module)| module.kind.name()),
2168 )
2169 .chain(
2170 self.extern_module_map
2171 .borrow()
2172 .iter()
2173 .filter(|(_, module)| {
2174 current_module.is_ancestor_of(**module) && current_module != **module
2175 })
2176 .flat_map(|(_, module)| module.kind.name()),
2177 )
2178 .filter(|c| !c.to_string().is_empty())
2179 .collect::<Vec<_>>();
2180 candidates.sort();
2181 candidates.dedup();
2182 find_best_match_for_name(&candidates, ident, None).filter(|sugg| *sugg != ident)
2183 }
2184
2185 pub(crate) fn report_path_resolution_error(
2186 &mut self,
2187 path: &[Segment],
2188 opt_ns: Option<Namespace>, parent_scope: &ParentScope<'ra>,
2190 ribs: Option<&PerNS<Vec<Rib<'ra>>>>,
2191 ignore_binding: Option<NameBinding<'ra>>,
2192 ignore_import: Option<Import<'ra>>,
2193 module: Option<ModuleOrUniformRoot<'ra>>,
2194 failed_segment_idx: usize,
2195 ident: Ident,
2196 ) -> (String, Option<Suggestion>) {
2197 let is_last = failed_segment_idx == path.len() - 1;
2198 let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
2199 let module_res = match module {
2200 Some(ModuleOrUniformRoot::Module(module)) => module.res(),
2201 _ => None,
2202 };
2203 if module_res == self.graph_root.res() {
2204 let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
2205 let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
2206 candidates
2207 .sort_by_cached_key(|c| (c.path.segments.len(), pprust::path_to_string(&c.path)));
2208 if let Some(candidate) = candidates.get(0) {
2209 let path = {
2210 let len = candidate.path.segments.len();
2212 let start_index = (0..=failed_segment_idx.min(len - 1))
2213 .find(|&i| path[i].ident.name != candidate.path.segments[i].ident.name)
2214 .unwrap_or_default();
2215 let segments =
2216 (start_index..len).map(|s| candidate.path.segments[s].clone()).collect();
2217 Path { segments, span: Span::default(), tokens: None }
2218 };
2219 (
2220 String::from("unresolved import"),
2221 Some((
2222 vec![(ident.span, pprust::path_to_string(&path))],
2223 String::from("a similar path exists"),
2224 Applicability::MaybeIncorrect,
2225 )),
2226 )
2227 } else if ident.name == sym::core {
2228 (
2229 format!("you might be missing crate `{ident}`"),
2230 Some((
2231 vec![(ident.span, "std".to_string())],
2232 "try using `std` instead of `core`".to_string(),
2233 Applicability::MaybeIncorrect,
2234 )),
2235 )
2236 } else if ident.name == kw::Underscore {
2237 (format!("`_` is not a valid crate or module name"), None)
2238 } else if self.tcx.sess.is_rust_2015() {
2239 (
2240 format!("use of unresolved module or unlinked crate `{ident}`"),
2241 Some((
2242 vec![(
2243 self.current_crate_outer_attr_insert_span,
2244 format!("extern crate {ident};\n"),
2245 )],
2246 if was_invoked_from_cargo() {
2247 format!(
2248 "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2249 to add it to your `Cargo.toml` and import it in your code",
2250 )
2251 } else {
2252 format!(
2253 "you might be missing a crate named `{ident}`, add it to your \
2254 project and import it in your code",
2255 )
2256 },
2257 Applicability::MaybeIncorrect,
2258 )),
2259 )
2260 } else {
2261 (format!("could not find `{ident}` in the crate root"), None)
2262 }
2263 } else if failed_segment_idx > 0 {
2264 let parent = path[failed_segment_idx - 1].ident.name;
2265 let parent = match parent {
2266 kw::PathRoot if self.tcx.sess.edition() > Edition::Edition2015 => {
2269 "the list of imported crates".to_owned()
2270 }
2271 kw::PathRoot | kw::Crate => "the crate root".to_owned(),
2272 _ => format!("`{parent}`"),
2273 };
2274
2275 let mut msg = format!("could not find `{ident}` in {parent}");
2276 if ns == TypeNS || ns == ValueNS {
2277 let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
2278 let binding = if let Some(module) = module {
2279 self.resolve_ident_in_module(
2280 module,
2281 ident,
2282 ns_to_try,
2283 parent_scope,
2284 None,
2285 ignore_binding,
2286 ignore_import,
2287 )
2288 .ok()
2289 } else if let Some(ribs) = ribs
2290 && let Some(TypeNS | ValueNS) = opt_ns
2291 {
2292 assert!(ignore_import.is_none());
2293 match self.resolve_ident_in_lexical_scope(
2294 ident,
2295 ns_to_try,
2296 parent_scope,
2297 None,
2298 &ribs[ns_to_try],
2299 ignore_binding,
2300 ) {
2301 Some(LexicalScopeBinding::Item(binding)) => Some(binding),
2303 _ => None,
2304 }
2305 } else {
2306 self.early_resolve_ident_in_lexical_scope(
2307 ident,
2308 ScopeSet::All(ns_to_try),
2309 parent_scope,
2310 None,
2311 false,
2312 ignore_binding,
2313 ignore_import,
2314 )
2315 .ok()
2316 };
2317 if let Some(binding) = binding {
2318 msg = format!(
2319 "expected {}, found {} `{ident}` in {parent}",
2320 ns.descr(),
2321 binding.res().descr(),
2322 );
2323 };
2324 }
2325 (msg, None)
2326 } else if ident.name == kw::SelfUpper {
2327 if opt_ns.is_none() {
2331 ("`Self` cannot be used in imports".to_string(), None)
2332 } else {
2333 (
2334 "`Self` is only available in impls, traits, and type definitions".to_string(),
2335 None,
2336 )
2337 }
2338 } else if ident.name.as_str().chars().next().is_some_and(|c| c.is_ascii_uppercase()) {
2339 let binding = if let Some(ribs) = ribs {
2341 assert!(ignore_import.is_none());
2342 self.resolve_ident_in_lexical_scope(
2343 ident,
2344 ValueNS,
2345 parent_scope,
2346 None,
2347 &ribs[ValueNS],
2348 ignore_binding,
2349 )
2350 } else {
2351 None
2352 };
2353 let match_span = match binding {
2354 Some(LexicalScopeBinding::Res(Res::Local(id))) => {
2363 Some(*self.pat_span_map.get(&id).unwrap())
2364 }
2365 Some(LexicalScopeBinding::Item(name_binding)) => Some(name_binding.span),
2377 _ => None,
2378 };
2379 let suggestion = match_span.map(|span| {
2380 (
2381 vec![(span, String::from(""))],
2382 format!("`{ident}` is defined here, but is not a type"),
2383 Applicability::MaybeIncorrect,
2384 )
2385 });
2386
2387 (format!("use of undeclared type `{ident}`"), suggestion)
2388 } else {
2389 let mut suggestion = None;
2390 if ident.name == sym::alloc {
2391 suggestion = Some((
2392 vec![],
2393 String::from("add `extern crate alloc` to use the `alloc` crate"),
2394 Applicability::MaybeIncorrect,
2395 ))
2396 }
2397
2398 suggestion = suggestion.or_else(|| {
2399 self.find_similarly_named_module_or_crate(ident.name, parent_scope.module).map(
2400 |sugg| {
2401 (
2402 vec![(ident.span, sugg.to_string())],
2403 String::from("there is a crate or module with a similar name"),
2404 Applicability::MaybeIncorrect,
2405 )
2406 },
2407 )
2408 });
2409 if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
2410 ident,
2411 ScopeSet::All(ValueNS),
2412 parent_scope,
2413 None,
2414 false,
2415 ignore_binding,
2416 ignore_import,
2417 ) {
2418 let descr = binding.res().descr();
2419 (format!("{descr} `{ident}` is not a crate or module"), suggestion)
2420 } else {
2421 let suggestion = if suggestion.is_some() {
2422 suggestion
2423 } else if let Some(m) = self.undeclared_module_exists(ident) {
2424 self.undeclared_module_suggest_declare(ident, m)
2425 } else if was_invoked_from_cargo() {
2426 Some((
2427 vec![],
2428 format!(
2429 "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2430 to add it to your `Cargo.toml`",
2431 ),
2432 Applicability::MaybeIncorrect,
2433 ))
2434 } else {
2435 Some((
2436 vec![],
2437 format!("you might be missing a crate named `{ident}`",),
2438 Applicability::MaybeIncorrect,
2439 ))
2440 };
2441 (format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
2442 }
2443 }
2444 }
2445
2446 fn undeclared_module_suggest_declare(
2447 &self,
2448 ident: Ident,
2449 path: std::path::PathBuf,
2450 ) -> Option<(Vec<(Span, String)>, String, Applicability)> {
2451 Some((
2452 vec![(self.current_crate_outer_attr_insert_span, format!("mod {ident};\n"))],
2453 format!(
2454 "to make use of source file {}, use `mod {ident}` \
2455 in this file to declare the module",
2456 path.display()
2457 ),
2458 Applicability::MaybeIncorrect,
2459 ))
2460 }
2461
2462 fn undeclared_module_exists(&self, ident: Ident) -> Option<std::path::PathBuf> {
2463 let map = self.tcx.sess.source_map();
2464
2465 let src = map.span_to_filename(ident.span).into_local_path()?;
2466 let i = ident.as_str();
2467 let dir = src.parent()?;
2469 let src = src.file_stem()?.to_str()?;
2470 for file in [
2471 dir.join(i).with_extension("rs"),
2473 dir.join(i).join("mod.rs"),
2475 ] {
2476 if file.exists() {
2477 return Some(file);
2478 }
2479 }
2480 if !matches!(src, "main" | "lib" | "mod") {
2481 for file in [
2482 dir.join(src).join(i).with_extension("rs"),
2484 dir.join(src).join(i).join("mod.rs"),
2486 ] {
2487 if file.exists() {
2488 return Some(file);
2489 }
2490 }
2491 }
2492 None
2493 }
2494
2495 #[instrument(level = "debug", skip(self, parent_scope))]
2497 pub(crate) fn make_path_suggestion(
2498 &mut self,
2499 mut path: Vec<Segment>,
2500 parent_scope: &ParentScope<'ra>,
2501 ) -> Option<(Vec<Segment>, Option<String>)> {
2502 match path[..] {
2503 [first, second, ..]
2506 if first.ident.name == kw::PathRoot && !second.ident.is_path_segment_keyword() => {}
2507 [first, ..]
2509 if first.ident.span.at_least_rust_2018()
2510 && !first.ident.is_path_segment_keyword() =>
2511 {
2512 path.insert(0, Segment::from_ident(Ident::dummy()));
2514 }
2515 _ => return None,
2516 }
2517
2518 self.make_missing_self_suggestion(path.clone(), parent_scope)
2519 .or_else(|| self.make_missing_crate_suggestion(path.clone(), parent_scope))
2520 .or_else(|| self.make_missing_super_suggestion(path.clone(), parent_scope))
2521 .or_else(|| self.make_external_crate_suggestion(path, parent_scope))
2522 }
2523
2524 #[instrument(level = "debug", skip(self, parent_scope))]
2532 fn make_missing_self_suggestion(
2533 &mut self,
2534 mut path: Vec<Segment>,
2535 parent_scope: &ParentScope<'ra>,
2536 ) -> Option<(Vec<Segment>, Option<String>)> {
2537 path[0].ident.name = kw::SelfLower;
2539 let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2540 debug!(?path, ?result);
2541 if let PathResult::Module(..) = result { Some((path, None)) } else { None }
2542 }
2543
2544 #[instrument(level = "debug", skip(self, parent_scope))]
2552 fn make_missing_crate_suggestion(
2553 &mut self,
2554 mut path: Vec<Segment>,
2555 parent_scope: &ParentScope<'ra>,
2556 ) -> Option<(Vec<Segment>, Option<String>)> {
2557 path[0].ident.name = kw::Crate;
2559 let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2560 debug!(?path, ?result);
2561 if let PathResult::Module(..) = result {
2562 Some((
2563 path,
2564 Some(
2565 "`use` statements changed in Rust 2018; read more at \
2566 <https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-\
2567 clarity.html>"
2568 .to_string(),
2569 ),
2570 ))
2571 } else {
2572 None
2573 }
2574 }
2575
2576 #[instrument(level = "debug", skip(self, parent_scope))]
2584 fn make_missing_super_suggestion(
2585 &mut self,
2586 mut path: Vec<Segment>,
2587 parent_scope: &ParentScope<'ra>,
2588 ) -> Option<(Vec<Segment>, Option<String>)> {
2589 path[0].ident.name = kw::Super;
2591 let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2592 debug!(?path, ?result);
2593 if let PathResult::Module(..) = result { Some((path, None)) } else { None }
2594 }
2595
2596 #[instrument(level = "debug", skip(self, parent_scope))]
2607 fn make_external_crate_suggestion(
2608 &mut self,
2609 mut path: Vec<Segment>,
2610 parent_scope: &ParentScope<'ra>,
2611 ) -> Option<(Vec<Segment>, Option<String>)> {
2612 if path[1].ident.span.is_rust_2015() {
2613 return None;
2614 }
2615
2616 let mut extern_crate_names =
2620 self.extern_prelude.keys().map(|ident| ident.name).collect::<Vec<_>>();
2621 extern_crate_names.sort_by(|a, b| b.as_str().cmp(a.as_str()));
2622
2623 for name in extern_crate_names.into_iter() {
2624 path[0].ident.name = name;
2626 let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2627 debug!(?path, ?name, ?result);
2628 if let PathResult::Module(..) = result {
2629 return Some((path, None));
2630 }
2631 }
2632
2633 None
2634 }
2635
2636 pub(crate) fn check_for_module_export_macro(
2649 &mut self,
2650 import: Import<'ra>,
2651 module: ModuleOrUniformRoot<'ra>,
2652 ident: Ident,
2653 ) -> Option<(Option<Suggestion>, Option<String>)> {
2654 let ModuleOrUniformRoot::Module(mut crate_module) = module else {
2655 return None;
2656 };
2657
2658 while let Some(parent) = crate_module.parent {
2659 crate_module = parent;
2660 }
2661
2662 if module == ModuleOrUniformRoot::Module(crate_module) {
2663 return None;
2665 }
2666
2667 let resolutions = self.resolutions(crate_module).borrow();
2668 let binding_key = BindingKey::new(ident, MacroNS);
2669 let resolution = resolutions.get(&binding_key)?;
2670 let binding = resolution.borrow().binding()?;
2671 let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() else {
2672 return None;
2673 };
2674 let module_name = crate_module.kind.name().unwrap_or(kw::Crate);
2675 let import_snippet = match import.kind {
2676 ImportKind::Single { source, target, .. } if source != target => {
2677 format!("{source} as {target}")
2678 }
2679 _ => format!("{ident}"),
2680 };
2681
2682 let mut corrections: Vec<(Span, String)> = Vec::new();
2683 if !import.is_nested() {
2684 corrections.push((import.span, format!("{module_name}::{import_snippet}")));
2687 } else {
2688 let (found_closing_brace, binding_span) = find_span_of_binding_until_next_binding(
2692 self.tcx.sess,
2693 import.span,
2694 import.use_span,
2695 );
2696 debug!(found_closing_brace, ?binding_span);
2697
2698 let mut removal_span = binding_span;
2699
2700 if found_closing_brace
2708 && let Some(previous_span) =
2709 extend_span_to_previous_binding(self.tcx.sess, binding_span)
2710 {
2711 debug!(?previous_span);
2712 removal_span = removal_span.with_lo(previous_span.lo());
2713 }
2714 debug!(?removal_span);
2715
2716 corrections.push((removal_span, "".to_string()));
2718
2719 let (has_nested, after_crate_name) =
2726 find_span_immediately_after_crate_name(self.tcx.sess, import.use_span);
2727 debug!(has_nested, ?after_crate_name);
2728
2729 let source_map = self.tcx.sess.source_map();
2730
2731 let is_definitely_crate = import
2733 .module_path
2734 .first()
2735 .is_some_and(|f| f.ident.name != kw::SelfLower && f.ident.name != kw::Super);
2736
2737 let start_point = source_map.start_point(after_crate_name);
2739 if is_definitely_crate
2740 && let Ok(start_snippet) = source_map.span_to_snippet(start_point)
2741 {
2742 corrections.push((
2743 start_point,
2744 if has_nested {
2745 format!("{start_snippet}{import_snippet}, ")
2747 } else {
2748 format!("{{{import_snippet}, {start_snippet}")
2751 },
2752 ));
2753
2754 if !has_nested {
2756 corrections.push((source_map.end_point(after_crate_name), "};".to_string()));
2757 }
2758 } else {
2759 corrections.push((
2761 import.use_span.shrink_to_lo(),
2762 format!("use {module_name}::{import_snippet};\n"),
2763 ));
2764 }
2765 }
2766
2767 let suggestion = Some((
2768 corrections,
2769 String::from("a macro with this name exists at the root of the crate"),
2770 Applicability::MaybeIncorrect,
2771 ));
2772 Some((
2773 suggestion,
2774 Some(
2775 "this could be because a macro annotated with `#[macro_export]` will be exported \
2776 at the root of the crate instead of the module where it is defined"
2777 .to_string(),
2778 ),
2779 ))
2780 }
2781
2782 pub(crate) fn find_cfg_stripped(&self, err: &mut Diag<'_>, segment: &Symbol, module: DefId) {
2784 let local_items;
2785 let symbols = if module.is_local() {
2786 local_items = self
2787 .stripped_cfg_items
2788 .iter()
2789 .filter_map(|item| {
2790 let parent_module = self.opt_local_def_id(item.parent_module)?.to_def_id();
2791 Some(StrippedCfgItem {
2792 parent_module,
2793 ident: item.ident,
2794 cfg: item.cfg.clone(),
2795 })
2796 })
2797 .collect::<Vec<_>>();
2798 local_items.as_slice()
2799 } else {
2800 self.tcx.stripped_cfg_items(module.krate)
2801 };
2802
2803 for &StrippedCfgItem { parent_module, ident, ref cfg } in symbols {
2804 if ident.name != *segment {
2805 continue;
2806 }
2807
2808 fn comes_from_same_module_for_glob(
2809 r: &Resolver<'_, '_>,
2810 parent_module: DefId,
2811 module: DefId,
2812 visited: &mut FxHashMap<DefId, bool>,
2813 ) -> bool {
2814 if let Some(&cached) = visited.get(&parent_module) {
2815 return cached;
2819 }
2820 visited.insert(parent_module, false);
2821 let m = r.expect_module(parent_module);
2822 let mut res = false;
2823 for importer in m.glob_importers.borrow().iter() {
2824 if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id() {
2825 if next_parent_module == module
2826 || comes_from_same_module_for_glob(
2827 r,
2828 next_parent_module,
2829 module,
2830 visited,
2831 )
2832 {
2833 res = true;
2834 break;
2835 }
2836 }
2837 }
2838 visited.insert(parent_module, res);
2839 res
2840 }
2841
2842 let comes_from_same_module = parent_module == module
2843 || comes_from_same_module_for_glob(
2844 self,
2845 parent_module,
2846 module,
2847 &mut Default::default(),
2848 );
2849 if !comes_from_same_module {
2850 continue;
2851 }
2852
2853 let note = errors::FoundItemConfigureOut { span: ident.span };
2854 err.subdiagnostic(note);
2855
2856 if let CfgEntry::NameValue { value: Some((feature, _)), .. } = cfg.0 {
2857 let note = errors::ItemWasBehindFeature { feature, span: cfg.1 };
2858 err.subdiagnostic(note);
2859 } else {
2860 let note = errors::ItemWasCfgOut { span: cfg.1 };
2861 err.subdiagnostic(note);
2862 }
2863 }
2864 }
2865}
2866
2867fn find_span_of_binding_until_next_binding(
2881 sess: &Session,
2882 binding_span: Span,
2883 use_span: Span,
2884) -> (bool, Span) {
2885 let source_map = sess.source_map();
2886
2887 let binding_until_end = binding_span.with_hi(use_span.hi());
2890
2891 let after_binding_until_end = binding_until_end.with_lo(binding_span.hi());
2894
2895 let mut found_closing_brace = false;
2902 let after_binding_until_next_binding =
2903 source_map.span_take_while(after_binding_until_end, |&ch| {
2904 if ch == '}' {
2905 found_closing_brace = true;
2906 }
2907 ch == ' ' || ch == ','
2908 });
2909
2910 let span = binding_span.with_hi(after_binding_until_next_binding.hi());
2915
2916 (found_closing_brace, span)
2917}
2918
2919fn extend_span_to_previous_binding(sess: &Session, binding_span: Span) -> Option<Span> {
2932 let source_map = sess.source_map();
2933
2934 let prev_source = source_map.span_to_prev_source(binding_span).ok()?;
2938
2939 let prev_comma = prev_source.rsplit(',').collect::<Vec<_>>();
2940 let prev_starting_brace = prev_source.rsplit('{').collect::<Vec<_>>();
2941 if prev_comma.len() <= 1 || prev_starting_brace.len() <= 1 {
2942 return None;
2943 }
2944
2945 let prev_comma = prev_comma.first().unwrap();
2946 let prev_starting_brace = prev_starting_brace.first().unwrap();
2947
2948 if prev_comma.len() > prev_starting_brace.len() {
2952 return None;
2953 }
2954
2955 Some(binding_span.with_lo(BytePos(
2956 binding_span.lo().0 - (prev_comma.as_bytes().len() as u32) - 1,
2959 )))
2960}
2961
2962#[instrument(level = "debug", skip(sess))]
2976fn find_span_immediately_after_crate_name(sess: &Session, use_span: Span) -> (bool, Span) {
2977 let source_map = sess.source_map();
2978
2979 let mut num_colons = 0;
2981 let until_second_colon = source_map.span_take_while(use_span, |c| {
2983 if *c == ':' {
2984 num_colons += 1;
2985 }
2986 !matches!(c, ':' if num_colons == 2)
2987 });
2988 let from_second_colon = use_span.with_lo(until_second_colon.hi() + BytePos(1));
2990
2991 let mut found_a_non_whitespace_character = false;
2992 let after_second_colon = source_map.span_take_while(from_second_colon, |c| {
2994 if found_a_non_whitespace_character {
2995 return false;
2996 }
2997 if !c.is_whitespace() {
2998 found_a_non_whitespace_character = true;
2999 }
3000 true
3001 });
3002
3003 let next_left_bracket = source_map.span_through_char(from_second_colon, '{');
3005
3006 (next_left_bracket == after_second_colon, from_second_colon)
3007}
3008
3009enum Instead {
3012 Yes,
3013 No,
3014}
3015
3016enum FoundUse {
3018 Yes,
3019 No,
3020}
3021
3022pub(crate) enum DiagMode {
3024 Normal,
3025 Pattern,
3027 Import {
3029 unresolved_import: bool,
3031 append: bool,
3034 },
3035}
3036
3037pub(crate) fn import_candidates(
3038 tcx: TyCtxt<'_>,
3039 err: &mut Diag<'_>,
3040 use_placement_span: Option<Span>,
3042 candidates: &[ImportSuggestion],
3043 mode: DiagMode,
3044 append: &str,
3045) {
3046 show_candidates(
3047 tcx,
3048 err,
3049 use_placement_span,
3050 candidates,
3051 Instead::Yes,
3052 FoundUse::Yes,
3053 mode,
3054 vec![],
3055 append,
3056 );
3057}
3058
3059type PathString<'a> = (String, &'a str, Option<Span>, &'a Option<String>, bool);
3060
3061fn show_candidates(
3066 tcx: TyCtxt<'_>,
3067 err: &mut Diag<'_>,
3068 use_placement_span: Option<Span>,
3070 candidates: &[ImportSuggestion],
3071 instead: Instead,
3072 found_use: FoundUse,
3073 mode: DiagMode,
3074 path: Vec<Segment>,
3075 append: &str,
3076) -> bool {
3077 if candidates.is_empty() {
3078 return false;
3079 }
3080
3081 let mut showed = false;
3082 let mut accessible_path_strings: Vec<PathString<'_>> = Vec::new();
3083 let mut inaccessible_path_strings: Vec<PathString<'_>> = Vec::new();
3084
3085 candidates.iter().for_each(|c| {
3086 if c.accessible {
3087 if c.doc_visible {
3089 accessible_path_strings.push((
3090 pprust::path_to_string(&c.path),
3091 c.descr,
3092 c.did.and_then(|did| Some(tcx.source_span(did.as_local()?))),
3093 &c.note,
3094 c.via_import,
3095 ))
3096 }
3097 } else {
3098 inaccessible_path_strings.push((
3099 pprust::path_to_string(&c.path),
3100 c.descr,
3101 c.did.and_then(|did| Some(tcx.source_span(did.as_local()?))),
3102 &c.note,
3103 c.via_import,
3104 ))
3105 }
3106 });
3107
3108 for path_strings in [&mut accessible_path_strings, &mut inaccessible_path_strings] {
3111 path_strings.sort_by(|a, b| a.0.cmp(&b.0));
3112 path_strings.dedup_by(|a, b| a.0 == b.0);
3113 let core_path_strings =
3114 path_strings.extract_if(.., |p| p.0.starts_with("core::")).collect::<Vec<_>>();
3115 let std_path_strings =
3116 path_strings.extract_if(.., |p| p.0.starts_with("std::")).collect::<Vec<_>>();
3117 let foreign_crate_path_strings =
3118 path_strings.extract_if(.., |p| !p.0.starts_with("crate::")).collect::<Vec<_>>();
3119
3120 if std_path_strings.len() == core_path_strings.len() {
3123 path_strings.extend(std_path_strings);
3125 } else {
3126 path_strings.extend(std_path_strings);
3127 path_strings.extend(core_path_strings);
3128 }
3129 path_strings.extend(foreign_crate_path_strings);
3131 }
3132
3133 if !accessible_path_strings.is_empty() {
3134 let (determiner, kind, s, name, through) =
3135 if let [(name, descr, _, _, via_import)] = &accessible_path_strings[..] {
3136 (
3137 "this",
3138 *descr,
3139 "",
3140 format!(" `{name}`"),
3141 if *via_import { " through its public re-export" } else { "" },
3142 )
3143 } else {
3144 let kinds = accessible_path_strings
3147 .iter()
3148 .map(|(_, descr, _, _, _)| *descr)
3149 .collect::<UnordSet<&str>>();
3150 let kind = if let Some(kind) = kinds.get_only() { kind } else { "item" };
3151 let s = if kind.ends_with('s') { "es" } else { "s" };
3152
3153 ("one of these", kind, s, String::new(), "")
3154 };
3155
3156 let instead = if let Instead::Yes = instead { " instead" } else { "" };
3157 let mut msg = if let DiagMode::Pattern = mode {
3158 format!(
3159 "if you meant to match on {kind}{s}{instead}{name}, use the full path in the \
3160 pattern",
3161 )
3162 } else {
3163 format!("consider importing {determiner} {kind}{s}{through}{instead}")
3164 };
3165
3166 for note in accessible_path_strings.iter().flat_map(|cand| cand.3.as_ref()) {
3167 err.note(note.clone());
3168 }
3169
3170 let append_candidates = |msg: &mut String, accessible_path_strings: Vec<PathString<'_>>| {
3171 msg.push(':');
3172
3173 for candidate in accessible_path_strings {
3174 msg.push('\n');
3175 msg.push_str(&candidate.0);
3176 }
3177 };
3178
3179 if let Some(span) = use_placement_span {
3180 let (add_use, trailing) = match mode {
3181 DiagMode::Pattern => {
3182 err.span_suggestions(
3183 span,
3184 msg,
3185 accessible_path_strings.into_iter().map(|a| a.0),
3186 Applicability::MaybeIncorrect,
3187 );
3188 return true;
3189 }
3190 DiagMode::Import { .. } => ("", ""),
3191 DiagMode::Normal => ("use ", ";\n"),
3192 };
3193 for candidate in &mut accessible_path_strings {
3194 let additional_newline = if let FoundUse::No = found_use
3197 && let DiagMode::Normal = mode
3198 {
3199 "\n"
3200 } else {
3201 ""
3202 };
3203 candidate.0 =
3204 format!("{add_use}{}{append}{trailing}{additional_newline}", candidate.0);
3205 }
3206
3207 match mode {
3208 DiagMode::Import { append: true, .. } => {
3209 append_candidates(&mut msg, accessible_path_strings);
3210 err.span_help(span, msg);
3211 }
3212 _ => {
3213 err.span_suggestions_with_style(
3214 span,
3215 msg,
3216 accessible_path_strings.into_iter().map(|a| a.0),
3217 Applicability::MaybeIncorrect,
3218 SuggestionStyle::ShowAlways,
3219 );
3220 }
3221 }
3222
3223 if let [first, .., last] = &path[..] {
3224 let sp = first.ident.span.until(last.ident.span);
3225 if sp.can_be_used_for_suggestions() && !sp.is_empty() {
3228 err.span_suggestion_verbose(
3229 sp,
3230 format!("if you import `{}`, refer to it directly", last.ident),
3231 "",
3232 Applicability::Unspecified,
3233 );
3234 }
3235 }
3236 } else {
3237 append_candidates(&mut msg, accessible_path_strings);
3238 err.help(msg);
3239 }
3240 showed = true;
3241 }
3242 if !inaccessible_path_strings.is_empty()
3243 && (!matches!(mode, DiagMode::Import { unresolved_import: false, .. }))
3244 {
3245 let prefix =
3246 if let DiagMode::Pattern = mode { "you might have meant to match on " } else { "" };
3247 if let [(name, descr, source_span, note, _)] = &inaccessible_path_strings[..] {
3248 let msg = format!(
3249 "{prefix}{descr} `{name}`{} exists but is inaccessible",
3250 if let DiagMode::Pattern = mode { ", which" } else { "" }
3251 );
3252
3253 if let Some(source_span) = source_span {
3254 let span = tcx.sess.source_map().guess_head_span(*source_span);
3255 let mut multi_span = MultiSpan::from_span(span);
3256 multi_span.push_span_label(span, "not accessible");
3257 err.span_note(multi_span, msg);
3258 } else {
3259 err.note(msg);
3260 }
3261 if let Some(note) = (*note).as_deref() {
3262 err.note(note.to_string());
3263 }
3264 } else {
3265 let (_, descr_first, _, _, _) = &inaccessible_path_strings[0];
3266 let descr = if inaccessible_path_strings
3267 .iter()
3268 .skip(1)
3269 .all(|(_, descr, _, _, _)| descr == descr_first)
3270 {
3271 descr_first
3272 } else {
3273 "item"
3274 };
3275 let plural_descr =
3276 if descr.ends_with('s') { format!("{descr}es") } else { format!("{descr}s") };
3277
3278 let mut msg = format!("{prefix}these {plural_descr} exist but are inaccessible");
3279 let mut has_colon = false;
3280
3281 let mut spans = Vec::new();
3282 for (name, _, source_span, _, _) in &inaccessible_path_strings {
3283 if let Some(source_span) = source_span {
3284 let span = tcx.sess.source_map().guess_head_span(*source_span);
3285 spans.push((name, span));
3286 } else {
3287 if !has_colon {
3288 msg.push(':');
3289 has_colon = true;
3290 }
3291 msg.push('\n');
3292 msg.push_str(name);
3293 }
3294 }
3295
3296 let mut multi_span = MultiSpan::from_spans(spans.iter().map(|(_, sp)| *sp).collect());
3297 for (name, span) in spans {
3298 multi_span.push_span_label(span, format!("`{name}`: not accessible"));
3299 }
3300
3301 for note in inaccessible_path_strings.iter().flat_map(|cand| cand.3.as_ref()) {
3302 err.note(note.clone());
3303 }
3304
3305 err.span_note(multi_span, msg);
3306 }
3307 showed = true;
3308 }
3309 showed
3310}
3311
3312#[derive(Debug)]
3313struct UsePlacementFinder {
3314 target_module: NodeId,
3315 first_legal_span: Option<Span>,
3316 first_use_span: Option<Span>,
3317}
3318
3319impl UsePlacementFinder {
3320 fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, FoundUse) {
3321 let mut finder =
3322 UsePlacementFinder { target_module, first_legal_span: None, first_use_span: None };
3323 finder.visit_crate(krate);
3324 if let Some(use_span) = finder.first_use_span {
3325 (Some(use_span), FoundUse::Yes)
3326 } else {
3327 (finder.first_legal_span, FoundUse::No)
3328 }
3329 }
3330}
3331
3332impl<'tcx> visit::Visitor<'tcx> for UsePlacementFinder {
3333 fn visit_crate(&mut self, c: &Crate) {
3334 if self.target_module == CRATE_NODE_ID {
3335 let inject = c.spans.inject_use_span;
3336 if is_span_suitable_for_use_injection(inject) {
3337 self.first_legal_span = Some(inject);
3338 }
3339 self.first_use_span = search_for_any_use_in_items(&c.items);
3340 } else {
3341 visit::walk_crate(self, c);
3342 }
3343 }
3344
3345 fn visit_item(&mut self, item: &'tcx ast::Item) {
3346 if self.target_module == item.id {
3347 if let ItemKind::Mod(_, _, ModKind::Loaded(items, _inline, mod_spans, _)) = &item.kind {
3348 let inject = mod_spans.inject_use_span;
3349 if is_span_suitable_for_use_injection(inject) {
3350 self.first_legal_span = Some(inject);
3351 }
3352 self.first_use_span = search_for_any_use_in_items(items);
3353 }
3354 } else {
3355 visit::walk_item(self, item);
3356 }
3357 }
3358}
3359
3360fn search_for_any_use_in_items(items: &[P<ast::Item>]) -> Option<Span> {
3361 for item in items {
3362 if let ItemKind::Use(..) = item.kind
3363 && is_span_suitable_for_use_injection(item.span)
3364 {
3365 let mut lo = item.span.lo();
3366 for attr in &item.attrs {
3367 if attr.span.eq_ctxt(item.span) {
3368 lo = std::cmp::min(lo, attr.span.lo());
3369 }
3370 }
3371 return Some(Span::new(lo, lo, item.span.ctxt(), item.span.parent()));
3372 }
3373 }
3374 None
3375}
3376
3377fn is_span_suitable_for_use_injection(s: Span) -> bool {
3378 !s.from_expansion()
3381}