rustc_middle: Rename Export to ModChild and add some comments

Also rename `module_exports`/`export_map` to `module_reexports`/`reexport_map` for clarity.
This commit is contained in:
Vadim Petrochenkov 2021-12-21 11:24:43 +08:00
parent 3051f6e9c4
commit 4b03fd910c
17 changed files with 66 additions and 64 deletions

View File

@ -21,7 +21,7 @@ use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
use rustc_hir::diagnostic_items::DiagnosticItems;
use rustc_hir::lang_items;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::hir::exports::Export;
use rustc_middle::metadata::ModChild;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
use rustc_middle::mir::{self, Body, Promoted};
@ -1082,7 +1082,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
fn for_each_module_child(
&self,
id: DefIndex,
mut callback: impl FnMut(Export),
mut callback: impl FnMut(ModChild),
sess: &Session,
) {
if let Some(data) = &self.root.proc_macro_data {
@ -1096,7 +1096,12 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
self.local_def_id(def_index),
);
let ident = self.item_ident(def_index, sess);
callback(Export { ident, res, vis: ty::Visibility::Public, span: ident.span });
callback(ModChild {
ident,
res,
vis: ty::Visibility::Public,
span: ident.span,
});
}
}
return;
@ -1117,7 +1122,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let vis = self.get_visibility(child_index);
let span = self.get_span(child_index, sess);
callback(Export { ident, res, vis, span });
callback(ModChild { ident, res, vis, span });
// For non-re-export structs and variants add their constructors to children.
// Re-export lists automatically contain constructors when necessary.
@ -1129,7 +1134,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let ctor_res =
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
let vis = self.get_visibility(ctor_def_id.index);
callback(Export { res: ctor_res, vis, ident, span });
callback(ModChild { ident, res: ctor_res, vis, span });
}
}
DefKind::Variant => {
@ -1154,7 +1159,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
vis = ty::Visibility::Restricted(crate_def_id);
}
}
callback(Export { res: ctor_res, ident, vis, span });
callback(ModChild { ident, res: ctor_res, vis, span });
}
_ => {}
}

View File

@ -7,7 +7,7 @@ use rustc_data_structures::stable_map::FxHashMap;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
use rustc_middle::hir::exports::Export;
use rustc_middle::metadata::ModChild;
use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::ty::query::{ExternProviders, Providers};
@ -309,7 +309,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
bfs_queue.push_back(DefId { krate: cnum, index: CRATE_DEF_INDEX });
}
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &ModChild, parent: DefId| {
if !child.vis.is_public() {
return;
}
@ -388,7 +388,7 @@ impl CStore {
self.get_crate_data(def.krate).get_visibility(def.index)
}
pub fn module_children_untracked(&self, def_id: DefId, sess: &Session) -> Vec<Export> {
pub fn module_children_untracked(&self, def_id: DefId, sess: &Session) -> Vec<ModChild> {
let mut result = vec![];
self.get_crate_data(def_id.krate).for_each_module_child(
def_id.index,

View File

@ -1094,7 +1094,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// code uses it). However, we skip encoding anything relating to child
// items - we encode information about proc-macros later on.
let reexports = if !self.is_proc_macro {
match tcx.module_exports(local_def_id) {
match tcx.module_reexports(local_def_id) {
Some(exports) => self.lazy(exports),
_ => Lazy::empty(),
}

View File

@ -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::hir::exports::Export;
use rustc_middle::metadata::ModChild;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
use rustc_middle::mir;
use rustc_middle::thir;
@ -350,7 +350,7 @@ enum EntryKind {
Union(Lazy<VariantData>, ReprOptions),
Fn(Lazy<FnData>),
ForeignFn(Lazy<FnData>),
Mod(Lazy<[Export]>),
Mod(Lazy<[ModChild]>),
MacroDef(Lazy<MacroDef>),
ProcMacro(MacroKind),
Closure,

View File

@ -1,28 +0,0 @@
use crate::ty;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::Res;
use rustc_hir::def_id::LocalDefId;
use rustc_macros::HashStable;
use rustc_span::symbol::Ident;
use rustc_span::Span;
use std::fmt::Debug;
/// This is the replacement export map. It maps a module to all of the exports
/// within.
pub type ExportMap = FxHashMap<LocalDefId, Vec<Export>>;
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
pub struct Export {
/// The name of the target.
pub ident: Ident,
/// The resolution of the target.
/// Local variables cannot be exported, so this `Res` doesn't need the ID parameter.
pub res: Res<!>,
/// The span of the target.
pub span: Span,
/// The visibility of the export.
/// We include non-`pub` exports for hygienic macros that get used from extern crates.
pub vis: ty::Visibility,
}

View File

@ -2,7 +2,6 @@
//!
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
pub mod exports;
pub mod map;
pub mod place;

View File

@ -84,6 +84,7 @@ pub mod dep_graph;
pub mod hir;
pub mod infer;
pub mod lint;
pub mod metadata;
pub mod middle;
pub mod mir;
pub mod thir;

View File

@ -0,0 +1,24 @@
use crate::ty;
use rustc_hir::def::Res;
use rustc_macros::HashStable;
use rustc_span::symbol::Ident;
use rustc_span::Span;
/// This structure is supposed to keep enough data to re-create `NameBinding`s for other crates
/// during name resolution. Right now the bindings are not recreated entirely precisely so we may
/// 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)]
pub struct ModChild {
/// 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,
}

View File

@ -1300,8 +1300,8 @@ rustc_queries! {
desc { "traits in scope at a block" }
}
query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export]> {
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
query module_reexports(def_id: LocalDefId) -> Option<&'tcx [ModChild]> {
desc { |tcx| "looking up reexports of module `{}`", tcx.def_path_str(def_id.to_def_id()) }
}
query impl_defaultness(def_id: DefId) -> hir::Defaultness {
@ -1528,8 +1528,8 @@ rustc_queries! {
desc { "fetching what a crate is named" }
separate_provide_extern
}
query module_children(def_id: DefId) -> &'tcx [Export] {
desc { |tcx| "collecting child items of `{}`", tcx.def_path_str(def_id) }
query module_children(def_id: DefId) -> &'tcx [ModChild] {
desc { |tcx| "collecting child items of module `{}`", tcx.def_path_str(def_id) }
separate_provide_extern
}
query extern_mod_stmt_cnum(def_id: LocalDefId) -> Option<CrateNum> {

View File

@ -2820,7 +2820,8 @@ pub fn provide(providers: &mut ty::query::Providers) {
providers.in_scope_traits_map =
|tcx, id| tcx.hir_crate(()).owners[id].as_ref().map(|owner_info| &owner_info.trait_map);
providers.resolutions = |tcx, ()| &tcx.untracked_resolutions;
providers.module_exports = |tcx, id| tcx.resolutions(()).export_map.get(&id).map(|v| &v[..]);
providers.module_reexports =
|tcx, id| tcx.resolutions(()).reexport_map.get(&id).map(|v| &v[..]);
providers.crate_name = |tcx, id| {
assert_eq!(id, LOCAL_CRATE);
tcx.crate_name

View File

@ -19,7 +19,7 @@ pub use assoc::*;
pub use generics::*;
pub use vtable::*;
use crate::hir::exports::ExportMap;
use crate::metadata::ModChild;
use crate::mir::{Body, GeneratorLayout};
use crate::traits::{self, Reveal};
use crate::ty;
@ -126,7 +126,7 @@ pub struct ResolverOutputs {
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
pub export_map: ExportMap,
pub reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
/// Extern prelude entries. The value is `true` if the entry was introduced
/// via `extern crate` item and not `--extern` option or compiler built-in.

View File

@ -1,7 +1,7 @@
use crate::dep_graph;
use crate::hir::exports::Export;
use crate::infer::canonical::{self, Canonical};
use crate::lint::LintLevelMap;
use crate::metadata::ModChild;
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
use crate::middle::lib_features::LibFeatures;

View File

@ -520,7 +520,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
let vis = self.tcx.visibility(item_id.def_id);
self.update_macro_reachable_def(item_id.def_id, def_kind, vis, defining_mod);
}
if let Some(exports) = self.tcx.module_exports(module_def_id) {
if let Some(exports) = self.tcx.module_reexports(module_def_id) {
for export in exports {
if export.vis.is_accessible_from(defining_mod.to_def_id(), self.tcx) {
if let Res::Def(def_kind, def_id) = export.res {
@ -926,7 +926,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
// crate module gets processed as well.
if self.prev_level.is_some() {
let def_id = self.tcx.hir().local_def_id(id);
if let Some(exports) = self.tcx.module_exports(def_id) {
if let Some(exports) = self.tcx.module_reexports(def_id) {
for export in exports.iter() {
if export.vis.is_public() {
if let Some(def_id) = export.res.opt_def_id() {

View File

@ -26,7 +26,7 @@ use rustc_hir::def::{self, *};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_metadata::creader::LoadedMacro;
use rustc_middle::bug;
use rustc_middle::hir::exports::Export;
use rustc_middle::metadata::ModChild;
use rustc_middle::ty;
use rustc_session::cstore::CrateStore;
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
@ -938,9 +938,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
/// Builds the reduced graph for a single item in an external crate.
fn build_reduced_graph_for_external_crate_res(&mut self, child: Export) {
fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) {
let parent = self.parent_scope.module;
let Export { ident, res, vis, span } = child;
let ModChild { ident, res, vis, span } = child;
let res = res.expect_non_local();
let expansion = self.parent_scope.expansion;
// Record primary definitions.

View File

@ -15,7 +15,7 @@ use rustc_data_structures::ptr_key::PtrKey;
use rustc_errors::{pluralize, struct_span_err, Applicability};
use rustc_hir::def::{self, PartialRes};
use rustc_hir::def_id::DefId;
use rustc_middle::hir::exports::Export;
use rustc_middle::metadata::ModChild;
use rustc_middle::span_bug;
use rustc_middle::ty;
use rustc_session::lint::builtin::{PUB_USE_OF_PRIVATE_EXTERN_CRATE, UNUSED_IMPORTS};
@ -1409,7 +1409,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
if is_good_import || binding.is_macro_def() {
let res = binding.res().expect_non_local();
if res != def::Res::Err {
reexports.push(Export { ident, res, span: binding.span, vis: binding.vis });
reexports.push(ModChild { ident, res, vis: binding.vis, span: binding.span });
}
}
});
@ -1418,7 +1418,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
if let Some(def_id) = module.opt_def_id() {
// Call to `expect_local` should be fine because current
// code is only called for local modules.
self.r.export_map.insert(def_id.expect_local(), reexports);
self.r.reexport_map.insert(def_id.expect_local(), reexports);
}
}
}

View File

@ -49,7 +49,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::hir::exports::ExportMap;
use rustc_middle::metadata::ModChild;
use rustc_middle::span_bug;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, DefIdTree, MainDefinition, ResolverOutputs};
@ -927,7 +927,7 @@ pub struct Resolver<'a> {
/// `CrateNum` resolutions of `extern crate` items.
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
export_map: ExportMap,
reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
trait_map: NodeMap<Vec<TraitCandidate>>,
/// A map from nodes to anonymous modules.
@ -1333,7 +1333,7 @@ impl<'a> Resolver<'a> {
import_res_map: Default::default(),
label_res_map: Default::default(),
extern_crate_map: Default::default(),
export_map: FxHashMap::default(),
reexport_map: FxHashMap::default(),
trait_map: NodeMap::default(),
underscore_disambiguator: 0,
empty_module,
@ -1446,7 +1446,7 @@ impl<'a> Resolver<'a> {
let definitions = self.definitions;
let visibilities = self.visibilities;
let extern_crate_map = self.extern_crate_map;
let export_map = self.export_map;
let reexport_map = self.reexport_map;
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
let maybe_unused_extern_crates = self.maybe_unused_extern_crates;
let glob_map = self.glob_map;
@ -1457,7 +1457,7 @@ impl<'a> Resolver<'a> {
cstore: Box::new(self.crate_loader.into_cstore()),
visibilities,
extern_crate_map,
export_map,
reexport_map,
glob_map,
maybe_unused_trait_imports,
maybe_unused_extern_crates,
@ -1480,7 +1480,7 @@ impl<'a> Resolver<'a> {
cstore: Box::new(self.cstore().clone()),
visibilities: self.visibilities.clone(),
extern_crate_map: self.extern_crate_map.clone(),
export_map: self.export_map.clone(),
reexport_map: self.reexport_map.clone(),
glob_map: self.glob_map.clone(),
maybe_unused_trait_imports: self.maybe_unused_trait_imports.clone(),
maybe_unused_extern_crates: self.maybe_unused_extern_crates.clone(),

View File

@ -112,7 +112,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
// is declared but also a reexport of itself producing two exports of the same
// macro in the same module.
let mut inserted = FxHashSet::default();
for export in self.cx.tcx.module_exports(CRATE_DEF_ID).unwrap_or(&[]) {
for export in self.cx.tcx.module_reexports(CRATE_DEF_ID).unwrap_or(&[]) {
if let Res::Def(DefKind::Macro(_), def_id) = export.res {
if let Some(local_def_id) = def_id.as_local() {
if self.cx.tcx.has_attr(def_id, sym::macro_export) {