rust-lang / rust-lang/rust-analyzer

fill match arms assist put the code in the wrong place

Open
#12,938 10 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists A-proc-macro C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: v0.4.1157

rustc version: rustc 1.64.0-nightly (9067d5277 2022-07-28)

fill match arms assist put the code in the wrong place!

https://user-images.githubusercontent.com/13777628/182836330-27603145-5ff2-4755-bbd0-2e003719c92f.mp4

Reload the window doesn't solve the problem.

Minimal reproduce:

// crate macro_lab, lab.rs
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

#[proc_macro_attribute]
pub fn untouched(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as ItemFn);
    let tokens = quote! {
        #input
    };
    tokens.into()
}

// main.rs
use macro_lab::untouched;

#[untouched]
fn f() {
    enum E {
        A,
        B,
    }
    let e = E::A;
    match e {
    }
}

However if we define untouched like this:

pub fn untouched(_attr: TokenStream, item: TokenStream) -> TokenStream {
    item
}

the bug doesn't occur.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the proc-macro definitions in lab.rs and the annotated function in main.rs, then invoke the “fill match arms” assist on the empty match. Compare the behavior when the attribute macro expands through quote! versus returning item directly; done means the generated arms are inserted inside the intended match expression.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.