Skip to content

rustc: Anonymous lifetimes within structs are refused too rigorously #12741

@Florob

Description

@Florob
Contributor

I have the following code:

#[deriving(Clone)]
pub struct Foo {
    priv f: fn(char, |char|)
}

impl Foo {
    fn bar(&self) {
        ((*self).f)('a', |c: char| { println!("{}", c); });
    }
}

fn bla(c: char, cb: |char|) {
    cb(c);
}

pub fn make_foo() -> Foo {
    Foo {
        f: bla
    }
}

fn main() {
    let a = make_foo();

    a.bar();
}

Which currently produces this error:

test.rs:3:10: 3:29 error: mismatched types: expected `fn(char, |char|)` but found `fn(char, |char|)` (expected concrete lifetime, but found bound lifetime parameter &)
test.rs:3     priv f: fn(char, |char|)
                   ^~~~~~~~~~~~~~~~~~~
note: expected concrete lifetime is lifetime ReInfer(ReVar(middle::ty::RegionVid{id: 22u}))
error: aborting due to previous error

This is apparently caused by the closure passed to the referenced function not having a lifetime.
IMHO this should be perfectly legal, because the struct does not actually reference the closure itself.

Activity

ghost added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Oct 30, 2014
nikomatsakis

nikomatsakis commented on Dec 2, 2014

@nikomatsakis
Contributor

Dup of #10501 -- this was never fixed, but the corresponding check in the code was buggy

added a commit that references this issue on May 5, 2024
2800251
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @alexcrichton@Florob@nikomatsakis

      Issue actions

        rustc: Anonymous lifetimes within structs are refused too rigorously · Issue #12741 · rust-lang/rust