Skip to content

Lifetime Pointer to Lifetime Closure Bug #12717

Closed
@bvssvni

Description

@bvssvni

I was told in the IRC channel the bug is Test::new missing a "'a" and should give the same warning as Test2::new.

rustc 0.10-pre (0a5138c 2014-03-04 13:16:41 -0800)

/// A struct with borrowed pointer to a closure.
/// This is to be able to reuse the closure then struct runs out of scope.

pub struct Test<'a> {
    f: &'a 'a ||
}

impl<'a> Test<'a> {
        // This should give the same warning as Test2.
    pub fn new(f: &'a ||) -> Test<'a> {
        Test { f: f }
    }
}

pub struct Test2<'a> {
    f: Option<&'a 'a ||>
}

impl<'a> Test2<'a> {
    pub fn new(f: &'a ||) -> Test2<'a> {
        Test2 { f: Some(f) }
    }
}

fn main() {
    let x = || println!("hello!");

    // Works
    (*Test::new(&x).f)();

    // Fails
    (*Test2::new(&x).f.unwrap())();
}


Compiler error:
optionclosure.rs:21:3: 21:23 error: cannot infer an appropriate lifetime for region in type/impl due to conflicting requirements
optionclosure.rs:21         Test2 { f: Some(f) }
                            ^~~~~~~~~~~~~~~~~~~~
optionclosure.rs:21:14: 21:21 note: first, the lifetime cannot outlive the call at 21:13...
optionclosure.rs:21         Test2 { f: Some(f) }
                                       ^~~~~~~
optionclosure.rs:21:14: 21:21 note: ...so that return value is valid for the call
optionclosure.rs:21         Test2 { f: Some(f) }
                                       ^~~~~~~
optionclosure.rs:20:37: 22:3 note: but, the lifetime must be valid for the lifetime &'a  as defined on the block at 20:36...
optionclosure.rs:20     pub fn new(f: &'a ||) -> Test2<'a> {
optionclosure.rs:21         Test2 { f: Some(f) }
optionclosure.rs:22     }
optionclosure.rs:21:3: 21:23 note: ...so that types are compatible (expected `Test2<'a>` but found `Test2<>`)
optionclosure.rs:21         Test2 { f: Some(f) }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions