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

Closure inference is destroyed by wrappers #29834

Closed
ebfull opened this issue Nov 14, 2015 · 8 comments
Closed

Closure inference is destroyed by wrappers #29834

ebfull opened this issue Nov 14, 2015 · 8 comments
Labels
A-closures Area: Closures (`|…| { … }`) A-type-system Area: Type system

Comments

@ebfull
Copy link
Contributor

ebfull commented Nov 14, 2015

This doesn't work:

struct Wrapper<F>(F);
struct Foo;

impl Foo {
    fn stuff(self) { }
}

fn api<F: Fn(Foo)>(f: Wrapper<F>) { }

fn main() {
    api(Wrapper(|b| {
        b.stuff();
    }))
}

yet without the wrapper it works just fine:

struct Foo;

impl Foo {
    fn stuff(self) { }
}

fn api<F: Fn(Foo)>(f: F) { }

fn main() {
    api(|b| {
        b.stuff();
    })
}
@steveklabnik steveklabnik added A-type-system Area: Type system A-closures Area: Closures (`|…| { … }`) labels Nov 14, 2015
@arielb1
Copy link
Contributor

arielb1 commented Nov 14, 2015

Overloading inference evaluates a function in visitor order, so this is expected.

@arielb1 arielb1 closed this as completed Nov 14, 2015
@arielb1
Copy link
Contributor

arielb1 commented Nov 14, 2015

I mean, even without closures:

struct Foo;

impl Foo {
    fn stuff(self) {}
}

fn compiles() {
    let mut x = None;
    loop {
        x = Some(Foo);
        if let Some(s) = x { s.stuff(); }
    }
}


fn does_not_compile() {
    let mut x = None;
    loop {
        if let Some(s) = x { s.stuff(); } //~ ERROR the type of this value must be known
        x = Some(Foo);
    }
}

fn main() {}

@ebfull
Copy link
Contributor Author

ebfull commented Nov 14, 2015

Can you reopen this? It merits more discussion I think.

edit: edited OP

@ebfull
Copy link
Contributor Author

ebfull commented Nov 14, 2015

cc @nikomatsakis

@ebfull
Copy link
Contributor Author

ebfull commented Nov 14, 2015

You can keep this issue closed as a duplicate of #25165.

@arielb1 arielb1 changed the title Missing closure inference Closure inference is destroyed by wrappers Nov 15, 2015
@arielb1
Copy link
Contributor

arielb1 commented Nov 15, 2015

@ebfull

Your second issue is different enough. Could you edit your OP to represent that?

From my investigation, the problem is that we don't relate the return type of Wrapper to the argument type of api, because we are afraid there might be a coercion involved. This means the bound information is not passed to the closure. We could try to be better here - cc @nikomatsakis @eddyb.

@arielb1 arielb1 reopened this Nov 15, 2015
@eddyb
Copy link
Member

eddyb commented Nov 15, 2015

@arielb1 I think that downward propagation is always supposed to happen, this looks like a missing case.

@arielb1
Copy link
Contributor

arielb1 commented Nov 22, 2015

Duplicate of #20841. Closing and nominating that one.

@arielb1 arielb1 closed this as completed Nov 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

4 participants