summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/ui/typeck/bad-index-due-to-nested.rs30
-rw-r--r--tests/ui/typeck/bad-index-due-to-nested.stderr73
2 files changed, 75 insertions, 28 deletions
diff --git a/tests/ui/typeck/bad-index-due-to-nested.rs b/tests/ui/typeck/bad-index-due-to-nested.rs
index 8f3b97dc741..2564b530004 100644
--- a/tests/ui/typeck/bad-index-due-to-nested.rs
+++ b/tests/ui/typeck/bad-index-due-to-nested.rs
@@ -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<K, V> Index<&K> for HashMap<K, V>
+where
+ K: Hash,
+ V: Copy,
+{
+ type Output = 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
+ 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() {}
diff --git a/tests/ui/typeck/bad-index-due-to-nested.stderr b/tests/ui/typeck/bad-index-due-to-nested.stderr
index fdacba79396..e03b06b336e 100644
--- a/tests/ui/typeck/bad-index-due-to-nested.stderr
+++ b/tests/ui/typeck/bad-index-due-to-nested.stderr
@@ -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
-help: consider restricting type parameter `V`
+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 | impl<V: std::cmp::Eq> Graph<V> {
- | ++++++++++++++
+LL | fn index<'a, K: std::hash::Hash, V>(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[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`
|
-LL | self.node_index_map[&node]
- | ^^^^^^^^^^^^^^^^^^^ the trait `Hash` 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
|
-note: required by a bound in `<HashMap<K, V, S> as Index<&Q>>`
- --> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
+LL | V: Copy,
+ | ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>`
help: consider restricting type parameter `V`
|
-LL | impl<V: std::hash::Hash> Graph<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 2 previous errors
+error: aborting due to 4 previous errors
-For more information about this error, try `rustc --explain E0277`.
+Some errors have detailed explanations: E0277, E0308.
+For more information about an error, try `rustc --explain E0277`.