diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index e5e0cce198f46..bf4546a82046c 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1159,9 +1159,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } match self.kind(id) { - EntryKind::Mod(exports) => { - for exp in exports.decode((self, sess)) { - callback(exp); + EntryKind::Mod(reexports) => { + for reexport in reexports.decode((self, sess)) { + callback(reexport.mod_child()); } } EntryKind::Enum(..) | EntryKind::Trait(..) => {} diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index a30cc034c4a96..a62bc38804582 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -12,7 +12,7 @@ use rustc_hir::def_id::{DefId, DefIndex, DefPathHash, StableCrateId}; use rustc_hir::definitions::DefKey; use rustc_hir::lang_items; use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec}; -use rustc_middle::metadata::ModChild; +use rustc_middle::metadata::Reexport; use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; use rustc_middle::mir; use rustc_middle::thir; @@ -349,7 +349,7 @@ enum EntryKind { Union(Lazy, ReprOptions), Fn(Lazy), ForeignFn(Lazy), - Mod(Lazy<[ModChild]>), + Mod(Lazy<[Reexport]>), MacroDef(Lazy, /*macro_rules*/ bool), ProcMacro(MacroKind), Closure, diff --git a/compiler/rustc_middle/src/metadata.rs b/compiler/rustc_middle/src/metadata.rs index c8e78747d8e7b..8e0b215ed2f91 100644 --- a/compiler/rustc_middle/src/metadata.rs +++ b/compiler/rustc_middle/src/metadata.rs @@ -10,7 +10,7 @@ use rustc_span::Span; /// need to add more data in the future to correctly support macros 2.0, for example. /// Module child can be either a proper item or a reexport (including private imports). /// In case of reexport all the fields describe the reexport item itself, not what it refers to. -#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)] +#[derive(Clone, Copy, Debug, HashStable)] pub struct ModChild { /// Name of the item. pub ident: Ident, @@ -24,3 +24,24 @@ pub struct ModChild { /// A proper `macro_rules` item (not a reexport). pub macro_rules: bool, } + +/// Same as `ModChild`, but without some data that is redundant for reexports. +#[derive(Clone, Copy, Debug, HashStable, TyEncodable, TyDecodable)] +pub struct Reexport { + /// Name of the item. + pub ident: Ident, + /// Resolution result corresponding to the item. + /// Local variables cannot be exported, so this `Res` doesn't need the ID parameter. + pub res: Res, + /// Visibility of the item. + pub vis: ty::Visibility, + /// Span of the item. + pub span: Span, +} + +impl Reexport { + pub fn mod_child(self) -> ModChild { + let Reexport { ident, res, vis, span } = self; + ModChild { ident, res, vis, span, macro_rules: false } + } +} diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 6b572690e2113..0dc93c5d00ae1 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1314,7 +1314,7 @@ rustc_queries! { desc { "traits in scope at a block" } } - query module_reexports(def_id: LocalDefId) -> Option<&'tcx [ModChild]> { + query module_reexports(def_id: LocalDefId) -> Option<&'tcx [Reexport]> { desc { |tcx| "looking up reexports of module `{}`", tcx.def_path_str(def_id.to_def_id()) } } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 6edcfbcdc6cee..dca2f07903315 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -20,7 +20,7 @@ pub use generics::*; use rustc_data_structures::fingerprint::Fingerprint; pub use vtable::*; -use crate::metadata::ModChild; +use crate::metadata::Reexport; use crate::middle::privacy::AccessLevels; use crate::mir::{Body, GeneratorLayout}; use crate::traits::{self, Reveal}; @@ -132,7 +132,7 @@ pub struct ResolverOutputs { pub extern_crate_map: FxHashMap, pub maybe_unused_trait_imports: FxHashSet, pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>, - pub reexport_map: FxHashMap>, + pub reexport_map: FxHashMap>, pub glob_map: FxHashMap>, /// Extern prelude entries. The value is `true` if the entry was introduced /// via `extern crate` item and not `--extern` option or compiler built-in. diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 03e4a7dcefd71..2017f423af124 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -1,7 +1,7 @@ use crate::dep_graph; use crate::infer::canonical::{self, Canonical}; use crate::lint::LintLevelMap; -use crate::metadata::ModChild; +use crate::metadata::{ModChild, Reexport}; use crate::middle::codegen_fn_attrs::CodegenFnAttrs; use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; use crate::middle::lib_features::LibFeatures; diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 70ade7a5600ba..dd589f42935b3 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -15,7 +15,7 @@ use rustc_data_structures::intern::Interned; use rustc_errors::{pluralize, struct_span_err, Applicability}; use rustc_hir::def::{self, PartialRes}; use rustc_hir::def_id::DefId; -use rustc_middle::metadata::ModChild; +use rustc_middle::metadata::Reexport; use rustc_middle::span_bug; use rustc_middle::ty; use rustc_session::lint::builtin::{PUB_USE_OF_PRIVATE_EXTERN_CRATE, UNUSED_IMPORTS}; @@ -1410,13 +1410,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { // Ambiguous imports are treated as errors at this point and are // not exposed to other crates (see #36837 for more details). if res != def::Res::Err && !binding.is_ambiguity() { - reexports.push(ModChild { - ident, - res, - vis: binding.vis, - span: binding.span, - macro_rules: false, - }); + reexports.push(Reexport { ident, res, vis: binding.vis, span: binding.span }); } } }); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 3342bd146c4ee..0d8b001280fd4 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -52,7 +52,7 @@ use rustc_hir::definitions::{DefKey, DefPathData, Definitions}; use rustc_hir::TraitCandidate; use rustc_index::vec::IndexVec; use rustc_metadata::creader::{CStore, CrateLoader}; -use rustc_middle::metadata::ModChild; +use rustc_middle::metadata::{ModChild, Reexport}; use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::span_bug; use rustc_middle::ty::query::Providers; @@ -939,7 +939,7 @@ pub struct Resolver<'a> { /// `CrateNum` resolutions of `extern crate` items. extern_crate_map: FxHashMap, - reexport_map: FxHashMap>, + reexport_map: FxHashMap>, trait_map: NodeMap>, /// A map from nodes to anonymous modules. @@ -3421,7 +3421,13 @@ impl<'a> Resolver<'a> { /// For local modules returns only reexports, for external modules returns all children. pub fn module_children_or_reexports(&self, def_id: DefId) -> Vec { if let Some(def_id) = def_id.as_local() { - self.reexport_map.get(&def_id).cloned().unwrap_or_default() + self.reexport_map + .get(&def_id) + .iter() + .copied() + .flatten() + .map(|reexport| reexport.mod_child()) + .collect() } else { self.cstore().module_children_untracked(def_id, self.session) }