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 fn() converted to &Fn() isn't long enough #28546

Closed
eefriedman opened this Issue Sep 20, 2015 · 5 comments

Comments

Projects
None yet
6 participants
@eefriedman
Copy link
Contributor

eefriedman commented Sep 20, 2015

fn double(x: i32) -> i32 { x * 2 }
fn get_double_func() -> &'static (Fn(i32) -> i32)
{
    // Works
    const GET_DOUBLE_FUNC: &'static (Fn(i32) -> i32) = &double;
    GET_DOUBLE_FUNC
}
fn get_double_func_2() -> &'static (Fn(i32) -> i32)
{
    // Error message
    &double
}
fn main() {}

It would be nice if the lifetime of &double were the same in all contexts.

@arielb1

This comment has been minimized.

Copy link
Contributor

arielb1 commented Sep 21, 2015

An &'static Fn(i32) -> i32 is represented as a (shim vtable, pointer-to-pointer-to-function)

@krdln

This comment has been minimized.

Copy link
Contributor

krdln commented Oct 2, 2015

arielb: Note that double's type is fn(i32) -> i32 { double }, not just fn(i32) -> i32. In this case both the vtable and pointer-to-function are allocated in a static memory and the only change needed to fix that bug is making the lifetime 'static.

Casting arbitrary fn() function pointers to a &'static Fn() is a harder case, but it could be done too.

Discussion on reddit

@brson brson added the T-lang label Mar 23, 2017

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 23, 2017

Seems related to rust-lang/rfcs#1414. I agree it'd probably be nice for this to work.

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Mar 23, 2017

Tracking issue #38865

@brson brson added the C-enhancement label Mar 23, 2017

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 23, 2017

Closing in favor of #38865. I'll copy the example over there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.