Stream: t-compiler/help

Topic: How do I get the module that an Item belongs to?


Joshua Nelson (Jun 06 2020 at 16:15, on Zulip):

I managed to get a DefId for it but I'm not sure where to go from there.

Matthew Jasper (Jun 06 2020 at 16:30, on Zulip):

tcx.parent(def_id) using the DefIdTree trait will get the parent item of a def_id. For items this will be the parent module.
If you may have an associated item or something that's not an item, you'll have to call parent in a loop until tcx.def_kind(def_id) is DefKind::Mod.
If the item is guaranteed to be in the current crate, then parent_module on the HirId would be easiest.

Joshua Nelson (Jun 06 2020 at 16:41, on Zulip):

If the item is guaranteed to be in the current crate, then parent_module on the HirId would be easiest.

This is actually only for cross-crate re-exports. tcx.parent() looks like what I need, thank you!

Last update: Jan 22 2021 at 13:30UTC