-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[RFC] On_unimplemented_trait_use #3643
Open
B-2U
wants to merge
6
commits into
rust-lang:master
Choose a base branch
from
B-2U:on_unimplemented_trait_use
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f68ed1c
Motivation
B-2U 11d7436
Reference-level explanation
B-2U 463a7bb
rephrase
B-2U 5d7422a
Bring 0000-template.md back
B-2U 57a2752
put it into the right place
B-2U 2c59581
Unresolved questions: syntax for generic traits
B-2U File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
text/0000-custom-error-message-on-unimplemented-trait-use.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
- Feature Name: `on_unimplemented_trait_use` | ||
- Start Date: 2024-05-22 | ||
- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000) | ||
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) | ||
|
||
# Summary | ||
[summary]: #summary | ||
|
||
Add `[diagnostic::on_unimplemented_trait_use]` in `#[diagnostic]` on structs that will influence error messages emitted by unsatisfied traits bounds. | ||
|
||
# Motivation | ||
[motivation]: #motivation | ||
|
||
The idea came about when I was trying to print out a PathBuf, there's a custom message that said: | ||
>in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead | ||
call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data | ||
|
||
And found out its hardcoded in trait `Display` | ||
```rust | ||
#[rustc_on_unimplemented( | ||
on( | ||
any(_Self = "std::path::Path", _Self = "std::path::PathBuf"), | ||
label = "`{Self}` cannot be formatted with the default formatter; call `.display()` on it", | ||
note = "call `.display()` or `.to_string_lossy()` to safely print paths, \ | ||
as they may contain non-Unicode data" | ||
), | ||
message = "`{Self}` doesn't implement `{Display}`", | ||
label = "`{Self}` cannot be formatted with the default formatter", | ||
note = "in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead" | ||
)] | ||
pub trait Display {...} | ||
``` | ||
It would be nice if this functionality is exposed to libraries as well, so that when the user tries to use an unimplemented trait (e.g. maybe Display isn't implemented because it's insufficient to clearly express intentions) the author can explain why via this diagnostic and offer a recommendation/alternative. | ||
|
||
For example: | ||
```rust | ||
#[diagnostic::on_unimplemented_trait_use( | ||
trait = Display, | ||
message = "`{Self}` doesn't implement `{Display}`", | ||
label = "`{Self}` cannot be formatted with the default formatter; call `.display()` on it", | ||
note = "call `.display()` or `.to_string_lossy()` to safely print paths, \ | ||
as they may contain non-Unicode data" | ||
)] | ||
struct PathBuf; | ||
```` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the syntax when the trait is generic? e.g.
std::ops::Index<T>
forstr
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tbh, I'm not sure. Let me add it into the Unresolved questions