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

Lifetime of function behaves differently in function pointer assignment and initialization #30456

Closed
palazzo-train opened this issue Dec 18, 2015 · 5 comments
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@palazzo-train
Copy link

Lifetimes of a function are treated differently between
Initialization

        let myFunctor: &Fn(i32) -> i32 = &fx ;

and Assignment

        let myFunctor: &Fn(i32) -> i32;
        myFunctor= &fx ;

See the below example

    fn fx(x: i32) -> i32
    {
        x
    }

    fn test_Functor()
    {
        // case 1. Initialization
        let myFunctor: &Fn(i32) -> i32 = &fx ;

        // case 2. Assignment
        //let myFunctor: &Fn(i32) -> i32;
        //myFunctor= &fx ;

    }

This can compile.
If I comment case 1 and uncomment case 2. I got an error

   Compiling study_rust v0.0.1 (main.rs)
src/main.rs:45:17: 45:19 error: borrowed value does not live long enough
src/main.rs:45     myFunctor= &fx ;
                               ^~
src/main.rs:44:36: 47:2 note: reference must be valid for the block suffix following statement 0 at 44:35...
src/main.rs:44     let myFunctor: &Fn(i32) -> i32;
src/main.rs:45     myFunctor= &fx ;
src/main.rs:46
src/main.rs:47 }
src/main.rs:45:5: 45:21 note: ...but borrowed value is only valid for the statement at 45:4
src/main.rs:45     myFunctor= &fx ;
                   ^~~~~~~~~~~~~~~~
src/main.rs:45:5: 45:21 help: consider using a `let` binding to increase its lifetime
src/main.rs:45     myFunctor= &fx ;
                   ^~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `study_rust`.

Is it a bug? I am using rustc 1.5.0

rustc 1.5.0 (3d7cd77e4 2015-12-04)
@steveklabnik steveklabnik added the A-lifetimes Area: Lifetimes / regions label Dec 18, 2015
@steveklabnik
Copy link
Member

/cc @rust-lang/lang

@bluss
Copy link
Member

bluss commented Dec 19, 2015

It's in general for references, for example this has the same lifetime error (playground)

fn main() {
    let s: &String;
    s = &"".to_string();
}

@nikomatsakis
Copy link
Contributor

Indeed, as @bluss suggests, this is fallout from our rules about the scope used for temporary values. In the case of a let initializer, the scope is the enclosing block, but in the assignment that is not the case. I would classify this as "not a bug" -- or, at least, obeying the rules as designed, for better or worse. One could imagine tweaking the temporary lifetime rules to better handle cases like these, but it'd be an RFC-worthy change I would think.

@Mark-Simulacrum
Copy link
Member

Should this be closed as not a bug? We'd need an RFC to change this, so at least should be moved to rust-lang/rfcs.

@aturon
Copy link
Member

aturon commented Apr 17, 2017

Closing; as the last two comments have noted, a change here merits an RFC.

@aturon aturon closed this as completed Apr 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

6 participants