rust/library/alloc/src
Matthias Krüger c4ee2df539
Rollup merge of #120248 - WaffleLapkin:bonk-ptr-object-casts, r=compiler-errors,oli-obk,lnicola
Make casts of pointers to trait objects stricter

This is an attempt to `fix` https://github.com/rust-lang/rust/issues/120222 and https://github.com/rust-lang/rust/issues/120217.

This is done by adding restrictions on casting pointers to trait objects.

Before this PR the rules were as follows:

> When casting `*const X<dyn A>` -> `*const Y<dyn B>`, principal traits in `A` and `B` must refer to the same trait definition (or no trait).

With this PR the rules are changed to

> When casting `*const X<dyn Src>` -> `*const Y<dyn Dst>`
> - if `Dst` has a principal trait `DstP`,
>   - `Src` must have a principal trait `SrcP`
>   - `dyn SrcP` and `dyn DstP` must be the same type (modulo the trait object lifetime, `dyn T+'a` -> `dyn T+'b` is allowed)
>   - Auto traits in `Dst` must be a subset of auto traits in `Src`
>     - Not adhering to this is currently a FCW (warn-by-default + `FutureReleaseErrorReportInDeps`), instead of an error
> - if `Src` has a principal trait `Dst` must as well
>   - this restriction will be removed in a follow up PR

This ensures that
1. Principal trait's generic arguments match (no `*const dyn Tr<A>` -> `*const dyn Tr<B>` casts, which are a problem for [#120222](https://github.com/rust-lang/rust/issues/120222))
2. Principal trait's lifetime arguments match (no `*const dyn Tr<'a>` -> `*const dyn Tr<'b>` casts, which are a problem for [#120217](https://github.com/rust-lang/rust/issues/120217))
3. No auto traits can be _added_ (this is a problem for arbitrary self types, see [this comment](https://github.com/rust-lang/rust/pull/120248#discussion_r1463835350))

Some notes:
 - We only care about the metadata/last field, so you can still cast `*const dyn T` to `*const WithHeader<dyn T>`, etc
- The lifetime of the trait object itself (`dyn A + 'lt`) is not checked, so you can still cast `*mut FnOnce() + '_` to `*mut FnOnce() + 'static`, etc
  - This feels fishy, but I couldn't come up with a reason it must be checked

The diagnostics are currently not great, to say the least, but as far as I can tell this correctly fixes the issues.

cc `@oli-obk` `@compiler-errors` `@lcnr`
2024-07-08 16:28:15 +02:00
..
alloc run alloc benchmarks in Miri and fix UB 2022-11-07 10:34:04 +01:00
boxed Convert some module-level // and /// comments to //!. 2024-06-20 09:23:18 +10:00
collections Auto merge of #127454 - matthiaskrgr:rollup-k3vfen2, r=matthiaskrgr 2024-07-07 16:29:52 +00:00
ffi replace version placeholder 2024-06-11 16:52:02 +02:00
raw_vec try_with_capacity for RawVec 2024-03-01 18:20:48 +00:00
rc Generalize {Rc,Arc}::make_mut() to unsized types. 2024-06-22 08:08:00 -07:00
slice Revert panic_safe test changes 2024-06-17 22:05:35 +02:00
sync Detect unused structs which derived Default 2024-06-25 23:29:44 +08:00
testing Share testing utilities with non-btree test cases 2022-05-02 10:07:50 +02:00
vec Specialize TrustedLen for Iterator::unzip() 2024-07-07 06:58:52 +03:00
alloc.rs Replace WriteCloneIntoRaw with CloneToUninit. 2024-06-22 08:08:00 -07:00
borrow.rs Impl DerefPure for more std types 2024-04-05 00:02:42 +02:00
boxed.rs Add more checks for pointers with vtable meta 2024-07-04 17:56:09 +02:00
fmt.rs Auto merge of #125012 - RalfJung:format-error, r=Mark-Simulacrum,workingjubilee 2024-05-12 08:34:32 +00:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Specialize TrustedLen for Iterator::unzip() 2024-07-07 06:58:52 +03:00
macros.rs Remove __rust_force_expr. 2024-06-25 14:35:09 +10:00
raw_vec.rs Avoid MIR bloat in inlining 2024-07-01 05:17:13 -07:00
rc.rs Generalize {Rc,Arc}::make_mut() to unsized types. 2024-06-22 08:08:00 -07:00
slice.rs Fix wrong big O star bracing in the doc comments 2024-06-20 18:07:04 +02:00
str.rs Simplify str::clone_into 2024-06-25 22:34:41 +02:00
string.rs Auto merge of #99969 - calebsander:feature/collect-box-str, r=dtolnay 2024-05-19 02:13:06 +00:00
sync.rs Run alloc sync tests 2024-07-05 14:48:39 +00:00
task.rs lib: fix some unnecessary_cast clippy lint 2024-03-25 23:19:40 +03:00
tests.rs remove redundant imports 2023-12-10 10:56:22 +08:00