Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message E0608 when the type supports Index but Idx is missing trait bounds #110373

Closed
Zannick opened this issue Apr 15, 2023 · 2 comments · Fixed by #110432
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Zannick
Copy link

Zannick commented Apr 15, 2023

I tried this code (playground):

use std::collections::HashMap;
use std::hash::Hash;

pub struct Edge<E> {
    id: E,
    src: usize,
    dst: usize,
    wt: u64
}

pub struct Graph<V, E> {
    nodes: Vec<V>,
    node_index_map: HashMap<V, usize>,
    edges: Vec<Edge<E>>,
}

pub fn build_graph<V, E>(v: V, e: E) -> Graph<V, E>
where V: Copy + Eq + Hash, E: Eq + Hash {
    let mut node_index_map = HashMap::new();
    let nodes = vec![v];
    node_index_map.insert(v, 0);
    let edges = vec![Edge { id: e, src: node_index_map[&v], dst: 0, wt: 1}];
    Graph {
        nodes,
        node_index_map,
        edges,
    }
}

impl<V, E> Graph<V, E> {
    pub fn node_index(&self, node: V) -> usize {
        self.node_index_map[&node]  // produces an error
    }
}

I expected to see this happen: rustc outputs an error saying that I need to add the Eq and std::hash::Hash traits on V.

Instead, this happened:

error[E0608]: cannot index into a value of type HashMap<V, usize>

which is jarring to see, and unhelpful, because HashMap does support Index, but only with particular trait bounds on V.

@Zannick Zannick added the C-bug Category: This is a bug. label Apr 15, 2023
@Ezrashaw
Copy link
Contributor

The reduced case:

use core::ops::Index;

trait Trait {}
struct Container;

impl<T: Trait> Index<T> for Container {
    type Output = T;

    fn index(&self, idx: T) -> &Self::Output {
        &idx
    }
}

fn main() {
    struct Containee;

    Container[Containee];
}

works as expected, perhaps it is caused when the Index implementer is not from the local crate?

@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Apr 16, 2023
@compiler-errors
Copy link
Member

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants