Skip to content

Commit f8a2e49

Browse files
committed
Auto merge of rust-lang#104730 - petrochenkov:modchild5, r=cjgillot
rustc_metadata: Switch module children decoding to an iterator Previously rust-lang#103578, rust-lang#103524 and previous PRs simplified it as much as possible. A couple of cleanup commits is also added. r? `@cjgillot`
2 parents 5fa44b5 + 24f2ee1 commit f8a2e49

File tree

5 files changed

+54
-82
lines changed

5 files changed

+54
-82
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+38-61
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@ use rustc_session::cstore::{
2929
CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib,
3030
};
3131
use rustc_session::Session;
32-
use rustc_span::hygiene::{ExpnIndex, MacroKind};
32+
use rustc_span::hygiene::ExpnIndex;
3333
use rustc_span::source_map::{respan, Spanned};
3434
use rustc_span::symbol::{kw, Ident, Symbol};
3535
use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};
3636

3737
use proc_macro::bridge::client::ProcMacro;
38-
use std::io;
3938
use std::iter::TrustedLen;
40-
use std::mem;
4139
use std::num::NonZeroUsize;
4240
use std::path::Path;
41+
use std::{io, iter, mem};
4342

4443
pub(super) use cstore_impl::provide;
4544
pub use cstore_impl::provide_extern;
@@ -984,64 +983,52 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
984983
DiagnosticItems { id_to_name, name_to_id }
985984
}
986985

986+
fn get_mod_child(self, id: DefIndex, sess: &Session) -> ModChild {
987+
let ident = self.item_ident(id, sess);
988+
let kind = self.def_kind(id);
989+
let def_id = self.local_def_id(id);
990+
let res = Res::Def(kind, def_id);
991+
let vis = self.get_visibility(id);
992+
let span = self.get_span(id, sess);
993+
let macro_rules = match kind {
994+
DefKind::Macro(..) => self.root.tables.macro_rules.get(self, id).is_some(),
995+
_ => false,
996+
};
997+
998+
ModChild { ident, res, vis, span, macro_rules }
999+
}
1000+
9871001
/// Iterates over all named children of the given module,
9881002
/// including both proper items and reexports.
9891003
/// Module here is understood in name resolution sense - it can be a `mod` item,
9901004
/// or a crate root, or an enum, or a trait.
991-
fn for_each_module_child(
1005+
fn get_module_children(
9921006
self,
9931007
id: DefIndex,
994-
mut callback: impl FnMut(ModChild),
995-
sess: &Session,
996-
) {
997-
if let Some(data) = &self.root.proc_macro_data {
998-
// If we are loading as a proc macro, we want to return
999-
// the view of this crate as a proc macro crate.
1000-
if id == CRATE_DEF_INDEX {
1001-
for def_index in data.macros.decode(self) {
1002-
let raw_macro = self.raw_proc_macro(def_index);
1003-
let res = Res::Def(
1004-
DefKind::Macro(macro_kind(raw_macro)),
1005-
self.local_def_id(def_index),
1006-
);
1007-
let ident = self.item_ident(def_index, sess);
1008-
callback(ModChild {
1009-
ident,
1010-
res,
1011-
vis: ty::Visibility::Public,
1012-
span: ident.span,
1013-
macro_rules: false,
1014-
});
1008+
sess: &'a Session,
1009+
) -> impl Iterator<Item = ModChild> + 'a {
1010+
iter::from_generator(move || {
1011+
if let Some(data) = &self.root.proc_macro_data {
1012+
// If we are loading as a proc macro, we want to return
1013+
// the view of this crate as a proc macro crate.
1014+
if id == CRATE_DEF_INDEX {
1015+
for child_index in data.macros.decode(self) {
1016+
yield self.get_mod_child(child_index, sess);
1017+
}
1018+
}
1019+
} else {
1020+
// Iterate over all children.
1021+
for child_index in self.root.tables.children.get(self, id).unwrap().decode(self) {
1022+
yield self.get_mod_child(child_index, sess);
10151023
}
1016-
}
1017-
return;
1018-
}
10191024

1020-
// Iterate over all children.
1021-
if let Some(children) = self.root.tables.children.get(self, id) {
1022-
for child_index in children.decode((self, sess)) {
1023-
let ident = self.item_ident(child_index, sess);
1024-
let kind = self.def_kind(child_index);
1025-
let def_id = self.local_def_id(child_index);
1026-
let res = Res::Def(kind, def_id);
1027-
let vis = self.get_visibility(child_index);
1028-
let span = self.get_span(child_index, sess);
1029-
let macro_rules = match kind {
1030-
DefKind::Macro(..) => {
1031-
self.root.tables.macro_rules.get(self, child_index).is_some()
1025+
if let Some(reexports) = self.root.tables.module_reexports.get(self, id) {
1026+
for reexport in reexports.decode((self, sess)) {
1027+
yield reexport;
10321028
}
1033-
_ => false,
1034-
};
1035-
1036-
callback(ModChild { ident, res, vis, span, macro_rules });
1037-
}
1038-
}
1039-
1040-
if let Some(exports) = self.root.tables.module_reexports.get(self, id) {
1041-
for exp in exports.decode((self, sess)) {
1042-
callback(exp);
1029+
}
10431030
}
1044-
}
1031+
})
10451032
}
10461033

10471034
fn is_ctfe_mir_available(self, id: DefIndex) -> bool {
@@ -1778,13 +1765,3 @@ impl CrateMetadata {
17781765
None
17791766
}
17801767
}
1781-
1782-
// Cannot be implemented on 'ProcMacro', as libproc_macro
1783-
// does not depend on librustc_ast
1784-
fn macro_kind(raw: &ProcMacro) -> MacroKind {
1785-
match raw {
1786-
ProcMacro::CustomDerive { .. } => MacroKind::Derive,
1787-
ProcMacro::Attr { .. } => MacroKind::Attr,
1788-
ProcMacro::Bang { .. } => MacroKind::Bang,
1789-
}
1790-
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc_span::source_map::{Span, Spanned};
2121
use rustc_span::symbol::{kw, Symbol};
2222

2323
use rustc_data_structures::sync::Lrc;
24-
use smallvec::SmallVec;
2524
use std::any::Any;
2625

2726
use super::{Decodable, DecodeContext, DecodeIterator};
@@ -298,9 +297,7 @@ provide! { tcx, def_id, other, cdata,
298297
r
299298
}
300299
module_children => {
301-
let mut result = SmallVec::<[_; 8]>::new();
302-
cdata.for_each_module_child(def_id.index, |child| result.push(child), tcx.sess);
303-
tcx.arena.alloc_slice(&result)
300+
tcx.arena.alloc_from_iter(cdata.get_module_children(def_id.index, tcx.sess))
304301
}
305302
defined_lib_features => { cdata.get_lib_features(tcx) }
306303
stability_implications => {
@@ -503,14 +500,12 @@ impl CStore {
503500
self.get_crate_data(def.krate).get_visibility(def.index)
504501
}
505502

506-
pub fn module_children_untracked(&self, def_id: DefId, sess: &Session) -> Vec<ModChild> {
507-
let mut result = vec![];
508-
self.get_crate_data(def_id.krate).for_each_module_child(
509-
def_id.index,
510-
|child| result.push(child),
511-
sess,
512-
);
513-
result
503+
pub fn module_children_untracked<'a>(
504+
&'a self,
505+
def_id: DefId,
506+
sess: &'a Session,
507+
) -> impl Iterator<Item = ModChild> + 'a {
508+
self.get_crate_data(def_id.krate).get_module_children(def_id.index, sess)
514509
}
515510

516511
pub fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro {

compiler/rustc_metadata/src/rmeta/encoder.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1267,13 +1267,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12671267
// the crate root for consistency with other crates (some of the resolver
12681268
// code uses it). However, we skip encoding anything relating to child
12691269
// items - we encode information about proc-macros later on.
1270-
let reexports = if !self.is_proc_macro {
1271-
tcx.module_reexports(local_def_id).unwrap_or(&[])
1272-
} else {
1273-
&[]
1274-
};
1275-
1276-
record_array!(self.tables.module_reexports[def_id] <- reexports);
12771270
if self.is_proc_macro {
12781271
// Encode this here because we don't do it in encode_def_ids.
12791272
record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
@@ -1305,6 +1298,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13051298
}
13061299
}
13071300
}));
1301+
1302+
if let Some(reexports) = tcx.module_reexports(local_def_id) {
1303+
assert!(!reexports.is_empty());
1304+
record_array!(self.tables.module_reexports[def_id] <- reexports);
1305+
}
13081306
}
13091307
}
13101308

compiler/rustc_resolve/src/build_reduced_graph.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ impl<'a> Resolver<'a> {
204204
}
205205

206206
pub(crate) fn build_reduced_graph_external(&mut self, module: Module<'a>) {
207-
for child in self.cstore().module_children_untracked(module.def_id(), self.session) {
207+
for child in
208+
Vec::from_iter(self.cstore().module_children_untracked(module.def_id(), self.session))
209+
{
208210
let parent_scope = ParentScope::module(module, self);
209211
BuildReducedGraphVisitor { r: self, parent_scope }
210212
.build_reduced_graph_for_external_crate_res(child);

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ impl<'a> Resolver<'a> {
19201920
if let Some(def_id) = def_id.as_local() {
19211921
self.reexport_map.get(&def_id).cloned().unwrap_or_default()
19221922
} else {
1923-
self.cstore().module_children_untracked(def_id, self.session)
1923+
self.cstore().module_children_untracked(def_id, self.session).collect()
19241924
}
19251925
}
19261926

0 commit comments

Comments
 (0)