compiler/rustc_resolve/src/imports.rs RUST 1,900 lines View on github.com → Search inside
1//! A bunch of methods and structures more or less related to resolving imports.23use std::cmp::Ordering;4use std::mem;56use rustc_ast::NodeId;7use rustc_data_structures::fx::{FxHashSet, FxIndexSet};8use rustc_data_structures::intern::Interned;9use rustc_errors::{Applicability, BufferedEarlyLint, Diagnostic};10use rustc_expand::base::SyntaxExtensionKind;11use rustc_hir::def::{self, DefKind, PartialRes};12use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap};13use rustc_middle::metadata::{AmbigModChild, ModChild, Reexport};14use rustc_middle::span_bug;15use rustc_middle::ty::Visibility;16use rustc_session::errors::feature_err;17use rustc_session::lint::LintId;18use rustc_session::lint::builtin::{19    AMBIGUOUS_GLOB_REEXPORTS, EXPORTED_PRIVATE_DEPENDENCIES, HIDDEN_GLOB_REEXPORTS,20    PUB_USE_OF_PRIVATE_EXTERN_CRATE, REDUNDANT_IMPORTS, UNUSED_IMPORTS,21};22use rustc_span::edit_distance::find_best_match_for_name;23use rustc_span::hygiene::LocalExpnId;24use rustc_span::{Ident, Span, Symbol, kw, sym};25use tracing::debug;2627use crate::Namespace::{self, *};28use crate::diagnostics::{29    self, CannotBeReexportedCratePublic, CannotBeReexportedCratePublicNS,30    CannotBeReexportedPrivate, CannotBeReexportedPrivateNS, CannotDetermineImportResolution,31    CannotGlobImportAllCrates, ConsiderAddingMacroExport, ConsiderMarkingAsPub,32    ConsiderMarkingAsPubCrate,33};34use crate::error_helper::{OnUnknownData, Suggestion};35use crate::ref_mut::CmCell;36use crate::{37    AmbiguityError, BindingKey, CmResolver, Decl, DeclData, DeclKind, Determinacy, Finalize,38    IdentKey, ImportSuggestion, ImportSummary, LocalModule, ModuleOrUniformRoot, ParentScope,39    PathResult, PerNS, Res, ResolutionError, Resolver, ScopeSet, Segment, Used, module_to_string,40    names_to_string,41};4243/// A potential import declaration in the process of being planted into a module.44/// Also used for lazily planting names from `--extern` flags to extern prelude.45#[derive(Clone, Copy, Default, PartialEq, Debug)]46pub(crate) enum PendingDecl<'ra> {47    Ready(Option<Decl<'ra>>),48    #[default]49    Pending,50}5152enum ImportResolutionKind<'ra> {53    Single(PerNS<PendingDecl<'ra>>),54    Glob(Vec<(Decl<'ra>, BindingKey, Span /* orig_ident_span */)>),55}5657struct ImportResolution<'ra> {58    kind: ImportResolutionKind<'ra>,59    imported_module: ModuleOrUniformRoot<'ra>,60}6162impl<'ra> PendingDecl<'ra> {63    pub(crate) fn decl(self) -> Option<Decl<'ra>> {64        match self {65            PendingDecl::Ready(decl) => decl,66            PendingDecl::Pending => None,67        }68    }69}7071/// Contains data for specific kinds of imports.72#[derive(Clone)]73pub(crate) enum ImportKind<'ra> {74    Single {75        /// `source` in `use prefix::source as target`.76        source: Ident,77        /// `target` in `use prefix::source as target`.78        /// It will directly use `source` when the format is `use prefix::source`.79        target: Ident,80        /// Name declarations introduced by the import.81        decls: PerNS<CmCell<PendingDecl<'ra>>>,82        /// Did this import result from a nested import? i.e. `use foo::{bar, baz};`83        nested: bool,84        /// The ID of the `UseTree` that imported this `Import`.85        ///86        /// In the case where the `Import` was expanded from a "nested" use tree,87        /// this id is the ID of the leaf tree. For example:88        ///89        /// ```ignore (pacify the merciless tidy)90        /// use foo::bar::{a, b}91        /// ```92        ///93        /// If this is the import for `foo::bar::a`, we would have the ID of the `UseTree`94        /// for `a` in this field.95        id: NodeId,96        def_id: LocalDefId,97    },98    Glob {99        // The visibility of the greatest re-export.100        // n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.101        max_vis: CmCell<Option<Visibility>>,102        id: NodeId,103        def_id: LocalDefId,104    },105    ExternCrate {106        source: Option<Symbol>,107        target: Ident,108        id: NodeId,109        def_id: LocalDefId,110    },111    MacroUse {112        /// A field has been added indicating whether it should be reported as a lint,113        /// addressing issue#119301.114        warn_private: bool,115    },116    MacroExport,117}118119/// Manually implement `Debug` for `ImportKind` because the `source/target_bindings`120/// contain `Cell`s which can introduce infinite loops while printing.121impl<'ra> std::fmt::Debug for ImportKind<'ra> {122    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {123        use ImportKind::*;124        match self {125            Single { source, target, decls, nested, id, def_id } => f126                .debug_struct("Single")127                .field("source", source)128                .field("target", target)129                // Ignore the nested bindings to avoid an infinite loop while printing.130                .field(131                    "decls",132                    &decls.clone().map(|b| b.into_inner().decl().map(|_| format_args!(".."))),133                )134                .field("nested", nested)135                .field("id", id)136                .field("def_id", def_id)137                .finish(),138            Glob { max_vis, id, def_id } => f139                .debug_struct("Glob")140                .field("max_vis", max_vis)141                .field("id", id)142                .field("def_id", def_id)143                .finish(),144            ExternCrate { source, target, id, def_id } => f145                .debug_struct("ExternCrate")146                .field("source", source)147                .field("target", target)148                .field("id", id)149                .field("def_id", def_id)150                .finish(),151            MacroUse { warn_private } => {152                f.debug_struct("MacroUse").field("warn_private", warn_private).finish()153            }154            MacroExport => f.debug_struct("MacroExport").finish(),155        }156    }157}158159/// One import.160#[derive(Debug, Clone)]161pub(crate) struct ImportData<'ra> {162    pub kind: ImportKind<'ra>,163164    /// Node ID of the "root" use item -- this is always the same as `ImportKind`'s `id`165    /// (if it exists) except in the case of "nested" use trees, in which case166    /// it will be the ID of the root use tree. e.g., in the example167    /// ```ignore (incomplete code)168    /// use foo::bar::{a, b}169    /// ```170    /// this would be the ID of the `use foo::bar` `UseTree` node.171    /// In case of imports without their own node ID it's the closest node that can be used,172    /// for example, for reporting lints.173    pub root_id: NodeId,174175    /// Span of the entire use statement.176    pub use_span: Span,177178    /// Span of the entire use statement with attributes.179    pub use_span_with_attributes: Span,180181    /// Did the use statement have any attributes?182    pub has_attributes: bool,183184    /// Span of this use tree.185    pub span: Span,186187    /// Span of the *root* use tree (see `root_id`).188    pub root_span: Span,189190    pub parent_scope: ParentScope<'ra>,191    pub module_path: Vec<Segment>,192    /// The resolution of `module_path`:193    ///194    /// | `module_path` | `imported_module` | remark |195    /// |-|-|-|196    /// |`use prefix::foo`| `ModuleOrUniformRoot::Module(prefix)`         | - |197    /// |`use ::foo`      | `ModuleOrUniformRoot::ExternPrelude`          | 2018+ editions |198    /// |`use ::foo`      | `ModuleOrUniformRoot::ModuleAndExternPrelude` | a special case in 2015 edition |199    /// |`use foo`        | `ModuleOrUniformRoot::CurrentScope`           | - |200    pub imported_module: CmCell<Option<ModuleOrUniformRoot<'ra>>>,201    pub vis: Visibility,202203    /// Span of the visibility.204    pub vis_span: Span,205206    /// A `#[diagnostic::on_unknown]` attribute applied207    /// to the given import. This allows crates to specify208    /// custom error messages for a specific import209    ///210    /// This is `None` if the feature flag for `diagnostic::on_unknown` is disabled.211    pub on_unknown_attr: Option<OnUnknownData>,212}213214/// All imports are unique and allocated on a same arena,215/// so we can use referential equality to compare them.216pub(crate) type Import<'ra> = Interned<'ra, ImportData<'ra>>;217218// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the219// contained data.220// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees221// are upheld.222impl std::hash::Hash for ImportData<'_> {223    fn hash<H>(&self, _: &mut H)224    where225        H: std::hash::Hasher,226    {227        unreachable!()228    }229}230231impl<'ra> ImportData<'ra> {232    pub(crate) fn is_glob(&self) -> bool {233        matches!(self.kind, ImportKind::Glob { .. })234    }235236    pub(crate) fn is_nested(&self) -> bool {237        match self.kind {238            ImportKind::Single { nested, .. } => nested,239            _ => false,240        }241    }242243    pub(crate) fn id(&self) -> Option<NodeId> {244        match self.kind {245            ImportKind::Single { id, .. }246            | ImportKind::Glob { id, .. }247            | ImportKind::ExternCrate { id, .. } => Some(id),248            ImportKind::MacroUse { .. } | ImportKind::MacroExport => None,249        }250    }251252    pub(crate) fn def_id(&self) -> Option<LocalDefId> {253        match self.kind {254            ImportKind::Single { def_id, .. }255            | ImportKind::Glob { def_id, .. }256            | ImportKind::ExternCrate { def_id, .. } => Some(def_id),257            ImportKind::MacroUse { .. } | ImportKind::MacroExport => None,258        }259    }260261    pub(crate) fn simplify(&self) -> Reexport {262        match self.kind {263            ImportKind::Single { def_id, .. } => Reexport::Single(def_id.to_def_id()),264            ImportKind::Glob { def_id, .. } => Reexport::Glob(def_id.to_def_id()),265            ImportKind::ExternCrate { def_id, .. } => Reexport::ExternCrate(def_id.to_def_id()),266            ImportKind::MacroUse { .. } => Reexport::MacroUse,267            ImportKind::MacroExport => Reexport::MacroExport,268        }269    }270271    fn summary(&self) -> ImportSummary {272        ImportSummary {273            vis: self.vis,274            nearest_parent_mod: self.parent_scope.module.nearest_parent_mod().expect_local(),275            is_single: matches!(self.kind, ImportKind::Single { .. }),276            priv_macro_use: matches!(self.kind, ImportKind::MacroUse { warn_private: true }),277            span: self.span,278        }279    }280}281282/// Records information about the resolution of a name in a namespace of a module.283#[derive(Clone, Debug)]284pub(crate) struct NameResolution<'ra> {285    /// Single imports that may define the name in the namespace.286    /// Imports are arena-allocated, so it's ok to use pointers as keys.287    pub single_imports: FxIndexSet<Import<'ra>>,288    /// The non-glob declaration for this name, if it is known to exist.289    pub non_glob_decl: Option<Decl<'ra>> = None,290    /// The glob declaration for this name, if it is known to exist.291    pub glob_decl: Option<Decl<'ra>> = None,292    pub orig_ident_span: Span,293}294295impl<'ra> NameResolution<'ra> {296    pub(crate) fn new(orig_ident_span: Span) -> Self {297        NameResolution { single_imports: FxIndexSet::default(), orig_ident_span, .. }298    }299300    /// Returns the best declaration if it is not going to change, and `None` if the best301    /// declaration may still change to something else.302    /// FIXME: this function considers `single_imports`, but not `unexpanded_invocations`, so303    /// the returned declaration may actually change after expanding macros in the same module,304    /// because of this fact we have glob overwriting (`select_glob_decl`). Consider using305    /// `unexpanded_invocations` here and avoiding glob overwriting entirely, if it doesn't cause306    /// code breakage in practice.307    /// FIXME: relationship between this function and similar `DeclData::determined` is unclear.308    pub(crate) fn determined_decl(&self) -> Option<Decl<'ra>> {309        if self.non_glob_decl.is_some() {310            self.non_glob_decl311        } else if self.glob_decl.is_some() && self.single_imports.is_empty() {312            self.glob_decl313        } else {314            None315        }316    }317318    pub(crate) fn best_decl(&self) -> Option<Decl<'ra>> {319        self.non_glob_decl.or(self.glob_decl)320    }321}322323/// An error that may be transformed into a diagnostic later. Used to combine multiple unresolved324/// import errors within the same use tree into a single diagnostic.325#[derive(Debug, Clone)]326pub(crate) struct UnresolvedImportError {327    pub(crate) span: Span,328    pub(crate) label: Option<String>,329    pub(crate) note: Option<String>,330    pub(crate) suggestion: Option<Suggestion>,331    pub(crate) candidates: Option<Vec<ImportSuggestion>>,332    pub(crate) segment: Option<Ident>,333    /// comes from `PathRes::Failed { module }`334    pub(crate) module: Option<DefId>,335    pub(crate) on_unknown_attr: Option<OnUnknownData>,336}337338// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;`339// are permitted for backward-compatibility under a deprecation lint.340fn pub_use_of_private_extern_crate_hack(341    import: ImportSummary,342    decl: Decl<'_>,343) -> Option<LocalDefId> {344    match (import.is_single, decl.kind) {345        (true, DeclKind::Import { import: decl_import, .. })346            if let ImportKind::ExternCrate { def_id, .. } = decl_import.kind347                && import.vis.is_public() =>348        {349            Some(def_id)350        }351        _ => None,352    }353}354355/// Removes identical import layers from two declarations.356fn remove_same_import<'ra>(d1: Decl<'ra>, d2: Decl<'ra>) -> (Decl<'ra>, Decl<'ra>) {357    if let DeclKind::Import { import: import1, source_decl: d1_next } = d1.kind358        && let DeclKind::Import { import: import2, source_decl: d2_next } = d2.kind359        && import1 == import2360    {361        assert_eq!(d1.expansion, d2.expansion);362        assert_eq!(d1.span, d2.span);363        if d1.ambiguity.get() != d2.ambiguity.get() {364            assert!(d1.ambiguity.get().is_some());365        }366        // Visibility of the new import declaration may be different,367        // because it already incorporates the visibility of the source binding.368        remove_same_import(d1_next, d2_next)369    } else {370        (d1, d2)371    }372}373374impl<'ra, 'tcx> Resolver<'ra, 'tcx> {375    pub(crate) fn import_decl_vis(&self, decl: Decl<'ra>, import: ImportSummary) -> Visibility {376        self.import_decl_vis_ext(decl, import, false)377    }378379    pub(crate) fn import_decl_vis_ext(380        &self,381        decl: Decl<'ra>,382        import: ImportSummary,383        min: bool,384    ) -> Visibility {385        assert!(import.vis.is_accessible_from(import.nearest_parent_mod, self.tcx));386        let decl_vis = if min { decl.min_vis() } else { decl.vis() };387        let ord = decl_vis.partial_cmp(import.vis, self.tcx);388        let extern_crate_hack = pub_use_of_private_extern_crate_hack(import, decl).is_some();389        if ord == Some(Ordering::Less)390            && decl_vis.is_accessible_from(import.nearest_parent_mod, self.tcx)391            && !extern_crate_hack392        {393            // Imported declaration is less visible than the import, but is still visible394            // from the current module, use the declaration's visibility.395            decl_vis.expect_local()396        } else {397            // Good case - imported declaration is more visible than the import, or the same,398            // use the import's visibility.399            //400            // Bad case - imported declaration is too private for the current module.401            // It doesn't matter what visibility we choose here (except in the `PRIVATE_MACRO_USE`402            // and `PUB_USE_OF_PRIVATE_EXTERN_CRATE` cases), because an error will be reported.403            // Use import visibility to keep the all declaration visibilities in a module ordered.404            if !min405                && matches!(ord, None | Some(Ordering::Less))406                && !extern_crate_hack407                && !import.priv_macro_use408            {409                let msg = format!("cannot extend visibility from {decl_vis:?} to {:?}", import.vis);410                self.dcx().span_delayed_bug(import.span, msg);411            }412            import.vis413        }414    }415416    /// Given an import and the declaration that it points to,417    /// create the corresponding import declaration.418    pub(crate) fn new_import_decl(&self, decl: Decl<'ra>, import: Import<'ra>) -> Decl<'ra> {419        let vis = self.import_decl_vis(decl, import.summary());420421        if let ImportKind::Glob { ref max_vis, .. } = import.kind422            && (vis == import.vis423                || max_vis.get().is_none_or(|max_vis| vis.greater_than(max_vis, self.tcx)))424        {425            // `set` can't fail because this can only happen during "write_import_resolutions"426            max_vis.set(Some(vis), self)427        }428429        self.arenas.alloc_decl(DeclData {430            kind: DeclKind::Import { source_decl: decl, import },431            ambiguity: CmCell::new(None),432            span: import.span,433            initial_vis: vis.to_def_id(),434            ambiguity_vis_max: CmCell::new(None),435            ambiguity_vis_min: CmCell::new(None),436            expansion: import.parent_scope.expansion,437            parent_module: Some(import.parent_scope.module),438        })439    }440441    fn is_noise_0_7_0(&self, old_glob_decl: Decl<'ra>, glob_decl: Decl<'ra>) -> bool {442        let DeclKind::Import { import: i1, .. } = glob_decl.kind else { unreachable!() };443        let DeclKind::Import { import: i2, .. } = old_glob_decl.kind else { unreachable!() };444        let [seg1, seg2] = &i1.module_path[..] else { return false };445        if seg1.ident.name != kw::SelfLower || seg2.ident.name.as_str() != "perlin_surflet" {446            return false;447        }448        let [seg1, seg2] = &i2.module_path[..] else { return false };449        if seg1.ident.name != kw::SelfLower || seg2.ident.name.as_str() != "perlin" {450            return false;451        }452        let Some(def_id1) = glob_decl.res().opt_def_id() else { return false };453        let Some(def_id2) = old_glob_decl.res().opt_def_id() else { return false };454        self.def_path_str(def_id1).ends_with("noise_fns::generators::perlin_surflet::Perlin")455            && self.def_path_str(def_id2).ends_with("noise_fns::generators::perlin::Perlin")456    }457458    fn is_rustybuzz_0_4_0(&self, old_glob_decl: Decl<'ra>, glob_decl: Decl<'ra>) -> bool {459        let DeclKind::Import { import: i1, .. } = glob_decl.kind else { unreachable!() };460        let DeclKind::Import { import: i2, .. } = old_glob_decl.kind else { unreachable!() };461        let [seg1, seg2] = &i1.module_path[..] else { return false };462        if seg1.ident.name != kw::Super || seg2.ident.name.as_str() != "gsubgpos" {463            return false;464        }465        let [seg1] = &i2.module_path[..] else { return false };466        if seg1.ident.name != kw::Super {467            return false;468        }469        let Some(def_id1) = glob_decl.res().opt_def_id() else { return false };470        let Some(def_id2) = old_glob_decl.res().opt_def_id() else { return false };471        self.def_path_str(def_id1).ends_with("tables::gsubgpos::Class")472            && self.def_path_str(def_id2).ends_with("ggg::Class")473    }474475    fn is_pdf_0_9_0(&self, old_glob_decl: Decl<'ra>, glob_decl: Decl<'ra>) -> bool {476        let DeclKind::Import { import: i1, .. } = glob_decl.kind else { unreachable!() };477        let DeclKind::Import { import: i2, .. } = old_glob_decl.kind else { unreachable!() };478        let [seg1, seg2] = &i1.module_path[..] else { return false };479        if seg1.ident.name != kw::Crate || seg2.ident.name.as_str() != "content" {480            return false;481        }482        let [seg1, seg2] = &i2.module_path[..] else { return false };483        if seg1.ident.name != kw::Crate || seg2.ident.name.as_str() != "object" {484            return false;485        }486        let Some(def_id1) = glob_decl.res().opt_def_id() else { return false };487        let Some(def_id2) = old_glob_decl.res().opt_def_id() else { return false };488        self.def_path_str(def_id1).ends_with("crate::content::Rect")489            && self.def_path_str(def_id2).ends_with("crate::object::types::Rect")490    }491492    fn is_net2_0_2_39(&self, old_glob_decl: Decl<'ra>, glob_decl: Decl<'ra>) -> bool {493        let DeclKind::Import { import: i1, .. } = glob_decl.kind else { unreachable!() };494        let DeclKind::Import { import: i2, .. } = old_glob_decl.kind else { unreachable!() };495        let [seg1, seg2, seg3, seg4] = &i1.module_path[..] else { return false };496        if seg1.ident.name != kw::PathRoot497            || seg2.ident.name.as_str() != "winapi"498            || seg3.ident.name.as_str() != "shared"499            || seg4.ident.name.as_str() != "ws2def"500        {501            return false;502        }503        let [seg1, seg2, seg3, seg4] = &i2.module_path[..] else { return false };504        if seg1.ident.name != kw::PathRoot505            || seg2.ident.name.as_str() != "winapi"506            || seg3.ident.name.as_str() != "um"507            || seg4.ident.name.as_str() != "winsock2"508        {509            return false;510        }511        let Some(def_id1) = glob_decl.res().opt_def_id() else { return false };512        let Some(def_id2) = old_glob_decl.res().opt_def_id() else { return false };513        self.def_path_str(def_id1).starts_with("winapi::shared::ws2def::")514            && self.def_path_str(def_id2).starts_with("winapi::um::winsock2::")515    }516517    /// If `glob_decl` attempts to overwrite `old_glob_decl` in a module,518    /// decide which one to keep.519    fn select_glob_decl(&self, old_glob_decl: Decl<'ra>, glob_decl: Decl<'ra>) -> Decl<'ra> {520        assert!(glob_decl.is_glob_import());521        assert!(old_glob_decl.is_glob_import());522        assert_ne!(glob_decl, old_glob_decl);523        // `best_decl` with a given key in a module may be overwritten in a524        // number of cases (all of them can be seen below in the `match` in `try_define_local`),525        // all these overwrites will be re-fetched by glob imports importing526        // from that module without generating new ambiguities.527        // - A glob decl is overwritten by a non-glob decl arriving later.528        // - A glob decl is overwritten by a glob decl re-fetching an529        //   overwritten decl from other module (the recursive case).530        // Here we are detecting all such re-fetches and overwrite old decls531        // with the re-fetched decls.532        // This is probably incorrect in corner cases, and the outdated decls still get533        // propagated to other places and get stuck there, but that's what we have at the moment.534        let (old_deep_decl, deep_decl) = remove_same_import(old_glob_decl, glob_decl);535        if deep_decl != glob_decl {536            // Some import layers have been removed, need to overwrite.537            assert_ne!(old_deep_decl, old_glob_decl);538            assert!(!deep_decl.is_glob_import());539            if let Some((old_ambig, _)) = old_glob_decl.ambiguity.get()540                && glob_decl.ambiguity.get().is_none()541            {542                // Do not lose glob ambiguities when re-fetching the glob.543                glob_decl.ambiguity.set(Some((old_ambig, true)), self);544            }545            glob_decl546        } else if glob_decl.res() != old_glob_decl.res() {547            let warning = self.is_noise_0_7_0(old_glob_decl, glob_decl)548                || self.is_rustybuzz_0_4_0(old_glob_decl, glob_decl)549                || self.is_pdf_0_9_0(old_glob_decl, glob_decl)550                || self.is_net2_0_2_39(old_glob_decl, glob_decl);551            old_glob_decl.ambiguity.set(Some((glob_decl, warning)), self);552            old_glob_decl553        } else if let old_vis = old_glob_decl.vis()554            && let vis = glob_decl.vis()555            && old_vis != vis556        {557            // We are glob-importing the same item but with a different visibility.558            // All visibilities here are ordered because all of them are ancestors of `module`.559            if vis.greater_than(old_vis, self.tcx) {560                old_glob_decl.ambiguity_vis_max.set(Some(glob_decl), self);561            } else if let old_min_vis = old_glob_decl.min_vis()562                && old_min_vis != vis563                && old_min_vis.greater_than(vis, self.tcx)564            {565                old_glob_decl.ambiguity_vis_min.set(Some(glob_decl), self);566            }567            old_glob_decl568        } else if glob_decl.is_ambiguity_recursive() && !old_glob_decl.is_ambiguity_recursive() {569            // Overwriting a non-ambiguous glob import with an ambiguous glob import.570            old_glob_decl.ambiguity.set(Some((glob_decl, true)), self);571            old_glob_decl572        } else {573            old_glob_decl574        }575    }576577    /// Attempt to put the declaration with the given name and namespace into the module,578    /// and return existing declaration if there is a collision.579    pub(crate) fn try_plant_decl_into_local_module(580        &mut self,581        ident: IdentKey,582        orig_ident_span: Span,583        ns: Namespace,584        decl: Decl<'ra>,585    ) -> Result<(), Decl<'ra>> {586        assert!(decl.ambiguity.get().is_none());587        assert!(decl.ambiguity_vis_max.get().is_none());588        assert!(decl.ambiguity_vis_min.get().is_none());589        let module = decl.parent_module.unwrap().expect_local();590        assert!(self.is_accessible_from(decl.vis(), module.to_module()));591        let res = decl.res();592        self.check_reserved_macro_name(ident.name, orig_ident_span, res);593        // Even if underscore names cannot be looked up, we still need to add them to modules,594        // because they can be fetched by glob imports from those modules, and bring traits595        // into scope both directly and through glob imports.596        let key = BindingKey::new_disambiguated(ident, ns, || {597            module.underscore_disambiguator.update(self, |d| d + 1);598            module.underscore_disambiguator.get()599        });600        self.update_local_resolution(module, key, orig_ident_span, |this, resolution| {601            if res == Res::Err602                && let Some(old_decl) = resolution.best_decl()603                && old_decl.res() != Res::Err604            {605                // Do not override real declarations with `Res::Err`s from error recovery.606                // FIXME: this special case shouldn't be necessary, but removing it triggers an ICE607                // due to some other issues (#157406, tests/ui/imports/dummy-import-ice.rs).608                return Ok(());609            }610            if decl.is_glob_import() {611                resolution.glob_decl = Some(match resolution.glob_decl {612                    Some(old_decl) => this.select_glob_decl(old_decl, decl),613                    None => decl,614                });615            } else {616                resolution.non_glob_decl = Some(match resolution.non_glob_decl {617                    Some(old_decl) => return Err(old_decl),618                    None => decl,619                })620            }621622            Ok(())623        })624    }625626    // Use `f` to mutate the resolution of the name in the module.627    // If the resolution becomes a success, define it in the module's glob importers.628    fn update_local_resolution<T, F>(629        &mut self,630        module: LocalModule<'ra>,631        key: BindingKey,632        orig_ident_span: Span,633        f: F,634    ) -> T635    where636        F: FnOnce(&Resolver<'ra, 'tcx>, &mut NameResolution<'ra>) -> T,637    {638        // Ensure that `resolution` isn't borrowed when defining in the module's glob importers,639        // during which the resolution might end up getting re-defined via a glob cycle.640        let (binding, t) = {641            let resolution = &mut *self642                .resolution_or_default(module.to_module(), key, orig_ident_span)643                .borrow_mut(self);644            let old_decl = resolution.determined_decl();645            let old_vis = old_decl.map(|d| d.vis());646647            let t = f(self, resolution);648649            if let Some(binding) = resolution.determined_decl()650                && (old_decl != Some(binding) || old_vis != Some(binding.vis()))651            {652                (binding, t)653            } else {654                return t;655            }656        };657658        let Ok(glob_importers) = module.glob_importers.try_borrow_mut(self) else {659            return t;660        };661662        // Define or update `binding` in `module`s glob importers.663        for import in glob_importers.iter() {664            let mut ident = key.ident;665            let scope = match ident666                .ctxt667                .update_unchecked(|ctxt| ctxt.reverse_glob_adjust(module.expansion, import.span))668            {669                Some(Some(def)) => self.expn_def_scope(def),670                Some(None) => import.parent_scope.module,671                None => continue,672            };673            if self.is_accessible_from(binding.vis(), scope) {674                let import_decl = self.new_import_decl(binding, *import);675                self.try_plant_decl_into_local_module(ident, orig_ident_span, key.ns, import_decl)676                    .expect("planting a glob cannot fail");677            }678        }679680        t681    }682683    // Define a dummy resolution containing a `Res::Err` as a placeholder for a failed684    // or indeterminate resolution, also mark such failed imports as used to avoid duplicate diagnostics.685    fn import_dummy_binding(&mut self, import: Import<'ra>, is_indeterminate: bool) {686        if let ImportKind::Single { target, ref decls, .. } = import.kind {687            if !(is_indeterminate || decls.iter().all(|d| d.get().decl().is_none())) {688                return; // Has resolution, do not create the dummy binding689            }690            let dummy_decl = self.dummy_decl;691            let dummy_decl = self.new_import_decl(dummy_decl, import);692            self.per_ns(|this, ns| {693                let ident = IdentKey::new(target);694                // This can fail, dummies are inserted only in non-occupied slots.695                let _ = this.try_plant_decl_into_local_module(ident, target.span, ns, dummy_decl);696                // Don't remove underscores from `single_imports`, they were never added.697                if target.name != kw::Underscore {698                    let key = BindingKey::new(ident, ns);699                    this.update_local_resolution(700                        import.parent_scope.module.expect_local(),701                        key,702                        target.span,703                        |_, resolution| {704                            resolution.single_imports.swap_remove(&import);705                        },706                    )707                }708            });709            self.record_use(target, dummy_decl, Used::Other);710        } else if import.imported_module.get().is_none() {711            self.import_use_map.insert(import, Used::Other);712            if let Some(id) = import.id() {713                self.used_imports.insert(id);714            }715        }716    }717718    // Import resolution719    //720    // This is a batched fixed-point algorithm. Each import is resolved in721    // isolation, with any resolutions collected for later.722    // After a full pass over the current set of `indeterminate_imports`,723    // the collected resolutions are committed together. The process724    // repeats until either no imports remain or no further progress can725    // be made.726727    /// Resolves all imports for the crate. This method performs the fixed-728    /// point iteration.729    pub(crate) fn resolve_imports(&mut self) {730        let mut prev_indeterminate_count = usize::MAX;731        let mut indeterminate_count = self.indeterminate_imports.len() * 3;732        while indeterminate_count < prev_indeterminate_count {733            prev_indeterminate_count = indeterminate_count;734            indeterminate_count = 0;735            let mut resolutions = Vec::new();736            self.assert_speculative = true;737            for import in mem::take(&mut self.indeterminate_imports) {738                let (resolution, import_indeterminate_count) = self.cm().resolve_import(import);739                indeterminate_count += import_indeterminate_count;740                match import_indeterminate_count {741                    0 => self.determined_imports.push(import),742                    _ => self.indeterminate_imports.push(import),743                }744                if let Some(resolution) = resolution {745                    resolutions.push((import, resolution));746                }747            }748            self.assert_speculative = false;749            self.write_import_resolutions(resolutions);750        }751    }752753    fn write_import_resolutions(754        &mut self,755        import_resolutions: Vec<(Import<'ra>, ImportResolution<'ra>)>,756    ) {757        for (import, resolution) in &import_resolutions {758            let ImportResolution { imported_module, .. } = resolution;759            import.imported_module.set(Some(*imported_module), self);760761            if import.is_glob()762                && let ModuleOrUniformRoot::Module(module) = imported_module763                && import.parent_scope.module != *module764                && module.is_local()765            {766                module.glob_importers.borrow_mut(self).push(*import);767            }768        }769770        for (import, resolution) in import_resolutions {771            let ImportResolution { imported_module, kind: resolution_kind } = resolution;772773            match (&import.kind, resolution_kind) {774                (775                    ImportKind::Single { target, decls, .. },776                    ImportResolutionKind::Single(import_decls),777                ) => {778                    self.per_ns(|this, ns| {779                        match import_decls[ns] {780                            PendingDecl::Ready(Some(import_decl)) => {781                                if import_decl.is_assoc_item()782                                    && !this.tcx.features().import_trait_associated_functions()783                                {784                                    feature_err(785                                        this.tcx.sess,786                                        sym::import_trait_associated_functions,787                                        import.span,788                                        "`use` associated items of traits is unstable",789                                    )790                                    .emit();791                                }792                                this.plant_decl_into_local_module(793                                    IdentKey::new(*target),794                                    target.span,795                                    ns,796                                    import_decl,797                                );798                                decls[ns].set(PendingDecl::Ready(Some(import_decl)), this);799                            }800                            PendingDecl::Ready(None) => {801                                // Don't remove underscores from `single_imports`, they were never added.802                                if target.name != kw::Underscore {803                                    let key = BindingKey::new(IdentKey::new(*target), ns);804                                    this.update_local_resolution(805                                        import.parent_scope.module.expect_local(),806                                        key,807                                        target.span,808                                        |_, resolution| {809                                            resolution.single_imports.swap_remove(&import);810                                        },811                                    );812                                }813                                decls[ns].set(PendingDecl::Ready(None), this);814                            }815                            PendingDecl::Pending => {}816                        }817                    });818                }819                (ImportKind::Glob { id, .. }, ImportResolutionKind::Glob(imported_decls)) => {820                    let ModuleOrUniformRoot::Module(module) = imported_module else {821                        self.dcx().emit_err(CannotGlobImportAllCrates { span: import.span });822                        continue;823                    };824825                    if module.is_trait() && !self.tcx.features().import_trait_associated_functions()826                    {827                        feature_err(828                            self.tcx.sess,829                            sym::import_trait_associated_functions,830                            import.span,831                            "`use` associated items of traits is unstable",832                        )833                        .emit();834                    }835836                    for (binding, key, orig_ident_span) in imported_decls {837                        let import_decl = self.new_import_decl(binding, import);838                        let _ = self839                            .try_plant_decl_into_local_module(840                                key.ident,841                                orig_ident_span,842                                key.ns,843                                import_decl,844                            )845                            .expect("planting a glob cannot fail");846                    }847848                    self.record_partial_res(*id, PartialRes::new(module.res().unwrap()));849                }850851                // Something weird happened, which shouldn't have happened.852                _ => unreachable!("mismatched import and resolution kind"),853            }854        }855    }856857    pub(crate) fn finalize_imports(&mut self) {858        let mut module_children = Default::default();859        let mut ambig_module_children = Default::default();860        for module in &self.local_modules {861            self.finalize_resolutions_in(*module, &mut module_children, &mut ambig_module_children);862        }863        self.module_children = module_children;864        self.ambig_module_children = ambig_module_children;865866        let mut seen_spans = FxHashSet::default();867        let mut errors = vec![];868        let mut prev_root_id: NodeId = NodeId::ZERO;869        let determined_imports = mem::take(&mut self.determined_imports);870        let indeterminate_imports = mem::take(&mut self.indeterminate_imports);871872        let mut glob_error = false;873        for (is_indeterminate, import) in determined_imports874            .iter()875            .map(|i| (false, i))876            .chain(indeterminate_imports.iter().map(|i| (true, i)))877        {878            let unresolved_import_error = self.finalize_import(*import);879            // If this import is unresolved then create a dummy import880            // resolution for it so that later resolve stages won't complain.881            self.import_dummy_binding(*import, is_indeterminate);882883            let Some(err) = unresolved_import_error else { continue };884885            glob_error |= import.is_glob();886887            if let ImportKind::Single { source, ref decls, .. } = import.kind888                && source.name == kw::SelfLower889                // Silence `unresolved import` error if E0429 is already emitted890                && let PendingDecl::Ready(None) = decls.value_ns.get()891            {892                continue;893            }894895            if prev_root_id != NodeId::ZERO && prev_root_id != import.root_id && !errors.is_empty()896            {897                // In the case of a new import line, throw a diagnostic message898                // for the previous line.899                self.throw_unresolved_import_error(errors, glob_error);900                errors = vec![];901            }902            if seen_spans.insert(err.span) {903                errors.push((*import, err));904                prev_root_id = import.root_id;905            }906        }907908        if self.cstore().had_extern_crate_load_failure() {909            self.tcx.sess.dcx().abort_if_errors();910        }911912        if !errors.is_empty() {913            self.throw_unresolved_import_error(errors, glob_error);914            return;915        }916917        for import in &indeterminate_imports {918            let path = import_path_to_string(919                &import.module_path.iter().map(|seg| seg.ident).collect::<Vec<_>>(),920                &import.kind,921                import.span,922            );923            // FIXME: there should be a better way of doing this than924            // formatting this as a string then checking for `::`925            if path.contains("::") {926                let err = UnresolvedImportError {927                    span: import.span,928                    label: None,929                    note: None,930                    suggestion: None,931                    candidates: None,932                    segment: None,933                    module: None,934                    on_unknown_attr: import.on_unknown_attr.clone(),935                };936                errors.push((*import, err))937            }938        }939940        if !errors.is_empty() {941            self.throw_unresolved_import_error(errors, glob_error);942        }943    }944945    pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<Decl<'ra>>) {946        for module in &self.local_modules {947            for (key, resolution) in self.resolutions(module.to_module()).borrow().iter() {948                let resolution = resolution.borrow();949                let Some(binding) = resolution.best_decl() else { continue };950951                // Report "cannot reexport" errors for exotic cases involving macros 2.0952                // privacy bending or invariant-breaking code under deprecation lints.953                for decl in [resolution.non_glob_decl, resolution.glob_decl] {954                    if let Some(decl) = decl955                        && let DeclKind::Import { source_decl, import } = decl.kind956                        // FIXME: Do not check visibility-ambiguous imports for now. To check them957                        // properly we need to preserve all imports in ambiguous glob sets and958                        // check them all individually.959                        && decl.ambiguity_vis_max.get().is_none()960                    {961                        // The source entity is too private to be reexported962                        // with the given import declaration's visibility.963                        let ord = source_decl.vis().partial_cmp(decl.vis(), self.tcx);964                        if matches!(ord, None | Some(Ordering::Less)) {965                            let ident = match import.kind {966                                ImportKind::Single { source, .. } => source,967                                _ => key.ident.orig(resolution.orig_ident_span),968                            };969                            if let Some(lint) =970                                self.report_cannot_reexport(import, source_decl, ident, key.ns)971                            {972                                self.lint_buffer.add_early_lint(lint);973                            }974                        }975                    }976                }977978                if let DeclKind::Import { import, .. } = binding.kind979                    && let Some((amb_binding, _)) = binding.ambiguity.get()980                    && binding.res() != Res::Err981                    && exported_ambiguities.contains(&binding)982                {983                    self.lint_buffer.buffer_lint(984                        AMBIGUOUS_GLOB_REEXPORTS,985                        import.root_id,986                        import.root_span,987                        diagnostics::AmbiguousGlobReexports {988                            name: key.ident.name.to_string(),989                            namespace: key.ns.descr().to_string(),990                            first_reexport: import.root_span,991                            duplicate_reexport: amb_binding.span,992                        },993                    );994                }995996                if let Some(glob_decl) = resolution.glob_decl997                    && resolution.non_glob_decl.is_some()998                {999                    if binding.res() != Res::Err1000                        && glob_decl.res() != Res::Err1001                        && let DeclKind::Import { import: glob_import, .. } = glob_decl.kind1002                        && let Some(glob_import_def_id) = glob_import.def_id()1003                        && self.effective_visibilities.is_exported(glob_import_def_id)1004                        && glob_decl.vis().is_public()1005                        && !binding.vis().is_public()1006                    {1007                        let binding_id = match binding.kind {1008                            DeclKind::Def(res) => {1009                                Some(self.def_id_to_node_id(res.def_id().expect_local()))1010                            }1011                            DeclKind::Import { import, .. } => import.id(),1012                        };1013                        if let Some(binding_id) = binding_id {1014                            self.lint_buffer.buffer_lint(1015                                HIDDEN_GLOB_REEXPORTS,1016                                binding_id,1017                                binding.span,1018                                diagnostics::HiddenGlobReexports {1019                                    name: key.ident.name.to_string(),1020                                    namespace: key.ns.descr().to_owned(),1021                                    glob_reexport: glob_decl.span,1022                                    private_item: binding.span,1023                                },1024                            );1025                        }1026                    }1027                }10281029                if let DeclKind::Import { import, .. } = binding.kind1030                    && let Some(binding_id) = import.id()1031                    && let import_def_id = import.def_id().unwrap()1032                    && self.effective_visibilities.is_exported(import_def_id)1033                    && let Res::Def(reexported_kind, reexported_def_id) = binding.res()1034                    && !matches!(reexported_kind, DefKind::Ctor(..))1035                    && !reexported_def_id.is_local()1036                    && self.tcx.is_private_dep(reexported_def_id.krate)1037                {1038                    self.lint_buffer.buffer_lint(1039                        EXPORTED_PRIVATE_DEPENDENCIES,1040                        binding_id,1041                        binding.span,1042                        crate::diagnostics::ReexportPrivateDependency {1043                            name: key.ident.name,1044                            kind: binding.res().descr(),1045                            krate: self.tcx.crate_name(reexported_def_id.krate),1046                        },1047                    );1048                }1049            }1050        }1051    }10521053    /// Attempts to resolve the given import, returning:1054    /// - `0` means its resolution is determined.1055    /// - Other values mean that indeterminate exists under certain namespaces.1056    ///1057    /// Meanwhile, if resolution is successful, its result is returned.1058    fn resolve_import<'r>(1059        mut self: CmResolver<'r, 'ra, 'tcx>,1060        import: Import<'ra>,1061    ) -> (Option<ImportResolution<'ra>>, usize) {1062        debug!(1063            "(resolving import for module) resolving import `{}::{}` in `{}`",1064            Segment::names_to_string(&import.module_path),1065            import_kind_to_string(&import.kind),1066            module_to_string(import.parent_scope.module).unwrap_or_else(|| "???".to_string()),1067        );1068        let module = if let Some(module) = import.imported_module.get() {1069            module1070        } else {1071            let path_res = self.reborrow().maybe_resolve_path(1072                &import.module_path,1073                None,1074                &import.parent_scope,1075                Some(import),1076            );10771078            match path_res {1079                PathResult::Module(module) => module,1080                PathResult::Indeterminate => return (None, 3),1081                PathResult::NonModule(..) | PathResult::Failed { .. } => return (None, 0),1082            }1083        };10841085        let (source, bindings) = match import.kind {1086            ImportKind::Single { source, ref decls, .. } => (source, decls),1087            ImportKind::Glob { .. } => {1088                let import_resolution = ImportResolution {1089                    imported_module: module,1090                    kind: self.resolve_glob_import(import, module),1091                };1092                return (Some(import_resolution), 0);1093            }1094            _ => unreachable!(),1095        };10961097        let mut import_decls = PerNS::default();1098        let mut indeterminate_count = 0;1099        self.per_ns_cm(|mut this, ns| {1100            if bindings[ns].get() != PendingDecl::Pending {1101                return;1102            };1103            let binding_result = this.reborrow().maybe_resolve_ident_in_module(1104                module,1105                source,1106                ns,1107                &import.parent_scope,1108                Some(import),1109            );1110            let pending_decl = match binding_result {1111                Ok(binding) => {1112                    // We need the `target`, `source` can be extracted.1113                    let import_decl = this.new_import_decl(binding, import);1114                    PendingDecl::Ready(Some(import_decl))1115                }1116                Err(Determinacy::Determined) => PendingDecl::Ready(None),1117                Err(Determinacy::Undetermined) => {1118                    indeterminate_count += 1;1119                    PendingDecl::Pending1120                }1121            };1122            import_decls[ns] = pending_decl;1123        });1124        let import_resolution = ImportResolution {1125            imported_module: module,1126            kind: ImportResolutionKind::Single(import_decls),1127        };11281129        (Some(import_resolution), indeterminate_count)1130    }11311132    /// Performs final import resolution, consistency checks and error reporting.1133    ///1134    /// Optionally returns an unresolved import error. This error is buffered and used to1135    /// consolidate multiple unresolved import errors into a single diagnostic.1136    fn finalize_import(&mut self, import: Import<'ra>) -> Option<UnresolvedImportError> {1137        let ignore_decl = match &import.kind {1138            ImportKind::Single { decls, .. } => decls[TypeNS].get().decl(),1139            _ => None,1140        };1141        let ambiguity_errors_len = |errors: &Vec<AmbiguityError<'_>>| {1142            errors.iter().filter(|error| error.warning.is_none()).count()1143        };1144        let prev_ambiguity_errors_len = ambiguity_errors_len(&self.ambiguity_errors);1145        let finalize = Finalize::with_root_span(import.root_id, import.span, import.root_span);11461147        // We'll provide more context to the privacy errors later, up to `len`.1148        let privacy_errors_len = self.privacy_errors.len();11491150        let path_res = self.cm().resolve_path(1151            &import.module_path,1152            None,1153            &import.parent_scope,1154            Some(finalize),1155            ignore_decl,1156            Some(import),1157        );11581159        let no_ambiguity =1160            ambiguity_errors_len(&self.ambiguity_errors) == prev_ambiguity_errors_len;11611162        let module = match path_res {1163            PathResult::Module(module) => {1164                // Consistency checks, analogous to `finalize_macro_resolutions`.1165                if let Some(initial_module) = import.imported_module.get() {1166                    if module != initial_module && no_ambiguity && !self.issue_145575_hack_applied {1167                        span_bug!(import.span, "inconsistent resolution for an import");1168                    }1169                } else if self.privacy_errors.is_empty() {1170                    self.dcx()1171                        .create_err(CannotDetermineImportResolution { span: import.span })1172                        .emit();1173                }11741175                module1176            }1177            PathResult::Failed {1178                is_error_from_last_segment: false,1179                span,1180                segment,1181                label,1182                suggestion,1183                module,1184                error_implied_by_parse_error: _,1185                message,1186                note: _,1187            } => {1188                if no_ambiguity {1189                    if !self.issue_145575_hack_applied {1190                        assert!(import.imported_module.get().is_none());1191                    }1192                    self.report_error(1193                        span,1194                        ResolutionError::FailedToResolve {1195                            segment: segment.name,1196                            label,1197                            suggestion,1198                            module,1199                            message,1200                        },1201                    );1202                }1203                return None;1204            }1205            PathResult::Failed {1206                is_error_from_last_segment: true,1207                span,1208                label,1209                suggestion,1210                module,1211                segment,1212                note,1213                ..1214            } => {1215                if no_ambiguity {1216                    if !self.issue_145575_hack_applied {1217                        assert!(import.imported_module.get().is_none());1218                    }1219                    let module = if let Some(ModuleOrUniformRoot::Module(m)) = module {1220                        m.opt_def_id()1221                    } else {1222                        None1223                    };1224                    let err = match self1225                        .make_path_suggestion(import.module_path.clone(), &import.parent_scope)1226                    {1227                        Some((suggestion, note)) => UnresolvedImportError {1228                            span,1229                            label: None,1230                            note,1231                            suggestion: Some((1232                                vec![(span, Segment::names_to_string(&suggestion))],1233                                String::from("a similar path exists"),1234                                Applicability::MaybeIncorrect,1235                            )),1236                            candidates: None,1237                            segment: Some(segment),1238                            module,1239                            on_unknown_attr: import.on_unknown_attr.clone(),1240                        },1241                        None => UnresolvedImportError {1242                            span,1243                            label: Some(label),1244                            note,1245                            suggestion,1246                            candidates: None,1247                            segment: Some(segment),1248                            module,1249                            on_unknown_attr: import.on_unknown_attr.clone(),1250                        },1251                    };1252                    return Some(err);1253                }1254                return None;1255            }1256            PathResult::NonModule(partial_res) => {1257                if no_ambiguity && partial_res.full_res() != Some(Res::Err) {1258                    // Check if there are no ambiguities and the result is not dummy.1259                    assert!(import.imported_module.get().is_none());1260                }1261                // The error was already reported earlier.1262                return None;1263            }1264            PathResult::Indeterminate => unreachable!(),1265        };12661267        let (ident, target, bindings, import_id) = match import.kind {1268            ImportKind::Single { source, target, ref decls, id, .. } => (source, target, decls, id),1269            ImportKind::Glob { ref max_vis, id, def_id } => {1270                if import.module_path.len() <= 1 {1271                    // HACK(eddyb) `lint_if_path_starts_with_module` needs at least1272                    // 2 segments, so the `resolve_path` above won't trigger it.1273                    let mut full_path = import.module_path.clone();1274                    full_path.push(Segment::from_ident(Ident::dummy()));1275                    self.lint_if_path_starts_with_module(finalize, &full_path, None);1276                }12771278                if let ModuleOrUniformRoot::Module(module) = module1279                    && module == import.parent_scope.module1280                {1281                    // Importing a module into itself is not allowed.1282                    return Some(UnresolvedImportError {1283                        span: import.span,1284                        label: Some(String::from("cannot glob-import a module into itself")),1285                        note: None,1286                        suggestion: None,1287                        candidates: None,1288                        segment: None,1289                        module: None,1290                        on_unknown_attr: None,1291                    });1292                }1293                if let Some(max_vis) = max_vis.get()1294                    && import.vis.greater_than(max_vis, self.tcx)1295                {1296                    self.lint_buffer.buffer_lint(1297                        UNUSED_IMPORTS,1298                        id,1299                        import.span,1300                        crate::diagnostics::RedundantImportVisibility {1301                            span: import.span,1302                            help: (),1303                            max_vis: max_vis.to_string(def_id, self.tcx),1304                            import_vis: import.vis.to_string(def_id, self.tcx),1305                        },1306                    );1307                }1308                return None;1309            }1310            _ => unreachable!(),1311        };13121313        if self.privacy_errors.len() != privacy_errors_len {1314            // Get the Res for the last element, so that we can point to alternative ways of1315            // importing it if available.1316            let mut path = import.module_path.clone();1317            path.push(Segment::from_ident(ident));1318            if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = self.cm().resolve_path(1319                &path,1320                None,1321                &import.parent_scope,1322                Some(finalize),1323                ignore_decl,1324                None,1325            ) {1326                let res = module.res().map(|r| (r, ident));1327                for error in &mut self.privacy_errors[privacy_errors_len..] {1328                    error.outermost_res = res;1329                }1330            } else {1331                // The final item is not a module (e.g., a struct, function, or macro).1332                // Resolve it directly in the parent module to get its Res, so1333                // `report_privacy_error()` can search for public re-export paths.1334                for ns in [TypeNS, ValueNS, MacroNS] {1335                    if let Ok(binding) = self.cm().resolve_ident_in_module(1336                        module,1337                        ident,1338                        ns,1339                        &import.parent_scope,1340                        None,1341                        ignore_decl,1342                        None,1343                    ) {1344                        let res = binding.res();1345                        for error in &mut self.privacy_errors[privacy_errors_len..] {1346                            error.outermost_res = Some((res, ident));1347                        }1348                        break;1349                    }1350                }1351            }1352        }13531354        let mut all_ns_err = true;1355        self.per_ns(|this, ns| {1356            let binding = this.cm().resolve_ident_in_module(1357                module,1358                ident,1359                ns,1360                &import.parent_scope,1361                Some(Finalize {1362                    report_private: false,1363                    import: Some(import.summary()),1364                    ..finalize1365                }),1366                bindings[ns].get().decl(),1367                Some(import),1368            );13691370            match binding {1371                Ok(binding) => {1372                    // Consistency checks, analogous to `finalize_macro_resolutions`.1373                    let initial_res = bindings[ns].get().decl().map(|binding| {1374                        let initial_binding = binding.import_source();1375                        all_ns_err = false;1376                        if target.name == kw::Underscore1377                            && initial_binding.is_extern_crate()1378                            && !initial_binding.is_import()1379                        {1380                            let used = if import.module_path.is_empty() {1381                                Used::Scope1382                            } else {1383                                Used::Other1384                            };1385                            this.record_use(ident, binding, used);1386                        }1387                        initial_binding.res()1388                    });1389                    let res = binding.res();1390                    let has_ambiguity_error =1391                        this.ambiguity_errors.iter().any(|error| error.warning.is_none());1392                    if res == Res::Err || has_ambiguity_error {1393                        this.dcx()1394                            .span_delayed_bug(import.span, "some error happened for an import");1395                        return;1396                    }1397                    if let Some(initial_res) = initial_res {1398                        if res != initial_res && !this.issue_145575_hack_applied {1399                            span_bug!(import.span, "inconsistent resolution for an import");1400                        }1401                    } else if this.privacy_errors.is_empty() {1402                        this.dcx()1403                            .create_err(CannotDetermineImportResolution { span: import.span })1404                            .emit();1405                    }1406                }1407                Err(..) => {1408                    // FIXME: This assert may fire if public glob is later shadowed by a private1409                    // single import (see test `issue-55884-2.rs`). In theory single imports should1410                    // always block globs, even if they are not yet resolved, so that this kind of1411                    // self-inconsistent resolution never happens.1412                    // Re-enable the assert when the issue is fixed.1413                    // assert!(result[ns].get().is_err());1414                }1415            }1416        });14171418        if all_ns_err {1419            let mut all_ns_failed = true;1420            self.per_ns(|this, ns| {1421                let binding = this.cm().resolve_ident_in_module(1422                    module,1423                    ident,1424                    ns,1425                    &import.parent_scope,1426                    Some(finalize),1427                    None,1428                    None,1429                );1430                if binding.is_ok() {1431                    all_ns_failed = false;1432                }1433            });14341435            return if all_ns_failed {1436                let names = match module {1437                    ModuleOrUniformRoot::Module(module) => {1438                        self.resolutions(module)1439                            .borrow()1440                            .iter()1441                            .filter_map(|(BindingKey { ident: i, .. }, resolution)| {1442                                if i.name == ident.name {1443                                    return None;1444                                } // Never suggest the same name1445                                if i.name == kw::Underscore {1446                                    return None;1447                                } // `use _` is never valid14481449                                let resolution = resolution.borrow();1450                                if let Some(name_binding) = resolution.best_decl() {1451                                    match name_binding.kind {1452                                        DeclKind::Import { source_decl, .. } => {1453                                            match source_decl.kind {1454                                                // Never suggest names that previously could not1455                                                // be resolved.1456                                                DeclKind::Def(Res::Err) => None,1457                                                _ => Some(i.name),1458                                            }1459                                        }1460                                        _ => Some(i.name),1461                                    }1462                                } else if resolution.single_imports.is_empty() {1463                                    None1464                                } else {1465                                    Some(i.name)1466                                }1467                            })1468                            .collect()1469                    }1470                    _ => Vec::new(),1471                };14721473                let lev_suggestion =1474                    find_best_match_for_name(&names, ident.name, None).map(|suggestion| {1475                        (1476                            vec![(ident.span, suggestion.to_string())],1477                            String::from("a similar name exists in the module"),1478                            Applicability::MaybeIncorrect,1479                        )1480                    });14811482                let (suggestion, note) =1483                    match self.check_for_module_export_macro(import, module, ident) {1484                        Some((suggestion, note)) => (suggestion.or(lev_suggestion), note),1485                        _ => (lev_suggestion, None),1486                    };14871488                // If importing of trait asscoiated items is enabled, an also find an1489                // `Enum`, then note that inherent associated items cannot be imported.1490                let note = if self.tcx.features().import_trait_associated_functions()1491                    && let PathResult::Module(ModuleOrUniformRoot::Module(m)) = path_res1492                    && let Some(Res::Def(DefKind::Enum, _)) = m.res()1493                {1494                    note.or(Some(1495                        "cannot import inherent associated items, only trait associated items"1496                            .to_string(),1497                    ))1498                } else {1499                    note1500                };15011502                let label = match module {1503                    ModuleOrUniformRoot::Module(module) => {1504                        let module_str = module_to_string(module);1505                        if let Some(module_str) = module_str {1506                            format!("no `{ident}` in `{module_str}`")1507                        } else {1508                            format!("no `{ident}` in the root")1509                        }1510                    }1511                    _ => {1512                        if !ident.is_path_segment_keyword() {1513                            format!("no external crate `{ident}`")1514                        } else {1515                            // HACK(eddyb) this shows up for `self` & `super`, which1516                            // should work instead - for now keep the same error message.1517                            format!("no `{ident}` in the root")1518                        }1519                    }1520                };15211522                let parent_suggestion =1523                    self.lookup_import_candidates(ident, TypeNS, &import.parent_scope, |_| true);15241525                Some(UnresolvedImportError {1526                    span: import.span,1527                    label: Some(label),1528                    note,1529                    suggestion,1530                    candidates: if !parent_suggestion.is_empty() {1531                        Some(parent_suggestion)1532                    } else {1533                        None1534                    },1535                    module: import.imported_module.get().and_then(|module| {1536                        if let ModuleOrUniformRoot::Module(m) = module {1537                            m.opt_def_id()1538                        } else {1539                            None1540                        }1541                    }),1542                    segment: Some(ident),1543                    on_unknown_attr: import.on_unknown_attr.clone(),1544                })1545            } else {1546                // `resolve_ident_in_module` reported a privacy error.1547                None1548            };1549        }15501551        let mut reexport_error = None;1552        let mut any_successful_reexport = false;1553        self.per_ns(|this, ns| {1554            let Some(binding) = bindings[ns].get().decl() else {1555                return;1556            };15571558            if import.vis.greater_than(binding.vis(), this.tcx) {1559                // In isolation, a declaration like this is not an error, but if *all* 1-31560                // declarations introduced by the import are more private than the import item's1561                // nominal visibility, then it's an error.1562                reexport_error = Some((ns, binding.import_source()));1563            } else {1564                any_successful_reexport = true;1565            }1566        });15671568        if !any_successful_reexport {1569            let (ns, binding) = reexport_error.unwrap();1570            if let Some(lint) = self.report_cannot_reexport(import, binding, ident, ns) {1571                self.lint_buffer.add_early_lint(lint);1572            }1573        }15741575        if import.module_path.len() <= 1 {1576            // HACK(eddyb) `lint_if_path_starts_with_module` needs at least1577            // 2 segments, so the `resolve_path` above won't trigger it.1578            let mut full_path = import.module_path.clone();1579            full_path.push(Segment::from_ident(ident));1580            self.per_ns(|this, ns| {1581                if let Some(binding) = bindings[ns].get().decl().map(|b| b.import_source()) {1582                    this.lint_if_path_starts_with_module(finalize, &full_path, Some(binding));1583                }1584            });1585        }15861587        // Record what this import resolves to for later uses in documentation,1588        // this may resolve to either a value or a type, but for documentation1589        // purposes it's good enough to just favor one over the other.1590        self.per_ns(|this, ns| {1591            if let Some(binding) = bindings[ns].get().decl().map(|b| b.import_source()) {1592                this.owners.get_mut(&import_id).unwrap().import_res[ns] = Some(binding.res());1593            }1594        });15951596        debug!("(resolving single import) successfully resolved import");1597        None1598    }15991600    fn report_cannot_reexport(1601        &self,1602        import: Import<'ra>,1603        decl: Decl<'ra>,1604        ident: Ident,1605        ns: Namespace,1606    ) -> Option<BufferedEarlyLint> {1607        let crate_private_reexport = match decl.vis() {1608            Visibility::Restricted(def_id) if def_id.is_top_level_module() => true,1609            _ => false,1610        };16111612        if let Some(extern_crate_id) = pub_use_of_private_extern_crate_hack(import.summary(), decl)1613        {1614            let ImportKind::Single { id, .. } = import.kind else { unreachable!() };1615            let sugg = self.tcx.source_span(extern_crate_id).shrink_to_lo();1616            let diagnostic = crate::diagnostics::PrivateExternCrateReexport { ident, sugg };1617            return Some(BufferedEarlyLint {1618                lint_id: LintId::of(PUB_USE_OF_PRIVATE_EXTERN_CRATE),1619                node_id: id,1620                span: Some(import.span.into()),1621                diagnostic: diagnostic.into(),1622            });1623        } else if ns == TypeNS {1624            let err = if crate_private_reexport {1625                self.dcx().create_err(CannotBeReexportedCratePublicNS { span: import.span, ident })1626            } else {1627                self.dcx().create_err(CannotBeReexportedPrivateNS { span: import.span, ident })1628            };1629            err.emit();1630        } else {1631            let mut err = if crate_private_reexport {1632                self.dcx().create_err(CannotBeReexportedCratePublic { span: import.span, ident })1633            } else {1634                self.dcx().create_err(CannotBeReexportedPrivate { span: import.span, ident })1635            };16361637            match decl.kind {1638                // exclude decl_macro1639                DeclKind::Def(Res::Def(DefKind::Macro(_), def_id))1640                    if let SyntaxExtensionKind::MacroRules(mr) =1641                        &self.get_macro_by_def_id(def_id).kind1642                        && mr.is_macro_rules() =>1643                {1644                    err.subdiagnostic(ConsiderAddingMacroExport { span: decl.span });1645                    err.subdiagnostic(ConsiderMarkingAsPubCrate { vis_span: import.vis_span });1646                }1647                _ => {1648                    err.subdiagnostic(ConsiderMarkingAsPub { span: import.span, ident });1649                }1650            }1651            err.emit();1652        }16531654        None1655    }16561657    pub(crate) fn check_for_redundant_imports(&mut self, import: Import<'ra>) -> bool {1658        // This function is only called for single imports.1659        let ImportKind::Single { source, target, ref decls, id, def_id, .. } = import.kind else {1660            unreachable!()1661        };16621663        // Skip if the import is of the form `use source as target` and source != target.1664        if source != target {1665            return false;1666        }16671668        // Skip if the import was produced by a macro.1669        if import.parent_scope.expansion != LocalExpnId::ROOT {1670            return false;1671        }16721673        // Skip if we are inside a named module (in contrast to an anonymous1674        // module defined by a block).1675        // Skip if the import is public or was used through non scope-based resolution,1676        // e.g. through a module-relative path.1677        if self.import_use_map.get(&import) == Some(&Used::Other)1678            || self.effective_visibilities.is_exported(def_id)1679        {1680            return false;1681        }16821683        let mut is_redundant = true;1684        let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None };1685        self.per_ns(|this, ns| {1686            let binding = decls[ns].get().decl().map(|b| b.import_source());1687            if is_redundant && let Some(binding) = binding {1688                if binding.res() == Res::Err {1689                    return;1690                }16911692                match this.cm().resolve_ident_in_scope_set(1693                    target,1694                    ScopeSet::All(ns),1695                    &import.parent_scope,1696                    None,1697                    decls[ns].get().decl(),1698                    None,1699                ) {1700                    Ok(other_binding) => {1701                        is_redundant = binding.res() == other_binding.res()1702                            && !other_binding.is_ambiguity_recursive();1703                        if is_redundant {1704                            redundant_span[ns] =1705                                Some((other_binding.span, other_binding.is_import()));1706                        }1707                    }1708                    Err(_) => is_redundant = false,1709                }1710            }1711        });17121713        if is_redundant && !redundant_span.is_empty() {1714            let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();1715            redundant_spans.sort();1716            redundant_spans.dedup();1717            self.lint_buffer.dyn_buffer_lint(1718                REDUNDANT_IMPORTS,1719                id,1720                import.span,1721                move |dcx, level| {1722                    let ident = source;1723                    let subs = redundant_spans1724                        .into_iter()1725                        .map(|(span, is_imported)| match (span.is_dummy(), is_imported) {1726                            (false, true) => {1727                                diagnostics::RedundantImportSub::ImportedHere { span, ident }1728                            }1729                            (false, false) => {1730                                diagnostics::RedundantImportSub::DefinedHere { span, ident }1731                            }1732                            (true, true) => {1733                                diagnostics::RedundantImportSub::ImportedPrelude { span, ident }1734                            }1735                            (true, false) => {1736                                diagnostics::RedundantImportSub::DefinedPrelude { span, ident }1737                            }1738                        })1739                        .collect();1740                    diagnostics::RedundantImport { subs, ident }.into_diag(dcx, level)1741                },1742            );1743            return true;1744        }17451746        false1747    }17481749    fn resolve_glob_import(1750        &self,1751        import: Import<'ra>,1752        imported_module: ModuleOrUniformRoot<'ra>,1753    ) -> ImportResolutionKind<'ra> {1754        let import_bindings = match imported_module {1755            ModuleOrUniformRoot::Module(module) if module != import.parent_scope.module => self1756                .resolutions(module)1757                .borrow()1758                .iter()1759                .filter_map(|(key, resolution)| {1760                    let res = resolution.borrow();1761                    let decl = res.determined_decl()?;1762                    let mut key = *key;1763                    let scope = match key.ident.ctxt.update_unchecked(|ctxt| {1764                        ctxt.reverse_glob_adjust(module.expansion, import.span)1765                    }) {1766                        Some(Some(def)) => self.expn_def_scope(def),1767                        Some(None) => import.parent_scope.module,1768                        None => return None,1769                    };1770                    self.is_accessible_from(decl.vis(), scope).then_some((1771                        decl,1772                        key,1773                        res.orig_ident_span,1774                    ))1775                })1776                .collect::<Vec<_>>(),17771778            // Errors are reported in `write_imports_resolutions`1779            _ => vec![],1780        };17811782        ImportResolutionKind::Glob(import_bindings)1783    }17841785    // Hack for the `rust_embed` regression observed in the crater run of #145108.1786    fn rust_embed_hack(&self, module: LocalModule<'ra>, decl: Decl<'ra>) -> bool {1787        // We are looking for this pattern:1788        // ```rust1789        // #[macro_use]1790        // extern crate rust_embed_impl;1791        // pub use rust_embed_impl::*;1792        //1793        // pub use RustEmbed as Embed;1794        // ```1795        if let DeclKind::Import { source_decl, import } = decl.kind1796            // Check that `decl` is the re-export: "pub use RustEmbed as Embed;"1797            && let ImportKind::Single { source, .. } = import.kind1798            && source.name == sym::RustEmbed1799            // make sure that the import points to the #[macro_use] import1800            && let DeclKind::Import { import, .. } = source_decl.kind1801            && matches!(import.kind, ImportKind::MacroUse { .. })1802            && self.macro_use_prelude.contains_key(&source.name) // and that the name actually exists in the macro_use_prelude1803            // Then check that `RustEmbed` exists in the modules Macro namespace.1804            && let Some(y_decl) = self1805                .resolution(module.to_module(), BindingKey::new(IdentKey::new(source), MacroNS))1806                .and_then(|res| res.best_decl())1807            // which comes from "pub use rust_embed_impl::*"1808            && y_decl.is_glob_import()1809            && y_decl.vis().is_public()1810        {1811            return true;1812        }18131814        false1815    }18161817    // Miscellaneous post-processing, including recording re-exports,1818    // reporting conflicts, and reporting unresolved imports.1819    fn finalize_resolutions_in(1820        &self,1821        module: LocalModule<'ra>,1822        module_children: &mut LocalDefIdMap<Vec<ModChild>>,1823        ambig_module_children: &mut LocalDefIdMap<Vec<AmbigModChild>>,1824    ) {1825        // Since import resolution is finished, globs will not define any more names.1826        *module.globs.borrow_mut(self) = Vec::new();18271828        let Some(def_id) = module.opt_def_id() else { return };18291830        let mut children = Vec::new();1831        let mut ambig_children = Vec::new();18321833        module.to_module().for_each_child(self, |this, ident, orig_ident_span, _, decl| {1834            let res = decl.res().expect_non_local();1835            if res != def::Res::Err {1836                let vis = if this.rust_embed_hack(module, decl) {1837                    Visibility::Public1838                } else {1839                    decl.vis()1840                };1841                let ident = ident.orig(orig_ident_span);1842                let child = |reexport_chain| ModChild { ident, res, vis, reexport_chain };1843                if let Some((ambig_binding1, ambig_binding2)) = decl.descent_to_ambiguity() {1844                    let main = child(ambig_binding1.reexport_chain());1845                    let second = ModChild {1846                        ident,1847                        res: ambig_binding2.res().expect_non_local(),1848                        vis: ambig_binding2.vis(),1849                        reexport_chain: ambig_binding2.reexport_chain(),1850                    };1851                    ambig_children.push(AmbigModChild { main, second })1852                } else {1853                    children.push(child(decl.reexport_chain()));1854                }1855            }1856        });18571858        if !children.is_empty() {1859            module_children.insert(def_id.expect_local(), children);1860        }1861        if !ambig_children.is_empty() {1862            ambig_module_children.insert(def_id.expect_local(), ambig_children);1863        }1864    }1865}18661867pub(crate) fn import_path_to_string(1868    names: &[Ident],1869    import_kind: &ImportKind<'_>,1870    span: Span,1871) -> String {1872    let pos = names.iter().position(|p| span == p.span && p.name != kw::PathRoot);1873    let global = !names.is_empty() && names[0].name == kw::PathRoot;1874    if let Some(pos) = pos {1875        let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] };1876        names_to_string(names.iter().map(|ident| ident.name))1877    } else {1878        let names = if global { &names[1..] } else { names };1879        if names.is_empty() {1880            import_kind_to_string(import_kind)1881        } else {1882            format!(1883                "{}::{}",1884                names_to_string(names.iter().map(|ident| ident.name)),1885                import_kind_to_string(import_kind),1886            )1887        }1888    }1889}18901891fn import_kind_to_string(import_kind: &ImportKind<'_>) -> String {1892    match import_kind {1893        ImportKind::Single { source, .. } => source.to_string(),1894        ImportKind::Glob { .. } => "*".to_string(),1895        ImportKind::ExternCrate { .. } => "<extern crate>".to_string(),1896        ImportKind::MacroUse { .. } => "#[macro_use]".to_string(),1897        ImportKind::MacroExport => "#[macro_export]".to_string(),1898    }1899}

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.