Skip to content

Tracking issue for as_unsafe_cell stabilization #27708

Closed

Description

@aturon

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.

Activity

  1. added
    T-libs-api
    B-unstable
    on Aug 12, 2015
  2. aturon commented on Aug 12, 2015

    @aturon
    ContributorAuthor

    cc @kmcallister, I think you may have employed these methods in Servo?

  3. jdm commented on Aug 12, 2015

    @jdm
    Contributor

    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.

  4. alexcrichton commented on Sep 17, 2015

    @alexcrichton
    Member

    Nominating for resolution in 1.5

  5. alexcrichton commented on Sep 24, 2015

    @alexcrichton
    Member

    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?

  6. jdm commented on Sep 24, 2015

    @jdm
    Contributor

    When you describe it like that, it does sound pretty incorrect.

  7. aturon commented on Sep 24, 2015

    @aturon
    ContributorAuthor

    @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.)

  8. alexcrichton commented on Sep 24, 2015

    @alexcrichton
    Member

    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.

  9. jdm commented on Sep 24, 2015

    @jdm
    Contributor

    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.

  10. seanmonstar commented on Nov 3, 2015

    @seanmonstar
    Contributor

    This function is unsafe because UnsafeCell's field is public.

    It seems that the unsafe could be removed from this method, since you can no longer access the UnsafeCell's value directly.

  11. Columbus240 commented on May 27, 2016

    @Columbus240

    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.

  12. SimonSapin commented on May 27, 2016

    @SimonSapin
    Contributor

    @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?

    fn same_refs<T>(a: &T, b: &T) -> bool {
        let a: *const T = a;
        let b: *const T = b;
        a == b
    }
  13. 13 remaining items

  14. alexcrichton commented on Jul 13, 2016

    @alexcrichton
    Member

    Oh I'd be fine with as_ here as well

  15. aturon commented on Jul 13, 2016

    @aturon
    ContributorAuthor

    What’s the naming convention for as_ vs get_? Slices and strings have as_ptr.

    There's a bit of a clash, since we use get when extracting from cells and containers, and as for conversions, but this is kind of both.

    I think as_ptr is probably better, because get suggests some amount of "action" (like touching the borrowing status) that isn't happening.

  16. alexcrichton commented on Aug 11, 2016

    @alexcrichton
    Member

    Discussed recently, the libs team decided to stabilize.

    Our conclusion was to call these methods as_ptr returning a raw pointer.

  17. added a commit that references this issue on Aug 18, 2016
    7f38beb
  18. added a commit that references this issue on Aug 20, 2016
    99867ee
  19. nox commented on Aug 20, 2016

    @nox
    Contributor

    Our conclusion was to call these methods as_ptr returning a raw pointer.

    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.

  20. alexcrichton commented on Aug 20, 2016

    @alexcrichton
    Member
  21. nox commented on Aug 20, 2016

    @nox
    Contributor

    @alexcrichton Yeah sorry, just didn't wake up enough before opening my mouth.

  22. added a commit that references this issue on Aug 23, 2016
    4ce3c86
  23. added a commit that references this issue on Sep 27, 2016
    1076a3d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableT-libs-apifinal-comment-period

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions