Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
setzer22 opened this issue Mar 31, 2022 · 6 comments
Open

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

setzer22 opened this issue Mar 31, 2022 · 6 comments
Labels
A-proc-macro proc macro C-support Category: support questions

Comments

@setzer22
Copy link

setzer22 commented Mar 31, 2022

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?

@setzer22
Copy link
Author

setzer22 commented Mar 31, 2022

After doing a bit more research myself, I came across #11014, and there I found #11193. Adding my attribute macro to the "ignore" map in my settings seems to do the trick.

However, I can't expect all of my users to enable that workaround locally. I suspect there's something I can improve on my end: Many attribute macros work work just fine nowadays without that workaround. And, as the author, I'm more than willing to slightly adapt the definition to make it more IDE-friendly, I just don't know how 😄

Is there a better way to let rust-analyzer know that my attribute macro is not going to change the input function?

@Veykril
Copy link
Member

Veykril commented Mar 31, 2022

So, since you said that it works fine for another proc-macro that basically does the same as the one you are writing, I assume you are testing yours while you are modifying the proc-macro itself right now?

What I assume you are running into here is that r-a does not reload proc-macros while you edit them, so r-a is expanding the macro in your testing with an older version of the macro. Restarting your editor session should update the macro being used in that case. (Basically r-a asks cargo to build the proc-macros once on start up, but not after edits).

@setzer22
Copy link
Author

setzer22 commented Mar 31, 2022

Hey! Thanks for the quick response 👍

What I assume you are running into here is that r-a does not reload proc-macros while you edit them

This is indeed something I've experienced, but I don't think it's related to the issue I'm currently facing. I'm already used to restarting the editor often when developing proc macros 😄

It seems the issue persists even if I restart the editor. As I mentioned there seems to be some degree of functionality inside the function (auto-completion is there, just not quite, but starting to type a half-parsed line highlights all the function in red).

@HKalbasi
Copy link
Member

syn emits a compile_error! if it fails parsing the input (which is the case when you are typing). You can catch that error and emit original stream to make r-a happy.

@flodiebold
Copy link
Member

flodiebold commented Mar 31, 2022

After #11444, auto completion should work properly in attribute macros except in a) cases that we don't handle in the syntax fixup logic yet, which if you see them please report them, or b) edge cases where the attribute macro has some particular expectations or checks for the code it gets passed, in which case the macro has to cooperate better. So please report specific cases where you encounter problems (including the exact code inside the function and where you're trying to triggering completion). (Also make sure that you're using a recent version of rust-analyzer.)

syn emits a compile_error! if it fails parsing the input (which is the case when you are typing). You can catch that error and emit original stream to make r-a happy.

I wouldn't recommend that in general, it shouldn't be necessary and isn't really the right thing to do either.

@flodiebold flodiebold added the C-support Category: support questions label Apr 3, 2022
@dgsantana
Copy link

Just to leave a note, I got the same issue, in this case is using leptos component attribute macro. This doesn't happen in all projects, but this one is getting complex and as a few dependencies and feature gate flags. If I have some time I will try to understand where it's failing inside RA proc server.

@Veykril Veykril added the A-proc-macro proc macro label Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macro proc macro C-support Category: support questions
Projects
None yet
Development

No branches or pull requests

5 participants