rust/library/alloc/src
Dylan DPC 07f586fe74
Rollup merge of #96642 - thomcc:thinbox-zst-ugh, r=yaahc
Avoid zero-sized allocs in ThinBox if T and H are both ZSTs.

This was surprisingly tricky, and took longer to get right than expected. `ThinBox` is a surprisingly subtle piece of code. That said, in the end, a lot of this was due to overthinking[^overthink] -- ultimately the fix ended up fairly clean and simple.

[^overthink]: Honestly, for a while I was convinced this couldn't be done without allocations or runtime branches in these cases, but that's obviously untrue.

Anyway, as a result of spending all that time debugging, I've extended the tests quite a bit, and also added more debug assertions. Many of these helped for subtle bugs I made in the middle (for example, the alloc/drop tracking is because I ended up double-dropping the value in the case where both were ZSTs), they're arguably a bit of overkill at this point, although I imagine they could help in the future too.

Anyway, these tests cover a wide range of size/align cases, nd fully pass under miri[^1]. They also do some smoke-check asserting that the value has the correct alignment, although in practice it's totally within the compiler's rights to delete these assertions since we'd have already done UB if they get hit. They have more boilerplate than they really need, but it's not *too* bad on a per-test basis.

A notable absence from testing is atypical header types, but at the moment it's impossible to manually implement `Pointee`. It would be really nice to have testing here, since it's not 100% obvious to me that the aligned read/write we use for `H` are correct in the face of arbitrary combinations of `size_of::<H>()`, `align_of::<H>()`, and `align_of::<T>()`. (That said, I spent a while thinking through it and am *pretty* sure it's fine -- I'd just feel... better if we could test some cases for non-ZST headers which have unequal and align).

[^1]: Or at least, they pass under miri if I copy the code and tests into a new crate and run miri on it (after making it less stdlibified).

Fixes #96485.

I'd request review ``@yaahc,`` but I believe you're taking some time away from reviews, so I'll request from the previous PR's reviewer (I think that the context helps, even if the actual change didn't end up being bad here).

r? ``@joshtriplett``
2022-06-04 11:06:39 +02:00
..
alloc Use Box::new() instead of box syntax in alloc tests 2022-05-29 00:41:14 +02:00
boxed Use pointer::is_aligned in ThinBox debug assert 2022-05-27 22:19:43 -07:00
collections Tweak insert docs 2022-05-31 22:08:14 -07:00
ffi Finish bumping stage0 2022-05-27 07:36:17 -04:00
raw_vec Add a unit test for zero-sized types in RawVec. 2021-11-26 19:30:45 +11:00
rc Use Box::new() instead of box syntax in alloc tests 2022-05-29 00:41:14 +02:00
sync Use Box::new() instead of box syntax in alloc tests 2022-05-29 00:41:14 +02:00
vec Auto merge of #97293 - est31:remove_box, r=oli-obk 2022-06-02 13:20:19 +00:00
alloc.rs Auto merge of #97033 - nbdd0121:unwind3, r=Amanieu 2022-05-19 04:04:40 +00:00
borrow.rs Stabilize toowned_clone_into 2022-05-28 01:07:45 +09:00
boxed.rs Rollup merge of #97655 - steffahn:better-pin-box-construction-docs, r=thomcc 2022-06-02 23:39:07 +02:00
fmt.rs remove useless cold 2022-05-29 20:40:56 +01:00
lib.rs Rollup merge of #96642 - thomcc:thinbox-zst-ugh, r=yaahc 2022-06-04 11:06:39 +02:00
macros.rs Use #[rustc_box] in alloc instead of box syntax 2022-06-01 02:28:34 +02:00
raw_vec.rs Remove some unnecessary rustc_allow_const_fn_unstable attributes. 2022-05-13 16:01:18 +10:00
rc.rs Use Box::new() instead of box syntax in alloc tests 2022-05-29 00:41:14 +02:00
slice.rs Add reexport of slice::from{,_mut}_ptr_range to alloc & std 2022-05-30 15:44:56 +04:00
str.rs improve case conversion happy path 2022-05-26 13:18:57 +01:00
string.rs Auto merge of #96605 - Urgau:string-retain-codegen, r=thomcc 2022-05-21 01:56:51 +00:00
sync.rs Update sync.rs 2022-06-01 14:12:36 +02:00
task.rs Document From implementations for Waker and RawWaker 2021-04-22 14:16:33 -07:00
tests.rs Use implicit capture syntax in format_args 2022-03-10 10:23:40 -05:00