-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Compiler suggests #[derive((DeriveMacro).try_into().unwrap())] #78862
Comments
@msrd0 In general, there's not much that the compiler can do in this type of situation - it's up to the proc-macro to emit tokens with reasonable spans. Is |
This is a small extract of my macro that reproduces the error and suggestion: https://github.com/msrd0/proc-macro-try-into-suggestion I believe the So maybe this would've been a better suggestion, eventhough the code will always panic:
|
Still, the error message is unhelpful even if the code was completely unrelated to the macro's input and hence cannot point to a reasonable span outside of the generated code. So maybe, if the only available span is that of the call site, do not emit any suggestion? |
Testing this further, it seems that the compiler is confused by the span of every token in |
Yes, I don't think spans can point inside a string literal, at least not in stable Rust. The best you could achieve is to manually respan the parsed w.r.t. the compiler, maybe some mechanism to express there is no actual span to apply suggestions to would be useful, so that you could use a
|
I didn't know that
This is probably the behaviour I expected to begin with when the span is that of the call site. |
fwiw, I believe we're running into similar issues with various Clippy lints:
AIUI, there is currently no way to tell if a span originated from |
Do not provide suggestions when the spans come from expanded code that doesn't point at user code Hide invalid proc-macro suggestions and track spans coming from proc-macros pointing at attribute. Effectively, unless the proc-macro keeps user spans, suggestions will not be produced for the code they produce. r? `@ghost` Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557. Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
Do not provide suggestions when the spans come from expanded code that doesn't point at user code Hide invalid proc-macro suggestions and track spans coming from proc-macros pointing at attribute. Effectively, unless the proc-macro keeps user spans, suggestions will not be produced for the code they produce. r? ``@ghost`` Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557. Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
Today I encountered a rather unhelpful suggestion from the compiler (1.47.0):
The code emited from the derive macro contains something along the lines of
let value: usize = -1 as i32
.Applying the suggestion, however, doesn't really help (almost as if I had to fix the problem in the derive macro):
I believe the compiler should only emit that suggestion if the code it applies to is actually an expression of type
i32
.The text was updated successfully, but these errors were encountered: