Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Closed
aidanhs opened this issue Feb 24, 2017 · 2 comments
Closed

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

aidanhs opened this issue Feb 24, 2017 · 2 comments

Comments

@aidanhs
Copy link
Member

aidanhs commented Feb 24, 2017

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.

@est31
Copy link
Member

est31 commented Feb 25, 2017

Seems its not doing the coercion. This works:

    let b :fn() = bar;
    S[b];

@aidanhs
Copy link
Member Author

aidanhs commented Feb 25, 2017

Suspect it has something to do with this line - https://github.com/rust-lang/rust/blob/1.15.1/src/librustc_typeck/check/mod.rs#L3978

Going to try and work on a patch.

aidanhs added a commit to aidanhs/rust that referenced this issue Mar 1, 2017
frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 2, 2017
…tsakis

Allow types passed to [] to coerce, like .index()

Fixes rust-lang#40085

Basically steals the relevant part of [check_argument_types](https://github.com/rust-lang/rust/blob/1.15.1/src/librustc_typeck/check/mod.rs#L2653-L2672).
frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 2, 2017
…tsakis

Allow types passed to [] to coerce, like .index()

Fixes rust-lang#40085

Basically steals the relevant part of [check_argument_types](https://github.com/rust-lang/rust/blob/1.15.1/src/librustc_typeck/check/mod.rs#L2653-L2672).
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants