Skip to content

Commit 4ecb49e

Browse files
committed
Handle cross-crate module ExpnIds consistently
- Always use the ExpnId serialized to `tables` - Use the Id for traits and enums from other crates in resolution.
1 parent 547a6ff commit 4ecb49e

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1219,10 +1219,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12191219
}
12201220

12211221
fn module_expansion(&self, id: DefIndex, sess: &Session) -> ExpnId {
1222-
if let EntryKind::Mod(m) = self.kind(id) {
1223-
m.decode((self, sess)).expansion
1224-
} else {
1225-
panic!("Expected module, found {:?}", self.local_def_id(id))
1222+
match self.kind(id) {
1223+
EntryKind::Mod(_) | EntryKind::Enum(_) | EntryKind::Trait(_) => {
1224+
self.get_expn_that_defined(id, sess)
1225+
}
1226+
_ => panic!("Expected module, found {:?}", self.local_def_id(id)),
12261227
}
12271228
}
12281229

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1086,11 +1086,13 @@ impl EncodeContext<'a, 'tcx> {
10861086
Lazy::empty()
10871087
};
10881088

1089-
let data = ModData { reexports, expansion: tcx.expn_that_defined(local_def_id) };
1089+
let data = ModData { reexports };
10901090

10911091
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
10921092
if self.is_proc_macro {
10931093
record!(self.tables.children[def_id] <- &[]);
1094+
// Encode this here because we don't do it in encode_def_ids.
1095+
record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
10941096
} else {
10951097
record!(self.tables.children[def_id] <- md.item_ids.iter().map(|item_id| {
10961098
item_id.def_id.local_def_index

compiler/rustc_metadata/src/rmeta/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ struct RenderedConst(String);
367367
#[derive(MetadataEncodable, MetadataDecodable)]
368368
struct ModData {
369369
reexports: Lazy<[Export]>,
370-
expansion: ExpnId,
371370
}
372371

373372
#[derive(MetadataEncodable, MetadataDecodable)]

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,7 @@ impl<'a> Resolver<'a> {
145145
} else {
146146
def_key.disambiguated_data.data.get_opt_name().expect("module without name")
147147
};
148-
let expn_id = if def_kind == DefKind::Mod {
149-
self.cstore().module_expansion_untracked(def_id, &self.session)
150-
} else {
151-
// FIXME: Parent expansions for enums and traits are not kept in metadata.
152-
ExpnId::root()
153-
};
148+
let expn_id = self.cstore().module_expansion_untracked(def_id, &self.session);
154149

155150
Some(self.new_module(
156151
parent,

src/test/ui/proc-macro/meta-macro-hygiene.stdout

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ crate0::{{expn1}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt:
4949
crate0::{{expn2}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Bang, "produce_it")
5050
crate0::{{expn3}}: parent: crate0::{{expn2}}, call_site_ctxt: #4, def_site_ctxt: #0, kind: Macro(Bang, "meta_macro::print_def_site")
5151
crate0::{{expn4}}: parent: crate0::{{expn3}}, call_site_ctxt: #5, def_site_ctxt: #0, kind: Macro(Bang, "$crate::dummy")
52+
crate1::{{expnNNN}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Attr, "derive")
53+
crate1::{{expnNNN}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Attr, "derive")
5254
crate1::{{expnNNN}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Bang, "include")
5355
crate2::{{expn1}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: AstPass(StdImports)
5456

src/test/ui/proc-macro/nonterminal-token-hygiene.stdout

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ crate0::{{expn1}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt:
7373
crate0::{{expn2}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Bang, "outer")
7474
crate0::{{expn3}}: parent: crate0::{{expn2}}, call_site_ctxt: #4, def_site_ctxt: #4, kind: Macro(Bang, "inner")
7575
crate0::{{expn4}}: parent: crate0::{{expn3}}, call_site_ctxt: #6, def_site_ctxt: #0, kind: Macro(Bang, "print_bang")
76+
crate1::{{expnNNN}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Attr, "derive")
77+
crate1::{{expnNNN}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Attr, "derive")
7678
crate1::{{expnNNN}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Bang, "include")
7779
crate2::{{expn1}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: AstPass(StdImports)
7880

0 commit comments

Comments
 (0)