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

fix: More accurate lifetime bounds on scoped methods to allow valid code to compile #919

Merged
merged 1 commit into from
Jul 22, 2022

Conversation

kjvalencik
Copy link
Member

Prior to this change, it was impossible to escape a value in execute_scoped that was created in an outer scope. This is because the
function definition allowed for the outer and inner lifetimes to be entirely unrelated.

This is technically a breaking change, but in many cases the more strict bound will cause code to be more likely to compile.

The following code compiles after this change, but not before:

fn example(mut cx: FunctionContext) -> JsResult<JsValue> {
    let value = cx.argument::<JsValue>(0)?;

    cx.compute_scoped(move |_| Ok(value))
}

…ode to compile

Prior to this change, it was impossible to escape a value in `execute_scoped` that was created in an outer scope. This is because the
function definition allowed for the outer and inner lifetimes to be entirely unrelated.

This is technically a breaking change, but in many cases the more strict bound will cause code to be more likely to compile.
env: Env,
phantom_inner: PhantomData<&'a ()>,
phantom_outer: PhantomData<&'outer ()>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was required before to ensure that ComputeContext couldn't accidentally outlive the outer context. However, compute_scoped now includes a 'a: 'b bound that provides the same guarantee.

@@ -242,9 +242,10 @@ pub trait Context<'a>: ContextInternal<'a> {
/// Handles created in the new scope are kept alive only for the duration of the computation and cannot escape.
///
/// This method can be useful for limiting the life of temporary values created during long-running computations, to prevent leaks.
fn execute_scoped<T, F>(&mut self, f: F) -> T
fn execute_scoped<'b, T, F>(&mut self, f: F) -> T
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't provide as much tangible value, but I thought it was good to keep them aligned.

Copy link
Collaborator

@dherman dherman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's not a huge deal but the fact that you were able to eliminate the extra phantom lifetime feels like an affirmation that it's a cleanup of the previous signature.

@kjvalencik kjvalencik merged commit c6f75a7 into main Jul 22, 2022
@kjvalencik kjvalencik deleted the kv/scoped-lifetimes branch July 22, 2022 19:52
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

Successfully merging this pull request may close these issues.

2 participants