Skip to content

MutableHandle::new should be pub(crate) #552

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

Closed
gterzian opened this issue Jan 22, 2025 · 3 comments · Fixed by #557
Closed

MutableHandle::new should be pub(crate) #552

gterzian opened this issue Jan 22, 2025 · 3 comments · Fixed by #557

Comments

@gterzian
Copy link
Member

pub(crate) fn new(ptr: &'a T) -> Self {

is private to this crate, but

pub fn new(ptr: &'a mut T) -> Self {

is not. I assume both should be private to this crate(In both cases one should not directly call new, but rather use set on a newly rooted undefined value?).

@jdm
Copy link
Member

jdm commented Jan 23, 2025

Yep, that looks like the right fix to me.

gmorenz added a commit to gmorenz/mozjs that referenced this issue Mar 4, 2025
github-merge-queue bot pushed a commit that referenced this issue Mar 4, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Fixes #552

Signed-off-by: Greg Morenz <[email protected]>
@jdm jdm closed this as completed in #557 Mar 4, 2025
@simonwuelker
Copy link
Contributor

simonwuelker commented Apr 8, 2025

I don't understand this issue and I don't understand the fix. Please explain it to me.

I assume both should be private to this crate(In both cases one should not directly call new, but rather use set on a newly rooted undefined value?).

How would you use MutableHandle::set to replace the constructor functions?

Is this about some soundness issue that I'm not seeing?

For context:
I have a function that takes an out-parameter T and initializes it. T is gc-managed, the function can cause a garbage collection. When calling it, I sometimes have a &mut T and sometimes a RootedGuard<T>. I can't give the function a &mut T, that would be unsound because of the gc. So I would like to pass it a MutableHandle and then use MutableHandle::as_mut when I actually want to modify the value, avoiding the gc issue by creating a short-lived borrow. But that's impossible now because MutableHandle::new is not public anymore.

edit: Actually I suppose calling the function while I have &mut T is already unsound. So I would need something like pin_project for handles...

@sagudev
Copy link
Member

sagudev commented Apr 8, 2025

You probably want to root T, and then you can obtain mutable handle to pass it to function.

MutableHandle::new is similar to MutableHandle::fromMarkedLocation, that has this comment in mozilla:

This may be called only if the location of the T is guaranteed
to be marked (for some reason other than being a Rooted),
e.g., if it is guaranteed to be reachable from an implicit root.

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 a pull request may close this issue.

4 participants