Skip to content

[WIP] metadata: Split ModChild into two structures #94854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..) => {}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -349,7 +349,7 @@ enum EntryKind {
Union(Lazy<VariantData>, ReprOptions),
Fn(Lazy<FnData>),
ForeignFn(Lazy<FnData>),
Mod(Lazy<[ModChild]>),
Mod(Lazy<[Reexport]>),
MacroDef(Lazy<ast::MacArgs>, /*macro_rules*/ bool),
ProcMacro(MacroKind),
Closure,
Expand Down
23 changes: 22 additions & 1 deletion compiler/rustc_middle/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) }
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -132,7 +132,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 reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
pub reexport_map: FxHashMap<LocalDefId, Vec<Reexport>>,
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.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/query.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 2 additions & 8 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 });
}
}
});
Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -939,7 +939,7 @@ pub struct Resolver<'a> {

/// `CrateNum` resolutions of `extern crate` items.
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
reexport_map: FxHashMap<LocalDefId, Vec<Reexport>>,
trait_map: NodeMap<Vec<TraitCandidate>>,

/// A map from nodes to anonymous modules.
Expand Down Expand Up @@ -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<ModChild> {
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)
}
Expand Down