You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking issue for as_unsafe_cell stabilization #27708
B-unstableBlocker: Implemented in the nightly compiler and unstable.T-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
The Cell::as_unsafe_cell and RefCell::as_unsafe_cell methods return a reference to the underlying UnsafeCell in both cases.
It's not clear what the use cases are for these methods and whether they can be achieved in some alternative way. If you are using these methods, please leave a comment with your use case.
The libs team decided to not move this issue to FCP for this cycle.
@jdm, can you clarify something for us? It looks like this method is used to subvert any active borrows of a RefCell during tracing, but isn't this fundamentally unsafe? How can accessing a RefCell while it's borrowed mutably elsewhere be safe?
@jdm@alexcrichton If this is done during a tracing phase in which the thread owning the RefCell (the "mutator" in GC parlance) is paused, it may be OK. (IMO, any GC has to have some interaction with RefCell that subverts the borrow tracking.)
I'd be a little worried in terms of optimizations here if we told LLVM something along the lines of "you have a mutable pointer, make any assumption you like knowing that no one else will ever have this pointer unless you hand it to them". That sort of contract would be violated where even if the mutator were paused an optimization may still trigger undefined behavior.
Concretely, if the source looked like:
let slot = a.borrow_mut();println!("{}", slot.foo);function();println!("{}", slot.foo);
It seems like a valid optimization that the value loaded from slot.foo doesn't need to be reloaded across the call to function. The function has mutable access to the contents, so it's "guaranteed" that no one else has access to the pointer, but if some sort of mutation or something happened then badness may arise.
Again though, this is all very hypothetical, I'd certainly be willing to believe that the exact situation here actually works out due to some combination of some actions or whatnot.
Note that there should not be any mutation taking place in function in Servo's case. The use of unsafe cells like this is simply to avoid the difficulty of calling into any SM API that could trigger a GC while a mutable borrow is outstanding.
I'm using this function to check if two RefCells point to the same memory location because there's no other way to do this in Rust. Could be stabilized in my opinion.
@Columbus240 Why is this needed to compare memory locations? Or rather, why is comparing the location of RefCells different from comparing that of any other type?
fnsame_refs<T>(a:&T,b:&T) -> bool{let a:*constT = a;let b:*constT = b;
a == b
}
B-unstableBlocker: Implemented in the nightly compiler and unstable.T-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
Activity
aturon commentedon Aug 12, 2015
cc @kmcallister, I think you may have employed these methods in Servo?
jdm commentedon Aug 12, 2015
We do use the methods for RefCell: http://mxr.mozilla.org/servo/search?string=as_unsafe_cell . We appear to do so in cases where a GC could occur at virtually any time and the there may be outstanding borrows of fields that need to be traced. Similarly, it gets used for cases when we want to ensure certain resources are cleaned up if a panic occurs but there may be outstanding borrows that would prevent doing this through the stable, safe interface. Finally, we also use this for parallel code, but @pcwalton will need to explain the motivation there I think.
alexcrichton commentedon Sep 17, 2015
Nominating for resolution in 1.5
alexcrichton commentedon Sep 24, 2015
The libs team decided to not move this issue to FCP for this cycle.
@jdm, can you clarify something for us? It looks like this method is used to subvert any active borrows of a
RefCellduring tracing, but isn't this fundamentally unsafe? How can accessing aRefCellwhile it's borrowed mutably elsewhere be safe?jdm commentedon Sep 24, 2015
When you describe it like that, it does sound pretty incorrect.
aturon commentedon Sep 24, 2015
@jdm @alexcrichton If this is done during a tracing phase in which the thread owning the
RefCell(the "mutator" in GC parlance) is paused, it may be OK. (IMO, any GC has to have some interaction withRefCellthat subverts the borrow tracking.)alexcrichton commentedon Sep 24, 2015
I'd be a little worried in terms of optimizations here if we told LLVM something along the lines of "you have a mutable pointer, make any assumption you like knowing that no one else will ever have this pointer unless you hand it to them". That sort of contract would be violated where even if the mutator were paused an optimization may still trigger undefined behavior.
Concretely, if the source looked like:
It seems like a valid optimization that the value loaded from
slot.foodoesn't need to be reloaded across the call tofunction. The function has mutable access to the contents, so it's "guaranteed" that no one else has access to the pointer, but if some sort of mutation or something happened then badness may arise.Again though, this is all very hypothetical, I'd certainly be willing to believe that the exact situation here actually works out due to some combination of some actions or whatnot.
jdm commentedon Sep 24, 2015
Note that there should not be any mutation taking place in
functionin Servo's case. The use of unsafe cells like this is simply to avoid the difficulty of calling into any SM API that could trigger a GC while a mutable borrow is outstanding.seanmonstar commentedon Nov 3, 2015
It seems that the
unsafecould be removed from this method, since you can no longer access theUnsafeCell's value directly.Columbus240 commentedon May 27, 2016
I'm using this function to check if two
RefCells point to the same memory location because there's no other way to do this in Rust. Could be stabilized in my opinion.SimonSapin commentedon May 27, 2016
@Columbus240 Why is this needed to compare memory locations? Or rather, why is comparing the location of
RefCells different from comparing that of any other type?13 remaining items
alexcrichton commentedon Jul 13, 2016
Oh I'd be fine with
as_here as wellaturon commentedon Jul 13, 2016
There's a bit of a clash, since we use
getwhen extracting from cells and containers, andasfor conversions, but this is kind of both.I think
as_ptris probably better, becausegetsuggests some amount of "action" (like touching the borrowing status) that isn't happening.alexcrichton commentedon Aug 11, 2016
Discussed recently, the libs team decided to stabilize.
Our conclusion was to call these methods
as_ptrreturning a raw pointer.Auto merge of #35607 - alexcrichton:stabilize-1.12, r=brson
Auto merge of #35607 - alexcrichton:stabilize-1.12, r=brson
nox commentedon Aug 20, 2016
So where is that method on Cell? On RefCell? We need this in Servo.
Edit: oh, didn't realise the docs online aren't up-to-date.
alexcrichton commentedon Aug 20, 2016
@nox
CellandRefCellnox commentedon Aug 20, 2016
@alexcrichton Yeah sorry, just didn't wake up enough before opening my mouth.
std: Stabilize APIs for the 1.12 release
std: Stabilize APIs for the 1.12 release