Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 797351d

Browse files
committedOct 23, 2024
Stabilize #[diagnostic::do_not_recommend]
This commit seeks to stabilize the `#[diagnostic::do_not_recommend]` attribute. This attribute was first proposed as `#[do_not_recommend`] attribute in RFC 2397 (rust-lang/rfcs#2397). It gives the crate authors the ability to not suggest to the compiler to not show certain traits in it's error messages. With the presence of the `#[diagnostic]` tool attribute namespace it was decided to move the attribute there, as that lowers the amount of guarantees the compiler needs to give about the exact way this influences error messages. It turns the attribute into a hint which can be ignored. In addition to the original proposed functionality this attribute now also hides the marked trait in help messages ("This trait is implemented by: "). The attribute does not accept any argument and can only be placed on trait implementations. If it is placed somewhere else a lint warning is emitted and the attribute is otherwise ignored. If an argument is detected a lint warning is emitted and the argument is ignored. This follows the rules outlined by the diagnostic namespace. This attribute allows crates like diesel to improve their error messages drastically. The most common example here is the following error message: ``` error[E0277]: the trait bound `&str: Expression` is not satisfied --> /home/weiznich/Documents/rust/rust/tests/ui/diagnostic_namespace/do_not_recommend.rs:53:15 | LL | SelectInt.check("bar"); | ^^^^^ the trait `Expression` is not implemented for `&str`, which is required by `&str: AsExpression<Integer>` | = help: the following other types implement trait `Expression`: Bound<T> SelectInt note: required for `&str` to implement `AsExpression<Integer>` --> /home/weiznich/Documents/rust/rust/tests/ui/diagnostic_namespace/do_not_recommend.rs:26:13 | LL | impl<T, ST> AsExpression<ST> for T | ^^^^^^^^^^^^^^^^ ^ LL | where LL | T: Expression<SqlType = ST>, | ------------------------ unsatisfied trait bound introduced here ``` By applying the new attribute to the wild card trait implementation of `AsExpression` for `T: Expression` the error message becomes: ``` error[E0277]: the trait bound `&str: AsExpression<Integer>` is not satisfied --> $DIR/as_expression.rs:55:15 | LL | SelectInt.check("bar"); | ^^^^^ the trait `AsExpression<Integer>` is not implemented for `&str` | = help: the trait `AsExpression<Text>` is implemented for `&str` = help: for that trait implementation, expected `Text`, found `Integer` ``` which makes it much easier for users to understand that they are facing a type mismatch. Other explored example usages included * This standard library error message: #128008 * That bevy derived example: https://github.com/rust-lang/rust/blob/e1f306899514ea80abc1d1c9f6a57762afb304a3/tests/ui/diagnostic_namespace/do_not_recommend/supress_suggestions_in_help.rs (No more tuple pyramids) Fixes #51992
1 parent ea6eb85 commit 797351d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+55
-231
lines changed
 

‎tests/ui/diagnostic_namespace/do_not_recommend/feature-gate-do_not_recommend.rs

-20
This file was deleted.

‎tests/ui/diagnostic_namespace/do_not_recommend/incorrect-locations.current.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
2-
--> $DIR/incorrect-locations.rs:7:1
2+
--> $DIR/incorrect-locations.rs:6:1
33
|
44
LL | #[diagnostic::do_not_recommend]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
88

99
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
10-
--> $DIR/incorrect-locations.rs:11:1
10+
--> $DIR/incorrect-locations.rs:10:1
1111
|
1212
LL | #[diagnostic::do_not_recommend]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
16-
--> $DIR/incorrect-locations.rs:15:1
16+
--> $DIR/incorrect-locations.rs:14:1
1717
|
1818
LL | #[diagnostic::do_not_recommend]
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
22-
--> $DIR/incorrect-locations.rs:19:1
22+
--> $DIR/incorrect-locations.rs:18:1
2323
|
2424
LL | #[diagnostic::do_not_recommend]
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
28-
--> $DIR/incorrect-locations.rs:23:1
28+
--> $DIR/incorrect-locations.rs:22:1
2929
|
3030
LL | #[diagnostic::do_not_recommend]
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

3333
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
34-
--> $DIR/incorrect-locations.rs:27:1
34+
--> $DIR/incorrect-locations.rs:26:1
3535
|
3636
LL | #[diagnostic::do_not_recommend]
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
40-
--> $DIR/incorrect-locations.rs:31:1
40+
--> $DIR/incorrect-locations.rs:30:1
4141
|
4242
LL | #[diagnostic::do_not_recommend]
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

4545
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
46-
--> $DIR/incorrect-locations.rs:35:1
46+
--> $DIR/incorrect-locations.rs:34:1
4747
|
4848
LL | #[diagnostic::do_not_recommend]
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.