Test downstream errors from bad index expr

This commit is contained in:
Michael Goulet 2023-04-18 19:25:57 +00:00
parent d84b5f9b3d
commit 8d75a8f699
2 changed files with 76 additions and 29 deletions

View file

@ -1,15 +1,27 @@
use std::collections::HashMap;
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::Index;
pub struct Graph<V> {
node_index_map: HashMap<V, usize>,
}
struct HashMap<K, V>(PhantomData<(K, V)>);
impl<V> Graph<V> {
pub fn node_index(&self, node: V) -> usize {
self.node_index_map[&node]
//~^ ERROR the trait bound `V: Eq` is not satisfied
//~| ERROR the trait bound `V: Hash` is not satisfied
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() {}

View file

@ -1,29 +1,64 @@
error[E0277]: the trait bound `V: Eq` is not satisfied
--> $DIR/bad-index-due-to-nested.rs:9:9
error[E0277]: the trait bound `K: Hash` is not satisfied
--> $DIR/bad-index-due-to-nested.rs:20:5
|
LL | self.node_index_map[&node]
| ^^^^^^^^^^^^^^^^^^^ the trait `Eq` is not implemented for `V`
LL | map[k]
| ^^^ the trait `Hash` is not implemented for `K`
|
note: required by a bound in `<HashMap<K, V, S> as Index<&Q>>`
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
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 | impl<V: std::cmp::Eq> Graph<V> {
| ++++++++++++++
LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap<K, V>, k: K) -> &'a V {
| +++++++++++++++++++
error[E0277]: the trait bound `V: Hash` is not satisfied
--> $DIR/bad-index-due-to-nested.rs:9:9
error[E0308]: mismatched types
--> $DIR/bad-index-due-to-nested.rs:20:9
|
LL | self.node_index_map[&node]
| ^^^^^^^^^^^^^^^^^^^ the trait `Hash` is not implemented for `V`
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: required by a bound in `<HashMap<K, V, S> as Index<&Q>>`
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
help: consider restricting type parameter `V`
|
LL | impl<V: std::hash::Hash> Graph<V> {
| +++++++++++++++++
= note: expected reference `&K`
found type parameter `K`
error: aborting due to 2 previous errors
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`
For more information about this error, try `rustc --explain E0277`.
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.