Skip to content

Lifetime of fn() converted to &Fn() isn't long enough #28546

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Lifetime of fn() converted to &Fn() isn't long enough #28546

eefriedman opened this issue Sep 20, 2015 · 5 comments
Labels
A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@eefriedman
Copy link
Contributor

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
Copy link
Contributor

arielb1 commented Sep 21, 2015

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

@steveklabnik steveklabnik added the A-lifetimes Area: Lifetimes / regions label Sep 28, 2015
@krdln
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 Relevant to the language team, which will review and decide on the PR/issue. label Mar 23, 2017
@nikomatsakis
Copy link
Contributor

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

@brson
Copy link
Contributor

brson commented Mar 23, 2017

Tracking issue #38865

@brson brson added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Mar 23, 2017
@nikomatsakis
Copy link
Contributor

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants