Stream: t-compiler/help

Topic: Why is def_id_visibility private?


Joshua Nelson (May 30 2020 at 05:20, on Zulip):

Hello all, I'm trying to fix https://github.com/rust-lang/rust/issues/72769 and I need to know the visibility of the linked item for that. My idea was to use rustc_privacy::def_id_visibility to look it up, but that function is private. Is there a reason for that, is there a better way to find that information?

Matthew Jasper (May 30 2020 at 08:51, on Zulip):

tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_id)

Joshua Nelson (May 30 2020 at 15:18, on Zulip):

Hmm, if I try that I get this error instead:

error[E0308]: mismatched types
   --> src/librustdoc/passes/collect_intra_doc_links.rs:709:103
    |
709 |                     if !self.cx.tcx.privacy_access_levels(rustc_hir::def_id::LOCAL_CRATE).is_exported(def_id) {
    |                                                                                                       ^^^^^^ expected struct `rustc_hir::HirId`, found struct `rustc_span::def_id::DefId`
Joshua Nelson (May 30 2020 at 15:19, on Zulip):

I got def_id from Res::opt_def_id(): https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.Res.html#method.opt_def_id

lcnr (May 30 2020 at 15:20, on Zulip):

you can use tcx.hir().as_local_hir_id(def_id.as_local()?) to get the relevant HirId

marmeladema (May 30 2020 at 15:27, on Zulip):

You can also use def_id.expect_local(), "localize" the def id or panic if the def is is foreign. You can use that if the call should never fail.

Joshua Nelson (May 30 2020 at 15:32, on Zulip):

I'd rather avoid expect() calls until I know the code base better, as_local() works pretty well though :)

                if let Some(local) = res.opt_def_id().and_then(|def_id| def_id.as_local()) {
                    let hir_id = self.cx.tcx.hir().as_local_hir_id(local);
                    if !self.cx.tcx.privacy_access_levels(rustc_hir::def_id::LOCAL_CRATE).is_exported(hir_id) {
...
Last update: Sep 27 2020 at 14:15UTC