Skip to content

Commit 6a233b5

Browse files
committed
rustc_metadata: Switch module children decoding to an iterator
1 parent f3b5791 commit 6a233b5

File tree

4 files changed

+58
-61
lines changed

4 files changed

+58
-61
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+47-47
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ 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;
@@ -994,60 +993,61 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
994993
/// including both proper items and reexports.
995994
/// Module here is understood in name resolution sense - it can be a `mod` item,
996995
/// or a crate root, or an enum, or a trait.
997-
fn for_each_module_child(
996+
fn get_module_children(
998997
self,
999998
id: DefIndex,
1000-
mut callback: impl FnMut(ModChild),
1001-
sess: &Session,
1002-
) {
1003-
if let Some(data) = &self.root.proc_macro_data {
1004-
// If we are loading as a proc macro, we want to return
1005-
// the view of this crate as a proc macro crate.
1006-
if id == CRATE_DEF_INDEX {
1007-
for def_index in data.macros.decode(self) {
1008-
let raw_macro = self.raw_proc_macro(def_index);
1009-
let res = Res::Def(
1010-
DefKind::Macro(macro_kind(raw_macro)),
1011-
self.local_def_id(def_index),
1012-
);
1013-
let ident = self.item_ident(def_index, sess);
1014-
callback(ModChild {
1015-
ident,
1016-
res,
1017-
vis: ty::Visibility::Public,
1018-
span: ident.span,
1019-
macro_rules: false,
1020-
});
999+
sess: &'a Session,
1000+
) -> impl Iterator<Item = ModChild> + 'a {
1001+
iter::from_generator(move || {
1002+
if let Some(data) = &self.root.proc_macro_data {
1003+
// If we are loading as a proc macro, we want to return
1004+
// the view of this crate as a proc macro crate.
1005+
if id == CRATE_DEF_INDEX {
1006+
for def_index in data.macros.decode(self) {
1007+
let raw_macro = self.raw_proc_macro(def_index);
1008+
let res = Res::Def(
1009+
DefKind::Macro(macro_kind(raw_macro)),
1010+
self.local_def_id(def_index),
1011+
);
1012+
let ident = self.item_ident(def_index, sess);
1013+
yield ModChild {
1014+
ident,
1015+
res,
1016+
vis: ty::Visibility::Public,
1017+
span: ident.span,
1018+
macro_rules: false,
1019+
};
1020+
}
10211021
}
1022+
return;
10221023
}
1023-
return;
1024-
}
10251024

1026-
// Iterate over all children.
1027-
if let Some(children) = self.root.tables.children.get(self, id) {
1028-
for child_index in children.decode((self, sess)) {
1029-
let ident = self.item_ident(child_index, sess);
1030-
let kind = self.def_kind(child_index);
1031-
let def_id = self.local_def_id(child_index);
1032-
let res = Res::Def(kind, def_id);
1033-
let vis = self.get_visibility(child_index);
1034-
let span = self.get_span(child_index, sess);
1035-
let macro_rules = match kind {
1036-
DefKind::Macro(..) => {
1037-
self.root.tables.macro_rules.get(self, child_index).is_some()
1038-
}
1039-
_ => false,
1040-
};
1025+
// Iterate over all children.
1026+
if let Some(children) = self.root.tables.children.get(self, id) {
1027+
for child_index in children.decode((self, sess)) {
1028+
let ident = self.item_ident(child_index, sess);
1029+
let kind = self.def_kind(child_index);
1030+
let def_id = self.local_def_id(child_index);
1031+
let res = Res::Def(kind, def_id);
1032+
let vis = self.get_visibility(child_index);
1033+
let span = self.get_span(child_index, sess);
1034+
let macro_rules = match kind {
1035+
DefKind::Macro(..) => {
1036+
self.root.tables.macro_rules.get(self, child_index).is_some()
1037+
}
1038+
_ => false,
1039+
};
10411040

1042-
callback(ModChild { ident, res, vis, span, macro_rules });
1041+
yield ModChild { ident, res, vis, span, macro_rules };
1042+
}
10431043
}
1044-
}
10451044

1046-
if let Some(exports) = self.root.tables.module_reexports.get(self, id) {
1047-
for exp in exports.decode((self, sess)) {
1048-
callback(exp);
1045+
if let Some(exports) = self.root.tables.module_reexports.get(self, id) {
1046+
for exp in exports.decode((self, sess)) {
1047+
yield exp;
1048+
}
10491049
}
1050-
}
1050+
})
10511051
}
10521052

10531053
fn is_ctfe_mir_available(self, id: DefIndex) -> bool {

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_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)