-
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
Diagnostics: improve error message for incorrect inner attribute #89566
Comments
I'm fairly sure I know what is causing this however I believe that it will require a new Error message as the issue is that the parser is parsing @estebank I'm not 100% on the process of designing new error messages for rust but I wanted to see if this makes sense: A) the B) the I think the only issue I have with this is that I feel I may need to spend a while trying to understand the parser to fix this one - it already seems like it will be difficult to selectively parse attributes when I don't want to or even to tell what kind of attributes I have actually parsed |
…nkov Updated error message for accidental uses of derive attribute as a crate attribute This partially fixes the original issue rust-lang#89566 by adding derive to the list of invalid crate attributes and then providing an updated error message however I'm not sure how to prevent the resolution error message from emitting without causing the compiler to just abort when it finds an invalid crate attribute (which I'd prefer not to do so we can find and emit other errors). `@petrochenkov` I have been told you may have some insight on why it's emitting the resolution error though honestly I'm not sure if we need to worry about fixing it as long as we can provide the invalid crate attribute error also (which happens first anyway)
Current output:
All we need to do is try to silence the first error, if possible, if the second one is emitted. If there's an item before it the error is better:
|
Hi Estebank, long time - I think I remember hitting a bit of a wall with suppressing the first error as it was pretty deeply embedded in the resolver stage of the compiler & if I wanted to suppress it I had to abort any step that would allow me to output a useful error about the attribute symbol not being usable in that location. I think the issue with having to suppress the first error is that the context of resolution of inner attributes has not been fully defined yet #54726 goes into this more but until that is resolved (pardon the pun) I'm not sure we can suppress the first error. Although potentially we can just look for the first error being emitted and just not emit it? Not sure if that is possible? |
It'll be difficult to silence the first error, because that one happens during macro expansion, while the later occurs in a much later stage. For now, I feel that it is ok to have both errors, but let's leave this ticket open to maybe address in the future. |
Tweak suggestion span for outer attr and point at item following invalid inner attr After: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LL | LL | fn main() {} | ------------ the inner attribute doesn't annotate this function | help: perhaps you meant to use an outer attribute | LL - #![unix_sigpipe = "inherit"] LL + #[unix_sigpipe = "inherit"] | ``` Before: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: perhaps you meant to use an outer attribute | LL | #[unix_sigpipe = "inherit"] | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` CC rust-lang#89566.
Tweak suggestion span for outer attr and point at item following invalid inner attr After: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LL | LL | fn main() {} | ------------ the inner attribute doesn't annotate this function | help: perhaps you meant to use an outer attribute | LL - #![unix_sigpipe = "inherit"] LL + #[unix_sigpipe = "inherit"] | ``` Before: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: perhaps you meant to use an outer attribute | LL | #[unix_sigpipe = "inherit"] | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` CC rust-lang#89566.
Tweak suggestion span for outer attr and point at item following invalid inner attr After: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LL | LL | fn main() {} | ------------ the inner attribute doesn't annotate this function | help: perhaps you meant to use an outer attribute | LL - #![unix_sigpipe = "inherit"] LL + #[unix_sigpipe = "inherit"] | ``` Before: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: perhaps you meant to use an outer attribute | LL | #[unix_sigpipe = "inherit"] | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` CC rust-lang#89566.
Rollup merge of rust-lang#116868 - estebank:suggestion, r=petrochenkov Tweak suggestion span for outer attr and point at item following invalid inner attr After: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LL | LL | fn main() {} | ------------ the inner attribute doesn't annotate this function | help: perhaps you meant to use an outer attribute | LL - #![unix_sigpipe = "inherit"] LL + #[unix_sigpipe = "inherit"] | ``` Before: ``` error: `unix_sigpipe` attribute cannot be used at crate level --> $DIR/unix_sigpipe-crate.rs:2:1 | LL | #![unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: perhaps you meant to use an outer attribute | LL | #[unix_sigpipe = "inherit"] | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` CC rust-lang#89566.
…trochenkov Suppress unhelpful diagnostics for unresolved top level attributes Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them. Also fixes rust-lang#89566, fixes rust-lang#67107. r? `@petrochenkov`
…trochenkov Suppress unhelpful diagnostics for unresolved top level attributes Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them. Also fixes rust-lang#89566, fixes rust-lang#67107. r? ``@petrochenkov``
…trochenkov Suppress unhelpful diagnostics for unresolved top level attributes Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them. Also fixes rust-lang#89566, fixes rust-lang#67107. r? ```@petrochenkov```
…ochenkov Suppress unhelpful diagnostics for unresolved top level attributes Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them. Also fixes rust-lang#89566, fixes rust-lang#67107. r? `@petrochenkov`
Rollup merge of rust-lang#118533 - chenyukang:yukang-fix-118455, r=petrochenkov Suppress unhelpful diagnostics for unresolved top level attributes Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them. Also fixes rust-lang#89566, fixes rust-lang#67107. r? `@petrochenkov`
I was assisting someone new to Rust and they were confused by an error message. This is the complete file of code they had written:
Using rustc 1.55.0 (c8dfcfe04 2021-09-06) and rustc 1.57.0-nightly (f03eb6b 2021-10-02), the compiler outputs the following error (playground link):
The fix is to use
#[derive]
, an outer attribute, rather than#![derive]
, an inner attribute, but the compiler's error does not make that clear. And its suggestion to simplify macro imports doesn't help, as from the user's perspective, nothing is being imported in this source file!I think a more specific error message that checks whether the inner attribute's name would fit as an outer attribute would be helpful here. A suggestion to remove the
!
would be even better. Annoyingly, having#!
at the start of the file looked correct as it resembles a shebang.Note that there is a good error for using
#![]
instead of#[]
in the general case. It even mentions the difference between inner and outer attributes in its message. It was just bad luck that this particular struct happened to be at the very start of the source file, so that error was not displayed.I also spotted that in issue #67107, which has a very similar piece of example code, the compiler used to provide a suggestion to "try an outer attribute". But, running that example on the latest stable and nightly, it looks like that suggestion is not being produced anymore.
The text was updated successfully, but these errors were encountered: