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

Returned lifetimes are too restrictive. #1

Closed
Dirbaio opened this issue Mar 18, 2024 · 4 comments
Closed

Returned lifetimes are too restrictive. #1

Dirbaio opened this issue Mar 18, 2024 · 4 comments

Comments

@Dirbaio
Copy link
Owner

Dirbaio commented Mar 18, 2024

If you call a method on a Local<'env, Foo>, it gets borrowed for a new shorter lifetime 'short, and deref'd to &'short Foo. The method then returns a Local<'short, Bar>.

This is unnecessarily restrictive, it could return a Local<'env, Bar>, Returning Local<'short, Bar> is annoying, for example it prevents moving or destroying the original Foo while the Bar is live.

I have prototyped a possible solution using arbitrary self types. Methods take self: Ref<'env, Self> instead of &self, so they can "see" the original 'env lifetime and return Local<'env>. Seems it works nicely so far. See branch: https://github.com/Dirbaio/java-spaghetti/tree/receiver

There's a newer RFC for arbitrary self types v2, hopefully there's a push to stabilize it soon. rust-lang/rfcs#3519

I don't know if there's a reasonably ergonomic way of solving this without arbitrary self types. Solutions are welcome.

@Dirbaio
Copy link
Owner Author

Dirbaio commented Jan 31, 2025

Updated receiver branch to use arbitrary self types v2. Works pretty great!

@wuwbobo2021
Copy link
Contributor

wuwbobo2021 commented Jan 31, 2025

You have pointed out that the Deref implementation for Local<'env, T> is unsound in release version 0.2.0: "this is unsound because Ref can be cloned/copied. You can get a Ref out of the Local, copy it, drop the Local, then use-after-free through the Ref." Thank you very much!

It seems like your solution for this problem requires the Rust "arbitrary_self_types" feature, hopefully this will be stabilized in Rust 1.86 (I'm not sure): rust-lang/rust#135881

@Dirbaio
Copy link
Owner Author

Dirbaio commented Feb 2, 2025

yep! I actually didn't realize it also affects the main branch. Maybe the same fix (make Ref not Copy/Clone, make everything take &Ref instead of Ref) should work on main, but the ObjectAndEnv hack makes it much more annoying.

@Dirbaio
Copy link
Owner Author

Dirbaio commented Feb 7, 2025

i've merged receiver into main, so this is now fixed.

Not sure whether to wait until arbitrary_self_types is stable for a release or what

@Dirbaio Dirbaio closed this as completed Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants