Skip to content

Commit a5efcea

Browse files
committed
rustc_metadata: Merge items from extern blocks into their parent modules
during metadata encoding rather than during metadata decoding
1 parent 41ce641 commit a5efcea

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

Diff for: compiler/rustc_metadata/src/rmeta/decoder.rs

+5-36
Original file line numberDiff line numberDiff line change
@@ -1104,42 +1104,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11041104
let children = self.root.tables.children.get(self, id).unwrap_or_else(Lazy::empty);
11051105

11061106
for child_index in children.decode((self, sess)) {
1107-
// Get the item.
1108-
let child_kind = match self.maybe_kind(child_index) {
1109-
Some(child_kind) => child_kind,
1110-
None => continue,
1111-
};
1112-
1113-
// Hand off the item to the callback.
1114-
match child_kind {
1115-
// FIXME(eddyb) Don't encode these in children.
1116-
EntryKind::ForeignMod => {
1117-
let child_children = self
1118-
.root
1119-
.tables
1120-
.children
1121-
.get(self, child_index)
1122-
.unwrap_or_else(Lazy::empty);
1123-
for child_index in child_children.decode((self, sess)) {
1124-
let kind = self.def_kind(child_index);
1125-
callback(Export {
1126-
res: Res::Def(kind, self.local_def_id(child_index)),
1127-
ident: self.item_ident(child_index, sess),
1128-
vis: self.get_visibility(child_index),
1129-
span: self
1130-
.root
1131-
.tables
1132-
.span
1133-
.get(self, child_index)
1134-
.unwrap()
1135-
.decode((self, sess)),
1136-
});
1137-
}
1138-
continue;
1139-
}
1140-
EntryKind::Impl(_) => continue,
1141-
1142-
_ => {}
1107+
// FIXME: Merge with the logic below.
1108+
if let None | Some(EntryKind::ForeignMod | EntryKind::Impl(_)) =
1109+
self.maybe_kind(child_index)
1110+
{
1111+
continue;
11431112
}
11441113

11451114
let def_key = self.def_key(child_index);

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -1100,9 +1100,21 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11001100
// Encode this here because we don't do it in encode_def_ids.
11011101
record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
11021102
} else {
1103-
record!(self.tables.children[def_id] <- md.item_ids.iter().map(|item_id| {
1104-
item_id.def_id.local_def_index
1105-
}));
1103+
let direct_children = md.item_ids.iter().map(|item_id| item_id.def_id.local_def_index);
1104+
// Foreign items are planted into their parent modules from name resolution point of view.
1105+
let tcx = self.tcx;
1106+
let foreign_item_children = md
1107+
.item_ids
1108+
.iter()
1109+
.filter_map(|item_id| match tcx.hir().item(*item_id).kind {
1110+
hir::ItemKind::ForeignMod { items, .. } => {
1111+
Some(items.iter().map(|fi_ref| fi_ref.id.def_id.local_def_index))
1112+
}
1113+
_ => None,
1114+
})
1115+
.flatten();
1116+
1117+
record!(self.tables.children[def_id] <- direct_children.chain(foreign_item_children));
11061118
}
11071119
}
11081120

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

0 commit comments

Comments
 (0)