rust-lang / rust-lang/rust-analyzer

How to make attribute proc macros more user-friendly with RA

Open
#11,859 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-proc-macro C-support
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 13h
Merged PRs (30d)
69

Description

I am working on an attribute-like proc macro, which is intended to annotate functions, as such:

#[my_attribute]
pub fn(arg1: T1, arg2: T2) {
    // User code here
}

The thing is, even though my attribute is only meant to do some semantic checks and is not even modifying the function body, once I annotate the function with my attribute, rust analyzer gives up and stops doing most of its usual tasks:

  • Auto-completion works, but semi-consistently. Sometimes completions do not load until after I've written a large part of the call, whereas typically I can get a list of completion candidates simply by typing a . after an identifier.
  • Syntax errors are not highlighted at the place where they occur, and I instead get large parts of the function marked as a syntax error.

Attribute-like proc macros seem to be supported in RA to some extent. For instance, I use often use the profiling (https://crates.io/crates/profiling) crate and I don't seem to encounter those issues when using their #[profiling::function] attribute macro. I had a look at the implementation here and I can't see any difference with my implementation.

How does the mechanism in RA that expands proc macros work? Is there a way I can improve the user experience of my macro so that RA behaves just like when writing a regular function?

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

Start by tracing rust-analyzer's proc-macro expansion behavior and compare it with the profiling crate's attribute macro implementation at profiling-procmacros/src/lib.rs. Reproduce the completion and syntax-error problems with the described annotated function; done means identifying a concrete difference or reproducible rust-analyzer defect and documenting the expected behavior.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.