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),

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>(
    dbg!(&pat, pat.syntax().text()); /* [crates/...] pat.syntax().text() = "A::One(text, n)" */

    let fields = pat
        .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 = [] */


[crates/...] &field = IdentPat {
    syntax: IDENT_PAT@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): does something similar

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

Specifically, this bit I belive:

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 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): 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