]> git.lizzy.rs Git - rust.git/commit - src/tools/rustfmt
Rollup merge of #77844 - RalfJung:zst-box, r=nikomatsakis
authorDylan DPC <dylan.dpc@gmail.com>
Sat, 21 Nov 2020 18:44:07 +0000 (19:44 +0100)
committerGitHub <noreply@github.com>
Sat, 21 Nov 2020 18:44:07 +0000 (19:44 +0100)
commit6cd02a85f1e40f8feac4f09c987baba0821b7756
tree6f79a5964fe3c568e925b7ce3c81ab3ffb0ed91a
parentd806d656578c2d6b34cf96809862e8cffb293a68
parenta7677f77146bd69b26b5fb5eaa8f88ac080ff347
Rollup merge of #77844 - RalfJung:zst-box, r=nikomatsakis

clarify rules for ZST Boxes

LLVM's rules around `getelementptr inbounds` with offset 0 are a bit annoying, and as a consequence we have no choice but say that a `Box<()>` pointing to previously allocated memory that has since been freed is UB. Clarify the docs to reflect this.

This is based on conversations on the LLVM mailing list.
* Here's my initial mail: https://lists.llvm.org/pipermail/llvm-dev/2019-February/130452.html
* The first email of the March part of that thread: https://lists.llvm.org/pipermail/llvm-dev/2019-March/130831.html
* First email of the April part: https://lists.llvm.org/pipermail/llvm-dev/2019-April/131693.html

The conclusion for me at least was that `getelementptr inbounds` with offset 0 is *not* the identity function, but can sometimes return `poison` even when the input is a regular pointer -- specifically, it returns `poison` when this pointer points into something that LLVM "knows has been deallocated", i.e., a former LLVM-managed allocation. It is however the identity function on pointers obtained by casting integers.

Note that there [are formal proposals](https://people.mpi-sws.org/~jung/twinsem/twinsem.pdf) for LLVM semantics where `getelementptr inbounds` with offset 0 isn't quite the identity function but never returns `poison` (it affects the provenance of the pointer but in a way that doesn't matter if this pointer is never used for memory accesses), and indeed this is likely necessary to consistently describe LLVM semantics. But with the informal LLVM LangRef that we have right now, and with LLVM devs insisting otherwise, it seems unwise to rely on this.
library/alloc/src/boxed.rs
library/core/src/ptr/mod.rs