Skip to content

Indexing via [v] does not cast fn items to fn pointers #40085

@aidanhs

Description

@aidanhs
use std::ops::Index;
fn bar() {}
static UNIT: () = ();
struct S;
impl Index<fn()> for S {
    type Output = ();
    fn index(&self, f: fn()) -> &() { f(); &UNIT }
}
fn main() {
    *S.index(bar);
    // S[bar];
    // ^^^^^^ expected fn pointer, found fn item
}

https://is.gd/R97Dc7

The docs for Index (https://doc.rust-lang.org/std/ops/trait.Index.html) say:

container[index] is actually syntactic sugar for *container.index(index)

but the error above indicates that this isn't true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions