Commit Graph

369 Commits

Author SHA1 Message Date
Dylan DPC
0bc5fc99d3
Rollup merge of #68099 - lukaslueg:into_raw_unsafe, r=LukasKalbertodt
Amend Rc/Arc::from_raw() docs regarding unsafety

[This](https://stackoverflow.com/questions/59671647/is-it-safe-to-clone-a-type-erased-arc-via-raw-pointer) question on SO boils down to "is it safe to `::from_raw()` a `Rc<T>`/`Arc<T>` using a dummy `T` even if `T` is never dereferenced via the new `Rc`/`Arc`?". It almost never is.

This PR amends the docs of `from_raw()` regarding this point.
2020-03-22 15:48:28 +01:00
Yuki Okushi
4699b29a04
Rollup merge of #69609 - TimDiekmann:excess, r=Amanieu
Remove `usable_size` APIs

This removes the usable size APIs:
- remove `usable_size` (obv)
- change return type of allocating methods to include the allocated size
- remove `_excess` API

r? @Amanieu
closes rust-lang/wg-allocators#17
2020-03-03 17:50:06 +09:00
Tim Diekmann
d8e3557dba Remove usable_size APIs 2020-03-03 00:08:24 +01:00
Yuki Okushi
4e0bea326e Stabilize boxed_slice_try_from 2020-02-28 13:28:09 +09:00
Lukas Lueg
586c7e3907 Make rc::RcBox and sync::ArcInner repr(C)
Future-proof these types in case rustc reorders
the inner fields. As per discussion in PR #68099.
2020-02-07 22:59:24 +01:00
Lukas Lueg
b4c96a9199 Refine [Arc/Rc]::from_raw() docs 2020-01-28 22:28:13 +01:00
Tim Diekmann
7ca25db816
Rename Alloc to AllocRef 2020-01-27 21:39:51 +01:00
bors
e02c475da5 Auto merge of #67339 - CAD97:rc-provenance, r=sfackler
Use pointer offset instead of deref for A/Rc::into_raw

Internals thread: https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/2?u=cad97

The current implementation of (`A`)`Rc::into_raw` uses the `Deref::deref` implementation to get the pointer-to-data that is returned. This is problematic in the proposed Stacked Borrow rules, as this only gets shared provenance over the data location. (Note that the strong/weak counts are `UnsafeCell` (`Cell`/`Atomic`) so shared provenance can still mutate them, but the data itself is not.) When promoted back to a real reference counted pointer, the restored pointer can be used for mutation through `::get_mut` (if it is the only surviving reference). However, this mutates through a pointer ultimately derived from a `&T` borrow, violating the Stacked Borrow rules.

There are three known potential solutions to this issue:

- Stacked Borrows is wrong, liballoc is correct.
- Fully admit (`A`)`Rc` as an "internal mutability" type and store the data payload in an `UnsafeCell` like the strong/weak counts are. (Note: this is not needed generally since the `RcBox`/`ArcInner` is stored behind a shared `NonNull` which maintains shared write provenance as a raw pointer.)
- Adjust `into_raw` to do direct manipulation of the pointer (like `from_raw`) so that it maintains write provenance and doesn't derive the pointer from a reference.

This PR implements the third option, as recommended by @RalfJung.

Potential future work: provide `as_raw` and `clone_raw` associated functions to allow the [`&T` -> (`A`)`Rc<T>` pattern](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/2?u=cad97) to be used soundly without creating (`A`)`Rc` from references.
2020-01-16 00:47:45 +00:00
Lukas Lueg
becebf3106 Ammend Rc/Arc::from_raw() docs regarding unsafety
Constructing an Rc/Arc is unsafe even if the wrapped `T`
is never dereferenced.
2020-01-10 19:39:01 +01:00
Mark Rousskov
a06baa56b9 Format the world 2019-12-22 17:42:47 -05:00
Mazdak Farrokhzad
ca528fcc04
Rollup merge of #67504 - Mark-Simulacrum:note-data-offset, r=Centril
Warn against relying on ?Sized being last

Fixes #62522
2019-12-22 02:40:07 +01:00
Mark Rousskov
a34c2677af Warn against relying on ?Sized being last 2019-12-21 20:35:41 -05:00
Ross MacArthur
f7256d28d1
Require issue = "none" over issue = "0" in unstable attributes 2019-12-21 13:16:18 +02:00
CAD97
eb77f7ec6e Add internal safety docs to (A)Rc::into_raw 2019-12-17 15:52:13 -05:00
Mazdak Farrokhzad
c34ea91a9d
Rollup merge of #65778 - bdonlan:stable_weak_count, r=dtolnay
Stabilize `std::{rc,sync}::Weak::{weak_count, strong_count}`

* Original PR: #56696
* Tracking issue: #57977

Closes: #57977

Supporting comments:

> Although these were added for testing, it is occasionally useful to have a way to probe optimistically for whether a weak pointer has become dangling, without actually taking the overhead of manipulating atomics. Are there any plans to stabilize this?

_Originally posted by @bdonlan in https://github.com/rust-lang/rust/issues/57977#issuecomment-516970921_

> Having this stabilized would help. Currently, the only way to check if a weak pointer has become dangling is to call `upgrade`, which is by far expensive.

_Originally posted by @glebpom in https://github.com/rust-lang/rust/issues/57977#issuecomment-526934709_

Not sure if stabilizing these warrants a full RFC, so throwing this out here as a start for now.

Note: per CONTRIBUTING.md, I ran the tidy checks, but they seem to be failing on unchanged files (primarily in `src/stdsimd`).
2019-12-16 05:23:26 +01:00
cad97
c842f02dee
Use pointer offset instead of deref for A/Rc::into_raw 2019-12-15 19:57:18 -05:00
David Tolnay
9778e03665
Bump Weak::strong_count/weak_count stabilizations from 1.40 to 1.41 2019-12-14 19:26:25 -08:00
Mazdak Farrokhzad
bcf992dc6c
Rollup merge of #66710 - vorner:weak-into-raw-null-docs, r=dtolnay
weak-into-raw: Clarify some details in Safety

Clarify it is OK to pass a pointer that never owned a weak count (one
from Weak::new) back into it as it was created from it. Relates to
discussion in #60728.

@CAD97 Do you want to have a look at the new docs?
2019-12-05 19:03:08 +01:00
Michal 'vorner' Vaner
473151070b
weak-into-raw: Clarify some details in Safety
Clarify it is OK to pass a pointer that never owned a weak count (one
from Weak::new) back into it as it was created from it. Relates to
discussion in #60728.
2019-12-05 15:22:11 +01:00
bors
4787e97475 Auto merge of #66256 - CAD97:patch-2, r=RalfJung
Layout::pad_to_align is infallible

As per [this comment](https://github.com/rust-lang/rust/issues/55724#issuecomment-441421651) (cc @glandium).

> Per https://github.com/rust-lang/rust/blob/eb981a1/src/libcore/alloc.rs#L63-L65, `layout.size()` is always <= `usize::MAX - (layout.align() - 1)`.
>
> Which means:
>
> * The maximum value `layout.size()` can have is already aligned for `layout.align()` (`layout.align()` being a power of two, `usize::MAX - (layout.align() - 1)` is a multiple of `layout.align()`)
> * Incidentally, any value smaller than that maximum value will align at most to that maximum value.
>
> IOW, `pad_to_align` can not return `Err(LayoutErr)`, except for the layout not respecting its invariants, but we shouldn't care about that.

This PR makes `pad_to_align` return `Layout` directly, representing the fact that it cannot fail.
2019-12-03 01:50:33 +00:00
Tyler Mandry
941d915942
Rollup merge of #66128 - emilio:new-zeroed, r=SimonSapin
alloc: Add new_zeroed() versions like new_uninit().

MaybeUninit has both uninit() and zeroed(), it seems reasonable to have the same
surface on Box/Rc/Arc.

Needs tests.

cc #63291
2019-11-26 17:56:05 -06:00
Bryan Donlan
0d0b283c2c Make Weak::weak_count() return zero when no strong refs remain 2019-11-21 19:48:39 +00:00
Bryan Donlan
91ee3d1c31 Stabilize std::{rc,sync}::Weak::{weak_count, strong_count}
Closes: #57977
2019-11-21 18:33:03 +00:00
Christopher Durham
b688d480a6
Remove Layout::pad_to_align unwrap 2019-11-09 15:39:24 -05:00
Oleg Nosov
45f281d461
Reverted PhantomData in LinkedList, fixed PhantomData markers in Rc and Arc 2019-11-05 23:36:54 +03:00
Emilio Cobos Álvarez
b12e142bc5
alloc: Add new_zeroed() versions like new_uninit().
MaybeUninit has both uninit() and zeroed(), it seems reasonable to have the same
surface on Box/Rc/Arc.

Needs tests.
2019-11-05 19:27:42 +01:00
Ralf Jung
52a31f7a00 some more Rc tweaks 2019-10-19 13:47:32 +02:00
Ralf Jung
696cba6e25 the exampleis about drop, not (de)allocation 2019-10-19 10:14:10 +02:00
Ralf Jung
868a77263a more consistency and clarification 2019-10-17 19:51:42 +02:00
Ralf Jung
470e9d2789 Rc: value -> allocation 2019-10-17 19:37:05 +02:00
kalabukdima
e67fa776ee
Fix typo in docs for Rc 2019-10-13 15:46:28 +03:00
Lzu Tao
6c1b447f2e Remove unneeded fn main blocks from docs 2019-10-01 11:55:46 +00:00
Mazdak Farrokhzad
dee08ba55a
Rollup merge of #61797 - Thomasdezeeuw:stablise-weak_ptr_eq, r=RalfJung
Stabilise weak_ptr_eq

Implemented in #55987.

Closes #55981.
2019-09-14 22:16:03 +02:00
Alexander Regueiro
b0006dff10 A few cosmetic improvements to code & comments in liballoc and libcore 2019-09-06 16:02:25 +01:00
Thomas de Zeeuw
307804a00d Update {rc, sync}::Weak::ptr_eq doc about comparing Weak::new 2019-08-25 13:07:54 +02:00
Thomas de Zeeuw
d86516d91e Stabilise weak_ptr_eq 2019-08-25 13:06:57 +02:00
Simon Sapin
b79ce1b1b1 Rename private helper method allocate_for_unsized to allocate_for_layout 2019-08-17 17:01:04 +02:00
Simon Sapin
ba0328327c Doc nits
Co-Authored-By: Ralf Jung <post@ralfj.de>
2019-08-17 15:42:05 +02:00
Simon Sapin
7a641f7c51 Relax the safety condition for get_mut_unchecked 2019-08-16 17:45:44 +02:00
Simon Sapin
810dfd7cd4 Reuse more internal Rc and Arc methods 2019-08-16 17:45:08 +02:00
Simon Sapin
ae1e201a0c Add a comment on the usage of Layout:🆕:<RcBox<()>>() 2019-08-16 17:11:18 +02:00
Simon Sapin
78264f5e3c Add tracking issue numbers 2019-08-16 17:11:18 +02:00
Simon Sapin
1141136b90 Use ManuallyDrop instead of mem::forget
Per https://github.com/rust-lang/rust/pull/62451#discussion_r303197278
2019-08-16 17:11:18 +02:00
Simon Sapin
4eeb623e9e Fix intra-rustdoc links 2019-08-16 17:11:18 +02:00
Simon Sapin
170d933d2f Move constructors of boxed/rc’ed slices to matching impl blocks 2019-08-16 17:11:18 +02:00
Simon Sapin
bde1924059 Add new_uninit_slice and assume_init on Box, Rc, and Arc of [T] 2019-08-16 17:11:18 +02:00
Simon Sapin
7b02b9f8ec Add new_uninit and assume_init on Box, Rc, and Arc 2019-08-16 17:11:18 +02:00
Simon Sapin
1613fdae37 Add Rc::get_mut_unchecked, Arc::get_mut_unchecked 2019-08-16 17:11:18 +02:00
Jake Goulding
32324d22c3 Add implementations for converting boxed slices into boxed arrays
This mirrors the implementations of reference slices into arrays.
2019-08-05 10:26:53 -04:00
Vadim Petrochenkov
3d0d6ee271 liballoc: Unconfigure tests during normal build
Remove additional libcore-like restrictions from liballoc, turns out the testing works ok if the tests are a part of liballoc itself.
2019-08-02 01:59:01 +03:00