Stream: t-compiler/help

Topic: Why would `in_scope_traits()` return None?


Joshua Nelson (Jul 30 2020 at 01:37, on Zulip):

From https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/New.20query.20for.20traits.20that.20a.20type.20implements/near/205432986:

I have code that looks like

    let module = tcx.hir().as_local_hir_id(module);
    let traits_in_scope: FxHashSet<_> = match tcx.in_scope_traits(module) {
        Some(x) => x.iter().map(|candidate| candidate.def_id).collect(),
        None => {
            debug!("in_scope_traits returned None");
            return &[];
        }
    };

It turns out the None case is being hit an awful lot and it's breaking my other changes. Does anyone know why in_scope_traits would return None?

Joshua Nelson (Jul 30 2020 at 01:38, on Zulip):

for context, this is being called on

#![deny(intra_doc_link_resolution_failure)]

/// [`std::collections::BTreeMap::into_iter`]
pub fn foo() {}

where the module is the module of foo. So it's not as if the module is unknown.

Joshua Nelson (Aug 02 2020 at 21:59, on Zulip):

I think this is related to BTreeMap being in a separate crate

Joshua Nelson (Aug 02 2020 at 22:00, on Zulip):

is there a way to get traits in scope across crates? resolve_ast_path already does this so the data is there

Joshua Nelson (Aug 02 2020 at 22:00, on Zulip):

the reason I don't want to use resolve_ast_path is because it's super inefficient to first look at all traits and only then filter by whether they're in scope, just resolving the name is expensive

Jonas Schievink (Aug 02 2020 at 22:03, on Zulip):

looks like the map is only populated for some expressions

Jonas Schievink (Aug 02 2020 at 22:05, on Zulip):

ah, no, it's being rewritten to contain HirOwners as keys

Joshua Nelson (Aug 02 2020 at 22:05, on Zulip):

ooh is that an in-progress PR? have a link?

Jonas Schievink (Aug 02 2020 at 22:05, on Zulip):

try passing foos ID instead of its module

Jonas Schievink (Aug 02 2020 at 22:06, on Zulip):

no, I mean the code rewrites the map

Jonas Schievink (Aug 02 2020 at 22:06, on Zulip):

https://github.com/rust-lang/rust/blob/19cefa68640843956eedd86227ddc1d35dbc6754/src/librustc_middle/ty/context.rs#L1119-L1123

Joshua Nelson (Aug 02 2020 at 22:10, on Zulip):

getting foos id is going to take me a while

Joshua Nelson (Aug 02 2020 at 22:10, on Zulip):

rustdoc keeps throwing away info :angry:

Joshua Nelson (Aug 02 2020 at 22:12, on Zulip):

this will still only work for local items in any case :/

Joshua Nelson (Aug 02 2020 at 22:13, on Zulip):

because it takes a HirId

Joshua Nelson (Aug 02 2020 at 22:21, on Zulip):

more context in case it helps: https://github.com/rust-lang/rust/pull/74489#discussion_r464125208

Joshua Nelson (Aug 05 2020 at 00:10, on Zulip):

ok it seems that in_scope_traits() as written won't work, but is there an intrinsic reason it only works for the current crate? Could I modify it to store DefIds instead?

Joshua Nelson (Aug 05 2020 at 00:10, on Zulip):

and somewhat related - is there a way to tell if a given DefId is in scope for any given module?

Joshua Nelson (Aug 05 2020 at 00:11, on Zulip):

I found https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/struct.Resolver.html#method.resolve_ident_in_module but it doesn't seem to work cross-crate

Joshua Nelson (Aug 05 2020 at 00:22, on Zulip):

found it! https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.Visibility.html#method.is_accessible_from

Joshua Nelson (Aug 05 2020 at 00:24, on Zulip):

nope, that just tells me if it could be in scope :(

Last update: Sep 28 2020 at 15:45UTC