Stream: t-compiler/help

Topic: How do I compare a DefId to CRATE_HIR_ID?


Joshua Nelson (Jul 11 2020 at 17:19, on Zulip):

Currently I have the following code: item.def_id.as_local().map(|def_id| self.cx.tcx.hir().as_local_hir_id(def_id)) = Some(hir::CRATE_HIR_ID). Is there an easier way to do this?

Joshua Nelson (Jul 11 2020 at 17:21, on Zulip):

(deleted)

Joshua Nelson (Jul 11 2020 at 17:21, on Zulip):

(sorry, I keep replying in the wrong thread)

Joshua Nelson (Jul 11 2020 at 17:21, on Zulip):

hmm maybe def_id.is_top_level_module()?

Matthew Jasper (Jul 11 2020 at 17:23, on Zulip):

Yes, that should work.

eddyb (Jul 12 2020 at 00:32, on Zulip):

@Joshua Nelson you generally shouldn't need to convert a DefId into a HirId unless you plan to grab some HIR nodes. kind of weird that CRATE_HIR_ID even exists tbh

eddyb (Jul 12 2020 at 00:34, on Zulip):

I guess this makes sense but it could be a constant shrug https://github.com/rust-lang/rust/blob/master/src/librustc_span/def_id.rs#L167

Joshua Nelson (Jul 12 2020 at 00:36, on Zulip):

This is left over from when intra-doc links were first written and people assumed they would always be to within the local crate

Joshua Nelson (Jul 12 2020 at 00:37, on Zulip):

https://github.com/rust-lang/rust/pull/73101/commits/b11656273c16c647c367ffa95203722cd3cf41b3#diff-2bcad7b16b56ef2ebb92f8e60ae33773R475

eddyb (Jul 12 2020 at 00:37, on Zulip):

sure, what I mean is that HirId only makes sense if you actually go poking at the HIR

eddyb (Jul 12 2020 at 00:37, on Zulip):

most of the time, having a DefId is the "better" thing to have

eddyb (Jul 12 2020 at 00:37, on Zulip):

so a HirId is like a "downgrade"

eddyb (Jul 12 2020 at 00:38, on Zulip):

(it's also not free to convert to it, for now)

Joshua Nelson (Jul 12 2020 at 00:38, on Zulip):

hmm now I'm wondering if this code will break if you implement a foreign trait for a type in a different module https://github.com/rust-lang/rust/blob/b11656273c16c647c367ffa95203722cd3cf41b3/src/librustdoc/passes/collect_intra_doc_links.rs#L298

eddyb (Jul 12 2020 at 00:49, on Zulip):

it's and_then

eddyb (Jul 12 2020 at 00:49, on Zulip):

so it would just end up here https://github.com/rust-lang/rust/blob/b11656273c16c647c367ffa95203722cd3cf41b3/src/librustdoc/passes/collect_intra_doc_links.rs#L328-L329

eddyb (Jul 12 2020 at 00:49, on Zulip):

assuming you mean that the as_local_hir_id would return None

Joshua Nelson (Jul 12 2020 at 00:50, on Zulip):

yeah

Joshua Nelson (Jul 12 2020 at 00:50, on Zulip):

ok neat, and the only other place hir() is called is for Self which shouldn't lead to issues

Joshua Nelson (Jul 12 2020 at 00:51, on Zulip):

and checking if the item is private, which only happens if it's a local item anyway

Last update: Sep 28 2020 at 16:15UTC