rustc_resolve/
imports.rs

1//! A bunch of methods and structures more or less related to resolving imports.
2
3use std::cell::Cell;
4use std::mem;
5
6use rustc_ast::NodeId;
7use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
8use rustc_data_structures::intern::Interned;
9use rustc_errors::codes::*;
10use rustc_errors::{Applicability, MultiSpan, pluralize, struct_span_code_err};
11use rustc_hir::def::{self, DefKind, PartialRes};
12use rustc_hir::def_id::DefId;
13use rustc_middle::metadata::{ModChild, Reexport};
14use rustc_middle::span_bug;
15use rustc_middle::ty::Visibility;
16use rustc_session::lint::BuiltinLintDiag;
17use rustc_session::lint::builtin::{
18    AMBIGUOUS_GLOB_REEXPORTS, EXPORTED_PRIVATE_DEPENDENCIES, HIDDEN_GLOB_REEXPORTS,
19    PUB_USE_OF_PRIVATE_EXTERN_CRATE, REDUNDANT_IMPORTS, UNUSED_IMPORTS,
20};
21use rustc_session::parse::feature_err;
22use rustc_span::edit_distance::find_best_match_for_name;
23use rustc_span::hygiene::LocalExpnId;
24use rustc_span::{Ident, Span, Symbol, kw, sym};
25use smallvec::SmallVec;
26use tracing::debug;
27
28use crate::Namespace::{self, *};
29use crate::diagnostics::{DiagMode, Suggestion, import_candidates};
30use crate::errors::{
31    CannotBeReexportedCratePublic, CannotBeReexportedCratePublicNS, CannotBeReexportedPrivate,
32    CannotBeReexportedPrivateNS, CannotDetermineImportResolution, CannotGlobImportAllCrates,
33    ConsiderAddingMacroExport, ConsiderMarkingAsPub,
34};
35use crate::{
36    AmbiguityError, AmbiguityKind, BindingKey, CmResolver, Determinacy, Finalize, ImportSuggestion,
37    Module, ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope,
38    PathResult, PerNS, ResolutionError, Resolver, ScopeSet, Segment, Used, module_to_string,
39    names_to_string,
40};
41
42type Res = def::Res<NodeId>;
43
44/// A [`NameBinding`] in the process of being resolved.
45#[derive(Clone, Copy, Default, PartialEq)]
46pub(crate) enum PendingBinding<'ra> {
47    Ready(Option<NameBinding<'ra>>),
48    #[default]
49    Pending,
50}
51
52impl<'ra> PendingBinding<'ra> {
53    pub(crate) fn binding(self) -> Option<NameBinding<'ra>> {
54        match self {
55            PendingBinding::Ready(binding) => binding,
56            PendingBinding::Pending => None,
57        }
58    }
59}
60
61/// Contains data for specific kinds of imports.
62#[derive(Clone)]
63pub(crate) enum ImportKind<'ra> {
64    Single {
65        /// `source` in `use prefix::source as target`.
66        source: Ident,
67        /// `target` in `use prefix::source as target`.
68        /// It will directly use `source` when the format is `use prefix::source`.
69        target: Ident,
70        /// Bindings introduced by the import.
71        bindings: PerNS<Cell<PendingBinding<'ra>>>,
72        /// `true` for `...::{self [as target]}` imports, `false` otherwise.
73        type_ns_only: bool,
74        /// Did this import result from a nested import? ie. `use foo::{bar, baz};`
75        nested: bool,
76        /// The ID of the `UseTree` that imported this `Import`.
77        ///
78        /// In the case where the `Import` was expanded from a "nested" use tree,
79        /// this id is the ID of the leaf tree. For example:
80        ///
81        /// ```ignore (pacify the merciless tidy)
82        /// use foo::bar::{a, b}
83        /// ```
84        ///
85        /// If this is the import for `foo::bar::a`, we would have the ID of the `UseTree`
86        /// for `a` in this field.
87        id: NodeId,
88    },
89    Glob {
90        is_prelude: bool,
91        // The visibility of the greatest re-export.
92        // n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
93        max_vis: Cell<Option<Visibility>>,
94        id: NodeId,
95    },
96    ExternCrate {
97        source: Option<Symbol>,
98        target: Ident,
99        id: NodeId,
100    },
101    MacroUse {
102        /// A field has been added indicating whether it should be reported as a lint,
103        /// addressing issue#119301.
104        warn_private: bool,
105    },
106    MacroExport,
107}
108
109/// Manually implement `Debug` for `ImportKind` because the `source/target_bindings`
110/// contain `Cell`s which can introduce infinite loops while printing.
111impl<'ra> std::fmt::Debug for ImportKind<'ra> {
112    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113        use ImportKind::*;
114        match self {
115            Single { source, target, bindings, type_ns_only, nested, id, .. } => f
116                .debug_struct("Single")
117                .field("source", source)
118                .field("target", target)
119                // Ignore the nested bindings to avoid an infinite loop while printing.
120                .field(
121                    "bindings",
122                    &bindings.clone().map(|b| b.into_inner().binding().map(|_| format_args!(".."))),
123                )
124                .field("type_ns_only", type_ns_only)
125                .field("nested", nested)
126                .field("id", id)
127                .finish(),
128            Glob { is_prelude, max_vis, id } => f
129                .debug_struct("Glob")
130                .field("is_prelude", is_prelude)
131                .field("max_vis", max_vis)
132                .field("id", id)
133                .finish(),
134            ExternCrate { source, target, id } => f
135                .debug_struct("ExternCrate")
136                .field("source", source)
137                .field("target", target)
138                .field("id", id)
139                .finish(),
140            MacroUse { warn_private } => {
141                f.debug_struct("MacroUse").field("warn_private", warn_private).finish()
142            }
143            MacroExport => f.debug_struct("MacroExport").finish(),
144        }
145    }
146}
147
148/// One import.
149#[derive(Debug, Clone)]
150pub(crate) struct ImportData<'ra> {
151    pub kind: ImportKind<'ra>,
152
153    /// Node ID of the "root" use item -- this is always the same as `ImportKind`'s `id`
154    /// (if it exists) except in the case of "nested" use trees, in which case
155    /// it will be the ID of the root use tree. e.g., in the example
156    /// ```ignore (incomplete code)
157    /// use foo::bar::{a, b}
158    /// ```
159    /// this would be the ID of the `use foo::bar` `UseTree` node.
160    /// In case of imports without their own node ID it's the closest node that can be used,
161    /// for example, for reporting lints.
162    pub root_id: NodeId,
163
164    /// Span of the entire use statement.
165    pub use_span: Span,
166
167    /// Span of the entire use statement with attributes.
168    pub use_span_with_attributes: Span,
169
170    /// Did the use statement have any attributes?
171    pub has_attributes: bool,
172
173    /// Span of this use tree.
174    pub span: Span,
175
176    /// Span of the *root* use tree (see `root_id`).
177    pub root_span: Span,
178
179    pub parent_scope: ParentScope<'ra>,
180    pub module_path: Vec<Segment>,
181    /// The resolution of `module_path`:
182    ///
183    /// | `module_path` | `imported_module` | remark |
184    /// |-|-|-|
185    /// |`use prefix::foo`| `ModuleOrUniformRoot::Module(prefix)`         | - |
186    /// |`use ::foo`      | `ModuleOrUniformRoot::ExternPrelude`          | 2018+ editions |
187    /// |`use ::foo`      | `ModuleOrUniformRoot::ModuleAndExternPrelude` | a special case in 2015 edition |
188    /// |`use foo`        | `ModuleOrUniformRoot::CurrentScope`           | - |
189    pub imported_module: Cell<Option<ModuleOrUniformRoot<'ra>>>,
190    pub vis: Visibility,
191}
192
193/// All imports are unique and allocated on a same arena,
194/// so we can use referential equality to compare them.
195pub(crate) type Import<'ra> = Interned<'ra, ImportData<'ra>>;
196
197// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the
198// contained data.
199// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees
200// are upheld.
201impl std::hash::Hash for ImportData<'_> {
202    fn hash<H>(&self, _: &mut H)
203    where
204        H: std::hash::Hasher,
205    {
206        unreachable!()
207    }
208}
209
210impl<'ra> ImportData<'ra> {
211    pub(crate) fn is_glob(&self) -> bool {
212        matches!(self.kind, ImportKind::Glob { .. })
213    }
214
215    pub(crate) fn is_nested(&self) -> bool {
216        match self.kind {
217            ImportKind::Single { nested, .. } => nested,
218            _ => false,
219        }
220    }
221
222    pub(crate) fn id(&self) -> Option<NodeId> {
223        match self.kind {
224            ImportKind::Single { id, .. }
225            | ImportKind::Glob { id, .. }
226            | ImportKind::ExternCrate { id, .. } => Some(id),
227            ImportKind::MacroUse { .. } | ImportKind::MacroExport => None,
228        }
229    }
230
231    fn simplify(&self, r: &Resolver<'_, '_>) -> Reexport {
232        let to_def_id = |id| r.local_def_id(id).to_def_id();
233        match self.kind {
234            ImportKind::Single { id, .. } => Reexport::Single(to_def_id(id)),
235            ImportKind::Glob { id, .. } => Reexport::Glob(to_def_id(id)),
236            ImportKind::ExternCrate { id, .. } => Reexport::ExternCrate(to_def_id(id)),
237            ImportKind::MacroUse { .. } => Reexport::MacroUse,
238            ImportKind::MacroExport => Reexport::MacroExport,
239        }
240    }
241}
242
243/// Records information about the resolution of a name in a namespace of a module.
244#[derive(Clone, Default, Debug)]
245pub(crate) struct NameResolution<'ra> {
246    /// Single imports that may define the name in the namespace.
247    /// Imports are arena-allocated, so it's ok to use pointers as keys.
248    pub single_imports: FxIndexSet<Import<'ra>>,
249    /// The non-glob binding for this name, if it is known to exist.
250    pub non_glob_binding: Option<NameBinding<'ra>>,
251    /// The glob binding for this name, if it is known to exist.
252    pub glob_binding: Option<NameBinding<'ra>>,
253}
254
255impl<'ra> NameResolution<'ra> {
256    /// Returns the binding for the name if it is known or None if it not known.
257    pub(crate) fn binding(&self) -> Option<NameBinding<'ra>> {
258        self.best_binding().and_then(|binding| {
259            if !binding.is_glob_import() || self.single_imports.is_empty() {
260                Some(binding)
261            } else {
262                None
263            }
264        })
265    }
266
267    pub(crate) fn best_binding(&self) -> Option<NameBinding<'ra>> {
268        self.non_glob_binding.or(self.glob_binding)
269    }
270}
271
272/// An error that may be transformed into a diagnostic later. Used to combine multiple unresolved
273/// import errors within the same use tree into a single diagnostic.
274#[derive(Debug, Clone)]
275struct UnresolvedImportError {
276    span: Span,
277    label: Option<String>,
278    note: Option<String>,
279    suggestion: Option<Suggestion>,
280    candidates: Option<Vec<ImportSuggestion>>,
281    segment: Option<Symbol>,
282    /// comes from `PathRes::Failed { module }`
283    module: Option<DefId>,
284}
285
286// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;`
287// are permitted for backward-compatibility under a deprecation lint.
288fn pub_use_of_private_extern_crate_hack(
289    import: Import<'_>,
290    binding: NameBinding<'_>,
291) -> Option<NodeId> {
292    match (&import.kind, &binding.kind) {
293        (ImportKind::Single { .. }, NameBindingKind::Import { import: binding_import, .. })
294            if let ImportKind::ExternCrate { id, .. } = binding_import.kind
295                && import.vis.is_public() =>
296        {
297            Some(id)
298        }
299        _ => None,
300    }
301}
302
303impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
304    /// Given a binding and an import that resolves to it,
305    /// return the corresponding binding defined by the import.
306    pub(crate) fn import(
307        &self,
308        binding: NameBinding<'ra>,
309        import: Import<'ra>,
310    ) -> NameBinding<'ra> {
311        let import_vis = import.vis.to_def_id();
312        let vis = if binding.vis.is_at_least(import_vis, self.tcx)
313            || pub_use_of_private_extern_crate_hack(import, binding).is_some()
314        {
315            import_vis
316        } else {
317            binding.vis
318        };
319
320        if let ImportKind::Glob { ref max_vis, .. } = import.kind
321            && (vis == import_vis
322                || max_vis.get().is_none_or(|max_vis| vis.is_at_least(max_vis, self.tcx)))
323        {
324            max_vis.set(Some(vis.expect_local()))
325        }
326
327        self.arenas.alloc_name_binding(NameBindingData {
328            kind: NameBindingKind::Import { binding, import },
329            ambiguity: None,
330            warn_ambiguity: false,
331            span: import.span,
332            vis,
333            expansion: import.parent_scope.expansion,
334        })
335    }
336
337    /// Define the name or return the existing binding if there is a collision.
338    pub(crate) fn try_define_local(
339        &mut self,
340        module: Module<'ra>,
341        ident: Ident,
342        ns: Namespace,
343        binding: NameBinding<'ra>,
344        warn_ambiguity: bool,
345    ) -> Result<(), NameBinding<'ra>> {
346        let res = binding.res();
347        self.check_reserved_macro_name(ident, res);
348        self.set_binding_parent_module(binding, module);
349        // Even if underscore names cannot be looked up, we still need to add them to modules,
350        // because they can be fetched by glob imports from those modules, and bring traits
351        // into scope both directly and through glob imports.
352        let key = BindingKey::new_disambiguated(ident, ns, || {
353            module.underscore_disambiguator.update(|d| d + 1);
354            module.underscore_disambiguator.get()
355        });
356        self.update_local_resolution(module, key, warn_ambiguity, |this, resolution| {
357            if let Some(old_binding) = resolution.best_binding() {
358                if res == Res::Err && old_binding.res() != Res::Err {
359                    // Do not override real bindings with `Res::Err`s from error recovery.
360                    return Ok(());
361                }
362                match (old_binding.is_glob_import(), binding.is_glob_import()) {
363                    (true, true) => {
364                        let (glob_binding, old_glob_binding) = (binding, old_binding);
365                        // FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
366                        if !binding.is_ambiguity_recursive()
367                            && let NameBindingKind::Import { import: old_import, .. } =
368                                old_glob_binding.kind
369                            && let NameBindingKind::Import { import, .. } = glob_binding.kind
370                            && old_import == import
371                        {
372                            // When imported from the same glob-import statement, we should replace
373                            // `old_glob_binding` with `glob_binding`, regardless of whether
374                            // they have the same resolution or not.
375                            resolution.glob_binding = Some(glob_binding);
376                        } else if res != old_glob_binding.res() {
377                            resolution.glob_binding = Some(this.new_ambiguity_binding(
378                                AmbiguityKind::GlobVsGlob,
379                                old_glob_binding,
380                                glob_binding,
381                                warn_ambiguity,
382                            ));
383                        } else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
384                            // We are glob-importing the same item but with greater visibility.
385                            resolution.glob_binding = Some(glob_binding);
386                        } else if binding.is_ambiguity_recursive() {
387                            resolution.glob_binding =
388                                Some(this.new_warn_ambiguity_binding(glob_binding));
389                        }
390                    }
391                    (old_glob @ true, false) | (old_glob @ false, true) => {
392                        let (glob_binding, non_glob_binding) =
393                            if old_glob { (old_binding, binding) } else { (binding, old_binding) };
394                        if ns == MacroNS
395                            && non_glob_binding.expansion != LocalExpnId::ROOT
396                            && glob_binding.res() != non_glob_binding.res()
397                        {
398                            resolution.non_glob_binding = Some(this.new_ambiguity_binding(
399                                AmbiguityKind::GlobVsExpanded,
400                                non_glob_binding,
401                                glob_binding,
402                                false,
403                            ));
404                        } else {
405                            resolution.non_glob_binding = Some(non_glob_binding);
406                        }
407
408                        if let Some(old_glob_binding) = resolution.glob_binding {
409                            assert!(old_glob_binding.is_glob_import());
410                            if glob_binding.res() != old_glob_binding.res() {
411                                resolution.glob_binding = Some(this.new_ambiguity_binding(
412                                    AmbiguityKind::GlobVsGlob,
413                                    old_glob_binding,
414                                    glob_binding,
415                                    false,
416                                ));
417                            } else if !old_glob_binding.vis.is_at_least(binding.vis, this.tcx) {
418                                resolution.glob_binding = Some(glob_binding);
419                            }
420                        } else {
421                            resolution.glob_binding = Some(glob_binding);
422                        }
423                    }
424                    (false, false) => {
425                        return Err(old_binding);
426                    }
427                }
428            } else {
429                if binding.is_glob_import() {
430                    resolution.glob_binding = Some(binding);
431                } else {
432                    resolution.non_glob_binding = Some(binding);
433                }
434            }
435
436            Ok(())
437        })
438    }
439
440    fn new_ambiguity_binding(
441        &self,
442        ambiguity_kind: AmbiguityKind,
443        primary_binding: NameBinding<'ra>,
444        secondary_binding: NameBinding<'ra>,
445        warn_ambiguity: bool,
446    ) -> NameBinding<'ra> {
447        let ambiguity = Some((secondary_binding, ambiguity_kind));
448        let data = NameBindingData { ambiguity, warn_ambiguity, ..*primary_binding };
449        self.arenas.alloc_name_binding(data)
450    }
451
452    fn new_warn_ambiguity_binding(&self, binding: NameBinding<'ra>) -> NameBinding<'ra> {
453        assert!(binding.is_ambiguity_recursive());
454        self.arenas.alloc_name_binding(NameBindingData { warn_ambiguity: true, ..*binding })
455    }
456
457    // Use `f` to mutate the resolution of the name in the module.
458    // If the resolution becomes a success, define it in the module's glob importers.
459    fn update_local_resolution<T, F>(
460        &mut self,
461        module: Module<'ra>,
462        key: BindingKey,
463        warn_ambiguity: bool,
464        f: F,
465    ) -> T
466    where
467        F: FnOnce(&Resolver<'ra, 'tcx>, &mut NameResolution<'ra>) -> T,
468    {
469        // Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
470        // during which the resolution might end up getting re-defined via a glob cycle.
471        let (binding, t, warn_ambiguity) = {
472            let resolution = &mut *self.resolution_or_default(module, key).borrow_mut();
473            let old_binding = resolution.binding();
474
475            let t = f(self, resolution);
476
477            if let Some(binding) = resolution.binding()
478                && old_binding != Some(binding)
479            {
480                (binding, t, warn_ambiguity || old_binding.is_some())
481            } else {
482                return t;
483            }
484        };
485
486        let Ok(glob_importers) = module.glob_importers.try_borrow_mut() else {
487            return t;
488        };
489
490        // Define or update `binding` in `module`s glob importers.
491        for import in glob_importers.iter() {
492            let mut ident = key.ident;
493            let scope = match ident.0.span.reverse_glob_adjust(module.expansion, import.span) {
494                Some(Some(def)) => self.expn_def_scope(def),
495                Some(None) => import.parent_scope.module,
496                None => continue,
497            };
498            if self.is_accessible_from(binding.vis, scope) {
499                let imported_binding = self.import(binding, *import);
500                let _ = self.try_define_local(
501                    import.parent_scope.module,
502                    ident.0,
503                    key.ns,
504                    imported_binding,
505                    warn_ambiguity,
506                );
507            }
508        }
509
510        t
511    }
512
513    // Define a dummy resolution containing a `Res::Err` as a placeholder for a failed
514    // or indeterminate resolution, also mark such failed imports as used to avoid duplicate diagnostics.
515    fn import_dummy_binding(&mut self, import: Import<'ra>, is_indeterminate: bool) {
516        if let ImportKind::Single { target, ref bindings, .. } = import.kind {
517            if !(is_indeterminate
518                || bindings.iter().all(|binding| binding.get().binding().is_none()))
519            {
520                return; // Has resolution, do not create the dummy binding
521            }
522            let dummy_binding = self.dummy_binding;
523            let dummy_binding = self.import(dummy_binding, import);
524            self.per_ns(|this, ns| {
525                let module = import.parent_scope.module;
526                let _ = this.try_define_local(module, target, ns, dummy_binding, false);
527                // Don't remove underscores from `single_imports`, they were never added.
528                if target.name != kw::Underscore {
529                    let key = BindingKey::new(target, ns);
530                    this.update_local_resolution(module, key, false, |_, resolution| {
531                        resolution.single_imports.swap_remove(&import);
532                    })
533                }
534            });
535            self.record_use(target, dummy_binding, Used::Other);
536        } else if import.imported_module.get().is_none() {
537            self.import_use_map.insert(import, Used::Other);
538            if let Some(id) = import.id() {
539                self.used_imports.insert(id);
540            }
541        }
542    }
543
544    // Import resolution
545    //
546    // This is a fixed-point algorithm. We resolve imports until our efforts
547    // are stymied by an unresolved import; then we bail out of the current
548    // module and continue. We terminate successfully once no more imports
549    // remain or unsuccessfully when no forward progress in resolving imports
550    // is made.
551
552    /// Resolves all imports for the crate. This method performs the fixed-
553    /// point iteration.
554    pub(crate) fn resolve_imports(&mut self) {
555        self.assert_speculative = true;
556        let mut prev_indeterminate_count = usize::MAX;
557        let mut indeterminate_count = self.indeterminate_imports.len() * 3;
558        while indeterminate_count < prev_indeterminate_count {
559            prev_indeterminate_count = indeterminate_count;
560            indeterminate_count = 0;
561            for import in mem::take(&mut self.indeterminate_imports) {
562                let import_indeterminate_count = self.cm().resolve_import(import);
563                indeterminate_count += import_indeterminate_count;
564                match import_indeterminate_count {
565                    0 => self.determined_imports.push(import),
566                    _ => self.indeterminate_imports.push(import),
567                }
568            }
569        }
570        self.assert_speculative = false;
571    }
572
573    pub(crate) fn finalize_imports(&mut self) {
574        for module in self.arenas.local_modules().iter() {
575            self.finalize_resolutions_in(*module);
576        }
577
578        let mut seen_spans = FxHashSet::default();
579        let mut errors = vec![];
580        let mut prev_root_id: NodeId = NodeId::ZERO;
581        let determined_imports = mem::take(&mut self.determined_imports);
582        let indeterminate_imports = mem::take(&mut self.indeterminate_imports);
583
584        let mut glob_error = false;
585        for (is_indeterminate, import) in determined_imports
586            .iter()
587            .map(|i| (false, i))
588            .chain(indeterminate_imports.iter().map(|i| (true, i)))
589        {
590            let unresolved_import_error = self.finalize_import(*import);
591            // If this import is unresolved then create a dummy import
592            // resolution for it so that later resolve stages won't complain.
593            self.import_dummy_binding(*import, is_indeterminate);
594
595            let Some(err) = unresolved_import_error else { continue };
596
597            glob_error |= import.is_glob();
598
599            if let ImportKind::Single { source, ref bindings, .. } = import.kind
600                && source.name == kw::SelfLower
601                // Silence `unresolved import` error if E0429 is already emitted
602                && let PendingBinding::Ready(None) = bindings.value_ns.get()
603            {
604                continue;
605            }
606
607            if prev_root_id != NodeId::ZERO && prev_root_id != import.root_id && !errors.is_empty()
608            {
609                // In the case of a new import line, throw a diagnostic message
610                // for the previous line.
611                self.throw_unresolved_import_error(errors, glob_error);
612                errors = vec![];
613            }
614            if seen_spans.insert(err.span) {
615                errors.push((*import, err));
616                prev_root_id = import.root_id;
617            }
618        }
619
620        if !errors.is_empty() {
621            self.throw_unresolved_import_error(errors, glob_error);
622            return;
623        }
624
625        for import in &indeterminate_imports {
626            let path = import_path_to_string(
627                &import.module_path.iter().map(|seg| seg.ident).collect::<Vec<_>>(),
628                &import.kind,
629                import.span,
630            );
631            // FIXME: there should be a better way of doing this than
632            // formatting this as a string then checking for `::`
633            if path.contains("::") {
634                let err = UnresolvedImportError {
635                    span: import.span,
636                    label: None,
637                    note: None,
638                    suggestion: None,
639                    candidates: None,
640                    segment: None,
641                    module: None,
642                };
643                errors.push((*import, err))
644            }
645        }
646
647        if !errors.is_empty() {
648            self.throw_unresolved_import_error(errors, glob_error);
649        }
650    }
651
652    pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<NameBinding<'ra>>) {
653        for module in self.arenas.local_modules().iter() {
654            for (key, resolution) in self.resolutions(*module).borrow().iter() {
655                let resolution = resolution.borrow();
656                let Some(binding) = resolution.best_binding() else { continue };
657
658                if let NameBindingKind::Import { import, .. } = binding.kind
659                    && let Some((amb_binding, _)) = binding.ambiguity
660                    && binding.res() != Res::Err
661                    && exported_ambiguities.contains(&binding)
662                {
663                    self.lint_buffer.buffer_lint(
664                        AMBIGUOUS_GLOB_REEXPORTS,
665                        import.root_id,
666                        import.root_span,
667                        BuiltinLintDiag::AmbiguousGlobReexports {
668                            name: key.ident.to_string(),
669                            namespace: key.ns.descr().to_string(),
670                            first_reexport_span: import.root_span,
671                            duplicate_reexport_span: amb_binding.span,
672                        },
673                    );
674                }
675
676                if let Some(glob_binding) = resolution.glob_binding
677                    && resolution.non_glob_binding.is_some()
678                {
679                    if binding.res() != Res::Err
680                        && glob_binding.res() != Res::Err
681                        && let NameBindingKind::Import { import: glob_import, .. } =
682                            glob_binding.kind
683                        && let Some(glob_import_id) = glob_import.id()
684                        && let glob_import_def_id = self.local_def_id(glob_import_id)
685                        && self.effective_visibilities.is_exported(glob_import_def_id)
686                        && glob_binding.vis.is_public()
687                        && !binding.vis.is_public()
688                    {
689                        let binding_id = match binding.kind {
690                            NameBindingKind::Res(res) => {
691                                Some(self.def_id_to_node_id(res.def_id().expect_local()))
692                            }
693                            NameBindingKind::Import { import, .. } => import.id(),
694                        };
695                        if let Some(binding_id) = binding_id {
696                            self.lint_buffer.buffer_lint(
697                                HIDDEN_GLOB_REEXPORTS,
698                                binding_id,
699                                binding.span,
700                                BuiltinLintDiag::HiddenGlobReexports {
701                                    name: key.ident.name.to_string(),
702                                    namespace: key.ns.descr().to_owned(),
703                                    glob_reexport_span: glob_binding.span,
704                                    private_item_span: binding.span,
705                                },
706                            );
707                        }
708                    }
709                }
710
711                if let NameBindingKind::Import { import, .. } = binding.kind
712                    && let Some(binding_id) = import.id()
713                    && let import_def_id = self.local_def_id(binding_id)
714                    && self.effective_visibilities.is_exported(import_def_id)
715                    && let Res::Def(reexported_kind, reexported_def_id) = binding.res()
716                    && !matches!(reexported_kind, DefKind::Ctor(..))
717                    && !reexported_def_id.is_local()
718                    && self.tcx.is_private_dep(reexported_def_id.krate)
719                {
720                    self.lint_buffer.buffer_lint(
721                        EXPORTED_PRIVATE_DEPENDENCIES,
722                        binding_id,
723                        binding.span,
724                        BuiltinLintDiag::ReexportPrivateDependency {
725                            kind: binding.res().descr().to_string(),
726                            name: key.ident.name.to_string(),
727                            krate: self.tcx.crate_name(reexported_def_id.krate),
728                        },
729                    );
730                }
731            }
732        }
733    }
734
735    fn throw_unresolved_import_error(
736        &mut self,
737        mut errors: Vec<(Import<'_>, UnresolvedImportError)>,
738        glob_error: bool,
739    ) {
740        errors.retain(|(_import, err)| match err.module {
741            // Skip `use` errors for `use foo::Bar;` if `foo.rs` has unrecovered parse errors.
742            Some(def_id) if self.mods_with_parse_errors.contains(&def_id) => false,
743            _ => true,
744        });
745        errors.retain(|(_import, err)| {
746            // If we've encountered something like `use _;`, we've already emitted an error stating
747            // that `_` is not a valid identifier, so we ignore that resolve error.
748            err.segment != Some(kw::Underscore)
749        });
750
751        if errors.is_empty() {
752            self.tcx.dcx().delayed_bug("expected a parse or \"`_` can't be an identifier\" error");
753            return;
754        }
755
756        let span = MultiSpan::from_spans(errors.iter().map(|(_, err)| err.span).collect());
757
758        let paths = errors
759            .iter()
760            .map(|(import, err)| {
761                let path = import_path_to_string(
762                    &import.module_path.iter().map(|seg| seg.ident).collect::<Vec<_>>(),
763                    &import.kind,
764                    err.span,
765                );
766                format!("`{path}`")
767            })
768            .collect::<Vec<_>>();
769        let msg = format!("unresolved import{} {}", pluralize!(paths.len()), paths.join(", "),);
770
771        let mut diag = struct_span_code_err!(self.dcx(), span, E0432, "{msg}");
772
773        if let Some((_, UnresolvedImportError { note: Some(note), .. })) = errors.iter().last() {
774            diag.note(note.clone());
775        }
776
777        /// Upper limit on the number of `span_label` messages.
778        const MAX_LABEL_COUNT: usize = 10;
779
780        for (import, err) in errors.into_iter().take(MAX_LABEL_COUNT) {
781            if let Some(label) = err.label {
782                diag.span_label(err.span, label);
783            }
784
785            if let Some((suggestions, msg, applicability)) = err.suggestion {
786                if suggestions.is_empty() {
787                    diag.help(msg);
788                    continue;
789                }
790                diag.multipart_suggestion(msg, suggestions, applicability);
791            }
792
793            if let Some(candidates) = &err.candidates {
794                match &import.kind {
795                    ImportKind::Single { nested: false, source, target, .. } => import_candidates(
796                        self.tcx,
797                        &mut diag,
798                        Some(err.span),
799                        candidates,
800                        DiagMode::Import { append: false, unresolved_import: true },
801                        (source != target)
802                            .then(|| format!(" as {target}"))
803                            .as_deref()
804                            .unwrap_or(""),
805                    ),
806                    ImportKind::Single { nested: true, source, target, .. } => {
807                        import_candidates(
808                            self.tcx,
809                            &mut diag,
810                            None,
811                            candidates,
812                            DiagMode::Normal,
813                            (source != target)
814                                .then(|| format!(" as {target}"))
815                                .as_deref()
816                                .unwrap_or(""),
817                        );
818                    }
819                    _ => {}
820                }
821            }
822
823            if matches!(import.kind, ImportKind::Single { .. })
824                && let Some(segment) = err.segment
825                && let Some(module) = err.module
826            {
827                self.find_cfg_stripped(&mut diag, &segment, module)
828            }
829        }
830
831        let guar = diag.emit();
832        if glob_error {
833            self.glob_error = Some(guar);
834        }
835    }
836
837    /// Attempts to resolve the given import, returning:
838    /// - `0` means its resolution is determined.
839    /// - Other values mean that indeterminate exists under certain namespaces.
840    ///
841    /// Meanwhile, if resolve successful, the resolved bindings are written
842    /// into the module.
843    fn resolve_import<'r>(mut self: CmResolver<'r, 'ra, 'tcx>, import: Import<'ra>) -> usize {
844        debug!(
845            "(resolving import for module) resolving import `{}::...` in `{}`",
846            Segment::names_to_string(&import.module_path),
847            module_to_string(import.parent_scope.module).unwrap_or_else(|| "???".to_string()),
848        );
849        let module = if let Some(module) = import.imported_module.get() {
850            module
851        } else {
852            let path_res = self.reborrow().maybe_resolve_path(
853                &import.module_path,
854                None,
855                &import.parent_scope,
856                Some(import),
857            );
858
859            match path_res {
860                PathResult::Module(module) => module,
861                PathResult::Indeterminate => return 3,
862                PathResult::NonModule(..) | PathResult::Failed { .. } => return 0,
863            }
864        };
865
866        import.imported_module.set(Some(module));
867        let (source, target, bindings, type_ns_only) = match import.kind {
868            ImportKind::Single { source, target, ref bindings, type_ns_only, .. } => {
869                (source, target, bindings, type_ns_only)
870            }
871            ImportKind::Glob { .. } => {
872                // FIXME: Use mutable resolver directly as a hack, this should be an output of
873                // specualtive resolution.
874                self.get_mut_unchecked().resolve_glob_import(import);
875                return 0;
876            }
877            _ => unreachable!(),
878        };
879
880        let mut indeterminate_count = 0;
881        self.per_ns_cm(|this, ns| {
882            if !type_ns_only || ns == TypeNS {
883                if bindings[ns].get() != PendingBinding::Pending {
884                    return;
885                };
886                let binding_result = this.reborrow().maybe_resolve_ident_in_module(
887                    module,
888                    source,
889                    ns,
890                    &import.parent_scope,
891                    Some(import),
892                );
893                let parent = import.parent_scope.module;
894                let binding = match binding_result {
895                    Ok(binding) => {
896                        if binding.is_assoc_item()
897                            && !this.tcx.features().import_trait_associated_functions()
898                        {
899                            feature_err(
900                                this.tcx.sess,
901                                sym::import_trait_associated_functions,
902                                import.span,
903                                "`use` associated items of traits is unstable",
904                            )
905                            .emit();
906                        }
907                        // We need the `target`, `source` can be extracted.
908                        let imported_binding = this.import(binding, import);
909                        // FIXME: Use mutable resolver directly as a hack, this should be an output of
910                        // specualtive resolution.
911                        this.get_mut_unchecked().define_binding_local(
912                            parent,
913                            target,
914                            ns,
915                            imported_binding,
916                        );
917                        PendingBinding::Ready(Some(imported_binding))
918                    }
919                    Err(Determinacy::Determined) => {
920                        // Don't remove underscores from `single_imports`, they were never added.
921                        if target.name != kw::Underscore {
922                            let key = BindingKey::new(target, ns);
923                            // FIXME: Use mutable resolver directly as a hack, this should be an output of
924                            // specualtive resolution.
925                            this.get_mut_unchecked().update_local_resolution(
926                                parent,
927                                key,
928                                false,
929                                |_, resolution| {
930                                    resolution.single_imports.swap_remove(&import);
931                                },
932                            );
933                        }
934                        PendingBinding::Ready(None)
935                    }
936                    Err(Determinacy::Undetermined) => {
937                        indeterminate_count += 1;
938                        PendingBinding::Pending
939                    }
940                };
941                bindings[ns].set(binding);
942            }
943        });
944
945        indeterminate_count
946    }
947
948    /// Performs final import resolution, consistency checks and error reporting.
949    ///
950    /// Optionally returns an unresolved import error. This error is buffered and used to
951    /// consolidate multiple unresolved import errors into a single diagnostic.
952    fn finalize_import(&mut self, import: Import<'ra>) -> Option<UnresolvedImportError> {
953        let ignore_binding = match &import.kind {
954            ImportKind::Single { bindings, .. } => bindings[TypeNS].get().binding(),
955            _ => None,
956        };
957        let ambiguity_errors_len =
958            |errors: &Vec<AmbiguityError<'_>>| errors.iter().filter(|error| !error.warning).count();
959        let prev_ambiguity_errors_len = ambiguity_errors_len(&self.ambiguity_errors);
960        let finalize = Finalize::with_root_span(import.root_id, import.span, import.root_span);
961
962        // We'll provide more context to the privacy errors later, up to `len`.
963        let privacy_errors_len = self.privacy_errors.len();
964
965        let path_res = self.cm().resolve_path(
966            &import.module_path,
967            None,
968            &import.parent_scope,
969            Some(finalize),
970            ignore_binding,
971            Some(import),
972        );
973
974        let no_ambiguity =
975            ambiguity_errors_len(&self.ambiguity_errors) == prev_ambiguity_errors_len;
976
977        let module = match path_res {
978            PathResult::Module(module) => {
979                // Consistency checks, analogous to `finalize_macro_resolutions`.
980                if let Some(initial_module) = import.imported_module.get() {
981                    if module != initial_module && no_ambiguity {
982                        span_bug!(import.span, "inconsistent resolution for an import");
983                    }
984                } else if self.privacy_errors.is_empty() {
985                    self.dcx()
986                        .create_err(CannotDetermineImportResolution { span: import.span })
987                        .emit();
988                }
989
990                module
991            }
992            PathResult::Failed {
993                is_error_from_last_segment: false,
994                span,
995                segment_name,
996                label,
997                suggestion,
998                module,
999                error_implied_by_parse_error: _,
1000            } => {
1001                if no_ambiguity {
1002                    assert!(import.imported_module.get().is_none());
1003                    self.report_error(
1004                        span,
1005                        ResolutionError::FailedToResolve {
1006                            segment: Some(segment_name),
1007                            label,
1008                            suggestion,
1009                            module,
1010                        },
1011                    );
1012                }
1013                return None;
1014            }
1015            PathResult::Failed {
1016                is_error_from_last_segment: true,
1017                span,
1018                label,
1019                suggestion,
1020                module,
1021                segment_name,
1022                ..
1023            } => {
1024                if no_ambiguity {
1025                    assert!(import.imported_module.get().is_none());
1026                    let module = if let Some(ModuleOrUniformRoot::Module(m)) = module {
1027                        m.opt_def_id()
1028                    } else {
1029                        None
1030                    };
1031                    let err = match self
1032                        .make_path_suggestion(import.module_path.clone(), &import.parent_scope)
1033                    {
1034                        Some((suggestion, note)) => UnresolvedImportError {
1035                            span,
1036                            label: None,
1037                            note,
1038                            suggestion: Some((
1039                                vec![(span, Segment::names_to_string(&suggestion))],
1040                                String::from("a similar path exists"),
1041                                Applicability::MaybeIncorrect,
1042                            )),
1043                            candidates: None,
1044                            segment: Some(segment_name),
1045                            module,
1046                        },
1047                        None => UnresolvedImportError {
1048                            span,
1049                            label: Some(label),
1050                            note: None,
1051                            suggestion,
1052                            candidates: None,
1053                            segment: Some(segment_name),
1054                            module,
1055                        },
1056                    };
1057                    return Some(err);
1058                }
1059                return None;
1060            }
1061            PathResult::NonModule(partial_res) => {
1062                if no_ambiguity && partial_res.full_res() != Some(Res::Err) {
1063                    // Check if there are no ambiguities and the result is not dummy.
1064                    assert!(import.imported_module.get().is_none());
1065                }
1066                // The error was already reported earlier.
1067                return None;
1068            }
1069            PathResult::Indeterminate => unreachable!(),
1070        };
1071
1072        let (ident, target, bindings, type_ns_only, import_id) = match import.kind {
1073            ImportKind::Single { source, target, ref bindings, type_ns_only, id, .. } => {
1074                (source, target, bindings, type_ns_only, id)
1075            }
1076            ImportKind::Glob { is_prelude, ref max_vis, id } => {
1077                if import.module_path.len() <= 1 {
1078                    // HACK(eddyb) `lint_if_path_starts_with_module` needs at least
1079                    // 2 segments, so the `resolve_path` above won't trigger it.
1080                    let mut full_path = import.module_path.clone();
1081                    full_path.push(Segment::from_ident(Ident::dummy()));
1082                    self.lint_if_path_starts_with_module(finalize, &full_path, None);
1083                }
1084
1085                if let ModuleOrUniformRoot::Module(module) = module
1086                    && module == import.parent_scope.module
1087                {
1088                    // Importing a module into itself is not allowed.
1089                    return Some(UnresolvedImportError {
1090                        span: import.span,
1091                        label: Some(String::from("cannot glob-import a module into itself")),
1092                        note: None,
1093                        suggestion: None,
1094                        candidates: None,
1095                        segment: None,
1096                        module: None,
1097                    });
1098                }
1099                if !is_prelude
1100                    && let Some(max_vis) = max_vis.get()
1101                    && !max_vis.is_at_least(import.vis, self.tcx)
1102                {
1103                    let def_id = self.local_def_id(id);
1104                    self.lint_buffer.buffer_lint(
1105                        UNUSED_IMPORTS,
1106                        id,
1107                        import.span,
1108                        BuiltinLintDiag::RedundantImportVisibility {
1109                            max_vis: max_vis.to_string(def_id, self.tcx),
1110                            import_vis: import.vis.to_string(def_id, self.tcx),
1111                            span: import.span,
1112                        },
1113                    );
1114                }
1115                return None;
1116            }
1117            _ => unreachable!(),
1118        };
1119
1120        if self.privacy_errors.len() != privacy_errors_len {
1121            // Get the Res for the last element, so that we can point to alternative ways of
1122            // importing it if available.
1123            let mut path = import.module_path.clone();
1124            path.push(Segment::from_ident(ident));
1125            if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = self.cm().resolve_path(
1126                &path,
1127                None,
1128                &import.parent_scope,
1129                Some(finalize),
1130                ignore_binding,
1131                None,
1132            ) {
1133                let res = module.res().map(|r| (r, ident));
1134                for error in &mut self.privacy_errors[privacy_errors_len..] {
1135                    error.outermost_res = res;
1136                }
1137            }
1138        }
1139
1140        let mut all_ns_err = true;
1141        self.per_ns(|this, ns| {
1142            if !type_ns_only || ns == TypeNS {
1143                let binding = this.cm().resolve_ident_in_module(
1144                    module,
1145                    ident,
1146                    ns,
1147                    &import.parent_scope,
1148                    Some(Finalize { report_private: false, ..finalize }),
1149                    bindings[ns].get().binding(),
1150                    Some(import),
1151                );
1152
1153                match binding {
1154                    Ok(binding) => {
1155                        // Consistency checks, analogous to `finalize_macro_resolutions`.
1156                        let initial_res = bindings[ns].get().binding().map(|binding| {
1157                            let initial_binding = binding.import_source();
1158                            all_ns_err = false;
1159                            if target.name == kw::Underscore
1160                                && initial_binding.is_extern_crate()
1161                                && !initial_binding.is_import()
1162                            {
1163                                let used = if import.module_path.is_empty() {
1164                                    Used::Scope
1165                                } else {
1166                                    Used::Other
1167                                };
1168                                this.record_use(ident, binding, used);
1169                            }
1170                            initial_binding.res()
1171                        });
1172                        let res = binding.res();
1173                        let has_ambiguity_error =
1174                            this.ambiguity_errors.iter().any(|error| !error.warning);
1175                        if res == Res::Err || has_ambiguity_error {
1176                            this.dcx()
1177                                .span_delayed_bug(import.span, "some error happened for an import");
1178                            return;
1179                        }
1180                        if let Some(initial_res) = initial_res {
1181                            if res != initial_res {
1182                                span_bug!(import.span, "inconsistent resolution for an import");
1183                            }
1184                        } else if this.privacy_errors.is_empty() {
1185                            this.dcx()
1186                                .create_err(CannotDetermineImportResolution { span: import.span })
1187                                .emit();
1188                        }
1189                    }
1190                    Err(..) => {
1191                        // FIXME: This assert may fire if public glob is later shadowed by a private
1192                        // single import (see test `issue-55884-2.rs`). In theory single imports should
1193                        // always block globs, even if they are not yet resolved, so that this kind of
1194                        // self-inconsistent resolution never happens.
1195                        // Re-enable the assert when the issue is fixed.
1196                        // assert!(result[ns].get().is_err());
1197                    }
1198                }
1199            }
1200        });
1201
1202        if all_ns_err {
1203            let mut all_ns_failed = true;
1204            self.per_ns(|this, ns| {
1205                if !type_ns_only || ns == TypeNS {
1206                    let binding = this.cm().resolve_ident_in_module(
1207                        module,
1208                        ident,
1209                        ns,
1210                        &import.parent_scope,
1211                        Some(finalize),
1212                        None,
1213                        None,
1214                    );
1215                    if binding.is_ok() {
1216                        all_ns_failed = false;
1217                    }
1218                }
1219            });
1220
1221            return if all_ns_failed {
1222                let names = match module {
1223                    ModuleOrUniformRoot::Module(module) => {
1224                        self.resolutions(module)
1225                            .borrow()
1226                            .iter()
1227                            .filter_map(|(BindingKey { ident: i, .. }, resolution)| {
1228                                if i.name == ident.name {
1229                                    return None;
1230                                } // Never suggest the same name
1231
1232                                let resolution = resolution.borrow();
1233                                if let Some(name_binding) = resolution.best_binding() {
1234                                    match name_binding.kind {
1235                                        NameBindingKind::Import { binding, .. } => {
1236                                            match binding.kind {
1237                                                // Never suggest the name that has binding error
1238                                                // i.e., the name that cannot be previously resolved
1239                                                NameBindingKind::Res(Res::Err) => None,
1240                                                _ => Some(i.name),
1241                                            }
1242                                        }
1243                                        _ => Some(i.name),
1244                                    }
1245                                } else if resolution.single_imports.is_empty() {
1246                                    None
1247                                } else {
1248                                    Some(i.name)
1249                                }
1250                            })
1251                            .collect()
1252                    }
1253                    _ => Vec::new(),
1254                };
1255
1256                let lev_suggestion =
1257                    find_best_match_for_name(&names, ident.name, None).map(|suggestion| {
1258                        (
1259                            vec![(ident.span, suggestion.to_string())],
1260                            String::from("a similar name exists in the module"),
1261                            Applicability::MaybeIncorrect,
1262                        )
1263                    });
1264
1265                let (suggestion, note) =
1266                    match self.check_for_module_export_macro(import, module, ident) {
1267                        Some((suggestion, note)) => (suggestion.or(lev_suggestion), note),
1268                        _ => (lev_suggestion, None),
1269                    };
1270
1271                let label = match module {
1272                    ModuleOrUniformRoot::Module(module) => {
1273                        let module_str = module_to_string(module);
1274                        if let Some(module_str) = module_str {
1275                            format!("no `{ident}` in `{module_str}`")
1276                        } else {
1277                            format!("no `{ident}` in the root")
1278                        }
1279                    }
1280                    _ => {
1281                        if !ident.is_path_segment_keyword() {
1282                            format!("no external crate `{ident}`")
1283                        } else {
1284                            // HACK(eddyb) this shows up for `self` & `super`, which
1285                            // should work instead - for now keep the same error message.
1286                            format!("no `{ident}` in the root")
1287                        }
1288                    }
1289                };
1290
1291                let parent_suggestion =
1292                    self.lookup_import_candidates(ident, TypeNS, &import.parent_scope, |_| true);
1293
1294                Some(UnresolvedImportError {
1295                    span: import.span,
1296                    label: Some(label),
1297                    note,
1298                    suggestion,
1299                    candidates: if !parent_suggestion.is_empty() {
1300                        Some(parent_suggestion)
1301                    } else {
1302                        None
1303                    },
1304                    module: import.imported_module.get().and_then(|module| {
1305                        if let ModuleOrUniformRoot::Module(m) = module {
1306                            m.opt_def_id()
1307                        } else {
1308                            None
1309                        }
1310                    }),
1311                    segment: Some(ident.name),
1312                })
1313            } else {
1314                // `resolve_ident_in_module` reported a privacy error.
1315                None
1316            };
1317        }
1318
1319        let mut reexport_error = None;
1320        let mut any_successful_reexport = false;
1321        let mut crate_private_reexport = false;
1322        self.per_ns(|this, ns| {
1323            let Some(binding) = bindings[ns].get().binding().map(|b| b.import_source()) else {
1324                return;
1325            };
1326
1327            if !binding.vis.is_at_least(import.vis, this.tcx) {
1328                reexport_error = Some((ns, binding));
1329                if let Visibility::Restricted(binding_def_id) = binding.vis
1330                    && binding_def_id.is_top_level_module()
1331                {
1332                    crate_private_reexport = true;
1333                }
1334            } else {
1335                any_successful_reexport = true;
1336            }
1337        });
1338
1339        // All namespaces must be re-exported with extra visibility for an error to occur.
1340        if !any_successful_reexport {
1341            let (ns, binding) = reexport_error.unwrap();
1342            if let Some(extern_crate_id) = pub_use_of_private_extern_crate_hack(import, binding) {
1343                self.lint_buffer.buffer_lint(
1344                    PUB_USE_OF_PRIVATE_EXTERN_CRATE,
1345                    import_id,
1346                    import.span,
1347                    BuiltinLintDiag::PrivateExternCrateReexport {
1348                        source: ident,
1349                        extern_crate_span: self.tcx.source_span(self.local_def_id(extern_crate_id)),
1350                    },
1351                );
1352            } else if ns == TypeNS {
1353                let err = if crate_private_reexport {
1354                    self.dcx()
1355                        .create_err(CannotBeReexportedCratePublicNS { span: import.span, ident })
1356                } else {
1357                    self.dcx().create_err(CannotBeReexportedPrivateNS { span: import.span, ident })
1358                };
1359                err.emit();
1360            } else {
1361                let mut err = if crate_private_reexport {
1362                    self.dcx()
1363                        .create_err(CannotBeReexportedCratePublic { span: import.span, ident })
1364                } else {
1365                    self.dcx().create_err(CannotBeReexportedPrivate { span: import.span, ident })
1366                };
1367
1368                match binding.kind {
1369                        NameBindingKind::Res(Res::Def(DefKind::Macro(_), def_id))
1370                            // exclude decl_macro
1371                            if self.get_macro_by_def_id(def_id).macro_rules =>
1372                        {
1373                            err.subdiagnostic( ConsiderAddingMacroExport {
1374                                span: binding.span,
1375                            });
1376                        }
1377                        _ => {
1378                            err.subdiagnostic( ConsiderMarkingAsPub {
1379                                span: import.span,
1380                                ident,
1381                            });
1382                        }
1383                    }
1384                err.emit();
1385            }
1386        }
1387
1388        if import.module_path.len() <= 1 {
1389            // HACK(eddyb) `lint_if_path_starts_with_module` needs at least
1390            // 2 segments, so the `resolve_path` above won't trigger it.
1391            let mut full_path = import.module_path.clone();
1392            full_path.push(Segment::from_ident(ident));
1393            self.per_ns(|this, ns| {
1394                if let Some(binding) = bindings[ns].get().binding().map(|b| b.import_source()) {
1395                    this.lint_if_path_starts_with_module(finalize, &full_path, Some(binding));
1396                }
1397            });
1398        }
1399
1400        // Record what this import resolves to for later uses in documentation,
1401        // this may resolve to either a value or a type, but for documentation
1402        // purposes it's good enough to just favor one over the other.
1403        self.per_ns(|this, ns| {
1404            if let Some(binding) = bindings[ns].get().binding().map(|b| b.import_source()) {
1405                this.import_res_map.entry(import_id).or_default()[ns] = Some(binding.res());
1406            }
1407        });
1408
1409        debug!("(resolving single import) successfully resolved import");
1410        None
1411    }
1412
1413    pub(crate) fn check_for_redundant_imports(&mut self, import: Import<'ra>) -> bool {
1414        // This function is only called for single imports.
1415        let ImportKind::Single { source, target, ref bindings, id, .. } = import.kind else {
1416            unreachable!()
1417        };
1418
1419        // Skip if the import is of the form `use source as target` and source != target.
1420        if source != target {
1421            return false;
1422        }
1423
1424        // Skip if the import was produced by a macro.
1425        if import.parent_scope.expansion != LocalExpnId::ROOT {
1426            return false;
1427        }
1428
1429        // Skip if we are inside a named module (in contrast to an anonymous
1430        // module defined by a block).
1431        // Skip if the import is public or was used through non scope-based resolution,
1432        // e.g. through a module-relative path.
1433        if self.import_use_map.get(&import) == Some(&Used::Other)
1434            || self.effective_visibilities.is_exported(self.local_def_id(id))
1435        {
1436            return false;
1437        }
1438
1439        let mut is_redundant = true;
1440        let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None };
1441        self.per_ns(|this, ns| {
1442            let binding = bindings[ns].get().binding().map(|b| b.import_source());
1443            if is_redundant && let Some(binding) = binding {
1444                if binding.res() == Res::Err {
1445                    return;
1446                }
1447
1448                match this.cm().early_resolve_ident_in_lexical_scope(
1449                    target,
1450                    ScopeSet::All(ns),
1451                    &import.parent_scope,
1452                    None,
1453                    false,
1454                    bindings[ns].get().binding(),
1455                    None,
1456                ) {
1457                    Ok(other_binding) => {
1458                        is_redundant = binding.res() == other_binding.res()
1459                            && !other_binding.is_ambiguity_recursive();
1460                        if is_redundant {
1461                            redundant_span[ns] =
1462                                Some((other_binding.span, other_binding.is_import()));
1463                        }
1464                    }
1465                    Err(_) => is_redundant = false,
1466                }
1467            }
1468        });
1469
1470        if is_redundant && !redundant_span.is_empty() {
1471            let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();
1472            redundant_spans.sort();
1473            redundant_spans.dedup();
1474            self.lint_buffer.buffer_lint(
1475                REDUNDANT_IMPORTS,
1476                id,
1477                import.span,
1478                BuiltinLintDiag::RedundantImport(redundant_spans, source),
1479            );
1480            return true;
1481        }
1482
1483        false
1484    }
1485
1486    fn resolve_glob_import(&mut self, import: Import<'ra>) {
1487        // This function is only called for glob imports.
1488        let ImportKind::Glob { id, is_prelude, .. } = import.kind else { unreachable!() };
1489
1490        let ModuleOrUniformRoot::Module(module) = import.imported_module.get().unwrap() else {
1491            self.dcx().emit_err(CannotGlobImportAllCrates { span: import.span });
1492            return;
1493        };
1494
1495        if module.is_trait() && !self.tcx.features().import_trait_associated_functions() {
1496            feature_err(
1497                self.tcx.sess,
1498                sym::import_trait_associated_functions,
1499                import.span,
1500                "`use` associated items of traits is unstable",
1501            )
1502            .emit();
1503        }
1504
1505        if module == import.parent_scope.module {
1506            return;
1507        } else if is_prelude {
1508            self.prelude = Some(module);
1509            return;
1510        }
1511
1512        // Add to module's glob_importers
1513        module.glob_importers.borrow_mut().push(import);
1514
1515        // Ensure that `resolutions` isn't borrowed during `try_define`,
1516        // since it might get updated via a glob cycle.
1517        let bindings = self
1518            .resolutions(module)
1519            .borrow()
1520            .iter()
1521            .filter_map(|(key, resolution)| {
1522                resolution.borrow().binding().map(|binding| (*key, binding))
1523            })
1524            .collect::<Vec<_>>();
1525        for (mut key, binding) in bindings {
1526            let scope = match key.ident.0.span.reverse_glob_adjust(module.expansion, import.span) {
1527                Some(Some(def)) => self.expn_def_scope(def),
1528                Some(None) => import.parent_scope.module,
1529                None => continue,
1530            };
1531            if self.is_accessible_from(binding.vis, scope) {
1532                let imported_binding = self.import(binding, import);
1533                let warn_ambiguity = self
1534                    .resolution(import.parent_scope.module, key)
1535                    .and_then(|r| r.binding())
1536                    .is_some_and(|binding| binding.warn_ambiguity_recursive());
1537                let _ = self.try_define_local(
1538                    import.parent_scope.module,
1539                    key.ident.0,
1540                    key.ns,
1541                    imported_binding,
1542                    warn_ambiguity,
1543                );
1544            }
1545        }
1546
1547        // Record the destination of this import
1548        self.record_partial_res(id, PartialRes::new(module.res().unwrap()));
1549    }
1550
1551    // Miscellaneous post-processing, including recording re-exports,
1552    // reporting conflicts, and reporting unresolved imports.
1553    fn finalize_resolutions_in(&mut self, module: Module<'ra>) {
1554        // Since import resolution is finished, globs will not define any more names.
1555        *module.globs.borrow_mut() = Vec::new();
1556
1557        let Some(def_id) = module.opt_def_id() else { return };
1558
1559        let mut children = Vec::new();
1560
1561        module.for_each_child(self, |this, ident, _, binding| {
1562            let res = binding.res().expect_non_local();
1563            let error_ambiguity = binding.is_ambiguity_recursive() && !binding.warn_ambiguity;
1564            if res != def::Res::Err && !error_ambiguity {
1565                let mut reexport_chain = SmallVec::new();
1566                let mut next_binding = binding;
1567                while let NameBindingKind::Import { binding, import, .. } = next_binding.kind {
1568                    reexport_chain.push(import.simplify(this));
1569                    next_binding = binding;
1570                }
1571
1572                children.push(ModChild { ident: ident.0, res, vis: binding.vis, reexport_chain });
1573            }
1574        });
1575
1576        if !children.is_empty() {
1577            // Should be fine because this code is only called for local modules.
1578            self.module_children.insert(def_id.expect_local(), children);
1579        }
1580    }
1581}
1582
1583fn import_path_to_string(names: &[Ident], import_kind: &ImportKind<'_>, span: Span) -> String {
1584    let pos = names.iter().position(|p| span == p.span && p.name != kw::PathRoot);
1585    let global = !names.is_empty() && names[0].name == kw::PathRoot;
1586    if let Some(pos) = pos {
1587        let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] };
1588        names_to_string(names.iter().map(|ident| ident.name))
1589    } else {
1590        let names = if global { &names[1..] } else { names };
1591        if names.is_empty() {
1592            import_kind_to_string(import_kind)
1593        } else {
1594            format!(
1595                "{}::{}",
1596                names_to_string(names.iter().map(|ident| ident.name)),
1597                import_kind_to_string(import_kind),
1598            )
1599        }
1600    }
1601}
1602
1603fn import_kind_to_string(import_kind: &ImportKind<'_>) -> String {
1604    match import_kind {
1605        ImportKind::Single { source, .. } => source.to_string(),
1606        ImportKind::Glob { .. } => "*".to_string(),
1607        ImportKind::ExternCrate { .. } => "<extern crate>".to_string(),
1608        ImportKind::MacroUse { .. } => "#[macro_use]".to_string(),
1609        ImportKind::MacroExport => "#[macro_export]".to_string(),
1610    }
1611}