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

Function pointer lifetimes #252

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions 0000-fn-lifetimes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
- Start Date: (fill me in with today's date, YYYY-MM-DD)
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary

The current `fn()` types represent something close to `&'static Code`, which means that safely representing a function pointer into code with a non-static lifetime safely is impossible. This RFC describes the addition of the ability to assign a non-static lifetime to the `fn()` type, which would allow `fn():'a` to represent a type similar to `&'a Code`.

# Motivation

This is useful in safely handling plugins, as any function pointer into the plugin could be guaranteed not to outlive the plugin itself. It's also necessary to ensure safety in instances where dynamic code generation is taking place, such as a JIT (just-in-time compilation) engine.

# Detailed design
The syntax for an `fn()` type with the lifetime 'a and return type R would be:
```
fn():'a -> R
```
This grammar would accept any lifetime. A function pointer is always contravariant with regard to its lifetime.
This makes the `fn():'a` type roughly equivalent to the `||:'a` type without an environment.
To maintain backwards compability and code cleanness, the current syntax would imply a 'static lifetime bound.

# Drawbacks

This may introduce further complexity into borrowck.

# Alternatives

Turn `fn()` into a quasi-unsized type, in such a way that the current `fn()` would represent `&'static fn()`.

# Unresolved questions

Should eliding the lifetime lead to it being inferred (as per the lifetime elision RFC) or default to 'static?
One proposal is to have `fn():` represent an elided lifetime, and `fn()` a static one.
29 changes: 0 additions & 29 deletions 0000-template.md

This file was deleted.