mirror of https://github.com/rust-lang/rust.git
Test downstream errors from bad index expr
This commit is contained in:
parent
d84b5f9b3d
commit
8d75a8f699
|
@ -1,15 +1,27 @@
|
||||||
use std::collections::HashMap;
|
use std::hash::Hash;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::ops::Index;
|
||||||
|
|
||||||
pub struct Graph<V> {
|
struct HashMap<K, V>(PhantomData<(K, V)>);
|
||||||
node_index_map: HashMap<V, usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V> Graph<V> {
|
impl<K, V> Index<&K> for HashMap<K, V>
|
||||||
pub fn node_index(&self, node: V) -> usize {
|
where
|
||||||
self.node_index_map[&node]
|
K: Hash,
|
||||||
//~^ ERROR the trait bound `V: Eq` is not satisfied
|
V: Copy,
|
||||||
//~| ERROR the trait bound `V: Hash` is not satisfied
|
{
|
||||||
|
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() {}
|
fn main() {}
|
||||||
|
|
|
@ -1,29 +1,64 @@
|
||||||
error[E0277]: the trait bound `V: Eq` is not satisfied
|
error[E0277]: the trait bound `K: Hash` is not satisfied
|
||||||
--> $DIR/bad-index-due-to-nested.rs:9:9
|
--> $DIR/bad-index-due-to-nested.rs:20:5
|
||||||
|
|
|
|
||||||
LL | self.node_index_map[&node]
|
LL | map[k]
|
||||||
| ^^^^^^^^^^^^^^^^^^^ the trait `Eq` is not implemented for `V`
|
| ^^^ the trait `Hash` is not implemented for `K`
|
||||||
|
|
|
|
||||||
note: required by a bound in `<HashMap<K, V, S> as Index<&Q>>`
|
note: required by a bound in `<HashMap<K, V> as Index<&K>>`
|
||||||
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
|
--> $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`
|
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
|
error[E0308]: mismatched types
|
||||||
--> $DIR/bad-index-due-to-nested.rs:9:9
|
--> $DIR/bad-index-due-to-nested.rs:20:9
|
||||||
|
|
|
|
||||||
LL | self.node_index_map[&node]
|
LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V {
|
||||||
| ^^^^^^^^^^^^^^^^^^^ the trait `Hash` is not implemented for `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>>`
|
= note: expected reference `&K`
|
||||||
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
|
found type parameter `K`
|
||||||
help: consider restricting type parameter `V`
|
|
||||||
|
|
|
||||||
LL | impl<V: std::hash::Hash> Graph<V> {
|
|
||||||
| +++++++++++++++++
|
|
||||||
|
|
||||||
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`.
|
||||||
|
|
Loading…
Reference in New Issue