From 8d75a8f699c921724b1e01ce932de77d08533957 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 18 Apr 2023 19:25:57 +0000 Subject: [PATCH] Test downstream errors from bad index expr --- tests/ui/typeck/bad-index-due-to-nested.rs | 30 +++++--- .../ui/typeck/bad-index-due-to-nested.stderr | 75 ++++++++++++++----- 2 files changed, 76 insertions(+), 29 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 { - node_index_map: HashMap, -} +struct HashMap(PhantomData<(K, V)>); -impl Graph { - 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 Index<&K> for HashMap +where + K: Hash, + V: Copy, +{ + type Output = V; + + fn index(&self, k: &K) -> &V { + todo!() } } +fn index<'a, K, V>(map: &'a HashMap, 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 ` as Index<&Q>>` - --> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL +note: required by a bound in ` as Index<&K>>` + --> $DIR/bad-index-due-to-nested.rs:9:8 + | +LL | K: Hash, + | ^^^^ required by this bound in ` as Index<&K>>` +help: consider restricting type parameter `K` + | +LL | fn index<'a, K: std::hash::Hash, V>(map: &'a HashMap, 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 ` as Index<&K>>` + --> $DIR/bad-index-due-to-nested.rs:10:8 + | +LL | V: Copy, + | ^^^^ required by this bound in ` as Index<&K>>` help: consider restricting type parameter `V` | -LL | impl Graph { - | ++++++++++++++ +LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap, 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: 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 ` as Index<&Q>>` - --> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL -help: consider restricting type parameter `V` - | -LL | impl Graph { - | +++++++++++++++++ + = 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: 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`.