Customizable diagnostics for unsatisfied where bounds, such as HashMap::get's Borrow bound
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
use std::ffi::CStr;
use std::collections::HashMap;
fn main() {
let map: HashMap<&CStr, u32> = HashMap::new();
map.get("meow");
}
Current output
error[E0277]: the trait bound `&CStr: Borrow<str>` is not satisfied
--> src/main.rs:5:13
|
5 | map.get("meow");
| --- ^^^^^^ the trait `Borrow<str>` is not implemented for `&CStr`
| |
| required by a bound introduced by this call
|
note: required by a bound in `HashMap::<K, V, S>::get`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:911:12
|
909 | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
| --- required by a bound in this associated function
910 | where
911 | K: Borrow<Q>,
| ^^^^^^^^^ required by this bound in `HashMap::<K, V, S>::get`
For more information about this error, try `rustc --explain E0277`.
Desired output
error[E0277]: `&str` is not equivalent to `&CStr` and cannot be used to query a `HashMap<&CStr, u32>`
--> src/main.rs:5:13
|
5 | map.get("meow");
| --- ^^^^^^ `&str` cannot be used to query a `HashMap<&CStr, u32>`
| |
| required by a bound introduced by this call
|
help: the trait `Borrow<str>` is not implemented for `&CStr`
note: required by a bound in `HashMap::<K, V, S>::get`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:911:12
|
909 | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
| --- required by a bound in this associated function
910 | where
911 | K: Borrow<Q>,
| ^^^^^^^^^ required by this bound in `HashMap::<K, V, S>::get`
For more information about this error, try `rustc --explain E0277`.
Rationale and extra context
The Borrow trait, as used by various std collections, provides a lot of flexibility in how they can be used. However, the resulting diagnostics can be difficult to read at a glance even for seasoned developers.
For beginners, this can be even worse – I just talked to someone who spent 2 hours trying to figure out what's wrong with the code, because the error message was simply incomprehensible to them.
We already have diagnostic::on_unimplemented, as well as the more powerful rustc_on_unimplemented, which can be used to improve diagnostics in similar cases. However, the semantics of the Borrow trait are quite wide, and the actual function that's asking for an implementation of Borrow is in the best position to provide a legible explanation of what it represents.
Because of this, I'd like to propose an internal rustc attribute that could be used by std to improve this diagnostic:
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
where
#[rustc_on_unsatisfied(
message = "`&{Q}` is not equivalent to `{K}` and cannot be used to query a `{Self}`",
label = "`&{Q}` cannot be used to query a `{Self}`"
)]
K: Borrow<Q>,
Q: Hash + Eq,
Other cases
Rust Version
1.93.1 (current stable on playground)
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the HashMap::get where-clause in library/std/src/collections/hash/map.rs around line 911, then read the existing diagnostic::on_unimplemented and rustc_on_unimplemented mechanisms. Determine how a rustc_on_unsatisfied attribute could produce the proposed message and help text for the Borrow bound, and verify the resulting diagnostic against the supplied example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100