[E0277] Add Note when trait is not satisfied because of missing reference in where
When "where T: TraitX" is missing an "&" the error message E0277 could be improved with a note.
I'm also not sure why the same looking code (fn a<B>(b: &B) where B: TraitB;) inside a trait compiles just fine, maybe there is more to it.
I tried this code:
trait TraitA { fn a<B>(b: &B) where B: TraitB; }
struct StructA { }
impl TraitA for StructA { fn a<B>(b: &B) where B: TraitB { } }
trait TraitB { fn b(); }
struct StructB { }
impl TraitB for StructB { fn b() { } }
// (should be:) fn c<'a, B: 'a>(b: &'a B) where &'a B: TraitB
fn c<B>(b: &B) where B: TraitB {
StructA::a(&b);
}
I expected to see this happen (or similar):
[...]
= note: Try "where &'a B: TraitB" instead of "where B: TraitB"
Instead, this happened:
error[E0277]: the trait bound `&B: TraitB` is not satisfied
--> src/lib.rs:20:9
|
20 | StructA::a(&b);
| ^^^^^^^^^^ the trait `TraitB` is not implemented for `&B`
|
= note: required by `TraitA::a`
Meta
rustc --version --verbose:
rustc 1.24.0-nightly (9fe7aa353 2017-12-11)
binary: rustc
commit-hash: 9fe7aa353fac5084d0a44d6a15970310e9be67f4
commit-date: 2017-12-11
host: x86_64-unknown-linux-gnu
release: 1.24.0-nightly
LLVM version: 4.0
Current output:
error[E0277]: the trait bound `&B: TraitB` is not satisfied --> src/lib.rs:11:20 | 1 | trait TraitA { fn a<B>(b: &B) where B: TraitB; } | ------------------------------- required by `TraitA::a` ... 11 | StructA::a(&b); | -^ | | | the trait `TraitB` is not implemented for `&B` | help: consider removing 1 leading `&`-referencesI don't think we'll be able to suggest changing the
wherebounds in that way because we have to rely on the presumption that some part of the code is correct, and traits are usually the source of truth that you can't modify, but we could maybe have extra suggestions fortraits that come from the same crate/module as the code with the error...Esteban Kuber at 2020-01-22 19:36:24
Current output:
error[[E0277]](https://doc.rust-lang.org/stable/error_codes/E0277.html): the trait bound `&B: TraitB` is not satisfied --> src/lib.rs:11:20 | 11 | StructA::a(&b); | ---------- ^^ the trait `TraitB` is not implemented for `&B` | | | required by a bound introduced by this call | note: required by a bound in `TraitA::a` --> src/lib.rs:1:40 | 1 | trait TraitA { fn a<B>(b: &B) where B: TraitB; } | ^^^^^^ required by this bound in `TraitA::a` help: consider removing the leading `&`-reference | 11 - StructA::a(&b); 11 + StructA::a(b); |Dylan DPC at 2023-06-23 17:59:13