1use rustc_ast::expand::StrippedCfgItem;
2use rustc_ast::ptr::P;
3use rustc_ast::visit::{self, Visitor};
4use rustc_ast::{
5 self as ast, CRATE_NODE_ID, Crate, ItemKind, MetaItemInner, MetaItemKind, ModKind, NodeId, Path,
6};
7use rustc_ast_pretty::pprust;
8use rustc_data_structures::fx::FxHashSet;
9use rustc_data_structures::unord::UnordSet;
10use rustc_errors::codes::*;
11use rustc_errors::{
12 Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, MultiSpan, SuggestionStyle,
13 report_ambiguity_error, struct_span_code_err,
14};
15use rustc_feature::BUILTIN_ATTRIBUTES;
16use rustc_hir::PrimTy;
17use rustc_hir::def::Namespace::{self, *};
18use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
19use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
20use rustc_middle::bug;
21use rustc_middle::ty::TyCtxt;
22use rustc_session::Session;
23use rustc_session::lint::builtin::{
24 ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, AMBIGUOUS_GLOB_IMPORTS,
25 MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
26};
27use rustc_session::lint::{AmbiguityErrorDiag, BuiltinLintDiag};
28use rustc_session::utils::was_invoked_from_cargo;
29use rustc_span::edit_distance::find_best_match_for_name;
30use rustc_span::edition::Edition;
31use rustc_span::hygiene::MacroKind;
32use rustc_span::source_map::SourceMap;
33use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym};
34use thin_vec::{ThinVec, thin_vec};
35use tracing::{debug, instrument};
36
37use crate::errors::{
38 self, AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
39 ExplicitUnsafeTraits, MacroDefinedLater, MacroRulesNot, MacroSuggMovePosition,
40 MaybeMissingMacroRulesName,
41};
42use crate::imports::{Import, ImportKind};
43use crate::late::{PatternSource, Rib};
44use crate::{
45 AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, BindingKey, Finalize,
46 ForwardGenericParamBanReason, HasGenericParams, LexicalScopeBinding, MacroRulesScope, Module,
47 ModuleKind, ModuleOrUniformRoot, NameBinding, NameBindingKind, ParentScope, PathResult,
48 PrivacyError, ResolutionError, Resolver, Scope, ScopeSet, Segment, UseError, Used,
49 VisResolutionError, errors as errs, path_names_to_string,
50};
51
52type Res = def::Res<ast::NodeId>;
53
54pub(crate) type Suggestion = (Vec<(Span, String)>, String, Applicability);
56
57pub(crate) type LabelSuggestion = (Ident, bool);
60
61#[derive(Debug)]
62pub(crate) enum SuggestionTarget {
63 SimilarlyNamed,
65 SingleItem,
67}
68
69#[derive(Debug)]
70pub(crate) struct TypoSuggestion {
71 pub candidate: Symbol,
72 pub span: Option<Span>,
75 pub res: Res,
76 pub target: SuggestionTarget,
77}
78
79impl TypoSuggestion {
80 pub(crate) fn typo_from_ident(ident: Ident, res: Res) -> TypoSuggestion {
81 Self {
82 candidate: ident.name,
83 span: Some(ident.span),
84 res,
85 target: SuggestionTarget::SimilarlyNamed,
86 }
87 }
88 pub(crate) fn typo_from_name(candidate: Symbol, res: Res) -> TypoSuggestion {
89 Self { candidate, span: None, res, target: SuggestionTarget::SimilarlyNamed }
90 }
91 pub(crate) fn single_item_from_ident(ident: Ident, res: Res) -> TypoSuggestion {
92 Self {
93 candidate: ident.name,
94 span: Some(ident.span),
95 res,
96 target: SuggestionTarget::SingleItem,
97 }
98 }
99}
100
101#[derive(Debug, Clone)]
103pub(crate) struct ImportSuggestion {
104 pub did: Option<DefId>,
105 pub descr: &'static str,
106 pub path: Path,
107 pub accessible: bool,
108 pub doc_visible: bool,
110 pub via_import: bool,
111 pub note: Option<String>,
113}
114
115fn reduce_impl_span_to_impl_keyword(sm: &SourceMap, impl_span: Span) -> Span {
123 let impl_span = sm.span_until_char(impl_span, '<');
124 sm.span_until_whitespace(impl_span)
125}
126
127impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
128 pub(crate) fn dcx(&self) -> DiagCtxtHandle<'tcx> {
129 self.tcx.dcx()
130 }
131
132 pub(crate) fn report_errors(&mut self, krate: &Crate) {
133 self.report_with_use_injections(krate);
134
135 for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
136 self.lint_buffer.buffer_lint(
137 MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
138 CRATE_NODE_ID,
139 span_use,
140 BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def),
141 );
142 }
143
144 for ambiguity_error in &self.ambiguity_errors {
145 let diag = self.ambiguity_diagnostics(ambiguity_error);
146 if ambiguity_error.warning {
147 let NameBindingKind::Import { import, .. } = ambiguity_error.b1.0.kind else {
148 unreachable!()
149 };
150 self.lint_buffer.buffer_lint(
151 AMBIGUOUS_GLOB_IMPORTS,
152 import.root_id,
153 ambiguity_error.ident.span,
154 BuiltinLintDiag::AmbiguousGlobImports { diag },
155 );
156 } else {
157 let mut err = struct_span_code_err!(self.dcx(), diag.span, E0659, "{}", diag.msg);
158 report_ambiguity_error(&mut err, diag);
159 err.emit();
160 }
161 }
162
163 let mut reported_spans = FxHashSet::default();
164 for error in std::mem::take(&mut self.privacy_errors) {
165 if reported_spans.insert(error.dedup_span) {
166 self.report_privacy_error(&error);
167 }
168 }
169 }
170
171 fn report_with_use_injections(&mut self, krate: &Crate) {
172 for UseError { mut err, candidates, def_id, instead, suggestion, path, is_call } in
173 std::mem::take(&mut self.use_injections)
174 {
175 let (span, found_use) = if let Some(def_id) = def_id.as_local() {
176 UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id))
177 } else {
178 (None, FoundUse::No)
179 };
180
181 if !candidates.is_empty() {
182 show_candidates(
183 self.tcx,
184 &mut err,
185 span,
186 &candidates,
187 if instead { Instead::Yes } else { Instead::No },
188 found_use,
189 DiagMode::Normal,
190 path,
191 "",
192 );
193 err.emit();
194 } else if let Some((span, msg, sugg, appl)) = suggestion {
195 err.span_suggestion_verbose(span, msg, sugg, appl);
196 err.emit();
197 } else if let [segment] = path.as_slice()
198 && is_call
199 {
200 err.stash(segment.ident.span, rustc_errors::StashKey::CallIntoMethod);
201 } else {
202 err.emit();
203 }
204 }
205 }
206
207 pub(crate) fn report_conflict(
208 &mut self,
209 parent: Module<'_>,
210 ident: Ident,
211 ns: Namespace,
212 new_binding: NameBinding<'ra>,
213 old_binding: NameBinding<'ra>,
214 ) {
215 if old_binding.span.lo() > new_binding.span.lo() {
217 return self.report_conflict(parent, ident, ns, old_binding, new_binding);
218 }
219
220 let container = match parent.kind {
221 ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id()),
224 ModuleKind::Block => "block",
225 };
226
227 let (name, span) =
228 (ident.name, self.tcx.sess.source_map().guess_head_span(new_binding.span));
229
230 if self.name_already_seen.get(&name) == Some(&span) {
231 return;
232 }
233
234 let old_kind = match (ns, old_binding.module()) {
235 (ValueNS, _) => "value",
236 (MacroNS, _) => "macro",
237 (TypeNS, _) if old_binding.is_extern_crate() => "extern crate",
238 (TypeNS, Some(module)) if module.is_normal() => "module",
239 (TypeNS, Some(module)) if module.is_trait() => "trait",
240 (TypeNS, _) => "type",
241 };
242
243 let code = match (old_binding.is_extern_crate(), new_binding.is_extern_crate()) {
244 (true, true) => E0259,
245 (true, _) | (_, true) => match new_binding.is_import() && old_binding.is_import() {
246 true => E0254,
247 false => E0260,
248 },
249 _ => match (old_binding.is_import_user_facing(), new_binding.is_import_user_facing()) {
250 (false, false) => E0428,
251 (true, true) => E0252,
252 _ => E0255,
253 },
254 };
255
256 let label = match new_binding.is_import_user_facing() {
257 true => errors::NameDefinedMultipleTimeLabel::Reimported { span, name },
258 false => errors::NameDefinedMultipleTimeLabel::Redefined { span, name },
259 };
260
261 let old_binding_label =
262 (!old_binding.span.is_dummy() && old_binding.span != span).then(|| {
263 let span = self.tcx.sess.source_map().guess_head_span(old_binding.span);
264 match old_binding.is_import_user_facing() {
265 true => errors::NameDefinedMultipleTimeOldBindingLabel::Import {
266 span,
267 name,
268 old_kind,
269 },
270 false => errors::NameDefinedMultipleTimeOldBindingLabel::Definition {
271 span,
272 name,
273 old_kind,
274 },
275 }
276 });
277
278 let mut err = self
279 .dcx()
280 .create_err(errors::NameDefinedMultipleTime {
281 span,
282 descr: ns.descr(),
283 container,
284 label,
285 old_binding_label,
286 })
287 .with_code(code);
288
289 use NameBindingKind::Import;
291 let can_suggest = |binding: NameBinding<'_>, import: self::Import<'_>| {
292 !binding.span.is_dummy()
293 && !matches!(import.kind, ImportKind::MacroUse { .. } | ImportKind::MacroExport)
294 };
295 let import = match (&new_binding.kind, &old_binding.kind) {
296 (Import { import: new, .. }, Import { import: old, .. })
299 if {
300 (new.has_attributes || old.has_attributes)
301 && can_suggest(old_binding, *old)
302 && can_suggest(new_binding, *new)
303 } =>
304 {
305 if old.has_attributes {
306 Some((*new, new_binding.span, true))
307 } else {
308 Some((*old, old_binding.span, true))
309 }
310 }
311 (Import { import, .. }, other) if can_suggest(new_binding, *import) => {
313 Some((*import, new_binding.span, other.is_import()))
314 }
315 (other, Import { import, .. }) if can_suggest(old_binding, *import) => {
316 Some((*import, old_binding.span, other.is_import()))
317 }
318 _ => None,
319 };
320
321 let duplicate = new_binding.res().opt_def_id() == old_binding.res().opt_def_id();
323 let has_dummy_span = new_binding.span.is_dummy() || old_binding.span.is_dummy();
324 let from_item =
325 self.extern_prelude.get(&ident).is_none_or(|entry| entry.introduced_by_item);
326 let should_remove_import = duplicate
330 && !has_dummy_span
331 && ((new_binding.is_extern_crate() || old_binding.is_extern_crate()) || from_item);
332
333 match import {
334 Some((import, span, true)) if should_remove_import && import.is_nested() => {
335 self.add_suggestion_for_duplicate_nested_use(&mut err, import, span);
336 }
337 Some((import, _, true)) if should_remove_import && !import.is_glob() => {
338 err.subdiagnostic(errors::ToolOnlyRemoveUnnecessaryImport {
341 span: import.use_span_with_attributes,
342 });
343 }
344 Some((import, span, _)) => {
345 self.add_suggestion_for_rename_of_use(&mut err, name, import, span);
346 }
347 _ => {}
348 }
349
350 err.emit();
351 self.name_already_seen.insert(name, span);
352 }
353
354 fn add_suggestion_for_rename_of_use(
364 &self,
365 err: &mut Diag<'_>,
366 name: Symbol,
367 import: Import<'_>,
368 binding_span: Span,
369 ) {
370 let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
371 format!("Other{name}")
372 } else {
373 format!("other_{name}")
374 };
375
376 let mut suggestion = None;
377 let mut span = binding_span;
378 match import.kind {
379 ImportKind::Single { type_ns_only: true, .. } => {
380 suggestion = Some(format!("self as {suggested_name}"))
381 }
382 ImportKind::Single { source, .. } => {
383 if let Some(pos) = source.span.hi().0.checked_sub(binding_span.lo().0)
384 && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span)
385 && pos as usize <= snippet.len()
386 {
387 span = binding_span.with_lo(binding_span.lo() + BytePos(pos)).with_hi(
388 binding_span.hi() - BytePos(if snippet.ends_with(';') { 1 } else { 0 }),
389 );
390 suggestion = Some(format!(" as {suggested_name}"));
391 }
392 }
393 ImportKind::ExternCrate { source, target, .. } => {
394 suggestion = Some(format!(
395 "extern crate {} as {};",
396 source.unwrap_or(target.name),
397 suggested_name,
398 ))
399 }
400 _ => unreachable!(),
401 }
402
403 if let Some(suggestion) = suggestion {
404 err.subdiagnostic(ChangeImportBindingSuggestion { span, suggestion });
405 } else {
406 err.subdiagnostic(ChangeImportBinding { span });
407 }
408 }
409
410 fn add_suggestion_for_duplicate_nested_use(
433 &self,
434 err: &mut Diag<'_>,
435 import: Import<'_>,
436 binding_span: Span,
437 ) {
438 assert!(import.is_nested());
439
440 let (found_closing_brace, span) =
448 find_span_of_binding_until_next_binding(self.tcx.sess, binding_span, import.use_span);
449
450 if found_closing_brace {
453 if let Some(span) = extend_span_to_previous_binding(self.tcx.sess, span) {
454 err.subdiagnostic(errors::ToolOnlyRemoveUnnecessaryImport { span });
455 } else {
456 err.subdiagnostic(errors::RemoveUnnecessaryImport {
459 span: import.use_span_with_attributes,
460 });
461 }
462
463 return;
464 }
465
466 err.subdiagnostic(errors::RemoveUnnecessaryImport { span });
467 }
468
469 pub(crate) fn lint_if_path_starts_with_module(
470 &mut self,
471 finalize: Option<Finalize>,
472 path: &[Segment],
473 second_binding: Option<NameBinding<'_>>,
474 ) {
475 let Some(Finalize { node_id, root_span, .. }) = finalize else {
476 return;
477 };
478
479 let first_name = match path.get(0) {
480 Some(seg) if seg.ident.span.is_rust_2015() && self.tcx.sess.is_rust_2015() => {
482 seg.ident.name
483 }
484 _ => return,
485 };
486
487 if first_name != kw::PathRoot {
490 return;
491 }
492
493 match path.get(1) {
494 Some(Segment { ident, .. }) if ident.name == kw::Crate => return,
496 Some(_) => {}
498 None => return,
502 }
503
504 if let Some(binding) = second_binding
508 && let NameBindingKind::Import { import, .. } = binding.kind
509 && let ImportKind::ExternCrate { source: None, .. } = import.kind
511 {
512 return;
513 }
514
515 let diag = BuiltinLintDiag::AbsPathWithModule(root_span);
516 self.lint_buffer.buffer_lint(
517 ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
518 node_id,
519 root_span,
520 diag,
521 );
522 }
523
524 pub(crate) fn add_module_candidates(
525 &mut self,
526 module: Module<'ra>,
527 names: &mut Vec<TypoSuggestion>,
528 filter_fn: &impl Fn(Res) -> bool,
529 ctxt: Option<SyntaxContext>,
530 ) {
531 module.for_each_child(self, |_this, ident, _ns, binding| {
532 let res = binding.res();
533 if filter_fn(res) && ctxt.is_none_or(|ctxt| ctxt == ident.span.ctxt()) {
534 names.push(TypoSuggestion::typo_from_ident(ident, res));
535 }
536 });
537 }
538
539 pub(crate) fn report_error(
544 &mut self,
545 span: Span,
546 resolution_error: ResolutionError<'ra>,
547 ) -> ErrorGuaranteed {
548 self.into_struct_error(span, resolution_error).emit()
549 }
550
551 pub(crate) fn into_struct_error(
552 &mut self,
553 span: Span,
554 resolution_error: ResolutionError<'ra>,
555 ) -> Diag<'_> {
556 match resolution_error {
557 ResolutionError::GenericParamsFromOuterItem(
558 outer_res,
559 has_generic_params,
560 def_kind,
561 ) => {
562 use errs::GenericParamsFromOuterItemLabel as Label;
563 let static_or_const = match def_kind {
564 DefKind::Static { .. } => {
565 Some(errs::GenericParamsFromOuterItemStaticOrConst::Static)
566 }
567 DefKind::Const => Some(errs::GenericParamsFromOuterItemStaticOrConst::Const),
568 _ => None,
569 };
570 let is_self =
571 matches!(outer_res, Res::SelfTyParam { .. } | Res::SelfTyAlias { .. });
572 let mut err = errs::GenericParamsFromOuterItem {
573 span,
574 label: None,
575 refer_to_type_directly: None,
576 sugg: None,
577 static_or_const,
578 is_self,
579 };
580
581 let sm = self.tcx.sess.source_map();
582 let def_id = match outer_res {
583 Res::SelfTyParam { .. } => {
584 err.label = Some(Label::SelfTyParam(span));
585 return self.dcx().create_err(err);
586 }
587 Res::SelfTyAlias { alias_to: def_id, .. } => {
588 err.label = Some(Label::SelfTyAlias(reduce_impl_span_to_impl_keyword(
589 sm,
590 self.def_span(def_id),
591 )));
592 err.refer_to_type_directly = Some(span);
593 return self.dcx().create_err(err);
594 }
595 Res::Def(DefKind::TyParam, def_id) => {
596 err.label = Some(Label::TyParam(self.def_span(def_id)));
597 def_id
598 }
599 Res::Def(DefKind::ConstParam, def_id) => {
600 err.label = Some(Label::ConstParam(self.def_span(def_id)));
601 def_id
602 }
603 _ => {
604 bug!(
605 "GenericParamsFromOuterItem should only be used with \
606 Res::SelfTyParam, Res::SelfTyAlias, DefKind::TyParam or \
607 DefKind::ConstParam"
608 );
609 }
610 };
611
612 if let HasGenericParams::Yes(span) = has_generic_params {
613 let name = self.tcx.item_name(def_id);
614 let (span, snippet) = if span.is_empty() {
615 let snippet = format!("<{name}>");
616 (span, snippet)
617 } else {
618 let span = sm.span_through_char(span, '<').shrink_to_hi();
619 let snippet = format!("{name}, ");
620 (span, snippet)
621 };
622 err.sugg = Some(errs::GenericParamsFromOuterItemSugg { span, snippet });
623 }
624
625 self.dcx().create_err(err)
626 }
627 ResolutionError::NameAlreadyUsedInParameterList(name, first_use_span) => self
628 .dcx()
629 .create_err(errs::NameAlreadyUsedInParameterList { span, first_use_span, name }),
630 ResolutionError::MethodNotMemberOfTrait(method, trait_, candidate) => {
631 self.dcx().create_err(errs::MethodNotMemberOfTrait {
632 span,
633 method,
634 trait_,
635 sub: candidate.map(|c| errs::AssociatedFnWithSimilarNameExists {
636 span: method.span,
637 candidate: c,
638 }),
639 })
640 }
641 ResolutionError::TypeNotMemberOfTrait(type_, trait_, candidate) => {
642 self.dcx().create_err(errs::TypeNotMemberOfTrait {
643 span,
644 type_,
645 trait_,
646 sub: candidate.map(|c| errs::AssociatedTypeWithSimilarNameExists {
647 span: type_.span,
648 candidate: c,
649 }),
650 })
651 }
652 ResolutionError::ConstNotMemberOfTrait(const_, trait_, candidate) => {
653 self.dcx().create_err(errs::ConstNotMemberOfTrait {
654 span,
655 const_,
656 trait_,
657 sub: candidate.map(|c| errs::AssociatedConstWithSimilarNameExists {
658 span: const_.span,
659 candidate: c,
660 }),
661 })
662 }
663 ResolutionError::VariableNotBoundInPattern(binding_error, parent_scope) => {
664 let BindingError { name, target, origin, could_be_path } = binding_error;
665
666 let target_sp = target.iter().copied().collect::<Vec<_>>();
667 let origin_sp = origin.iter().copied().collect::<Vec<_>>();
668
669 let msp = MultiSpan::from_spans(target_sp.clone());
670 let mut err = self
671 .dcx()
672 .create_err(errors::VariableIsNotBoundInAllPatterns { multispan: msp, name });
673 for sp in target_sp {
674 err.subdiagnostic(errors::PatternDoesntBindName { span: sp, name });
675 }
676 for sp in origin_sp {
677 err.subdiagnostic(errors::VariableNotInAllPatterns { span: sp });
678 }
679 if could_be_path {
680 let import_suggestions = self.lookup_import_candidates(
681 name,
682 Namespace::ValueNS,
683 &parent_scope,
684 &|res: Res| {
685 matches!(
686 res,
687 Res::Def(
688 DefKind::Ctor(CtorOf::Variant, CtorKind::Const)
689 | DefKind::Ctor(CtorOf::Struct, CtorKind::Const)
690 | DefKind::Const
691 | DefKind::AssocConst,
692 _,
693 )
694 )
695 },
696 );
697
698 if import_suggestions.is_empty() {
699 let help_msg = format!(
700 "if you meant to match on a variant or a `const` item, consider \
701 making the path in the pattern qualified: `path::to::ModOrType::{name}`",
702 );
703 err.span_help(span, help_msg);
704 }
705 show_candidates(
706 self.tcx,
707 &mut err,
708 Some(span),
709 &import_suggestions,
710 Instead::No,
711 FoundUse::Yes,
712 DiagMode::Pattern,
713 vec![],
714 "",
715 );
716 }
717 err
718 }
719 ResolutionError::VariableBoundWithDifferentMode(variable_name, first_binding_span) => {
720 self.dcx().create_err(errs::VariableBoundWithDifferentMode {
721 span,
722 first_binding_span,
723 variable_name,
724 })
725 }
726 ResolutionError::IdentifierBoundMoreThanOnceInParameterList(identifier) => self
727 .dcx()
728 .create_err(errs::IdentifierBoundMoreThanOnceInParameterList { span, identifier }),
729 ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(identifier) => self
730 .dcx()
731 .create_err(errs::IdentifierBoundMoreThanOnceInSamePattern { span, identifier }),
732 ResolutionError::UndeclaredLabel { name, suggestion } => {
733 let ((sub_reachable, sub_reachable_suggestion), sub_unreachable) = match suggestion
734 {
735 Some((ident, true)) => (
737 (
738 Some(errs::LabelWithSimilarNameReachable(ident.span)),
739 Some(errs::TryUsingSimilarlyNamedLabel {
740 span,
741 ident_name: ident.name,
742 }),
743 ),
744 None,
745 ),
746 Some((ident, false)) => (
748 (None, None),
749 Some(errs::UnreachableLabelWithSimilarNameExists {
750 ident_span: ident.span,
751 }),
752 ),
753 None => ((None, None), None),
755 };
756 self.dcx().create_err(errs::UndeclaredLabel {
757 span,
758 name,
759 sub_reachable,
760 sub_reachable_suggestion,
761 sub_unreachable,
762 })
763 }
764 ResolutionError::SelfImportsOnlyAllowedWithin { root, span_with_rename } => {
765 let (suggestion, mpart_suggestion) = if root {
767 (None, None)
768 } else {
769 let suggestion = errs::SelfImportsOnlyAllowedWithinSuggestion { span };
772
773 let mpart_suggestion = errs::SelfImportsOnlyAllowedWithinMultipartSuggestion {
776 multipart_start: span_with_rename.shrink_to_lo(),
777 multipart_end: span_with_rename.shrink_to_hi(),
778 };
779 (Some(suggestion), Some(mpart_suggestion))
780 };
781 self.dcx().create_err(errs::SelfImportsOnlyAllowedWithin {
782 span,
783 suggestion,
784 mpart_suggestion,
785 })
786 }
787 ResolutionError::SelfImportCanOnlyAppearOnceInTheList => {
788 self.dcx().create_err(errs::SelfImportCanOnlyAppearOnceInTheList { span })
789 }
790 ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
791 self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
792 }
793 ResolutionError::FailedToResolve { segment, label, suggestion, module } => {
794 let mut err =
795 struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {label}");
796 err.span_label(span, label);
797
798 if let Some((suggestions, msg, applicability)) = suggestion {
799 if suggestions.is_empty() {
800 err.help(msg);
801 return err;
802 }
803 err.multipart_suggestion(msg, suggestions, applicability);
804 }
805 if let Some(ModuleOrUniformRoot::Module(module)) = module
806 && let Some(module) = module.opt_def_id()
807 && let Some(segment) = segment
808 {
809 self.find_cfg_stripped(&mut err, &segment, module);
810 }
811
812 err
813 }
814 ResolutionError::CannotCaptureDynamicEnvironmentInFnItem => {
815 self.dcx().create_err(errs::CannotCaptureDynamicEnvironmentInFnItem { span })
816 }
817 ResolutionError::AttemptToUseNonConstantValueInConstant {
818 ident,
819 suggestion,
820 current,
821 type_span,
822 } => {
823 let sp = self
832 .tcx
833 .sess
834 .source_map()
835 .span_extend_to_prev_str(ident.span, current, true, false);
836
837 let ((with, with_label), without) = match sp {
838 Some(sp) if !self.tcx.sess.source_map().is_multiline(sp) => {
839 let sp = sp
840 .with_lo(BytePos(sp.lo().0 - (current.len() as u32)))
841 .until(ident.span);
842 (
843 (Some(errs::AttemptToUseNonConstantValueInConstantWithSuggestion {
844 span: sp,
845 suggestion,
846 current,
847 type_span,
848 }), Some(errs::AttemptToUseNonConstantValueInConstantLabelWithSuggestion {span})),
849 None,
850 )
851 }
852 _ => (
853 (None, None),
854 Some(errs::AttemptToUseNonConstantValueInConstantWithoutSuggestion {
855 ident_span: ident.span,
856 suggestion,
857 }),
858 ),
859 };
860
861 self.dcx().create_err(errs::AttemptToUseNonConstantValueInConstant {
862 span,
863 with,
864 with_label,
865 without,
866 })
867 }
868 ResolutionError::BindingShadowsSomethingUnacceptable {
869 shadowing_binding,
870 name,
871 participle,
872 article,
873 shadowed_binding,
874 shadowed_binding_span,
875 } => self.dcx().create_err(errs::BindingShadowsSomethingUnacceptable {
876 span,
877 shadowing_binding,
878 shadowed_binding,
879 article,
880 sub_suggestion: match (shadowing_binding, shadowed_binding) {
881 (
882 PatternSource::Match,
883 Res::Def(DefKind::Ctor(CtorOf::Variant | CtorOf::Struct, CtorKind::Fn), _),
884 ) => Some(errs::BindingShadowsSomethingUnacceptableSuggestion { span, name }),
885 _ => None,
886 },
887 shadowed_binding_span,
888 participle,
889 name,
890 }),
891 ResolutionError::ForwardDeclaredGenericParam(param, reason) => match reason {
892 ForwardGenericParamBanReason::Default => {
893 self.dcx().create_err(errs::ForwardDeclaredGenericParam { param, span })
894 }
895 ForwardGenericParamBanReason::ConstParamTy => self
896 .dcx()
897 .create_err(errs::ForwardDeclaredGenericInConstParamTy { param, span }),
898 },
899 ResolutionError::ParamInTyOfConstParam { name } => {
900 self.dcx().create_err(errs::ParamInTyOfConstParam { span, name })
901 }
902 ResolutionError::ParamInNonTrivialAnonConst { name, param_kind: is_type } => {
903 self.dcx().create_err(errs::ParamInNonTrivialAnonConst {
904 span,
905 name,
906 param_kind: is_type,
907 help: self
908 .tcx
909 .sess
910 .is_nightly_build()
911 .then_some(errs::ParamInNonTrivialAnonConstHelp),
912 })
913 }
914 ResolutionError::ParamInEnumDiscriminant { name, param_kind: is_type } => self
915 .dcx()
916 .create_err(errs::ParamInEnumDiscriminant { span, name, param_kind: is_type }),
917 ResolutionError::ForwardDeclaredSelf(reason) => match reason {
918 ForwardGenericParamBanReason::Default => {
919 self.dcx().create_err(errs::SelfInGenericParamDefault { span })
920 }
921 ForwardGenericParamBanReason::ConstParamTy => {
922 self.dcx().create_err(errs::SelfInConstGenericTy { span })
923 }
924 },
925 ResolutionError::UnreachableLabel { name, definition_span, suggestion } => {
926 let ((sub_suggestion_label, sub_suggestion), sub_unreachable_label) =
927 match suggestion {
928 Some((ident, true)) => (
930 (
931 Some(errs::UnreachableLabelSubLabel { ident_span: ident.span }),
932 Some(errs::UnreachableLabelSubSuggestion {
933 span,
934 ident_name: ident.name,
937 }),
938 ),
939 None,
940 ),
941 Some((ident, false)) => (
943 (None, None),
944 Some(errs::UnreachableLabelSubLabelUnreachable {
945 ident_span: ident.span,
946 }),
947 ),
948 None => ((None, None), None),
950 };
951 self.dcx().create_err(errs::UnreachableLabel {
952 span,
953 name,
954 definition_span,
955 sub_suggestion,
956 sub_suggestion_label,
957 sub_unreachable_label,
958 })
959 }
960 ResolutionError::TraitImplMismatch {
961 name,
962 kind,
963 code,
964 trait_item_span,
965 trait_path,
966 } => self
967 .dcx()
968 .create_err(errors::TraitImplMismatch {
969 span,
970 name,
971 kind,
972 trait_path,
973 trait_item_span,
974 })
975 .with_code(code),
976 ResolutionError::TraitImplDuplicate { name, trait_item_span, old_span } => self
977 .dcx()
978 .create_err(errs::TraitImplDuplicate { span, name, trait_item_span, old_span }),
979 ResolutionError::InvalidAsmSym => self.dcx().create_err(errs::InvalidAsmSym { span }),
980 ResolutionError::LowercaseSelf => self.dcx().create_err(errs::LowercaseSelf { span }),
981 ResolutionError::BindingInNeverPattern => {
982 self.dcx().create_err(errs::BindingInNeverPattern { span })
983 }
984 }
985 }
986
987 pub(crate) fn report_vis_error(
988 &mut self,
989 vis_resolution_error: VisResolutionError<'_>,
990 ) -> ErrorGuaranteed {
991 match vis_resolution_error {
992 VisResolutionError::Relative2018(span, path) => {
993 self.dcx().create_err(errs::Relative2018 {
994 span,
995 path_span: path.span,
996 path_str: pprust::path_to_string(path),
999 })
1000 }
1001 VisResolutionError::AncestorOnly(span) => {
1002 self.dcx().create_err(errs::AncestorOnly(span))
1003 }
1004 VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error(
1005 span,
1006 ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None },
1007 ),
1008 VisResolutionError::ExpectedFound(span, path_str, res) => {
1009 self.dcx().create_err(errs::ExpectedModuleFound { span, res, path_str })
1010 }
1011 VisResolutionError::Indeterminate(span) => {
1012 self.dcx().create_err(errs::Indeterminate(span))
1013 }
1014 VisResolutionError::ModuleOnly(span) => self.dcx().create_err(errs::ModuleOnly(span)),
1015 }
1016 .emit()
1017 }
1018
1019 fn early_lookup_typo_candidate(
1021 &mut self,
1022 scope_set: ScopeSet<'ra>,
1023 parent_scope: &ParentScope<'ra>,
1024 ident: Ident,
1025 filter_fn: &impl Fn(Res) -> bool,
1026 ) -> Option<TypoSuggestion> {
1027 let mut suggestions = Vec::new();
1028 let ctxt = ident.span.ctxt();
1029 self.visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
1030 match scope {
1031 Scope::DeriveHelpers(expn_id) => {
1032 let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
1033 if filter_fn(res) {
1034 suggestions.extend(
1035 this.helper_attrs
1036 .get(&expn_id)
1037 .into_iter()
1038 .flatten()
1039 .map(|(ident, _)| TypoSuggestion::typo_from_ident(*ident, res)),
1040 );
1041 }
1042 }
1043 Scope::DeriveHelpersCompat => {
1044 let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
1045 if filter_fn(res) {
1046 for derive in parent_scope.derives {
1047 let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
1048 let Ok((Some(ext), _)) = this.resolve_macro_path(
1049 derive,
1050 Some(MacroKind::Derive),
1051 parent_scope,
1052 false,
1053 false,
1054 None,
1055 ) else {
1056 continue;
1057 };
1058 suggestions.extend(
1059 ext.helper_attrs
1060 .iter()
1061 .map(|name| TypoSuggestion::typo_from_name(*name, res)),
1062 );
1063 }
1064 }
1065 }
1066 Scope::MacroRules(macro_rules_scope) => {
1067 if let MacroRulesScope::Binding(macro_rules_binding) = macro_rules_scope.get() {
1068 let res = macro_rules_binding.binding.res();
1069 if filter_fn(res) {
1070 suggestions.push(TypoSuggestion::typo_from_ident(
1071 macro_rules_binding.ident,
1072 res,
1073 ))
1074 }
1075 }
1076 }
1077 Scope::CrateRoot => {
1078 let root_ident = Ident::new(kw::PathRoot, ident.span);
1079 let root_module = this.resolve_crate_root(root_ident);
1080 this.add_module_candidates(root_module, &mut suggestions, filter_fn, None);
1081 }
1082 Scope::Module(module, _) => {
1083 this.add_module_candidates(module, &mut suggestions, filter_fn, None);
1084 }
1085 Scope::MacroUsePrelude => {
1086 suggestions.extend(this.macro_use_prelude.iter().filter_map(
1087 |(name, binding)| {
1088 let res = binding.res();
1089 filter_fn(res).then_some(TypoSuggestion::typo_from_name(*name, res))
1090 },
1091 ));
1092 }
1093 Scope::BuiltinAttrs => {
1094 let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(sym::dummy));
1095 if filter_fn(res) {
1096 suggestions.extend(
1097 BUILTIN_ATTRIBUTES
1098 .iter()
1099 .map(|attr| TypoSuggestion::typo_from_name(attr.name, res)),
1100 );
1101 }
1102 }
1103 Scope::ExternPrelude => {
1104 suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
1105 let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
1106 filter_fn(res).then_some(TypoSuggestion::typo_from_ident(*ident, res))
1107 }));
1108 }
1109 Scope::ToolPrelude => {
1110 let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
1111 suggestions.extend(
1112 this.registered_tools
1113 .iter()
1114 .map(|ident| TypoSuggestion::typo_from_ident(*ident, res)),
1115 );
1116 }
1117 Scope::StdLibPrelude => {
1118 if let Some(prelude) = this.prelude {
1119 let mut tmp_suggestions = Vec::new();
1120 this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn, None);
1121 suggestions.extend(
1122 tmp_suggestions
1123 .into_iter()
1124 .filter(|s| use_prelude.into() || this.is_builtin_macro(s.res)),
1125 );
1126 }
1127 }
1128 Scope::BuiltinTypes => {
1129 suggestions.extend(PrimTy::ALL.iter().filter_map(|prim_ty| {
1130 let res = Res::PrimTy(*prim_ty);
1131 filter_fn(res)
1132 .then_some(TypoSuggestion::typo_from_name(prim_ty.name(), res))
1133 }))
1134 }
1135 }
1136
1137 None::<()>
1138 });
1139
1140 suggestions.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str()));
1142
1143 match find_best_match_for_name(
1144 &suggestions.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),
1145 ident.name,
1146 None,
1147 ) {
1148 Some(found) if found != ident.name => {
1149 suggestions.into_iter().find(|suggestion| suggestion.candidate == found)
1150 }
1151 _ => None,
1152 }
1153 }
1154
1155 fn lookup_import_candidates_from_module<FilterFn>(
1156 &mut self,
1157 lookup_ident: Ident,
1158 namespace: Namespace,
1159 parent_scope: &ParentScope<'ra>,
1160 start_module: Module<'ra>,
1161 crate_path: ThinVec<ast::PathSegment>,
1162 filter_fn: FilterFn,
1163 ) -> Vec<ImportSuggestion>
1164 where
1165 FilterFn: Fn(Res) -> bool,
1166 {
1167 let mut candidates = Vec::new();
1168 let mut seen_modules = FxHashSet::default();
1169 let start_did = start_module.def_id();
1170 let mut worklist = vec![(
1171 start_module,
1172 ThinVec::<ast::PathSegment>::new(),
1173 true,
1174 start_did.is_local() || !self.tcx.is_doc_hidden(start_did),
1175 )];
1176 let mut worklist_via_import = vec![];
1177
1178 while let Some((in_module, path_segments, accessible, doc_visible)) = match worklist.pop() {
1179 None => worklist_via_import.pop(),
1180 Some(x) => Some(x),
1181 } {
1182 let in_module_is_extern = !in_module.def_id().is_local();
1183 in_module.for_each_child(self, |this, ident, ns, name_binding| {
1184 if !name_binding.is_importable()
1186 || name_binding.is_assoc_const_or_fn()
1188 && !this.tcx.features().import_trait_associated_functions()
1189 {
1190 return;
1191 }
1192
1193 if ident.name == kw::Underscore {
1194 return;
1195 }
1196
1197 let child_accessible =
1198 accessible && this.is_accessible_from(name_binding.vis, parent_scope.module);
1199
1200 if in_module_is_extern && !child_accessible {
1202 return;
1203 }
1204
1205 let via_import = name_binding.is_import() && !name_binding.is_extern_crate();
1206
1207 if via_import && name_binding.is_possibly_imported_variant() {
1213 return;
1214 }
1215
1216 if let NameBindingKind::Import { binding, .. } = name_binding.kind
1218 && this.is_accessible_from(binding.vis, parent_scope.module)
1219 && !this.is_accessible_from(name_binding.vis, parent_scope.module)
1220 {
1221 return;
1222 }
1223
1224 let res = name_binding.res();
1225 let did = match res {
1226 Res::Def(DefKind::Ctor(..), did) => this.tcx.opt_parent(did),
1227 _ => res.opt_def_id(),
1228 };
1229 let child_doc_visible = doc_visible
1230 && did.is_none_or(|did| did.is_local() || !this.tcx.is_doc_hidden(did));
1231
1232 if ident.name == lookup_ident.name
1236 && ns == namespace
1237 && in_module != parent_scope.module
1238 && !ident.span.normalize_to_macros_2_0().from_expansion()
1239 && filter_fn(res)
1240 {
1241 let mut segms = if lookup_ident.span.at_least_rust_2018() {
1243 crate_path.clone()
1246 } else {
1247 ThinVec::new()
1248 };
1249 segms.append(&mut path_segments.clone());
1250
1251 segms.push(ast::PathSegment::from_ident(ident));
1252 let path = Path { span: name_binding.span, segments: segms, tokens: None };
1253
1254 if child_accessible
1255 && let Some(idx) = candidates
1257 .iter()
1258 .position(|v: &ImportSuggestion| v.did == did && !v.accessible)
1259 {
1260 candidates.remove(idx);
1261 }
1262
1263 if candidates.iter().all(|v: &ImportSuggestion| v.did != did) {
1264 let note = if let Some(did) = did {
1267 let requires_note = !did.is_local()
1268 && this.tcx.get_attrs(did, sym::rustc_diagnostic_item).any(
1269 |attr| {
1270 [sym::TryInto, sym::TryFrom, sym::FromIterator]
1271 .map(|x| Some(x))
1272 .contains(&attr.value_str())
1273 },
1274 );
1275
1276 requires_note.then(|| {
1277 format!(
1278 "'{}' is included in the prelude starting in Edition 2021",
1279 path_names_to_string(&path)
1280 )
1281 })
1282 } else {
1283 None
1284 };
1285
1286 candidates.push(ImportSuggestion {
1287 did,
1288 descr: res.descr(),
1289 path,
1290 accessible: child_accessible,
1291 doc_visible: child_doc_visible,
1292 note,
1293 via_import,
1294 });
1295 }
1296 }
1297
1298 if let Some(module) = name_binding.module() {
1300 let mut path_segments = path_segments.clone();
1302 path_segments.push(ast::PathSegment::from_ident(ident));
1303
1304 let alias_import = if let NameBindingKind::Import { import, .. } =
1305 name_binding.kind
1306 && let ImportKind::ExternCrate { source: Some(_), .. } = import.kind
1307 && import.parent_scope.expansion == parent_scope.expansion
1308 {
1309 true
1310 } else {
1311 false
1312 };
1313
1314 let is_extern_crate_that_also_appears_in_prelude =
1315 name_binding.is_extern_crate() && lookup_ident.span.at_least_rust_2018();
1316
1317 if !is_extern_crate_that_also_appears_in_prelude || alias_import {
1318 if seen_modules.insert(module.def_id()) {
1320 if via_import { &mut worklist_via_import } else { &mut worklist }
1321 .push((module, path_segments, child_accessible, child_doc_visible));
1322 }
1323 }
1324 }
1325 })
1326 }
1327
1328 candidates
1329 }
1330
1331 pub(crate) fn lookup_import_candidates<FilterFn>(
1339 &mut self,
1340 lookup_ident: Ident,
1341 namespace: Namespace,
1342 parent_scope: &ParentScope<'ra>,
1343 filter_fn: FilterFn,
1344 ) -> Vec<ImportSuggestion>
1345 where
1346 FilterFn: Fn(Res) -> bool,
1347 {
1348 let crate_path = thin_vec![ast::PathSegment::from_ident(Ident::with_dummy_span(kw::Crate))];
1349 let mut suggestions = self.lookup_import_candidates_from_module(
1350 lookup_ident,
1351 namespace,
1352 parent_scope,
1353 self.graph_root,
1354 crate_path,
1355 &filter_fn,
1356 );
1357
1358 if lookup_ident.span.at_least_rust_2018() {
1359 for ident in self.extern_prelude.clone().into_keys() {
1360 if ident.span.from_expansion() {
1361 continue;
1367 }
1368 let Some(crate_id) = self.crate_loader(|c| c.maybe_process_path_extern(ident.name))
1369 else {
1370 continue;
1371 };
1372
1373 let crate_def_id = crate_id.as_def_id();
1374 let crate_root = self.expect_module(crate_def_id);
1375
1376 let needs_disambiguation =
1380 self.resolutions(parent_scope.module).borrow().iter().any(
1381 |(key, name_resolution)| {
1382 if key.ns == TypeNS
1383 && key.ident == ident
1384 && let Some(binding) = name_resolution.borrow().binding
1385 {
1386 match binding.res() {
1387 Res::Def(_, def_id) => def_id != crate_def_id,
1390 Res::PrimTy(_) => true,
1391 _ => false,
1392 }
1393 } else {
1394 false
1395 }
1396 },
1397 );
1398 let mut crate_path = ThinVec::new();
1399 if needs_disambiguation {
1400 crate_path.push(ast::PathSegment::path_root(rustc_span::DUMMY_SP));
1401 }
1402 crate_path.push(ast::PathSegment::from_ident(ident));
1403
1404 suggestions.extend(self.lookup_import_candidates_from_module(
1405 lookup_ident,
1406 namespace,
1407 parent_scope,
1408 crate_root,
1409 crate_path,
1410 &filter_fn,
1411 ));
1412 }
1413 }
1414
1415 suggestions
1416 }
1417
1418 pub(crate) fn unresolved_macro_suggestions(
1419 &mut self,
1420 err: &mut Diag<'_>,
1421 macro_kind: MacroKind,
1422 parent_scope: &ParentScope<'ra>,
1423 ident: Ident,
1424 krate: &Crate,
1425 ) {
1426 let is_expected = &|res: Res| res.macro_kind() == Some(macro_kind);
1427 let suggestion = self.early_lookup_typo_candidate(
1428 ScopeSet::Macro(macro_kind),
1429 parent_scope,
1430 ident,
1431 is_expected,
1432 );
1433 self.add_typo_suggestion(err, suggestion, ident.span);
1434
1435 let import_suggestions =
1436 self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected);
1437 let (span, found_use) = match parent_scope.module.nearest_parent_mod().as_local() {
1438 Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id)),
1439 None => (None, FoundUse::No),
1440 };
1441 show_candidates(
1442 self.tcx,
1443 err,
1444 span,
1445 &import_suggestions,
1446 Instead::No,
1447 found_use,
1448 DiagMode::Normal,
1449 vec![],
1450 "",
1451 );
1452
1453 if macro_kind == MacroKind::Bang && ident.name == sym::macro_rules {
1454 let label_span = ident.span.shrink_to_hi();
1455 let mut spans = MultiSpan::from_span(label_span);
1456 spans.push_span_label(label_span, "put a macro name here");
1457 err.subdiagnostic(MaybeMissingMacroRulesName { spans });
1458 return;
1459 }
1460
1461 if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
1462 err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
1463 return;
1464 }
1465
1466 let unused_macro = self.unused_macros.iter().find_map(|(def_id, (_, unused_ident))| {
1467 if unused_ident.name == ident.name { Some((def_id, unused_ident)) } else { None }
1468 });
1469
1470 if let Some((def_id, unused_ident)) = unused_macro {
1471 let scope = self.local_macro_def_scopes[&def_id];
1472 let parent_nearest = parent_scope.module.nearest_parent_mod();
1473 if Some(parent_nearest) == scope.opt_def_id() {
1474 match macro_kind {
1475 MacroKind::Bang => {
1476 err.subdiagnostic(MacroDefinedLater { span: unused_ident.span });
1477 err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident });
1478 }
1479 MacroKind::Attr => {
1480 err.subdiagnostic(MacroRulesNot::Attr { span: unused_ident.span, ident });
1481 }
1482 MacroKind::Derive => {
1483 err.subdiagnostic(MacroRulesNot::Derive { span: unused_ident.span, ident });
1484 }
1485 }
1486
1487 return;
1488 }
1489 }
1490
1491 if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
1492 err.subdiagnostic(AddedMacroUse);
1493 return;
1494 }
1495
1496 if ident.name == kw::Default
1497 && let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
1498 {
1499 let span = self.def_span(def_id);
1500 let source_map = self.tcx.sess.source_map();
1501 let head_span = source_map.guess_head_span(span);
1502 err.subdiagnostic(ConsiderAddingADerive {
1503 span: head_span.shrink_to_lo(),
1504 suggestion: "#[derive(Default)]\n".to_string(),
1505 });
1506 }
1507 for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
1508 let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
1509 ident,
1510 ScopeSet::All(ns),
1511 parent_scope,
1512 None,
1513 false,
1514 None,
1515 None,
1516 ) else {
1517 continue;
1518 };
1519
1520 let desc = match binding.res() {
1521 Res::Def(DefKind::Macro(MacroKind::Bang), _) => "a function-like macro".to_string(),
1522 Res::Def(DefKind::Macro(MacroKind::Attr), _) | Res::NonMacroAttr(..) => {
1523 format!("an attribute: `#[{ident}]`")
1524 }
1525 Res::Def(DefKind::Macro(MacroKind::Derive), _) => {
1526 format!("a derive macro: `#[derive({ident})]`")
1527 }
1528 Res::ToolMod => {
1529 continue;
1531 }
1532 Res::Def(DefKind::Trait, _) if macro_kind == MacroKind::Derive => {
1533 "only a trait, without a derive macro".to_string()
1534 }
1535 res => format!(
1536 "{} {}, not {} {}",
1537 res.article(),
1538 res.descr(),
1539 macro_kind.article(),
1540 macro_kind.descr_expected(),
1541 ),
1542 };
1543 if let crate::NameBindingKind::Import { import, .. } = binding.kind
1544 && !import.span.is_dummy()
1545 {
1546 let note = errors::IdentImporterHereButItIsDesc {
1547 span: import.span,
1548 imported_ident: ident,
1549 imported_ident_desc: &desc,
1550 };
1551 err.subdiagnostic(note);
1552 self.record_use(ident, binding, Used::Other);
1555 return;
1556 }
1557 let note = errors::IdentInScopeButItIsDesc {
1558 imported_ident: ident,
1559 imported_ident_desc: &desc,
1560 };
1561 err.subdiagnostic(note);
1562 return;
1563 }
1564 }
1565
1566 pub(crate) fn add_typo_suggestion(
1567 &self,
1568 err: &mut Diag<'_>,
1569 suggestion: Option<TypoSuggestion>,
1570 span: Span,
1571 ) -> bool {
1572 let suggestion = match suggestion {
1573 None => return false,
1574 Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
1576 Some(suggestion) => suggestion,
1577 };
1578
1579 let mut did_label_def_span = false;
1580
1581 if let Some(def_span) = suggestion.res.opt_def_id().map(|def_id| self.def_span(def_id)) {
1582 if span.overlaps(def_span) {
1583 return false;
1602 }
1603 let span = self.tcx.sess.source_map().guess_head_span(def_span);
1604 let candidate_descr = suggestion.res.descr();
1605 let candidate = suggestion.candidate;
1606 let label = match suggestion.target {
1607 SuggestionTarget::SimilarlyNamed => {
1608 errors::DefinedHere::SimilarlyNamed { span, candidate_descr, candidate }
1609 }
1610 SuggestionTarget::SingleItem => {
1611 errors::DefinedHere::SingleItem { span, candidate_descr, candidate }
1612 }
1613 };
1614 did_label_def_span = true;
1615 err.subdiagnostic(label);
1616 }
1617
1618 let (span, msg, sugg) = if let SuggestionTarget::SimilarlyNamed = suggestion.target
1619 && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
1620 && let Some(span) = suggestion.span
1621 && let Some(candidate) = suggestion.candidate.as_str().strip_prefix('_')
1622 && snippet == candidate
1623 {
1624 let candidate = suggestion.candidate;
1625 let msg = format!(
1628 "the leading underscore in `{candidate}` marks it as unused, consider renaming it to `{snippet}`"
1629 );
1630 if !did_label_def_span {
1631 err.span_label(span, format!("`{candidate}` defined here"));
1632 }
1633 (span, msg, snippet)
1634 } else {
1635 let msg = match suggestion.target {
1636 SuggestionTarget::SimilarlyNamed => format!(
1637 "{} {} with a similar name exists",
1638 suggestion.res.article(),
1639 suggestion.res.descr()
1640 ),
1641 SuggestionTarget::SingleItem => {
1642 format!("maybe you meant this {}", suggestion.res.descr())
1643 }
1644 };
1645 (span, msg, suggestion.candidate.to_ident_string())
1646 };
1647 err.span_suggestion(span, msg, sugg, Applicability::MaybeIncorrect);
1648 true
1649 }
1650
1651 fn binding_description(&self, b: NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
1652 let res = b.res();
1653 if b.span.is_dummy() || !self.tcx.sess.source_map().is_span_accessible(b.span) {
1654 let add_built_in =
1656 !matches!(b.res(), Res::NonMacroAttr(..) | Res::PrimTy(..) | Res::ToolMod);
1657 let (built_in, from) = if from_prelude {
1658 ("", " from prelude")
1659 } else if b.is_extern_crate()
1660 && !b.is_import()
1661 && self.tcx.sess.opts.externs.get(ident.as_str()).is_some()
1662 {
1663 ("", " passed with `--extern`")
1664 } else if add_built_in {
1665 (" built-in", "")
1666 } else {
1667 ("", "")
1668 };
1669
1670 let a = if built_in.is_empty() { res.article() } else { "a" };
1671 format!("{a}{built_in} {thing}{from}", thing = res.descr())
1672 } else {
1673 let introduced = if b.is_import_user_facing() { "imported" } else { "defined" };
1674 format!("the {thing} {introduced} here", thing = res.descr())
1675 }
1676 }
1677
1678 fn ambiguity_diagnostics(&self, ambiguity_error: &AmbiguityError<'_>) -> AmbiguityErrorDiag {
1679 let AmbiguityError { kind, ident, b1, b2, misc1, misc2, .. } = *ambiguity_error;
1680 let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() {
1681 (b2, b1, misc2, misc1, true)
1683 } else {
1684 (b1, b2, misc1, misc2, false)
1685 };
1686 let could_refer_to = |b: NameBinding<'_>, misc: AmbiguityErrorMisc, also: &str| {
1687 let what = self.binding_description(b, ident, misc == AmbiguityErrorMisc::FromPrelude);
1688 let note_msg = format!("`{ident}` could{also} refer to {what}");
1689
1690 let thing = b.res().descr();
1691 let mut help_msgs = Vec::new();
1692 if b.is_glob_import()
1693 && (kind == AmbiguityKind::GlobVsGlob
1694 || kind == AmbiguityKind::GlobVsExpanded
1695 || kind == AmbiguityKind::GlobVsOuter && swapped != also.is_empty())
1696 {
1697 help_msgs.push(format!(
1698 "consider adding an explicit import of `{ident}` to disambiguate"
1699 ))
1700 }
1701 if b.is_extern_crate() && ident.span.at_least_rust_2018() {
1702 help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously"))
1703 }
1704 match misc {
1705 AmbiguityErrorMisc::SuggestCrate => help_msgs
1706 .push(format!("use `crate::{ident}` to refer to this {thing} unambiguously")),
1707 AmbiguityErrorMisc::SuggestSelf => help_msgs
1708 .push(format!("use `self::{ident}` to refer to this {thing} unambiguously")),
1709 AmbiguityErrorMisc::FromPrelude | AmbiguityErrorMisc::None => {}
1710 }
1711
1712 (
1713 b.span,
1714 note_msg,
1715 help_msgs
1716 .iter()
1717 .enumerate()
1718 .map(|(i, help_msg)| {
1719 let or = if i == 0 { "" } else { "or " };
1720 format!("{or}{help_msg}")
1721 })
1722 .collect::<Vec<_>>(),
1723 )
1724 };
1725 let (b1_span, b1_note_msg, b1_help_msgs) = could_refer_to(b1, misc1, "");
1726 let (b2_span, b2_note_msg, b2_help_msgs) = could_refer_to(b2, misc2, " also");
1727
1728 AmbiguityErrorDiag {
1729 msg: format!("`{ident}` is ambiguous"),
1730 span: ident.span,
1731 label_span: ident.span,
1732 label_msg: "ambiguous name".to_string(),
1733 note_msg: format!("ambiguous because of {}", kind.descr()),
1734 b1_span,
1735 b1_note_msg,
1736 b1_help_msgs,
1737 b2_span,
1738 b2_note_msg,
1739 b2_help_msgs,
1740 }
1741 }
1742
1743 fn ctor_fields_span(&self, binding: NameBinding<'_>) -> Option<Span> {
1746 let NameBindingKind::Res(Res::Def(
1747 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn),
1748 ctor_def_id,
1749 )) = binding.kind
1750 else {
1751 return None;
1752 };
1753
1754 let def_id = self.tcx.parent(ctor_def_id);
1755 self.field_idents(def_id)?.iter().map(|&f| f.span).reduce(Span::to) }
1757
1758 fn report_privacy_error(&mut self, privacy_error: &PrivacyError<'ra>) {
1759 let PrivacyError { ident, binding, outermost_res, parent_scope, single_nested, dedup_span } =
1760 *privacy_error;
1761
1762 let res = binding.res();
1763 let ctor_fields_span = self.ctor_fields_span(binding);
1764 let plain_descr = res.descr().to_string();
1765 let nonimport_descr =
1766 if ctor_fields_span.is_some() { plain_descr + " constructor" } else { plain_descr };
1767 let import_descr = nonimport_descr.clone() + " import";
1768 let get_descr =
1769 |b: NameBinding<'_>| if b.is_import() { &import_descr } else { &nonimport_descr };
1770
1771 let ident_descr = get_descr(binding);
1773 let mut err =
1774 self.dcx().create_err(errors::IsPrivate { span: ident.span, ident_descr, ident });
1775
1776 let mut not_publicly_reexported = false;
1777 if let Some((this_res, outer_ident)) = outermost_res {
1778 let import_suggestions = self.lookup_import_candidates(
1779 outer_ident,
1780 this_res.ns().unwrap_or(Namespace::TypeNS),
1781 &parent_scope,
1782 &|res: Res| res == this_res,
1783 );
1784 let point_to_def = !show_candidates(
1785 self.tcx,
1786 &mut err,
1787 Some(dedup_span.until(outer_ident.span.shrink_to_hi())),
1788 &import_suggestions,
1789 Instead::Yes,
1790 FoundUse::Yes,
1791 DiagMode::Import { append: single_nested, unresolved_import: false },
1792 vec![],
1793 "",
1794 );
1795 if point_to_def && ident.span != outer_ident.span {
1797 not_publicly_reexported = true;
1798 let label = errors::OuterIdentIsNotPubliclyReexported {
1799 span: outer_ident.span,
1800 outer_ident_descr: this_res.descr(),
1801 outer_ident,
1802 };
1803 err.subdiagnostic(label);
1804 }
1805 }
1806
1807 let mut non_exhaustive = None;
1808 if let Some(def_id) = res.opt_def_id()
1812 && !def_id.is_local()
1813 && let Some(attr) = self.tcx.get_attr(def_id, sym::non_exhaustive)
1814 {
1815 non_exhaustive = Some(attr.span());
1816 } else if let Some(span) = ctor_fields_span {
1817 let label = errors::ConstructorPrivateIfAnyFieldPrivate { span };
1818 err.subdiagnostic(label);
1819 if let Res::Def(_, d) = res
1820 && let Some(fields) = self.field_visibility_spans.get(&d)
1821 {
1822 let spans = fields.iter().map(|span| *span).collect();
1823 let sugg =
1824 errors::ConsiderMakingTheFieldPublic { spans, number_of_fields: fields.len() };
1825 err.subdiagnostic(sugg);
1826 }
1827 }
1828
1829 let mut sugg_paths = vec![];
1830 if let Some(mut def_id) = res.opt_def_id() {
1831 let mut path = vec![def_id];
1833 while let Some(parent) = self.tcx.opt_parent(def_id) {
1834 def_id = parent;
1835 if !def_id.is_top_level_module() {
1836 path.push(def_id);
1837 } else {
1838 break;
1839 }
1840 }
1841 let path_names: Option<Vec<String>> = path
1843 .iter()
1844 .rev()
1845 .map(|def_id| {
1846 self.tcx.opt_item_name(*def_id).map(|n| {
1847 if def_id.is_top_level_module() {
1848 "crate".to_string()
1849 } else {
1850 n.to_string()
1851 }
1852 })
1853 })
1854 .collect();
1855 if let Some(def_id) = path.get(0)
1856 && let Some(path) = path_names
1857 {
1858 if let Some(def_id) = def_id.as_local() {
1859 if self.effective_visibilities.is_directly_public(def_id) {
1860 sugg_paths.push((path, false));
1861 }
1862 } else if self.is_accessible_from(self.tcx.visibility(def_id), parent_scope.module)
1863 {
1864 sugg_paths.push((path, false));
1865 }
1866 }
1867 }
1868
1869 let first_binding = binding;
1871 let mut next_binding = Some(binding);
1872 let mut next_ident = ident;
1873 let mut path = vec![];
1874 while let Some(binding) = next_binding {
1875 let name = next_ident;
1876 next_binding = match binding.kind {
1877 _ if res == Res::Err => None,
1878 NameBindingKind::Import { binding, import, .. } => match import.kind {
1879 _ if binding.span.is_dummy() => None,
1880 ImportKind::Single { source, .. } => {
1881 next_ident = source;
1882 Some(binding)
1883 }
1884 ImportKind::Glob { .. }
1885 | ImportKind::MacroUse { .. }
1886 | ImportKind::MacroExport => Some(binding),
1887 ImportKind::ExternCrate { .. } => None,
1888 },
1889 _ => None,
1890 };
1891
1892 match binding.kind {
1893 NameBindingKind::Import { import, .. } => {
1894 for segment in import.module_path.iter().skip(1) {
1895 path.push(segment.ident.to_string());
1896 }
1897 sugg_paths.push((
1898 path.iter()
1899 .cloned()
1900 .chain(vec![ident.to_string()].into_iter())
1901 .collect::<Vec<_>>(),
1902 true, ));
1904 }
1905 NameBindingKind::Res(_) | NameBindingKind::Module(_) => {}
1906 }
1907 let first = binding == first_binding;
1908 let def_span = self.tcx.sess.source_map().guess_head_span(binding.span);
1909 let mut note_span = MultiSpan::from_span(def_span);
1910 if !first && binding.vis.is_public() {
1911 let desc = match binding.kind {
1912 NameBindingKind::Import { .. } => "re-export",
1913 _ => "directly",
1914 };
1915 note_span.push_span_label(def_span, format!("you could import this {desc}"));
1916 }
1917 if next_binding.is_none()
1920 && let Some(span) = non_exhaustive
1921 {
1922 note_span.push_span_label(
1923 span,
1924 "cannot be constructed because it is `#[non_exhaustive]`",
1925 );
1926 }
1927 let note = errors::NoteAndRefersToTheItemDefinedHere {
1928 span: note_span,
1929 binding_descr: get_descr(binding),
1930 binding_name: name,
1931 first,
1932 dots: next_binding.is_some(),
1933 };
1934 err.subdiagnostic(note);
1935 }
1936 sugg_paths.sort_by_key(|(p, reexport)| (p.len(), p[0] == "core", *reexport));
1938 for (sugg, reexport) in sugg_paths {
1939 if not_publicly_reexported {
1940 break;
1941 }
1942 if sugg.len() <= 1 {
1943 continue;
1946 }
1947 let path = sugg.join("::");
1948 let sugg = if reexport {
1949 errors::ImportIdent::ThroughReExport { span: dedup_span, ident, path }
1950 } else {
1951 errors::ImportIdent::Directly { span: dedup_span, ident, path }
1952 };
1953 err.subdiagnostic(sugg);
1954 break;
1955 }
1956
1957 err.emit();
1958 }
1959
1960 pub(crate) fn find_similarly_named_module_or_crate(
1961 &mut self,
1962 ident: Symbol,
1963 current_module: Module<'ra>,
1964 ) -> Option<Symbol> {
1965 let mut candidates = self
1966 .extern_prelude
1967 .keys()
1968 .map(|ident| ident.name)
1969 .chain(
1970 self.module_map
1971 .iter()
1972 .filter(|(_, module)| {
1973 current_module.is_ancestor_of(**module) && current_module != **module
1974 })
1975 .flat_map(|(_, module)| module.kind.name()),
1976 )
1977 .filter(|c| !c.to_string().is_empty())
1978 .collect::<Vec<_>>();
1979 candidates.sort();
1980 candidates.dedup();
1981 find_best_match_for_name(&candidates, ident, None).filter(|sugg| *sugg != ident)
1982 }
1983
1984 pub(crate) fn report_path_resolution_error(
1985 &mut self,
1986 path: &[Segment],
1987 opt_ns: Option<Namespace>, parent_scope: &ParentScope<'ra>,
1989 ribs: Option<&PerNS<Vec<Rib<'ra>>>>,
1990 ignore_binding: Option<NameBinding<'ra>>,
1991 ignore_import: Option<Import<'ra>>,
1992 module: Option<ModuleOrUniformRoot<'ra>>,
1993 failed_segment_idx: usize,
1994 ident: Ident,
1995 ) -> (String, Option<Suggestion>) {
1996 let is_last = failed_segment_idx == path.len() - 1;
1997 let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
1998 let module_res = match module {
1999 Some(ModuleOrUniformRoot::Module(module)) => module.res(),
2000 _ => None,
2001 };
2002 if module_res == self.graph_root.res() {
2003 let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
2004 let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
2005 candidates
2006 .sort_by_cached_key(|c| (c.path.segments.len(), pprust::path_to_string(&c.path)));
2007 if let Some(candidate) = candidates.get(0) {
2008 let path = {
2009 let len = candidate.path.segments.len();
2011 let start_index = (0..=failed_segment_idx.min(len - 1))
2012 .find(|&i| path[i].ident.name != candidate.path.segments[i].ident.name)
2013 .unwrap_or_default();
2014 let segments =
2015 (start_index..len).map(|s| candidate.path.segments[s].clone()).collect();
2016 Path { segments, span: Span::default(), tokens: None }
2017 };
2018 (
2019 String::from("unresolved import"),
2020 Some((
2021 vec![(ident.span, pprust::path_to_string(&path))],
2022 String::from("a similar path exists"),
2023 Applicability::MaybeIncorrect,
2024 )),
2025 )
2026 } else if ident.name == sym::core {
2027 (
2028 format!("you might be missing crate `{ident}`"),
2029 Some((
2030 vec![(ident.span, "std".to_string())],
2031 "try using `std` instead of `core`".to_string(),
2032 Applicability::MaybeIncorrect,
2033 )),
2034 )
2035 } else if ident.name == kw::Underscore {
2036 (format!("`_` is not a valid crate or module name"), None)
2037 } else if self.tcx.sess.is_rust_2015() {
2038 (
2039 format!("use of unresolved module or unlinked crate `{ident}`"),
2040 Some((
2041 vec![(
2042 self.current_crate_outer_attr_insert_span,
2043 format!("extern crate {ident};\n"),
2044 )],
2045 if was_invoked_from_cargo() {
2046 format!(
2047 "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2048 to add it to your `Cargo.toml` and import it in your code",
2049 )
2050 } else {
2051 format!(
2052 "you might be missing a crate named `{ident}`, add it to your \
2053 project and import it in your code",
2054 )
2055 },
2056 Applicability::MaybeIncorrect,
2057 )),
2058 )
2059 } else {
2060 (format!("could not find `{ident}` in the crate root"), None)
2061 }
2062 } else if failed_segment_idx > 0 {
2063 let parent = path[failed_segment_idx - 1].ident.name;
2064 let parent = match parent {
2065 kw::PathRoot if self.tcx.sess.edition() > Edition::Edition2015 => {
2068 "the list of imported crates".to_owned()
2069 }
2070 kw::PathRoot | kw::Crate => "the crate root".to_owned(),
2071 _ => format!("`{parent}`"),
2072 };
2073
2074 let mut msg = format!("could not find `{ident}` in {parent}");
2075 if ns == TypeNS || ns == ValueNS {
2076 let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
2077 let binding = if let Some(module) = module {
2078 self.resolve_ident_in_module(
2079 module,
2080 ident,
2081 ns_to_try,
2082 parent_scope,
2083 None,
2084 ignore_binding,
2085 ignore_import,
2086 )
2087 .ok()
2088 } else if let Some(ribs) = ribs
2089 && let Some(TypeNS | ValueNS) = opt_ns
2090 {
2091 assert!(ignore_import.is_none());
2092 match self.resolve_ident_in_lexical_scope(
2093 ident,
2094 ns_to_try,
2095 parent_scope,
2096 None,
2097 &ribs[ns_to_try],
2098 ignore_binding,
2099 ) {
2100 Some(LexicalScopeBinding::Item(binding)) => Some(binding),
2102 _ => None,
2103 }
2104 } else {
2105 self.early_resolve_ident_in_lexical_scope(
2106 ident,
2107 ScopeSet::All(ns_to_try),
2108 parent_scope,
2109 None,
2110 false,
2111 ignore_binding,
2112 ignore_import,
2113 )
2114 .ok()
2115 };
2116 if let Some(binding) = binding {
2117 let mut found = |what| {
2118 msg = format!(
2119 "expected {}, found {} `{}` in {}",
2120 ns.descr(),
2121 what,
2122 ident,
2123 parent
2124 )
2125 };
2126 if binding.module().is_some() {
2127 found("module")
2128 } else {
2129 match binding.res() {
2130 Res::Def(kind, id) => found(kind.descr(id)),
2133 _ => found(ns_to_try.descr()),
2134 }
2135 }
2136 };
2137 }
2138 (msg, None)
2139 } else if ident.name == kw::SelfUpper {
2140 if opt_ns.is_none() {
2144 ("`Self` cannot be used in imports".to_string(), None)
2145 } else {
2146 (
2147 "`Self` is only available in impls, traits, and type definitions".to_string(),
2148 None,
2149 )
2150 }
2151 } else if ident.name.as_str().chars().next().is_some_and(|c| c.is_ascii_uppercase()) {
2152 let binding = if let Some(ribs) = ribs {
2154 assert!(ignore_import.is_none());
2155 self.resolve_ident_in_lexical_scope(
2156 ident,
2157 ValueNS,
2158 parent_scope,
2159 None,
2160 &ribs[ValueNS],
2161 ignore_binding,
2162 )
2163 } else {
2164 None
2165 };
2166 let match_span = match binding {
2167 Some(LexicalScopeBinding::Res(Res::Local(id))) => {
2176 Some(*self.pat_span_map.get(&id).unwrap())
2177 }
2178 Some(LexicalScopeBinding::Item(name_binding)) => Some(name_binding.span),
2190 _ => None,
2191 };
2192 let suggestion = match_span.map(|span| {
2193 (
2194 vec![(span, String::from(""))],
2195 format!("`{ident}` is defined here, but is not a type"),
2196 Applicability::MaybeIncorrect,
2197 )
2198 });
2199
2200 (format!("use of undeclared type `{ident}`"), suggestion)
2201 } else {
2202 let mut suggestion = None;
2203 if ident.name == sym::alloc {
2204 suggestion = Some((
2205 vec![],
2206 String::from("add `extern crate alloc` to use the `alloc` crate"),
2207 Applicability::MaybeIncorrect,
2208 ))
2209 }
2210
2211 suggestion = suggestion.or_else(|| {
2212 self.find_similarly_named_module_or_crate(ident.name, parent_scope.module).map(
2213 |sugg| {
2214 (
2215 vec![(ident.span, sugg.to_string())],
2216 String::from("there is a crate or module with a similar name"),
2217 Applicability::MaybeIncorrect,
2218 )
2219 },
2220 )
2221 });
2222 if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
2223 ident,
2224 ScopeSet::All(ValueNS),
2225 parent_scope,
2226 None,
2227 false,
2228 ignore_binding,
2229 ignore_import,
2230 ) {
2231 let descr = binding.res().descr();
2232 (format!("{descr} `{ident}` is not a crate or module"), suggestion)
2233 } else {
2234 let suggestion = if suggestion.is_some() {
2235 suggestion
2236 } else if was_invoked_from_cargo() {
2237 Some((
2238 vec![],
2239 format!(
2240 "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2241 to add it to your `Cargo.toml`",
2242 ),
2243 Applicability::MaybeIncorrect,
2244 ))
2245 } else {
2246 Some((
2247 vec![],
2248 format!("you might be missing a crate named `{ident}`",),
2249 Applicability::MaybeIncorrect,
2250 ))
2251 };
2252 (format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
2253 }
2254 }
2255 }
2256
2257 #[instrument(level = "debug", skip(self, parent_scope))]
2259 pub(crate) fn make_path_suggestion(
2260 &mut self,
2261 mut path: Vec<Segment>,
2262 parent_scope: &ParentScope<'ra>,
2263 ) -> Option<(Vec<Segment>, Option<String>)> {
2264 match path[..] {
2265 [first, second, ..]
2268 if first.ident.name == kw::PathRoot && !second.ident.is_path_segment_keyword() => {}
2269 [first, ..]
2271 if first.ident.span.at_least_rust_2018()
2272 && !first.ident.is_path_segment_keyword() =>
2273 {
2274 path.insert(0, Segment::from_ident(Ident::dummy()));
2276 }
2277 _ => return None,
2278 }
2279
2280 self.make_missing_self_suggestion(path.clone(), parent_scope)
2281 .or_else(|| self.make_missing_crate_suggestion(path.clone(), parent_scope))
2282 .or_else(|| self.make_missing_super_suggestion(path.clone(), parent_scope))
2283 .or_else(|| self.make_external_crate_suggestion(path, parent_scope))
2284 }
2285
2286 #[instrument(level = "debug", skip(self, parent_scope))]
2294 fn make_missing_self_suggestion(
2295 &mut self,
2296 mut path: Vec<Segment>,
2297 parent_scope: &ParentScope<'ra>,
2298 ) -> Option<(Vec<Segment>, Option<String>)> {
2299 path[0].ident.name = kw::SelfLower;
2301 let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2302 debug!(?path, ?result);
2303 if let PathResult::Module(..) = result { Some((path, None)) } else { None }
2304 }
2305
2306 #[instrument(level = "debug", skip(self, parent_scope))]
2314 fn make_missing_crate_suggestion(
2315 &mut self,
2316 mut path: Vec<Segment>,
2317 parent_scope: &ParentScope<'ra>,
2318 ) -> Option<(Vec<Segment>, Option<String>)> {
2319 path[0].ident.name = kw::Crate;
2321 let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2322 debug!(?path, ?result);
2323 if let PathResult::Module(..) = result {
2324 Some((
2325 path,
2326 Some(
2327 "`use` statements changed in Rust 2018; read more at \
2328 <https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-\
2329 clarity.html>"
2330 .to_string(),
2331 ),
2332 ))
2333 } else {
2334 None
2335 }
2336 }
2337
2338 #[instrument(level = "debug", skip(self, parent_scope))]
2346 fn make_missing_super_suggestion(
2347 &mut self,
2348 mut path: Vec<Segment>,
2349 parent_scope: &ParentScope<'ra>,
2350 ) -> Option<(Vec<Segment>, Option<String>)> {
2351 path[0].ident.name = kw::Super;
2353 let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2354 debug!(?path, ?result);
2355 if let PathResult::Module(..) = result { Some((path, None)) } else { None }
2356 }
2357
2358 #[instrument(level = "debug", skip(self, parent_scope))]
2369 fn make_external_crate_suggestion(
2370 &mut self,
2371 mut path: Vec<Segment>,
2372 parent_scope: &ParentScope<'ra>,
2373 ) -> Option<(Vec<Segment>, Option<String>)> {
2374 if path[1].ident.span.is_rust_2015() {
2375 return None;
2376 }
2377
2378 let mut extern_crate_names =
2382 self.extern_prelude.keys().map(|ident| ident.name).collect::<Vec<_>>();
2383 extern_crate_names.sort_by(|a, b| b.as_str().cmp(a.as_str()));
2384
2385 for name in extern_crate_names.into_iter() {
2386 path[0].ident.name = name;
2388 let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2389 debug!(?path, ?name, ?result);
2390 if let PathResult::Module(..) = result {
2391 return Some((path, None));
2392 }
2393 }
2394
2395 None
2396 }
2397
2398 pub(crate) fn check_for_module_export_macro(
2411 &mut self,
2412 import: Import<'ra>,
2413 module: ModuleOrUniformRoot<'ra>,
2414 ident: Ident,
2415 ) -> Option<(Option<Suggestion>, Option<String>)> {
2416 let ModuleOrUniformRoot::Module(mut crate_module) = module else {
2417 return None;
2418 };
2419
2420 while let Some(parent) = crate_module.parent {
2421 crate_module = parent;
2422 }
2423
2424 if module == ModuleOrUniformRoot::Module(crate_module) {
2425 return None;
2427 }
2428
2429 let resolutions = self.resolutions(crate_module).borrow();
2430 let binding_key = BindingKey::new(ident, MacroNS);
2431 let resolution = resolutions.get(&binding_key)?;
2432 let binding = resolution.borrow().binding()?;
2433 let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() else {
2434 return None;
2435 };
2436 let module_name = crate_module.kind.name().unwrap_or(kw::Empty);
2437 let import_snippet = match import.kind {
2438 ImportKind::Single { source, target, .. } if source != target => {
2439 format!("{source} as {target}")
2440 }
2441 _ => format!("{ident}"),
2442 };
2443
2444 let mut corrections: Vec<(Span, String)> = Vec::new();
2445 if !import.is_nested() {
2446 corrections.push((import.span, format!("{module_name}::{import_snippet}")));
2449 } else {
2450 let (found_closing_brace, binding_span) = find_span_of_binding_until_next_binding(
2454 self.tcx.sess,
2455 import.span,
2456 import.use_span,
2457 );
2458 debug!(found_closing_brace, ?binding_span);
2459
2460 let mut removal_span = binding_span;
2461
2462 if found_closing_brace
2470 && let Some(previous_span) =
2471 extend_span_to_previous_binding(self.tcx.sess, binding_span)
2472 {
2473 debug!(?previous_span);
2474 removal_span = removal_span.with_lo(previous_span.lo());
2475 }
2476 debug!(?removal_span);
2477
2478 corrections.push((removal_span, "".to_string()));
2480
2481 let (has_nested, after_crate_name) =
2488 find_span_immediately_after_crate_name(self.tcx.sess, import.use_span);
2489 debug!(has_nested, ?after_crate_name);
2490
2491 let source_map = self.tcx.sess.source_map();
2492
2493 let is_definitely_crate = import
2495 .module_path
2496 .first()
2497 .is_some_and(|f| f.ident.name != kw::SelfLower && f.ident.name != kw::Super);
2498
2499 let start_point = source_map.start_point(after_crate_name);
2501 if is_definitely_crate
2502 && let Ok(start_snippet) = source_map.span_to_snippet(start_point)
2503 {
2504 corrections.push((
2505 start_point,
2506 if has_nested {
2507 format!("{start_snippet}{import_snippet}, ")
2509 } else {
2510 format!("{{{import_snippet}, {start_snippet}")
2513 },
2514 ));
2515
2516 if !has_nested {
2518 corrections.push((source_map.end_point(after_crate_name), "};".to_string()));
2519 }
2520 } else {
2521 corrections.push((
2523 import.use_span.shrink_to_lo(),
2524 format!("use {module_name}::{import_snippet};\n"),
2525 ));
2526 }
2527 }
2528
2529 let suggestion = Some((
2530 corrections,
2531 String::from("a macro with this name exists at the root of the crate"),
2532 Applicability::MaybeIncorrect,
2533 ));
2534 Some((
2535 suggestion,
2536 Some(
2537 "this could be because a macro annotated with `#[macro_export]` will be exported \
2538 at the root of the crate instead of the module where it is defined"
2539 .to_string(),
2540 ),
2541 ))
2542 }
2543
2544 pub(crate) fn find_cfg_stripped(&self, err: &mut Diag<'_>, segment: &Symbol, module: DefId) {
2546 let local_items;
2547 let symbols = if module.is_local() {
2548 local_items = self
2549 .stripped_cfg_items
2550 .iter()
2551 .filter_map(|item| {
2552 let parent_module = self.opt_local_def_id(item.parent_module)?.to_def_id();
2553 Some(StrippedCfgItem {
2554 parent_module,
2555 ident: item.ident,
2556 cfg: item.cfg.clone(),
2557 })
2558 })
2559 .collect::<Vec<_>>();
2560 local_items.as_slice()
2561 } else {
2562 self.tcx.stripped_cfg_items(module.krate)
2563 };
2564
2565 for &StrippedCfgItem { parent_module, ident, ref cfg } in symbols {
2566 if parent_module != module || ident.name != *segment {
2567 continue;
2568 }
2569
2570 let note = errors::FoundItemConfigureOut { span: ident.span };
2571 err.subdiagnostic(note);
2572
2573 if let MetaItemKind::List(nested) = &cfg.kind
2574 && let MetaItemInner::MetaItem(meta_item) = &nested[0]
2575 && let MetaItemKind::NameValue(feature_name) = &meta_item.kind
2576 {
2577 let note = errors::ItemWasBehindFeature {
2578 feature: feature_name.symbol,
2579 span: meta_item.span,
2580 };
2581 err.subdiagnostic(note);
2582 } else {
2583 let note = errors::ItemWasCfgOut { span: cfg.span };
2584 err.subdiagnostic(note);
2585 }
2586 }
2587 }
2588}
2589
2590fn find_span_of_binding_until_next_binding(
2604 sess: &Session,
2605 binding_span: Span,
2606 use_span: Span,
2607) -> (bool, Span) {
2608 let source_map = sess.source_map();
2609
2610 let binding_until_end = binding_span.with_hi(use_span.hi());
2613
2614 let after_binding_until_end = binding_until_end.with_lo(binding_span.hi());
2617
2618 let mut found_closing_brace = false;
2625 let after_binding_until_next_binding =
2626 source_map.span_take_while(after_binding_until_end, |&ch| {
2627 if ch == '}' {
2628 found_closing_brace = true;
2629 }
2630 ch == ' ' || ch == ','
2631 });
2632
2633 let span = binding_span.with_hi(after_binding_until_next_binding.hi());
2638
2639 (found_closing_brace, span)
2640}
2641
2642fn extend_span_to_previous_binding(sess: &Session, binding_span: Span) -> Option<Span> {
2655 let source_map = sess.source_map();
2656
2657 let prev_source = source_map.span_to_prev_source(binding_span).ok()?;
2661
2662 let prev_comma = prev_source.rsplit(',').collect::<Vec<_>>();
2663 let prev_starting_brace = prev_source.rsplit('{').collect::<Vec<_>>();
2664 if prev_comma.len() <= 1 || prev_starting_brace.len() <= 1 {
2665 return None;
2666 }
2667
2668 let prev_comma = prev_comma.first().unwrap();
2669 let prev_starting_brace = prev_starting_brace.first().unwrap();
2670
2671 if prev_comma.len() > prev_starting_brace.len() {
2675 return None;
2676 }
2677
2678 Some(binding_span.with_lo(BytePos(
2679 binding_span.lo().0 - (prev_comma.as_bytes().len() as u32) - 1,
2682 )))
2683}
2684
2685#[instrument(level = "debug", skip(sess))]
2699fn find_span_immediately_after_crate_name(sess: &Session, use_span: Span) -> (bool, Span) {
2700 let source_map = sess.source_map();
2701
2702 let mut num_colons = 0;
2704 let until_second_colon = source_map.span_take_while(use_span, |c| {
2706 if *c == ':' {
2707 num_colons += 1;
2708 }
2709 !matches!(c, ':' if num_colons == 2)
2710 });
2711 let from_second_colon = use_span.with_lo(until_second_colon.hi() + BytePos(1));
2713
2714 let mut found_a_non_whitespace_character = false;
2715 let after_second_colon = source_map.span_take_while(from_second_colon, |c| {
2717 if found_a_non_whitespace_character {
2718 return false;
2719 }
2720 if !c.is_whitespace() {
2721 found_a_non_whitespace_character = true;
2722 }
2723 true
2724 });
2725
2726 let next_left_bracket = source_map.span_through_char(from_second_colon, '{');
2728
2729 (next_left_bracket == after_second_colon, from_second_colon)
2730}
2731
2732enum Instead {
2735 Yes,
2736 No,
2737}
2738
2739enum FoundUse {
2741 Yes,
2742 No,
2743}
2744
2745pub(crate) enum DiagMode {
2747 Normal,
2748 Pattern,
2750 Import {
2752 unresolved_import: bool,
2754 append: bool,
2757 },
2758}
2759
2760pub(crate) fn import_candidates(
2761 tcx: TyCtxt<'_>,
2762 err: &mut Diag<'_>,
2763 use_placement_span: Option<Span>,
2765 candidates: &[ImportSuggestion],
2766 mode: DiagMode,
2767 append: &str,
2768) {
2769 show_candidates(
2770 tcx,
2771 err,
2772 use_placement_span,
2773 candidates,
2774 Instead::Yes,
2775 FoundUse::Yes,
2776 mode,
2777 vec![],
2778 append,
2779 );
2780}
2781
2782type PathString<'a> = (String, &'a str, Option<Span>, &'a Option<String>, bool);
2783
2784fn show_candidates(
2789 tcx: TyCtxt<'_>,
2790 err: &mut Diag<'_>,
2791 use_placement_span: Option<Span>,
2793 candidates: &[ImportSuggestion],
2794 instead: Instead,
2795 found_use: FoundUse,
2796 mode: DiagMode,
2797 path: Vec<Segment>,
2798 append: &str,
2799) -> bool {
2800 if candidates.is_empty() {
2801 return false;
2802 }
2803
2804 let mut showed = false;
2805 let mut accessible_path_strings: Vec<PathString<'_>> = Vec::new();
2806 let mut inaccessible_path_strings: Vec<PathString<'_>> = Vec::new();
2807
2808 candidates.iter().for_each(|c| {
2809 if c.accessible {
2810 if c.doc_visible {
2812 accessible_path_strings.push((
2813 pprust::path_to_string(&c.path),
2814 c.descr,
2815 c.did.and_then(|did| Some(tcx.source_span(did.as_local()?))),
2816 &c.note,
2817 c.via_import,
2818 ))
2819 }
2820 } else {
2821 inaccessible_path_strings.push((
2822 pprust::path_to_string(&c.path),
2823 c.descr,
2824 c.did.and_then(|did| Some(tcx.source_span(did.as_local()?))),
2825 &c.note,
2826 c.via_import,
2827 ))
2828 }
2829 });
2830
2831 for path_strings in [&mut accessible_path_strings, &mut inaccessible_path_strings] {
2834 path_strings.sort_by(|a, b| a.0.cmp(&b.0));
2835 path_strings.dedup_by(|a, b| a.0 == b.0);
2836 let core_path_strings =
2837 path_strings.extract_if(.., |p| p.0.starts_with("core::")).collect::<Vec<_>>();
2838 let std_path_strings =
2839 path_strings.extract_if(.., |p| p.0.starts_with("std::")).collect::<Vec<_>>();
2840 let foreign_crate_path_strings =
2841 path_strings.extract_if(.., |p| !p.0.starts_with("crate::")).collect::<Vec<_>>();
2842
2843 if std_path_strings.len() == core_path_strings.len() {
2846 path_strings.extend(std_path_strings);
2848 } else {
2849 path_strings.extend(std_path_strings);
2850 path_strings.extend(core_path_strings);
2851 }
2852 path_strings.extend(foreign_crate_path_strings);
2854 }
2855
2856 if !accessible_path_strings.is_empty() {
2857 let (determiner, kind, s, name, through) =
2858 if let [(name, descr, _, _, via_import)] = &accessible_path_strings[..] {
2859 (
2860 "this",
2861 *descr,
2862 "",
2863 format!(" `{name}`"),
2864 if *via_import { " through its public re-export" } else { "" },
2865 )
2866 } else {
2867 let kinds = accessible_path_strings
2870 .iter()
2871 .map(|(_, descr, _, _, _)| *descr)
2872 .collect::<UnordSet<&str>>();
2873 let kind = if let Some(kind) = kinds.get_only() { kind } else { "item" };
2874 let s = if kind.ends_with('s') { "es" } else { "s" };
2875
2876 ("one of these", kind, s, String::new(), "")
2877 };
2878
2879 let instead = if let Instead::Yes = instead { " instead" } else { "" };
2880 let mut msg = if let DiagMode::Pattern = mode {
2881 format!(
2882 "if you meant to match on {kind}{s}{instead}{name}, use the full path in the \
2883 pattern",
2884 )
2885 } else {
2886 format!("consider importing {determiner} {kind}{s}{through}{instead}")
2887 };
2888
2889 for note in accessible_path_strings.iter().flat_map(|cand| cand.3.as_ref()) {
2890 err.note(note.clone());
2891 }
2892
2893 let append_candidates = |msg: &mut String, accessible_path_strings: Vec<PathString<'_>>| {
2894 msg.push(':');
2895
2896 for candidate in accessible_path_strings {
2897 msg.push('\n');
2898 msg.push_str(&candidate.0);
2899 }
2900 };
2901
2902 if let Some(span) = use_placement_span {
2903 let (add_use, trailing) = match mode {
2904 DiagMode::Pattern => {
2905 err.span_suggestions(
2906 span,
2907 msg,
2908 accessible_path_strings.into_iter().map(|a| a.0),
2909 Applicability::MaybeIncorrect,
2910 );
2911 return true;
2912 }
2913 DiagMode::Import { .. } => ("", ""),
2914 DiagMode::Normal => ("use ", ";\n"),
2915 };
2916 for candidate in &mut accessible_path_strings {
2917 let additional_newline = if let FoundUse::No = found_use
2920 && let DiagMode::Normal = mode
2921 {
2922 "\n"
2923 } else {
2924 ""
2925 };
2926 candidate.0 =
2927 format!("{add_use}{}{append}{trailing}{additional_newline}", candidate.0);
2928 }
2929
2930 match mode {
2931 DiagMode::Import { append: true, .. } => {
2932 append_candidates(&mut msg, accessible_path_strings);
2933 err.span_help(span, msg);
2934 }
2935 _ => {
2936 err.span_suggestions_with_style(
2937 span,
2938 msg,
2939 accessible_path_strings.into_iter().map(|a| a.0),
2940 Applicability::MaybeIncorrect,
2941 SuggestionStyle::ShowAlways,
2942 );
2943 }
2944 }
2945
2946 if let [first, .., last] = &path[..] {
2947 let sp = first.ident.span.until(last.ident.span);
2948 if sp.can_be_used_for_suggestions() && !sp.is_empty() {
2951 err.span_suggestion_verbose(
2952 sp,
2953 format!("if you import `{}`, refer to it directly", last.ident),
2954 "",
2955 Applicability::Unspecified,
2956 );
2957 }
2958 }
2959 } else {
2960 append_candidates(&mut msg, accessible_path_strings);
2961 err.help(msg);
2962 }
2963 showed = true;
2964 }
2965 if !inaccessible_path_strings.is_empty()
2966 && (!matches!(mode, DiagMode::Import { unresolved_import: false, .. }))
2967 {
2968 let prefix =
2969 if let DiagMode::Pattern = mode { "you might have meant to match on " } else { "" };
2970 if let [(name, descr, source_span, note, _)] = &inaccessible_path_strings[..] {
2971 let msg = format!(
2972 "{prefix}{descr} `{name}`{} exists but is inaccessible",
2973 if let DiagMode::Pattern = mode { ", which" } else { "" }
2974 );
2975
2976 if let Some(source_span) = source_span {
2977 let span = tcx.sess.source_map().guess_head_span(*source_span);
2978 let mut multi_span = MultiSpan::from_span(span);
2979 multi_span.push_span_label(span, "not accessible");
2980 err.span_note(multi_span, msg);
2981 } else {
2982 err.note(msg);
2983 }
2984 if let Some(note) = (*note).as_deref() {
2985 err.note(note.to_string());
2986 }
2987 } else {
2988 let (_, descr_first, _, _, _) = &inaccessible_path_strings[0];
2989 let descr = if inaccessible_path_strings
2990 .iter()
2991 .skip(1)
2992 .all(|(_, descr, _, _, _)| descr == descr_first)
2993 {
2994 descr_first
2995 } else {
2996 "item"
2997 };
2998 let plural_descr =
2999 if descr.ends_with('s') { format!("{descr}es") } else { format!("{descr}s") };
3000
3001 let mut msg = format!("{prefix}these {plural_descr} exist but are inaccessible");
3002 let mut has_colon = false;
3003
3004 let mut spans = Vec::new();
3005 for (name, _, source_span, _, _) in &inaccessible_path_strings {
3006 if let Some(source_span) = source_span {
3007 let span = tcx.sess.source_map().guess_head_span(*source_span);
3008 spans.push((name, span));
3009 } else {
3010 if !has_colon {
3011 msg.push(':');
3012 has_colon = true;
3013 }
3014 msg.push('\n');
3015 msg.push_str(name);
3016 }
3017 }
3018
3019 let mut multi_span = MultiSpan::from_spans(spans.iter().map(|(_, sp)| *sp).collect());
3020 for (name, span) in spans {
3021 multi_span.push_span_label(span, format!("`{name}`: not accessible"));
3022 }
3023
3024 for note in inaccessible_path_strings.iter().flat_map(|cand| cand.3.as_ref()) {
3025 err.note(note.clone());
3026 }
3027
3028 err.span_note(multi_span, msg);
3029 }
3030 showed = true;
3031 }
3032 showed
3033}
3034
3035#[derive(Debug)]
3036struct UsePlacementFinder {
3037 target_module: NodeId,
3038 first_legal_span: Option<Span>,
3039 first_use_span: Option<Span>,
3040}
3041
3042impl UsePlacementFinder {
3043 fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, FoundUse) {
3044 let mut finder =
3045 UsePlacementFinder { target_module, first_legal_span: None, first_use_span: None };
3046 finder.visit_crate(krate);
3047 if let Some(use_span) = finder.first_use_span {
3048 (Some(use_span), FoundUse::Yes)
3049 } else {
3050 (finder.first_legal_span, FoundUse::No)
3051 }
3052 }
3053}
3054
3055impl<'tcx> visit::Visitor<'tcx> for UsePlacementFinder {
3056 fn visit_crate(&mut self, c: &Crate) {
3057 if self.target_module == CRATE_NODE_ID {
3058 let inject = c.spans.inject_use_span;
3059 if is_span_suitable_for_use_injection(inject) {
3060 self.first_legal_span = Some(inject);
3061 }
3062 self.first_use_span = search_for_any_use_in_items(&c.items);
3063 } else {
3064 visit::walk_crate(self, c);
3065 }
3066 }
3067
3068 fn visit_item(&mut self, item: &'tcx ast::Item) {
3069 if self.target_module == item.id {
3070 if let ItemKind::Mod(_, _, ModKind::Loaded(items, _inline, mod_spans, _)) = &item.kind {
3071 let inject = mod_spans.inject_use_span;
3072 if is_span_suitable_for_use_injection(inject) {
3073 self.first_legal_span = Some(inject);
3074 }
3075 self.first_use_span = search_for_any_use_in_items(items);
3076 }
3077 } else {
3078 visit::walk_item(self, item);
3079 }
3080 }
3081}
3082
3083fn search_for_any_use_in_items(items: &[P<ast::Item>]) -> Option<Span> {
3084 for item in items {
3085 if let ItemKind::Use(..) = item.kind
3086 && is_span_suitable_for_use_injection(item.span)
3087 {
3088 let mut lo = item.span.lo();
3089 for attr in &item.attrs {
3090 if attr.span.eq_ctxt(item.span) {
3091 lo = std::cmp::min(lo, attr.span.lo());
3092 }
3093 }
3094 return Some(Span::new(lo, lo, item.span.ctxt(), item.span.parent()));
3095 }
3096 }
3097 None
3098}
3099
3100fn is_span_suitable_for_use_injection(s: Span) -> bool {
3101 !s.from_expansion()
3104}