Improve consistency of attribute error messages#159755
Conversation
|
Some changes occurred in compiler/rustc_attr_parsing cc @jdonszelmann, @JonathanBrouwer Some changes occurred to diagnostic attributes. cc @mejrs |
|
Some more details: there is tremendous inconsistency in how attributes are referred to in error messages. In
Furthermore, carets sometimes point at the full attribute (including the Often the error line says something that doesn't match the actual source code pointed to by the carets. Examples: I propose the following:
This covers the vast majority of cases. There will be some exceptions, e.g. when suggesting what to write: or when the delimiters are significant: These cases are rare. |
This comment has been minimized.
This comment has been minimized.
aaef4d8 to
d8b48a3
Compare
In case it's relevant: the Rust reference mostly uses this form, e.g. "the |
This comment has been minimized.
This comment has been minimized.
d8b48a3 to
1eb0ce9
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
I have been mildly annoyed by these inconsistencies before, this is much better 👍
Can you also add this to the diagnostic style guide? https://rustc-dev-guide.rust-lang.org/diagnostics.html#diagnostic-output-style-guide
1eb0ce9 to
41a538b
Compare
Happy to, if/when this gets approval from reviewers :) |
This comment has been minimized.
This comment has been minimized.
| // If the span is the full attribute (including the `#[`/`]` delimiters) shrink it to | ||
| // exclude those delimiters, because that's what we want in error messages. | ||
| if span == self.attr_span { | ||
| span = self.inner_span; |
41a538b to
f5283e1
Compare
|
|
|
I added an extra commit with a style guide addition. |
This comment has been minimized.
This comment has been minimized.
These changes involve the error messages produced in `check_attribute_stability`, plus some related ones. The basic idea: - Use "the `foo` attribute" in `error` lines as much as possible, where `foo` is the attribute's path. - Point carets at just the attribute's path, to match the `error` line.
Mostly in a fashion similar to the previous commit. There is one exceptional case: for errors involving `repr` the argument is significant. For these, the argument is included in the error line and the caret highlighting, e.g. ``` error: the `repr(C)` attribute cannot be used on functions --> $DIR/naked-with-invalid-repr-attr.rs:10:3 | LL | #[repr(C)] | ^^^^^^^ | = help: the `repr(C)` attribute can only be applied to data types ``` Contrast this with a more typical argument-excluding case used everywhere else: ``` error: the `rustc_abi` attribute cannot be used on associated consts --> $DIR/debug.rs:46:7 | LL | #[rustc_abi(debug)] | ^^^^^^^^^ | = help: the `rustc_abi` attribute can be applied to functions and type aliases ```
Similar to the previous two commits.
Similar to the previous three commits.
37ed28c to
cd8a779
Compare
|
@bors r=estebank |
Rollup of 14 pull requests Successful merges: - #159765 (Avoid spurious rebuilds of JSON docs in bootstrap) - #159781 (Update bootstrap to use -Zembed-metadata=no instead of -Zno-embed-metadata) - #158362 (trait solver: account for universes from replace_bound_vars) - #158372 (rustfmt: Discover modules via `cfg_select!`) - #159173 (Add allowed list check on EII implementations attributes) - #159718 (Make `DocLinkResMap` an `FxIndexMap`) - #159722 ( [rustdoc] Retrieve `cfg_attr` information for derived impls for `doc_cfg` feature) - #159731 (std: Implement futex on wasip3 targets, update target spec) - #159755 (Improve consistency of attribute error messages) - #155795 (constify `vec![1, 2, 3]` macro) - #157776 (ci: Enable autodiff tests on x86_64 linux) - #158766 (Promote riscv64-unknown-linux-musl to tier 2 with host tools) - #159271 (str: add ASCII fast path to word_to_titlecase) - #159667 (Make some parser structured suggestions verbose and tweak their wording)
Rollup merge of #159755 - nnethercote:attribute-error-messages, r=estebank Improve consistency of attribute error messages This PR aims to improve how attributes are referred to in error messages. A lot of test outputs will change, so there will be follow-ups, but this PR has enough changes to cover a lot of cases and give a good feel for where things are headed. r? @estebank
Rollup of 14 pull requests Successful merges: - rust-lang/rust#159765 (Avoid spurious rebuilds of JSON docs in bootstrap) - rust-lang/rust#159781 (Update bootstrap to use -Zembed-metadata=no instead of -Zno-embed-metadata) - rust-lang/rust#158362 (trait solver: account for universes from replace_bound_vars) - rust-lang/rust#158372 (rustfmt: Discover modules via `cfg_select!`) - rust-lang/rust#159173 (Add allowed list check on EII implementations attributes) - rust-lang/rust#159718 (Make `DocLinkResMap` an `FxIndexMap`) - rust-lang/rust#159722 ( [rustdoc] Retrieve `cfg_attr` information for derived impls for `doc_cfg` feature) - rust-lang/rust#159731 (std: Implement futex on wasip3 targets, update target spec) - rust-lang/rust#159755 (Improve consistency of attribute error messages) - rust-lang/rust#155795 (constify `vec![1, 2, 3]` macro) - rust-lang/rust#157776 (ci: Enable autodiff tests on x86_64 linux) - rust-lang/rust#158766 (Promote riscv64-unknown-linux-musl to tier 2 with host tools) - rust-lang/rust#159271 (str: add ASCII fast path to word_to_titlecase) - rust-lang/rust#159667 (Make some parser structured suggestions verbose and tweak their wording)
This PR aims to improve how attributes are referred to in error messages. A lot of test outputs will change, so there will be follow-ups, but this PR has enough changes to cover a lot of cases and give a good feel for where things are headed.
r? @estebank