Conversation
This commit just removes the feature gate and moves the attribute to be stable. There haven't been any problems so far, it seems to work and the `#[diagnostic]` namespace guarantees that changes to this can happen at later points as well so there is no risk of stabilizing it.
|
Some changes occurred in compiler/rustc_attr_parsing cc @jdonszelmann, @JonathanBrouwer Some changes occurred to diagnostic attributes. cc @mejrs |
|
r? @chenyukang rustbot has assigned @chenyukang. Use Why was this reviewer chosen?The reviewer was selected based on:
|
There was a problem hiding this comment.
r? me
There are some outdated comments wrt the feature gating of this attribute, which need deleting
rust/compiler/rustc_resolve/src/imports.rs
Line 209 in 4ddbc06
rust/compiler/rustc_resolve/src/diagnostics/impls.rs
Lines 180 to 181 in 4ddbc06
What isn't stabilized
Describe any parts of the feature not being stabilized. Talk about what we might want to do later and what doors are being left open for that. If what we're not stabilizing might lead to surprises for users, talk about that in particular.
Any usage that also requires another unstable feature is not stabilized in so far that the other feature is required to write the code using the attribute in that location
Can you clarify that this applies to the crate root, where the attribute has meaning but it remains gated by custom_inner_attributes?
Stabilization report
Summary
This PR seeks to stabilize the
#[diagnostic::on_unknown]attribute in the#[diagnostic]namespace. This attribute let users change the error message emitted by rustc for the case that an referenced item is not found. This allows crate authors to provide custom error messages for specific use-cases to help their users to more easily understand how a certain feature works and why a specific code example doesn't compile.Tracking:
#[diagnostic::on_unknown]#152900Reference PRs:
Not sure who to really ping on stabilization. The
#[diagnostic]namespace RFC states:I'm not sure what's the current state of that delegation, I remember that it was planned to delegate it to the compiler team at some point.
What is stabilized
The
#[diagnostic::on_unknown]attribute is an attribute in the#[diagnostic]tool namespace. It follows the general rules of that namespace, so it can only affect compiler diagnostics, not change actual behaviour. Any syntax error on any of the attributes parameters will be emitted as warning and otherwise ignored. There is no guarantee by the compiler that a certain output is emitted even with the attribute being accepted, so future changes (including ignoring it) to this attribute are possible.The attribute should be
placed on use and module declarations as well as the crate root, though it is not an error to be located in other
positions.
Format parameters with the given named parameter will be replaced with the following text:
{Unresolved}— TheSimplePathSegmentof the import path that could not be resolved.{This}— The name of the annotated item. Onusestatements this is identical to{Unresolved}.The original error message will not be suppressed but is emitted as a note instead.
On use declarations
This will result in the following error:
On module declarations
This will result in the following error:
Example
Consider the following pair of macros, one which creates a hidden module with a constant and another that reads it.
If the
#[instrument]macro is omitted it will emit this confusing error:#[diagnostic::on_unknown]can be used to customize this error message:This produces:
Edition differences
In the 2015 edition, use paths are relative to the crate root. For example,
use emptywill be resolved relative to the crate root and resolution will not encounter themod empty {}declaration.What isn't stabilized
Any usage that also requires another unstable feature is not stabilized in so far that the other feature is required to write the code using the attribute in that location
Design
Reference
The reference needs a description of the attribute, basically what's removed from the unstable book and in the stabilization report above.
RFC history
This feature is part of the
#[diagnostic]tool attribute namespace, which was defined in https://rust-lang.github.io/rfcs/3368-diagnostic-attribute-namespace.html. The attribute itself was not specified by an RFC as the RFC explicitly allows adding new attributes to that namespace without RFC.Answers to unresolved questions
There are no unresolved questions I'm aware of
Post-RFC changes
Given that there was no RFC for this particular attribute, there are no changes here to discuss
Key points
The name of the attribute got some discussion. It started as
#[diagnostic::on_unknown_item]and was then renamed to the current name. Otherwise I think @petrochenkov raised the question if we really want to allow customizing all the compiler error messages like that. Other than that there were no major arguments that I'm aware of.Nightly extensions
There are none
Doors closed
Feedback
Call for testing
There was no call for testing as this is a rather specialized and somewhat simple feature
Nightly use
The compiler itself uses this feature in
rustc_span:rust/compiler/rustc_span/src/symbol.rs
Lines 2986 to 2989 in a5c10c3
I also did a small test implementation in Diesel, to demonstrate how this can be used to significantly improve error messages emitted by one of the derives diesel provides:
For the given code:
the following error message is emitted
the underlying problem here is that the proc-macro derive needs access to another user defined module. By default it assumes a name based on the struct name, users can customize that name via an attribute. In the past users often struggled with discovering that this attribute existed, so it is really helpful to be able to point to the attribute as part of the error message.
See the full change here:
weiznich/diesel@15d6442
Implementation
Major parts
This feature was implemented in the following PR's:
#[diagnostic::on_unknown]attribute #152901#[diagnostic::on_unknown]for modules. #157926Coverage
There is a good test coverage in the UI test suite:
https://github.com/rust-lang/rust/tree/a5c10c39125788787370a1ab11c954b9dff37859/tests/ui/diagnostic_namespace/on_unknown
Outstanding bugs
None that I'm aware of
Outstanding FIXMEs
None that I'm aware of
Tool changes
Breaking changes
No breaking change required
Type system, opsem
Compile-time checks
It's not possible to use this to trigger undefined behaviour, so no compile time checks preventing that exit
Type system rules
None, as this feature doesn't affect the type system
Sound by default?
No, it cannot be used to introduce undefined behaviour, it only changes compiler diagnostics
Breaks the AM?
No, it cannot be used to introduce undefined behaviour, it only changes compiler diagnostics
Common interactions
Temporaries
This feature doesn't introduce new expressions
Drop order
This feature doesn't change the generated code, so it cannot change the drop order
Pre-expansion / post-expansion
This feature doesn't change any expansion results, it just changes diagnostics.
Edition hygiene
It's not gated on an edition
SemVer implications
No, the
#[diagnostic]namespace even allows to use that feature without bumping the minimal supported rust version as it ignores (with a warning) unknown diagnostic attributes by defaultExposing other features
None that I'm aware of
History
#[diagnostic]namespace and#[diagnostic::on_unimplemented]attribute #119888#[diagnostic::on_unknown]attribute #152901#[diagnostic::on_unknown]for modules. #157926Acknowledgments
@mejrs helped a lot to implement various features here and to guide the initial implementation. @estebank motivated the original work
Open items
None that I'm aware of