Skip to content

rustc_metadata: Merge items from extern blocks into their parent modules during metadata encoding rather than during metadata decoding #92153

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

Merged
merged 1 commit into from
Dec 28, 2021
Merged
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
41 changes: 5 additions & 36 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,42 +1104,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let children = self.root.tables.children.get(self, id).unwrap_or_else(Lazy::empty);

for child_index in children.decode((self, sess)) {
// Get the item.
let child_kind = match self.maybe_kind(child_index) {
Some(child_kind) => child_kind,
None => continue,
};

// Hand off the item to the callback.
match child_kind {
// FIXME(eddyb) Don't encode these in children.
EntryKind::ForeignMod => {
let child_children = self
.root
.tables
.children
.get(self, child_index)
.unwrap_or_else(Lazy::empty);
for child_index in child_children.decode((self, sess)) {
let kind = self.def_kind(child_index);
callback(Export {
res: Res::Def(kind, self.local_def_id(child_index)),
ident: self.item_ident(child_index, sess),
vis: self.get_visibility(child_index),
span: self
.root
.tables
.span
.get(self, child_index)
.unwrap()
.decode((self, sess)),
});
}
continue;
}
EntryKind::Impl(_) => continue,

_ => {}
// FIXME: Merge with the logic below.
if let None | Some(EntryKind::ForeignMod | EntryKind::Impl(_)) =
self.maybe_kind(child_index)
{
continue;
}

let def_key = self.def_key(child_index);
Expand Down
23 changes: 15 additions & 8 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,9 +1100,21 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// Encode this here because we don't do it in encode_def_ids.
record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
} else {
record!(self.tables.children[def_id] <- md.item_ids.iter().map(|item_id| {
item_id.def_id.local_def_index
}));
let direct_children = md.item_ids.iter().map(|item_id| item_id.def_id.local_def_index);
// Foreign items are planted into their parent modules from name resolution point of view.
let tcx = self.tcx;
let foreign_item_children = md
.item_ids
.iter()
.filter_map(|item_id| match tcx.hir().item(*item_id).kind {
hir::ItemKind::ForeignMod { items, .. } => {
Some(items.iter().map(|fi_ref| fi_ref.id.def_id.local_def_index))
}
_ => None,
})
.flatten();

record!(self.tables.children[def_id] <- direct_children.chain(foreign_item_children));
}
}

Expand Down Expand Up @@ -1503,11 +1515,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record!(self.tables.kind[def_id] <- entry_kind);
// FIXME(eddyb) there should be a nicer way to do this.
match item.kind {
hir::ItemKind::ForeignMod { items, .. } => record!(self.tables.children[def_id] <-
items
.iter()
.map(|foreign_item| foreign_item.id.def_id.local_def_index)
),
hir::ItemKind::Enum(..) => record!(self.tables.children[def_id] <-
self.tcx.adt_def(def_id).variants.iter().map(|v| {
assert!(v.def_id.is_local());
Expand Down