Skip to content

Commit 530e76d

Browse files
committed
metadata: Split ModChild into two structures
to avoid encoding redundant data.
1 parent f58d51b commit 530e76d

File tree

8 files changed

+42
-21
lines changed

8 files changed

+42
-21
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11591159
}
11601160

11611161
match self.kind(id) {
1162-
EntryKind::Mod(exports) => {
1163-
for exp in exports.decode((self, sess)) {
1164-
callback(exp);
1162+
EntryKind::Mod(reexports) => {
1163+
for reexport in reexports.decode((self, sess)) {
1164+
callback(reexport.mod_child());
11651165
}
11661166
}
11671167
EntryKind::Enum(..) | EntryKind::Trait(..) => {}

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::def_id::{DefId, DefIndex, DefPathHash, StableCrateId};
1212
use rustc_hir::definitions::DefKey;
1313
use rustc_hir::lang_items;
1414
use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
15-
use rustc_middle::metadata::ModChild;
15+
use rustc_middle::metadata::Reexport;
1616
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
1717
use rustc_middle::mir;
1818
use rustc_middle::thir;
@@ -349,7 +349,7 @@ enum EntryKind {
349349
Union(Lazy<VariantData>, ReprOptions),
350350
Fn(Lazy<FnData>),
351351
ForeignFn(Lazy<FnData>),
352-
Mod(Lazy<[ModChild]>),
352+
Mod(Lazy<[Reexport]>),
353353
MacroDef(Lazy<ast::MacArgs>, /*macro_rules*/ bool),
354354
ProcMacro(MacroKind),
355355
Closure,

compiler/rustc_middle/src/metadata.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::Span;
1010
/// need to add more data in the future to correctly support macros 2.0, for example.
1111
/// Module child can be either a proper item or a reexport (including private imports).
1212
/// In case of reexport all the fields describe the reexport item itself, not what it refers to.
13-
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
13+
#[derive(Clone, Copy, Debug, HashStable)]
1414
pub struct ModChild {
1515
/// Name of the item.
1616
pub ident: Ident,
@@ -24,3 +24,24 @@ pub struct ModChild {
2424
/// A proper `macro_rules` item (not a reexport).
2525
pub macro_rules: bool,
2626
}
27+
28+
/// Same as `ModChild`, but without some data that is redundant for reexports.
29+
#[derive(Clone, Copy, Debug, HashStable, TyEncodable, TyDecodable)]
30+
pub struct Reexport {
31+
/// Name of the item.
32+
pub ident: Ident,
33+
/// Resolution result corresponding to the item.
34+
/// Local variables cannot be exported, so this `Res` doesn't need the ID parameter.
35+
pub res: Res<!>,
36+
/// Visibility of the item.
37+
pub vis: ty::Visibility,
38+
/// Span of the item.
39+
pub span: Span,
40+
}
41+
42+
impl Reexport {
43+
pub fn mod_child(self) -> ModChild {
44+
let Reexport { ident, res, vis, span } = self;
45+
ModChild { ident, res, vis, span, macro_rules: false }
46+
}
47+
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ rustc_queries! {
13141314
desc { "traits in scope at a block" }
13151315
}
13161316

1317-
query module_reexports(def_id: LocalDefId) -> Option<&'tcx [ModChild]> {
1317+
query module_reexports(def_id: LocalDefId) -> Option<&'tcx [Reexport]> {
13181318
desc { |tcx| "looking up reexports of module `{}`", tcx.def_path_str(def_id.to_def_id()) }
13191319
}
13201320

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use generics::*;
2020
use rustc_data_structures::fingerprint::Fingerprint;
2121
pub use vtable::*;
2222

23-
use crate::metadata::ModChild;
23+
use crate::metadata::Reexport;
2424
use crate::middle::privacy::AccessLevels;
2525
use crate::mir::{Body, GeneratorLayout};
2626
use crate::traits::{self, Reveal};
@@ -132,7 +132,7 @@ pub struct ResolverOutputs {
132132
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
133133
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
134134
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
135-
pub reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
135+
pub reexport_map: FxHashMap<LocalDefId, Vec<Reexport>>,
136136
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
137137
/// Extern prelude entries. The value is `true` if the entry was introduced
138138
/// via `extern crate` item and not `--extern` option or compiler built-in.

compiler/rustc_middle/src/ty/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::dep_graph;
22
use crate::infer::canonical::{self, Canonical};
33
use crate::lint::LintLevelMap;
4-
use crate::metadata::ModChild;
4+
use crate::metadata::{ModChild, Reexport};
55
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
66
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
77
use crate::middle::lib_features::LibFeatures;

compiler/rustc_resolve/src/imports.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_data_structures::intern::Interned;
1515
use rustc_errors::{pluralize, struct_span_err, Applicability};
1616
use rustc_hir::def::{self, PartialRes};
1717
use rustc_hir::def_id::DefId;
18-
use rustc_middle::metadata::ModChild;
18+
use rustc_middle::metadata::Reexport;
1919
use rustc_middle::span_bug;
2020
use rustc_middle::ty;
2121
use rustc_session::lint::builtin::{PUB_USE_OF_PRIVATE_EXTERN_CRATE, UNUSED_IMPORTS};
@@ -1410,13 +1410,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14101410
// Ambiguous imports are treated as errors at this point and are
14111411
// not exposed to other crates (see #36837 for more details).
14121412
if res != def::Res::Err && !binding.is_ambiguity() {
1413-
reexports.push(ModChild {
1414-
ident,
1415-
res,
1416-
vis: binding.vis,
1417-
span: binding.span,
1418-
macro_rules: false,
1419-
});
1413+
reexports.push(Reexport { ident, res, vis: binding.vis, span: binding.span });
14201414
}
14211415
}
14221416
});

compiler/rustc_resolve/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
5252
use rustc_hir::TraitCandidate;
5353
use rustc_index::vec::IndexVec;
5454
use rustc_metadata::creader::{CStore, CrateLoader};
55-
use rustc_middle::metadata::ModChild;
55+
use rustc_middle::metadata::{ModChild, Reexport};
5656
use rustc_middle::middle::privacy::AccessLevels;
5757
use rustc_middle::span_bug;
5858
use rustc_middle::ty::query::Providers;
@@ -939,7 +939,7 @@ pub struct Resolver<'a> {
939939

940940
/// `CrateNum` resolutions of `extern crate` items.
941941
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
942-
reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
942+
reexport_map: FxHashMap<LocalDefId, Vec<Reexport>>,
943943
trait_map: NodeMap<Vec<TraitCandidate>>,
944944

945945
/// A map from nodes to anonymous modules.
@@ -3421,7 +3421,13 @@ impl<'a> Resolver<'a> {
34213421
/// For local modules returns only reexports, for external modules returns all children.
34223422
pub fn module_children_or_reexports(&self, def_id: DefId) -> Vec<ModChild> {
34233423
if let Some(def_id) = def_id.as_local() {
3424-
self.reexport_map.get(&def_id).cloned().unwrap_or_default()
3424+
self.reexport_map
3425+
.get(&def_id)
3426+
.iter()
3427+
.copied()
3428+
.flatten()
3429+
.map(|reexport| reexport.mod_child())
3430+
.collect()
34253431
} else {
34263432
self.cstore().module_children_untracked(def_id, self.session)
34273433
}

0 commit comments

Comments
 (0)