Stream: t-compiler/rust-analyzer

Topic: FindUsages help with local pattern


Arif Roktim (Nov 08 2020 at 04:52, on Zulip):

Hello, I need some help finding usages of a local binding.
Given the following fixture:

enum A {
    One(String, usize),
    Two(String),<|>
}

fn func(it: A) {
    if let A::One(text, n) = it {
        // repeat the text n times
        println!("{}", text.repeat(n));
    }
}

I have an ast::TupleStructPat: A::One(text, n). From this, I want to find usages for text and n . Here's the code I have so far:

fn update_usage(
    ctx: &AssistContext,
    reference: &Reference,
    source_file: &SourceFile,
) -> Option<()> {
    let pat = algo::find_node_at_offset::<ast::TupleStructPat>(
        source_file.syntax(),
        reference.file_range.range.start()
    )?;
    dbg!(&pat, pat.syntax().text()); /* [crates/...] pat.syntax().text() = "A::One(text, n)" */

    let fields = pat
        .fields()
        .filter_map(|field| match field {
            ast::Pat::IdentPat(ident) => Some(ident),
            _ => None,
        });
    for field in fields {
        dbg!(&field); /* See bottom for output */
        let usages = Definition::Local(ctx.sema.to_def(&field)?).usages(&ctx.sema).all();
        dbg!(usages); /* [crates/...] usages = [] */
    }

    Some(())
}

/*
[crates/...] &field = IdentPat {
    syntax: IDENT_PAT@88..92
      NAME@88..92
        IDENT@88..92 "text"
    ,
}
*/

But the usages come out empty.

Am I doing something wrong here? Or is FindUsage not able to do something like this?

Arif Roktim (Nov 09 2020 at 04:39, on Zulip):

It might be a bug. I replaced the macro println!("{}", text.repeat(n) with let foo = text.repeat(n) and I got the expected behavior

matklad (Nov 09 2020 at 09:38, on Zulip):

https://github.com/rust-analyzer/rust-analyzer/blob/945900bdbf5423fdfb8542901dfb813c2ec32e09/crates/assists/src/handlers/inline_local_variable.rs does something similar

matklad (Nov 09 2020 at 09:39, on Zulip):

Specifically, this bit I belive: https://github.com/rust-analyzer/rust-analyzer/blob/945900bdbf5423fdfb8542901dfb813c2ec32e09/crates/assists/src/handlers/inline_local_variable.rs#L45-L51

matklad (Nov 09 2020 at 09:39, on Zulip):

And yes, there might be some bugs around usages in macros -- don't handle them that well yet :(

Arif Roktim (Nov 09 2020 at 18:02, on Zulip):

I was using inline_local_variable.rs as inspiration :smile:
You said this is because tests don't have libstd/libcore. Is this the sort of thing FamousDefs tries to solve?

Lukas Wirth (Nov 09 2020 at 19:12, on Zulip):

Yep FamousDefs is there to solve this problem ideally by populating it with common things from std/core that tests want to test against

Lukas Wirth (Nov 09 2020 at 19:14, on Zulip):

https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ide/src/inlay_hints.rs#L441-L442 here its injected into the inlay_hints tests so that we can test that iterator hint shortening works for example

Arif Roktim (Nov 09 2020 at 19:27, on Zulip):

I see. Thanks all

Last update: Jul 29 2021 at 09:45UTC