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

Make autoreleasepool take the pool as a parameter #17

Merged
merged 7 commits into from
Sep 2, 2021

Conversation

madsmtm
Copy link
Owner

@madsmtm madsmtm commented Sep 2, 2021

The pool is given as a reference, and the lifetime of the reference is the same as the lifetime of autoreleased objects in the pool. This allows us to bound the lifetimes of autoreleased objects, thereby making them safe to return.

Replaces SSheldon/rust-objc#103. See SSheldon/rust-objc#95 for some more examples and the reasoning behind this.

The primary motivation is to make NSString::as_str safe, more info can be found in SSheldon/rust-objc#103 (comment).

I did try to explore the concept described in SSheldon/rust-objc#103 (comment) with making AutoReleaseSafe ~ Send + Sync on stable, but this proved too much of a usability issue, at least until it's more common to use custom objects (that would then be Send + Sync) rather than Object.

The lifetime of the parameter is the lifetime of references in the pool. This allows us to bound lifetimes on autoreleased objects, thereby making them safe to return.
A nightly-only feature that adds the auto trait `AutoreleaseSafe` and uses it to prevent calling `autoreleasepool` with closures that capture an outer `AutoreleasePool`.

This fixes on nightly the unsoundness hole preventing `autoreleasepool` from being used to safely construct references to autoreleased objects.
Prevent using the lifetime from an outer pool when an inner pool is active.
@madsmtm madsmtm added the enhancement New feature or request label Sep 2, 2021
@madsmtm madsmtm merged commit f04a951 into master Sep 2, 2021
@madsmtm madsmtm deleted the autoreleasepool-lifetime branch September 2, 2021 12:25
madsmtm added a commit that referenced this pull request Sep 2, 2021
Make `autoreleasepool` take the pool as a parameter
@madsmtm madsmtm mentioned this pull request Sep 5, 2021
80 tasks
@madsmtm madsmtm added this to the objc2 v0.3 milestone Apr 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant