rust-lang / rust-lang/rust-analyzer
"Fill struct fields" code action inside proc_macro invocation is broken by iterating the TokenStream
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
I hope this is the right place for this report, I have asked on the forum first.
How to reproduce
First I define two proc macros that should both just return the input stream.
#[proc_macro]
pub fn identity_correct(input: TokenStream) -> TokenStream {
input
}
#[proc_macro]
pub fn identity_broken(input: TokenStream) -> TokenStream {
let mut out = TokenStream::new();
out.extend(input.into_iter());
out
}
Then I use these proc macros on a struct expression that is incomplete:
struct Test {
x: u32,
y: u32,
};
fn main() {
identity_correct!(Test { x: 0 });
identity_broken!(Test { x: 0 });
}
When I then apply the rust-analyzer "Fill struct fields" code action on both I get the following result:
fn main() {
identity_correct!(Test {x:0, y: todo!() });
identity_broken!(Test {x:0, y: todo!() } });
}
Note that there is one extra closing brace } for the broken macro.
Conclusion
Clearly out.extend(input.into_iter()) loses some information and rust-analyzer can not handle it.
Since I don't use any external dependencies, this could be a bug in rust-analyzer or rust.
rust-analyzer version: 0.3.1815-standalone
rustc version: 1.77.0-nightly
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the "Fill struct fields" code action with the two proc macros shown in the issue, comparing direct return of the TokenStream with rebuilding it via input.into_iter(). Confirm whether the extra closing brace is introduced by rust-analyzer or by the proc-macro token representation; done means the code action produces balanced output for both examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100