diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 89c0cf4673001..9fa60caa88f3c 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -3553,12 +3553,17 @@ where // We panic if creating this type with all 0x01 bytes would // cause LLVM UB. // - // Therefore, in order for us to not panic, - // * the alignment of the pointer must be 1 - // (or we would have an unaligned pointer) + // Therefore, in order for us to not panic, it must either be a + // reference to [T] where T has align 1 (where we don't statically know + // the size, so we don't emit any dereferenceable), or a reference to str + // which acts much like a [u8]. // - // * the statically known size of the pointee must be 0. - // (or we would emit dereferenceable) + // We *do* need to panic for &dyn Trait, even though the layout of dyn Trait is + // size 0 align 1, because &dyn Trait holds a reference to a non-zero sized type, + // which also must be aligned. + // + // This even applies to *const dyn Trait, which holds a reference and therefore + // must be valid, so 1-initialization is not okay there. // // If this bypass didn't exist, old versions of `hyper` with no semver compatible // fix (0.11, 0.12, 0.13) would panic, as they make uninit &[u8] and &str.