Skip to content

Attribute proc-macro expansion issue #10498

New issue

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

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

Already on GitHub? # to your account

Closed
liuw opened this issue Oct 9, 2021 · 14 comments
Closed

Attribute proc-macro expansion issue #10498

liuw opened this issue Oct 9, 2021 · 14 comments
Labels
A-macro macro expansion

Comments

@liuw
Copy link

liuw commented Oct 9, 2021

Commit 81891f7 enabled attribute proc macro by default (released in v0.2.768). That rendered rust-analyzer unusable in my case. Basically whenever I change something, the whole function is marked as problematic. Please see the two screenshots for comparison.

First screenshot shows the result when that option is enabled; second screenshot shows the result when the option is disabled.

In the second screenshot, rust-analyzer correctly points out that let x is problematic.

image

image

@flodiebold
Copy link
Member

flodiebold commented Oct 9, 2021

Can you provide the crate where the macro comes from and ideally some example code?

The problem is likely that the proc macro emits an error when the function is syntactically invalid. (What error message does rustc emit when faced with this incomplete code?) In that case, probably your only options are to either report this to the proc macro authors and wait for them to fix it, or to disable attribute macro expansion again.

@Veykril
Copy link
Member

Veykril commented Oct 9, 2021

The crate is legion I believe

@liuw
Copy link
Author

liuw commented Oct 9, 2021

Can you provide the crate where the macro comes from and ideally some example code?

Like @Veykril said, the crate is legion. It not straight-forward for me to provide a minimal repro. The code is at https://github.com/amethyst/legion/blob/32df916517fa3b3bb5d827d824053bbb128a43be/codegen/src/lib.rs#L159.

The problem is likely that the proc macro emits an error when the function is syntactically invalid. (What error message does rustc emit when faced with this incomplete code?) In that case, probably your only options are to either report this to the proc macro authors and wait for them to fix it, or to disable attribute macro expansion again.

Please see the screenshot for the error message. Yes it is complaining about invalid syntax, but that's not very helpful in identifying the root cause.

image

@liuw
Copy link
Author

liuw commented Oct 9, 2021

If this is helpful, this is what I get when running cargo c. It is able to pinpoint the correct location of the incomplete code.

image

@flodiebold
Copy link
Member

Hm. The first error comes from rustc's parser; does the second one come from the macro? If so, how does rustc know the location? Does rustc even try to expand the macro if there's a syntax error in the input? 🤔

@flodiebold
Copy link
Member

flodiebold commented Oct 10, 2021

Ah, syn sets the span of the compile_error! invocation to be the error span. Probably we don't handle that correctly when mapping errors up?

Edit: But actually, with a test proc macro it seems to be working fine 🤔

@flodiebold flodiebold added the A-macro macro expansion label Oct 10, 2021
@lucacasonato
Copy link

+1 on this. I also happens with the #[tokio::test] and #[tracing::instrument] macros.

@bjorn3
Copy link
Member

bjorn3 commented Oct 14, 2021

Tracing: tokio-rs/tracing#1633
Tokio: tokio-rs/tokio#4154 (fixed on master)

@lucacasonato
Copy link

Beautiful, thank you for the hard work everyone ❤️

@Follpvosten
Copy link

So this is an issue of the specific proc macro implementation, right? So if I notice it with any other macros (like in actix or async-graphql...), I should prefer reporting it to them?

@lnicola
Copy link
Member

lnicola commented Oct 22, 2021

If it's the same issue, you could, e.g. actix/actix-web#2394.

@asdrubalinea
Copy link

I'm having the same issue with the async-trait macros, should I report it there?

@Veykril
Copy link
Member

Veykril commented Oct 22, 2021

Yep, that should have the problem as well, https://github.com/dtolnay/async-trait/blob/70bdfa4a8035393164a79391fdb862acc3310c66/src/lib.rs#L334-L335, so reporting that issue there would be a good idea.

@Veykril
Copy link
Member

Veykril commented Jan 5, 2022

Closing in favor of #11014

@Veykril Veykril closed this as completed Jan 5, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-macro macro expansion
Projects
None yet
Development

No branches or pull requests

8 participants