Skip to content

Implementation of Index<Foo> prevents indexing arrays by other index types #35873

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

Open
ghost opened this issue Aug 21, 2016 · 3 comments
Open
Labels
A-array Area: `[T; N]` A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@ghost
Copy link

ghost commented Aug 21, 2016

use std::ops::Index;

enum Foo {
    Zero,
    One,
    Two,
}

impl Index<Foo> for [u8; 3] {
    type Output = u8;

    fn index(&self, index: Foo) -> &u8 {
        unsafe { self.get_unchecked(index as usize) }
    }
}

fn main() {
    let arr: [u8; 3] = [0, 1, 2];

    // OK:
    let _ = arr[Foo::Two];

    // Error:
    let _ = arr[2usize];
    //          ^^^^^^ expected enum `Foo`, found `usize`

    // Error:
    let _ = &arr[0usize..2];
    //           ^^^^^^^^^ expected enum `Foo`, found struct `std::ops::Range<usize>`

    // Error:
    let _ = &arr[..];
    //           ^^ expected enum `Foo`, found struct `std::ops::RangeFull`
}
@hanna-kruppe
Copy link
Contributor

see also #24066

@arielb1
Copy link
Contributor

arielb1 commented Aug 21, 2016

@rkruppe

Different problem. Here you are looking for the <[u8] as Index<RangeFull>> impl, which is reachable only through an unsizing coercion. Because your impl matches, the unsizing is skipped.

@hanna-kruppe
Copy link
Contributor

Who's "me"? This issue isn't filed by me.

But yes, it does seem to be a different cause on closer inspection, sorry for the noise.

@Mark-Simulacrum Mark-Simulacrum added A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 26, 2017
@ghost ghost changed the title Implementation of Index<Foo> "hides" the std implementation of Index<RangeFull> for arrays Implementation of Index<Foo> prevents indexing arrays by other index types Jan 31, 2020
@workingjubilee workingjubilee added the A-array Area: `[T; N]` label Mar 7, 2023
@fmease fmease added A-type-system Area: Type system T-types Relevant to the types team, which will review and decide on the PR/issue. and removed A-type-system Area: Type system labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-array Area: `[T; N]` A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants