rust/library/alloc/tests
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
..
arc.rs Add Weak may_dangle tests 2021-05-20 19:42:29 -07:00
borrow.rs Move various ui const tests to library 2020-09-04 02:35:27 +02:00
boxed.rs Revert accidental stabilization 2022-03-10 14:36:51 +00:00
btree_set_hash.rs Include the length in BTree hashes 2021-10-01 12:29:09 -07:00
c_str.rs Remove use of reverted std::ffi::c_char 2022-04-27 14:01:04 -07:00
const_fns.rs impl const Default for Box<[T]> and Box<str> 2022-04-11 12:14:18 -07:00
cow_str.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
fmt.rs Debug print char 0 as '\0' rather than '\u{0}' 2022-03-27 04:49:10 -07:00
heap.rs Rename AllocRef to Allocator and (de)alloc to (de)allocate 2020-12-04 14:47:15 +01:00
lib.rs Avoid zero-sized allocs in ThinBox if T and H are both ZSTs. 2022-05-27 22:12:20 -07:00
linked_list.rs Classify BinaryHeap & LinkedList unit tests as such 2022-03-11 11:33:59 +01:00
rc.rs Add Weak may_dangle tests 2021-05-20 19:42:29 -07:00
slice.rs Use Box::new() instead of box syntax in alloc tests 2022-05-29 00:41:14 +02:00
str.rs improve case conversion happy path 2022-05-26 13:18:57 +01:00
string.rs Use implicit capture syntax in format_args 2022-03-10 10:23:40 -05:00
thin_box.rs Avoid zero-sized allocs in ThinBox if T and H are both ZSTs. 2022-05-27 22:12:20 -07:00
vec_deque.rs Use implicit capture syntax in format_args 2022-03-10 10:23:40 -05:00
vec.rs Use Box::new() instead of box syntax in alloc tests 2022-05-29 00:41:14 +02:00