Skip to content
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

Feature: MaybeItemFn for better rust-analyzer experience #1086

Closed
Diggsey opened this issue Oct 22, 2021 · 2 comments
Closed

Feature: MaybeItemFn for better rust-analyzer experience #1086

Diggsey opened this issue Oct 22, 2021 · 2 comments

Comments

@Diggsey
Copy link

Diggsey commented Oct 22, 2021

Currently when an attribute macro parses a function body it will cause rust-analyzer's completion to stop working within the function.

This is because the code within the function will fail to parse, and then the attribute macro will just expand to a compile error.

To avoid this, attribute macros can use a custom type to only parse the function signature, and leave the function body as an opaque token-stream: Diggsey/sqlxmq@ea408f7#diff-5796541ab2132b1f9f5e924468f24b542a59297c91a7543be0a5702926b7d3ecR95-R131

A similar approach has also been used in other crates like tokio.

It would be convenient if syn could provide a type like MaybeItemFn.

@dtolnay
Copy link
Owner

dtolnay commented Oct 26, 2021

I would prefer not to build this into syn. I consider the behavior that this is working around to be a rust-analyzer bug that needs to be fixed on their end. dtolnay/async-trait#178 has some related context.

@dtolnay dtolnay closed this as completed Oct 26, 2021
@Diggsey
Copy link
Author

Diggsey commented Oct 26, 2021

@dtolnay
Ok, let's imagine a world where RA correctly handles this. I think it's still worth having this functionality (ability to parse only a function signature) in syn, because many attribute macros don't care about the function body at all.

I could imagine a scenario where you have two attribute macros:

#[uses_fn_signature]
#[bf2rust]
fn foo() {
    [->+<]
}

In this case, if uses_fn_signature tries to parse the function body it will fail because it's not valid rust, but this is ignoring the fact that the #[bf2rust] attribute will transform it into something that is valid. The user would need to swap the order of attributes.

A perhaps more realistic example would be one where #[uses_fn_signature] is implemented against an older edition of rust, and is missing some new syntax from its parser. If this attribute only looks at the signature, then it won't stop you from using the newer features within the function body.

In summary, parsing more than is strictly necessary is redundant work and makes the attribute more fragile. I think there's value in making good practices easier.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants