Skip to content

Commit 8d75a8f

Browse files
Test downstream errors from bad index expr
1 parent d84b5f9 commit 8d75a8f

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed
+21-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
use std::collections::HashMap;
1+
use std::hash::Hash;
2+
use std::marker::PhantomData;
3+
use std::ops::Index;
24

3-
pub struct Graph<V> {
4-
node_index_map: HashMap<V, usize>,
5-
}
5+
struct HashMap<K, V>(PhantomData<(K, V)>);
6+
7+
impl<K, V> Index<&K> for HashMap<K, V>
8+
where
9+
K: Hash,
10+
V: Copy,
11+
{
12+
type Output = V;
613

7-
impl<V> Graph<V> {
8-
pub fn node_index(&self, node: V) -> usize {
9-
self.node_index_map[&node]
10-
//~^ ERROR the trait bound `V: Eq` is not satisfied
11-
//~| ERROR the trait bound `V: Hash` is not satisfied
14+
fn index(&self, k: &K) -> &V {
15+
todo!()
1216
}
1317
}
1418

19+
fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V {
20+
map[k]
21+
//~^ ERROR the trait bound `K: Hash` is not satisfied
22+
//~| ERROR the trait bound `V: Copy` is not satisfied
23+
//~| ERROR mismatched types
24+
//~| ERROR mismatched types
25+
}
26+
1527
fn main() {}
+54-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,64 @@
1-
error[E0277]: the trait bound `V: Eq` is not satisfied
2-
--> $DIR/bad-index-due-to-nested.rs:9:9
1+
error[E0277]: the trait bound `K: Hash` is not satisfied
2+
--> $DIR/bad-index-due-to-nested.rs:20:5
33
|
4-
LL | self.node_index_map[&node]
5-
| ^^^^^^^^^^^^^^^^^^^ the trait `Eq` is not implemented for `V`
4+
LL | map[k]
5+
| ^^^ the trait `Hash` is not implemented for `K`
66
|
7-
note: required by a bound in `<HashMap<K, V, S> as Index<&Q>>`
8-
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
9-
help: consider restricting type parameter `V`
7+
note: required by a bound in `<HashMap<K, V> as Index<&K>>`
8+
--> $DIR/bad-index-due-to-nested.rs:9:8
9+
|
10+
LL | K: Hash,
11+
| ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>`
12+
help: consider restricting type parameter `K`
1013
|
11-
LL | impl<V: std::cmp::Eq> Graph<V> {
12-
| ++++++++++++++
14+
LL | fn index<'a, K: std::hash::Hash, V>(map: &'a HashMap<K, V>, k: K) -> &'a V {
15+
| +++++++++++++++++
1316

14-
error[E0277]: the trait bound `V: Hash` is not satisfied
15-
--> $DIR/bad-index-due-to-nested.rs:9:9
17+
error[E0277]: the trait bound `V: Copy` is not satisfied
18+
--> $DIR/bad-index-due-to-nested.rs:20:5
19+
|
20+
LL | map[k]
21+
| ^^^ the trait `Copy` is not implemented for `V`
1622
|
17-
LL | self.node_index_map[&node]
18-
| ^^^^^^^^^^^^^^^^^^^ the trait `Hash` is not implemented for `V`
23+
note: required by a bound in `<HashMap<K, V> as Index<&K>>`
24+
--> $DIR/bad-index-due-to-nested.rs:10:8
1925
|
20-
note: required by a bound in `<HashMap<K, V, S> as Index<&Q>>`
21-
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
26+
LL | V: Copy,
27+
| ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>`
2228
help: consider restricting type parameter `V`
2329
|
24-
LL | impl<V: std::hash::Hash> Graph<V> {
25-
| +++++++++++++++++
30+
LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap<K, V>, k: K) -> &'a V {
31+
| +++++++++++++++++++
32+
33+
error[E0308]: mismatched types
34+
--> $DIR/bad-index-due-to-nested.rs:20:9
35+
|
36+
LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V {
37+
| - this type parameter
38+
LL | map[k]
39+
| ^
40+
| |
41+
| expected `&K`, found type parameter `K`
42+
| help: consider borrowing here: `&k`
43+
|
44+
= note: expected reference `&K`
45+
found type parameter `K`
46+
47+
error[E0308]: mismatched types
48+
--> $DIR/bad-index-due-to-nested.rs:20:5
49+
|
50+
LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V {
51+
| - this type parameter ----- expected `&'a V` because of return type
52+
LL | map[k]
53+
| ^^^^^^
54+
| |
55+
| expected `&V`, found type parameter `V`
56+
| help: consider borrowing here: `&map[k]`
57+
|
58+
= note: expected reference `&'a V`
59+
found type parameter `V`
2660

27-
error: aborting due to 2 previous errors
61+
error: aborting due to 4 previous errors
2862

29-
For more information about this error, try `rustc --explain E0277`.
63+
Some errors have detailed explanations: E0277, E0308.
64+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)