-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #110432 - compiler-errors:unsatisfied-index-impl, r=c…
…jgillot Report more detailed reason why `Index` impl is not satisfied Fixes #110373
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -204,6 +204,7 @@ symbols! { | |
HashSet, | ||
Hasher, | ||
Implied, | ||
IndexOutput, | ||
Input, | ||
Into, | ||
IntoDiagnostic, | ||
|
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
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,27 @@ | ||
use std::hash::Hash; | ||
use std::marker::PhantomData; | ||
use std::ops::Index; | ||
|
||
struct HashMap<K, V>(PhantomData<(K, V)>); | ||
|
||
impl<K, V> Index<&K> for HashMap<K, V> | ||
where | ||
K: Hash, | ||
V: Copy, | ||
{ | ||
type Output = V; | ||
|
||
fn index(&self, k: &K) -> &V { | ||
todo!() | ||
} | ||
} | ||
|
||
fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V { | ||
map[k] | ||
//~^ ERROR the trait bound `K: Hash` is not satisfied | ||
//~| ERROR the trait bound `V: Copy` is not satisfied | ||
//~| ERROR mismatched types | ||
//~| ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
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,64 @@ | ||
error[E0277]: the trait bound `K: Hash` is not satisfied | ||
--> $DIR/bad-index-due-to-nested.rs:20:5 | ||
| | ||
LL | map[k] | ||
| ^^^ the trait `Hash` is not implemented for `K` | ||
| | ||
note: required by a bound in `<HashMap<K, V> as Index<&K>>` | ||
--> $DIR/bad-index-due-to-nested.rs:9:8 | ||
| | ||
LL | K: Hash, | ||
| ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>` | ||
help: consider restricting type parameter `K` | ||
| | ||
LL | fn index<'a, K: std::hash::Hash, V>(map: &'a HashMap<K, V>, k: K) -> &'a V { | ||
| +++++++++++++++++ | ||
|
||
error[E0277]: the trait bound `V: Copy` is not satisfied | ||
--> $DIR/bad-index-due-to-nested.rs:20:5 | ||
| | ||
LL | map[k] | ||
| ^^^ the trait `Copy` is not implemented for `V` | ||
| | ||
note: required by a bound in `<HashMap<K, V> as Index<&K>>` | ||
--> $DIR/bad-index-due-to-nested.rs:10:8 | ||
| | ||
LL | V: Copy, | ||
| ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>` | ||
help: consider restricting type parameter `V` | ||
| | ||
LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap<K, V>, k: K) -> &'a V { | ||
| +++++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/bad-index-due-to-nested.rs:20:9 | ||
| | ||
LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V { | ||
| - this type parameter | ||
LL | map[k] | ||
| ^ | ||
| | | ||
| expected `&K`, found type parameter `K` | ||
| help: consider borrowing here: `&k` | ||
| | ||
= note: expected reference `&K` | ||
found type parameter `K` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/bad-index-due-to-nested.rs:20:5 | ||
| | ||
LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V { | ||
| - this type parameter ----- expected `&'a V` because of return type | ||
LL | map[k] | ||
| ^^^^^^ | ||
| | | ||
| expected `&V`, found type parameter `V` | ||
| help: consider borrowing here: `&map[k]` | ||
| | ||
= note: expected reference `&'a V` | ||
found type parameter `V` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0308. | ||
For more information about an error, try `rustc --explain E0277`. |