From 66ffdde137afdd6a0f86efef7830730d4ce33c06 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 26 May 2022 13:06:29 +0400 Subject: [PATCH 01/17] Make `from{,_mut}_ptr_range` const - `from_ptr_range` uses `#![feature(slice_from_ptr_range_const)]` - `from_mut_ptr_range` uses `#![feature(slice_from_mut_ptr_range_const)]` --- library/core/src/slice/raw.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs index 6bc60b04b5c64..fa24a481a1f60 100644 --- a/library/core/src/slice/raw.rs +++ b/library/core/src/slice/raw.rs @@ -213,7 +213,8 @@ pub const fn from_mut(s: &mut T) -> &mut [T] { /// /// [valid]: ptr#safety #[unstable(feature = "slice_from_ptr_range", issue = "89792")] -pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { +#[rustc_const_unstable(feature = "slice_from_ptr_range_const", issue = "89792")] +pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { // SAFETY: the caller must uphold the safety contract for `from_ptr_range`. unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } } @@ -263,7 +264,8 @@ pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { /// /// [valid]: ptr#safety #[unstable(feature = "slice_from_ptr_range", issue = "89792")] -pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] { +#[rustc_const_unstable(feature = "slice_from_mut_ptr_range_const", issue = "89792")] +pub const unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] { // SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`. unsafe { from_raw_parts_mut(range.start, range.end.sub_ptr(range.start)) } } From 80cbeb0d2f2e8fde7ac8e5c7f89c0394ff570879 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 27 May 2022 18:37:33 +0400 Subject: [PATCH 02/17] Add reexport of slice::from{,_mut}_ptr_range to alloc & std At first I was confused why `std::slice::from_ptr_range` didn't work :D --- library/alloc/src/lib.rs | 1 + library/alloc/src/slice.rs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index fd21b3671182b..35b8b386dceff 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -130,6 +130,7 @@ #![feature(ptr_sub_ptr)] #![feature(receiver_trait)] #![feature(set_ptr_value)] +#![feature(slice_from_ptr_range)] #![feature(slice_group_by)] #![feature(slice_ptr_get)] #![feature(slice_ptr_len)] diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 199b3c9d0290c..4a9cecd9b4e1a 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -114,6 +114,8 @@ pub use core::slice::EscapeAscii; pub use core::slice::SliceIndex; #[stable(feature = "from_ref", since = "1.28.0")] pub use core::slice::{from_mut, from_ref}; +#[unstable(feature = "slice_from_ptr_range", issue = "89792")] +pub use core::slice::{from_mut_ptr_range, from_ptr_range}; #[stable(feature = "rust1", since = "1.0.0")] pub use core::slice::{from_raw_parts, from_raw_parts_mut}; #[stable(feature = "rust1", since = "1.0.0")] From 8024fd68906aacc4ef2dfa92bda97ff99677c12d Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 27 May 2022 19:08:22 +0400 Subject: [PATCH 03/17] Add ui tests for `slice::from_{ptr_range,raw_parts}` --- src/test/ui/const-ptr/allowed_slices.rs | 106 +++++++ src/test/ui/const-ptr/forbidden_slices.rs | 95 ++++++ src/test/ui/const-ptr/forbidden_slices.stderr | 280 ++++++++++++++++++ 3 files changed, 481 insertions(+) create mode 100644 src/test/ui/const-ptr/allowed_slices.rs create mode 100644 src/test/ui/const-ptr/forbidden_slices.rs create mode 100644 src/test/ui/const-ptr/forbidden_slices.stderr diff --git a/src/test/ui/const-ptr/allowed_slices.rs b/src/test/ui/const-ptr/allowed_slices.rs new file mode 100644 index 0000000000000..12089c0a8e8d7 --- /dev/null +++ b/src/test/ui/const-ptr/allowed_slices.rs @@ -0,0 +1,106 @@ +// run-pass +#![feature( + const_slice_from_raw_parts, + slice_from_ptr_range, + slice_from_ptr_range_const, + pointer_byte_offsets, + const_pointer_byte_offsets +)] +use std::{ + mem::MaybeUninit, + ptr, + slice::{from_ptr_range, from_raw_parts}, +}; + +// Dangling is ok, as long as it's either for ZST reads or for no reads +pub static S0: &[u32] = unsafe { from_raw_parts(dangling(), 0) }; +pub static S1: &[()] = unsafe { from_raw_parts(dangling(), 3) }; + +// References are always valid of reads of a single element (basically `slice::from_ref`) +pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 1) }; +pub static S3: &[MaybeUninit<&u32>] = unsafe { from_raw_parts(&D1, 1) }; + +// Reinterpreting data is fine, as long as layouts match +pub static S4: &[u8] = unsafe { from_raw_parts((&D0) as *const _ as _, 3) }; +// This is only valid because D1 has uninitialized bytes, if it was an initialized pointer, +// that would reinterpret pointers as integers which is UB in CTFE. +pub static S5: &[MaybeUninit] = unsafe { from_raw_parts((&D1) as *const _ as _, 2) }; +// Even though u32 and [bool; 4] have different layouts, D0 has a value that +// is valid as [bool; 4], so this is not UB (it's basically a transmute) +pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; + +// Structs are considered single allocated objects, +// as long as you don't reinterpret padding as initialized +// data everything is ok. +pub static S7: &[u16] = unsafe { + let ptr = (&D2 as *const Struct as *const u16).byte_add(4); + + from_raw_parts(ptr, 3) +}; +pub static S8: &[MaybeUninit] = unsafe { + let ptr = &D2 as *const Struct as *const MaybeUninit; + + from_raw_parts(ptr, 6) +}; + +pub static R0: &[u32] = unsafe { from_ptr_range(dangling()..dangling()) }; +// from_ptr_range panics on zst +//pub static R1: &[()] = unsafe { from_ptr_range(dangling(), dangling().byte_add(3)) }; +pub static R2: &[u32] = unsafe { + let ptr = &D0 as *const u32; + from_ptr_range(ptr..ptr.add(1)) +}; +pub static R3: &[MaybeUninit<&u32>] = unsafe { + let ptr = &D1 as *const MaybeUninit<&u32>; + from_ptr_range(ptr..ptr.add(1)) +}; +pub static R4: &[u8] = unsafe { + let ptr = &D0 as *const u32 as *const u8; + from_ptr_range(ptr..ptr.add(3)) +}; +pub static R5: &[MaybeUninit] = unsafe { + let ptr = &D1 as *const MaybeUninit<&u32> as *const MaybeUninit; + from_ptr_range(ptr..ptr.add(2)) +}; +pub static R6: &[bool] = unsafe { + let ptr = &D0 as *const u32 as *const bool; + from_ptr_range(ptr..ptr.add(4)) +}; +pub static R7: &[u16] = unsafe { + let d2 = &D2; + let l = &d2.b as *const u32 as *const u16; + let r = &d2.d as *const u8 as *const u16; + + from_ptr_range(l..r) +}; +pub static R8: &[MaybeUninit] = unsafe { + let d2 = &D2; + let l = d2 as *const Struct as *const MaybeUninit; + let r = &d2.d as *const u8 as *const MaybeUninit; + + from_ptr_range(l..r) +}; + +// Using valid slice is always valid +pub static R9: &[u32] = unsafe { from_ptr_range(R0.as_ptr_range()) }; +pub static R10: &[u32] = unsafe { from_ptr_range(R2.as_ptr_range()) }; + +const D0: u32 = (1 << 16) | 1; +const D1: MaybeUninit<&u32> = MaybeUninit::uninit(); +const D2: Struct = Struct { a: 1, b: 2, c: 3, d: 4 }; + +const fn dangling() -> *const T { + ptr::NonNull::dangling().as_ptr() as _ +} + +#[repr(C)] +struct Struct { + a: u8, + // _pad: [MaybeUninit; 3] + b: u32, + c: u16, + d: u8, + // _pad: [MaybeUninit; 1] +} + +fn main() {} diff --git a/src/test/ui/const-ptr/forbidden_slices.rs b/src/test/ui/const-ptr/forbidden_slices.rs new file mode 100644 index 0000000000000..e68b8da48212e --- /dev/null +++ b/src/test/ui/const-ptr/forbidden_slices.rs @@ -0,0 +1,95 @@ +#![feature( + const_slice_from_raw_parts, + slice_from_ptr_range, + slice_from_ptr_range_const, + pointer_byte_offsets, + const_pointer_byte_offsets +)] +use std::{ + mem::{size_of, MaybeUninit}, + ptr, + slice::{from_ptr_range, from_raw_parts}, +}; + +// Null is never valid for reads +pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) }; +pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) }; + +// Out of bounds +pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) }; + +// Reading uninitialized data +pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) }; //~ ERROR: it is undefined behavior to use this value +// Reinterpret pointers as integers (UB in CTFE.) +pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) }; //~ ERROR: it is undefined behavior to use this value +// Layout mismatch +pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; //~ ERROR: it is undefined behavior to use this value + +// Reading padding is not ok +pub static S7: &[u16] = unsafe { + //~^ ERROR: it is undefined behavior to use this value + let ptr = (&D2 as *const Struct as *const u16).byte_add(1); + + from_raw_parts(ptr, 4) +}; + +// Unaligned read +pub static S8: &[u64] = unsafe { + let ptr = (&D4 as *const [u32; 2] as *const u32).byte_add(1).cast::(); + + from_raw_parts(ptr, 1) +}; + +pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; +pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; +pub static R2: &[u32] = unsafe { + let ptr = &D0 as *const u32; + from_ptr_range(ptr..ptr.add(2)) +}; +pub static R4: &[u8] = unsafe { + //~^ ERROR: it is undefined behavior to use this value + let ptr = (&D1) as *const MaybeUninit<&u32> as *const u8; + from_ptr_range(ptr..ptr.add(1)) +}; +pub static R5: &[u8] = unsafe { + //~^ ERROR: it is undefined behavior to use this value + let ptr = &D3 as *const &u32; + from_ptr_range(ptr.cast()..ptr.add(1).cast()) +}; +pub static R6: &[bool] = unsafe { + //~^ ERROR: it is undefined behavior to use this value + let ptr = &D0 as *const u32 as *const bool; + from_ptr_range(ptr..ptr.add(4)) +}; +pub static R7: &[u16] = unsafe { + //~^ ERROR: it is undefined behavior to use this value + let ptr = (&D2 as *const Struct as *const u16).byte_add(1); + from_ptr_range(ptr..ptr.add(4)) +}; +pub static R8: &[u64] = unsafe { + let ptr = (&D4 as *const [u32; 2] as *const u32).byte_add(1).cast::(); + from_ptr_range(ptr..ptr.add(1)) +}; + +// This is sneaky: &D0 and &D0 point to different objects +// (even if at runtime they have the same address) +pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) }; +pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) }; + +const D0: u32 = 0x11; +const D1: MaybeUninit<&u32> = MaybeUninit::uninit(); +const D2: Struct = Struct { a: 1, b: 2, c: 3, d: 4 }; +const D3: &u32 = &42; +const D4: [u32; 2] = [17, 42]; + +#[repr(C)] +struct Struct { + a: u8, + // _pad: [MaybeUninit; 3] + b: u32, + c: u16, + d: u8, + // _pad: [MaybeUninit; 1] +} + +fn main() {} diff --git a/src/test/ui/const-ptr/forbidden_slices.stderr b/src/test/ui/const-ptr/forbidden_slices.stderr new file mode 100644 index 0000000000000..5bc8e2c10d385 --- /dev/null +++ b/src/test/ui/const-ptr/forbidden_slices.stderr @@ -0,0 +1,280 @@ +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | &*ptr::slice_from_raw_parts(data, len) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | dereferencing pointer failed: null pointer is not a valid pointer + | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:15:34 + | +LL | pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) }; + | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:15:34 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | &*ptr::slice_from_raw_parts(data, len) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | dereferencing pointer failed: null pointer is not a valid pointer + | inside `std::slice::from_raw_parts::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:16:33 + | +LL | pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) }; + | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:16:33 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | &*ptr::slice_from_raw_parts(data, len) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | dereferencing pointer failed: alloc26 has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds + | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:19:34 + | +LL | pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) }; + | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:19:34 + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:22:1 + | +LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .[0]: encountered uninitialized bytes + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾───────alloc42───────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:24:1 + | +LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .: encountered a pointer, but expected plain (non-pointer) bytes + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾───────alloc55───────╼ 08 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:26:1 + | +LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .[0]: encountered 0x11, but expected a boolean + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾───────alloc65───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:29:1 + | +LL | / pub static S7: &[u16] = unsafe { +LL | | +LL | | let ptr = (&D2 as *const Struct as *const u16).byte_add(1); +LL | | +LL | | from_raw_parts(ptr, 4) +LL | | }; + | |__^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1) + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾─────alloc79+0x1─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | &*ptr::slice_from_raw_parts(data, len) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | dereferencing pointer failed: alloc96 has size 8, so pointer to 8 bytes starting at offset 1 is out-of-bounds + | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:40:5 + | +LL | from_raw_parts(ptr, 1) + | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:40:5 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | out-of-bounds offset_from: null pointer is not a valid pointer + | inside `ptr::const_ptr::::sub_ptr` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } + | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:43:34 + | +LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; + | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:43:34 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | assert!(0 < pointee_size && pointee_size <= isize::MAX as usize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | the evaluated program panicked at 'assertion failed: 0 < pointee_size && pointee_size <= isize::MAX as usize', $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | inside `ptr::const_ptr::::sub_ptr` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } + | ------------------------------ inside `from_ptr_range::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:44:33 + | +LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; + | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:44:33 + | + = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::offset(self, count) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | pointer arithmetic failed: alloc154 has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds + | inside `ptr::const_ptr::::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL +... +LL | unsafe { self.offset(count as isize) } + | --------------------------- inside `ptr::const_ptr::::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:47:25 + | +LL | from_ptr_range(ptr..ptr.add(2)) + | ---------- inside `R2` at $DIR/forbidden_slices.rs:47:25 + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:49:1 + | +LL | / pub static R4: &[u8] = unsafe { +LL | | +LL | | let ptr = (&D1) as *const MaybeUninit<&u32> as *const u8; +LL | | from_ptr_range(ptr..ptr.add(1)) +LL | | }; + | |__^ type validation failed at .[0]: encountered uninitialized bytes + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾──────alloc159───────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:54:1 + | +LL | / pub static R5: &[u8] = unsafe { +LL | | +LL | | let ptr = &D3 as *const &u32; +LL | | from_ptr_range(ptr.cast()..ptr.add(1).cast()) +LL | | }; + | |__^ type validation failed at .: encountered a pointer, but expected plain (non-pointer) bytes + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾──────alloc175───────╼ 08 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:59:1 + | +LL | / pub static R6: &[bool] = unsafe { +LL | | +LL | | let ptr = &D0 as *const u32 as *const bool; +LL | | from_ptr_range(ptr..ptr.add(4)) +LL | | }; + | |__^ type validation failed at .[0]: encountered 0x11, but expected a boolean + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾──────alloc191───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:64:1 + | +LL | / pub static R7: &[u16] = unsafe { +LL | | +LL | | let ptr = (&D2 as *const Struct as *const u16).byte_add(1); +LL | | from_ptr_range(ptr..ptr.add(4)) +LL | | }; + | |__^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1) + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾────alloc209+0x1─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::offset(self, count) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | pointer arithmetic failed: alloc230 has size 8, so pointer to 8 bytes starting at offset 1 is out-of-bounds + | inside `ptr::const_ptr::::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL +... +LL | unsafe { self.offset(count as isize) } + | --------------------------- inside `ptr::const_ptr::::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:71:25 + | +LL | from_ptr_range(ptr..ptr.add(1)) + | ---------- inside `R8` at $DIR/forbidden_slices.rs:71:25 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | ptr_offset_from_unsigned cannot compute offset of pointers into different allocations. + | inside `ptr::const_ptr::::sub_ptr` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } + | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:76:34 + | +LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) }; + | ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:76:34 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | ptr_offset_from_unsigned cannot compute offset of pointers into different allocations. + | inside `ptr::const_ptr::::sub_ptr` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } + | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:77:35 + | +LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) }; + | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:77:35 + +error: aborting due to 18 previous errors + +For more information about this error, try `rustc --explain E0080`. From e1efb5e4578e60d2cf9d937582c333aa5e49287a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 27 May 2022 19:09:46 +0400 Subject: [PATCH 04/17] Rename slice_from_ptr_range_const -> const_slice_from_ptr_range This is in line with other `const fn` features. --- library/core/src/slice/raw.rs | 2 +- src/test/ui/const-ptr/allowed_slices.rs | 2 +- src/test/ui/const-ptr/forbidden_slices.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs index fa24a481a1f60..8ce1d18caaeb9 100644 --- a/library/core/src/slice/raw.rs +++ b/library/core/src/slice/raw.rs @@ -213,7 +213,7 @@ pub const fn from_mut(s: &mut T) -> &mut [T] { /// /// [valid]: ptr#safety #[unstable(feature = "slice_from_ptr_range", issue = "89792")] -#[rustc_const_unstable(feature = "slice_from_ptr_range_const", issue = "89792")] +#[rustc_const_unstable(feature = "const_slice_from_ptr_range", issue = "89792")] pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { // SAFETY: the caller must uphold the safety contract for `from_ptr_range`. unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } diff --git a/src/test/ui/const-ptr/allowed_slices.rs b/src/test/ui/const-ptr/allowed_slices.rs index 12089c0a8e8d7..645283d8fe5ac 100644 --- a/src/test/ui/const-ptr/allowed_slices.rs +++ b/src/test/ui/const-ptr/allowed_slices.rs @@ -2,7 +2,7 @@ #![feature( const_slice_from_raw_parts, slice_from_ptr_range, - slice_from_ptr_range_const, + const_slice_from_ptr_range, pointer_byte_offsets, const_pointer_byte_offsets )] diff --git a/src/test/ui/const-ptr/forbidden_slices.rs b/src/test/ui/const-ptr/forbidden_slices.rs index e68b8da48212e..7718d830e6aec 100644 --- a/src/test/ui/const-ptr/forbidden_slices.rs +++ b/src/test/ui/const-ptr/forbidden_slices.rs @@ -1,7 +1,7 @@ #![feature( const_slice_from_raw_parts, slice_from_ptr_range, - slice_from_ptr_range_const, + const_slice_from_ptr_range, pointer_byte_offsets, const_pointer_byte_offsets )] From 5a6fd10ec9a6a65dabf6eb1bbb38bf8d6f06a611 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 29 May 2022 13:27:05 +0400 Subject: [PATCH 05/17] Use `// error-pattern:` header in `forbidden_slices.rs` test --- src/test/ui/const-ptr/forbidden_slices.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/ui/const-ptr/forbidden_slices.rs b/src/test/ui/const-ptr/forbidden_slices.rs index 7718d830e6aec..e76b9749ee199 100644 --- a/src/test/ui/const-ptr/forbidden_slices.rs +++ b/src/test/ui/const-ptr/forbidden_slices.rs @@ -1,3 +1,4 @@ +// error-pattern: could not evaluate static initializer #![feature( const_slice_from_raw_parts, slice_from_ptr_range, From f4fff0486a45ad12125ef70e7bed92e3fddca91e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 29 May 2022 13:43:01 +0400 Subject: [PATCH 06/17] --bless --- src/test/ui/const-ptr/forbidden_slices.stderr | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/test/ui/const-ptr/forbidden_slices.stderr b/src/test/ui/const-ptr/forbidden_slices.stderr index 5bc8e2c10d385..a48cd1abef15c 100644 --- a/src/test/ui/const-ptr/forbidden_slices.stderr +++ b/src/test/ui/const-ptr/forbidden_slices.stderr @@ -7,10 +7,10 @@ LL | &*ptr::slice_from_raw_parts(data, len) | dereferencing pointer failed: null pointer is not a valid pointer | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:15:34 + ::: $DIR/forbidden_slices.rs:16:34 | LL | pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) }; - | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:15:34 + | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:16:34 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/slice/raw.rs:LL:COL @@ -21,10 +21,10 @@ LL | &*ptr::slice_from_raw_parts(data, len) | dereferencing pointer failed: null pointer is not a valid pointer | inside `std::slice::from_raw_parts::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:16:33 + ::: $DIR/forbidden_slices.rs:17:33 | LL | pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) }; - | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:16:33 + | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:17:33 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/slice/raw.rs:LL:COL @@ -35,13 +35,13 @@ LL | &*ptr::slice_from_raw_parts(data, len) | dereferencing pointer failed: alloc26 has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:19:34 + ::: $DIR/forbidden_slices.rs:20:34 | LL | pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) }; - | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:19:34 + | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:20:34 error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:22:1 + --> $DIR/forbidden_slices.rs:23:1 | LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .[0]: encountered uninitialized bytes @@ -52,7 +52,7 @@ LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) } } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:24:1 + --> $DIR/forbidden_slices.rs:25:1 | LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .: encountered a pointer, but expected plain (non-pointer) bytes @@ -63,7 +63,7 @@ LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:26:1 + --> $DIR/forbidden_slices.rs:27:1 | LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .[0]: encountered 0x11, but expected a boolean @@ -74,7 +74,7 @@ LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:29:1 + --> $DIR/forbidden_slices.rs:30:1 | LL | / pub static S7: &[u16] = unsafe { LL | | @@ -98,10 +98,10 @@ LL | &*ptr::slice_from_raw_parts(data, len) | dereferencing pointer failed: alloc96 has size 8, so pointer to 8 bytes starting at offset 1 is out-of-bounds | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:40:5 + ::: $DIR/forbidden_slices.rs:41:5 | LL | from_raw_parts(ptr, 1) - | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:40:5 + | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:41:5 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -117,10 +117,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:43:34 + ::: $DIR/forbidden_slices.rs:44:34 | LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; - | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:43:34 + | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:44:34 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -136,10 +136,10 @@ LL | assert!(0 < pointee_size && pointee_size <= isize::MAX as usize); LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } | ------------------------------ inside `from_ptr_range::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:44:33 + ::: $DIR/forbidden_slices.rs:45:33 | LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; - | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:44:33 + | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:45:33 | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -155,13 +155,13 @@ LL | unsafe { intrinsics::offset(self, count) } LL | unsafe { self.offset(count as isize) } | --------------------------- inside `ptr::const_ptr::::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:47:25 + ::: $DIR/forbidden_slices.rs:48:25 | LL | from_ptr_range(ptr..ptr.add(2)) - | ---------- inside `R2` at $DIR/forbidden_slices.rs:47:25 + | ---------- inside `R2` at $DIR/forbidden_slices.rs:48:25 error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:49:1 + --> $DIR/forbidden_slices.rs:50:1 | LL | / pub static R4: &[u8] = unsafe { LL | | @@ -176,7 +176,7 @@ LL | | }; } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:54:1 + --> $DIR/forbidden_slices.rs:55:1 | LL | / pub static R5: &[u8] = unsafe { LL | | @@ -191,7 +191,7 @@ LL | | }; } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:59:1 + --> $DIR/forbidden_slices.rs:60:1 | LL | / pub static R6: &[bool] = unsafe { LL | | @@ -206,7 +206,7 @@ LL | | }; } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:64:1 + --> $DIR/forbidden_slices.rs:65:1 | LL | / pub static R7: &[u16] = unsafe { LL | | @@ -232,10 +232,10 @@ LL | unsafe { intrinsics::offset(self, count) } LL | unsafe { self.offset(count as isize) } | --------------------------- inside `ptr::const_ptr::::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:71:25 + ::: $DIR/forbidden_slices.rs:72:25 | LL | from_ptr_range(ptr..ptr.add(1)) - | ---------- inside `R8` at $DIR/forbidden_slices.rs:71:25 + | ---------- inside `R8` at $DIR/forbidden_slices.rs:72:25 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -251,10 +251,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:76:34 + ::: $DIR/forbidden_slices.rs:77:34 | LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) }; - | ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:76:34 + | ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:77:34 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -270,10 +270,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:77:35 + ::: $DIR/forbidden_slices.rs:78:35 | LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) }; - | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:77:35 + | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:78:35 error: aborting due to 18 previous errors From 9f68b998db87e030c5a9a96fdb99f3dcd8911932 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Sun, 29 May 2022 15:22:25 +0200 Subject: [PATCH 07/17] Fix order of closing HTML elements in rustdoc output --- src/librustdoc/html/render/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index eefb2c2358fbd..b8d6b340b14dd 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1764,7 +1764,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { write!(buffer, "
  • Version {}
  • ", Escape(version)); } write!(buffer, "
  • All Items
  • "); - buffer.write_str(""); + buffer.write_str(""); } match *it.kind { From 3414c03d7daf2f280287efb846ca8bd7efd7146e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 29 May 2022 20:31:51 +0400 Subject: [PATCH 08/17] test forbidden slices on all two usizesizes --- .../const-ptr/forbidden_slices.32bit.stderr | 280 ++++++++++++++++++ ...s.stderr => forbidden_slices.64bit.stderr} | 56 ++-- src/test/ui/const-ptr/forbidden_slices.rs | 1 + 3 files changed, 309 insertions(+), 28 deletions(-) create mode 100644 src/test/ui/const-ptr/forbidden_slices.32bit.stderr rename src/test/ui/const-ptr/{forbidden_slices.stderr => forbidden_slices.64bit.stderr} (92%) diff --git a/src/test/ui/const-ptr/forbidden_slices.32bit.stderr b/src/test/ui/const-ptr/forbidden_slices.32bit.stderr new file mode 100644 index 0000000000000..d376548b9b925 --- /dev/null +++ b/src/test/ui/const-ptr/forbidden_slices.32bit.stderr @@ -0,0 +1,280 @@ +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | &*ptr::slice_from_raw_parts(data, len) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | dereferencing pointer failed: null pointer is not a valid pointer + | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:17:34 + | +LL | pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) }; + | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:17:34 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | &*ptr::slice_from_raw_parts(data, len) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | dereferencing pointer failed: null pointer is not a valid pointer + | inside `std::slice::from_raw_parts::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:18:33 + | +LL | pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) }; + | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:18:33 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | &*ptr::slice_from_raw_parts(data, len) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | dereferencing pointer failed: alloc26 has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds + | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:21:34 + | +LL | pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) }; + | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:21:34 + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:24:1 + | +LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .[0]: encountered uninitialized bytes + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾─alloc42─╼ 01 00 00 00 │ ╾──╼.... + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:26:1 + | +LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .: encountered a pointer, but expected plain (non-pointer) bytes + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾─alloc55─╼ 04 00 00 00 │ ╾──╼.... + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:28:1 + | +LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .[0]: encountered 0x11, but expected a boolean + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾─alloc65─╼ 04 00 00 00 │ ╾──╼.... + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:31:1 + | +LL | / pub static S7: &[u16] = unsafe { +LL | | +LL | | let ptr = (&D2 as *const Struct as *const u16).byte_add(1); +LL | | +LL | | from_raw_parts(ptr, 4) +LL | | }; + | |__^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1) + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾─a79+0x1─╼ 04 00 00 00 │ ╾──╼.... + } + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | &*ptr::slice_from_raw_parts(data, len) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | dereferencing pointer failed: alloc96 has size 8, so pointer to 8 bytes starting at offset 1 is out-of-bounds + | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:42:5 + | +LL | from_raw_parts(ptr, 1) + | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:42:5 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | out-of-bounds offset_from: null pointer is not a valid pointer + | inside `ptr::const_ptr::::sub_ptr` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } + | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:45:34 + | +LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; + | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:45:34 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | assert!(0 < pointee_size && pointee_size <= isize::MAX as usize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | the evaluated program panicked at 'assertion failed: 0 < pointee_size && pointee_size <= isize::MAX as usize', $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | inside `ptr::const_ptr::::sub_ptr` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } + | ------------------------------ inside `from_ptr_range::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:46:33 + | +LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; + | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:46:33 + | + = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::offset(self, count) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | pointer arithmetic failed: alloc154 has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds + | inside `ptr::const_ptr::::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL +... +LL | unsafe { self.offset(count as isize) } + | --------------------------- inside `ptr::const_ptr::::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:49:25 + | +LL | from_ptr_range(ptr..ptr.add(2)) + | ---------- inside `R2` at $DIR/forbidden_slices.rs:49:25 + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:51:1 + | +LL | / pub static R4: &[u8] = unsafe { +LL | | +LL | | let ptr = (&D1) as *const MaybeUninit<&u32> as *const u8; +LL | | from_ptr_range(ptr..ptr.add(1)) +LL | | }; + | |__^ type validation failed at .[0]: encountered uninitialized bytes + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾alloc159─╼ 01 00 00 00 │ ╾──╼.... + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:56:1 + | +LL | / pub static R5: &[u8] = unsafe { +LL | | +LL | | let ptr = &D3 as *const &u32; +LL | | from_ptr_range(ptr.cast()..ptr.add(1).cast()) +LL | | }; + | |__^ type validation failed at .: encountered a pointer, but expected plain (non-pointer) bytes + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾alloc175─╼ 04 00 00 00 │ ╾──╼.... + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:61:1 + | +LL | / pub static R6: &[bool] = unsafe { +LL | | +LL | | let ptr = &D0 as *const u32 as *const bool; +LL | | from_ptr_range(ptr..ptr.add(4)) +LL | | }; + | |__^ type validation failed at .[0]: encountered 0x11, but expected a boolean + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾alloc191─╼ 04 00 00 00 │ ╾──╼.... + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/forbidden_slices.rs:66:1 + | +LL | / pub static R7: &[u16] = unsafe { +LL | | +LL | | let ptr = (&D2 as *const Struct as *const u16).byte_add(1); +LL | | from_ptr_range(ptr..ptr.add(4)) +LL | | }; + | |__^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1) + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾a209+0x1─╼ 04 00 00 00 │ ╾──╼.... + } + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::offset(self, count) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | pointer arithmetic failed: alloc230 has size 8, so pointer to 8 bytes starting at offset 1 is out-of-bounds + | inside `ptr::const_ptr::::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL +... +LL | unsafe { self.offset(count as isize) } + | --------------------------- inside `ptr::const_ptr::::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:73:25 + | +LL | from_ptr_range(ptr..ptr.add(1)) + | ---------- inside `R8` at $DIR/forbidden_slices.rs:73:25 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | ptr_offset_from_unsigned cannot compute offset of pointers into different allocations. + | inside `ptr::const_ptr::::sub_ptr` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } + | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:78:34 + | +LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) }; + | ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:78:34 + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | +LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | ptr_offset_from_unsigned cannot compute offset of pointers into different allocations. + | inside `ptr::const_ptr::::sub_ptr` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + | + ::: $SRC_DIR/core/src/slice/raw.rs:LL:COL + | +LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } + | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL + | + ::: $DIR/forbidden_slices.rs:79:35 + | +LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) }; + | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:79:35 + +error: aborting due to 18 previous errors + +For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/const-ptr/forbidden_slices.stderr b/src/test/ui/const-ptr/forbidden_slices.64bit.stderr similarity index 92% rename from src/test/ui/const-ptr/forbidden_slices.stderr rename to src/test/ui/const-ptr/forbidden_slices.64bit.stderr index a48cd1abef15c..cb294b16310a9 100644 --- a/src/test/ui/const-ptr/forbidden_slices.stderr +++ b/src/test/ui/const-ptr/forbidden_slices.64bit.stderr @@ -7,10 +7,10 @@ LL | &*ptr::slice_from_raw_parts(data, len) | dereferencing pointer failed: null pointer is not a valid pointer | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:16:34 + ::: $DIR/forbidden_slices.rs:17:34 | LL | pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) }; - | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:16:34 + | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:17:34 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/slice/raw.rs:LL:COL @@ -21,10 +21,10 @@ LL | &*ptr::slice_from_raw_parts(data, len) | dereferencing pointer failed: null pointer is not a valid pointer | inside `std::slice::from_raw_parts::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:17:33 + ::: $DIR/forbidden_slices.rs:18:33 | LL | pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) }; - | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:17:33 + | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:18:33 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/slice/raw.rs:LL:COL @@ -35,13 +35,13 @@ LL | &*ptr::slice_from_raw_parts(data, len) | dereferencing pointer failed: alloc26 has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:20:34 + ::: $DIR/forbidden_slices.rs:21:34 | LL | pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) }; - | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:20:34 + | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:21:34 error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:23:1 + --> $DIR/forbidden_slices.rs:24:1 | LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .[0]: encountered uninitialized bytes @@ -52,7 +52,7 @@ LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) } } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:25:1 + --> $DIR/forbidden_slices.rs:26:1 | LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .: encountered a pointer, but expected plain (non-pointer) bytes @@ -63,7 +63,7 @@ LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:27:1 + --> $DIR/forbidden_slices.rs:28:1 | LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .[0]: encountered 0x11, but expected a boolean @@ -74,7 +74,7 @@ LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:30:1 + --> $DIR/forbidden_slices.rs:31:1 | LL | / pub static S7: &[u16] = unsafe { LL | | @@ -98,10 +98,10 @@ LL | &*ptr::slice_from_raw_parts(data, len) | dereferencing pointer failed: alloc96 has size 8, so pointer to 8 bytes starting at offset 1 is out-of-bounds | inside `std::slice::from_raw_parts::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:41:5 + ::: $DIR/forbidden_slices.rs:42:5 | LL | from_raw_parts(ptr, 1) - | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:41:5 + | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:42:5 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -117,10 +117,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:44:34 + ::: $DIR/forbidden_slices.rs:45:34 | LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; - | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:44:34 + | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:45:34 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -136,10 +136,10 @@ LL | assert!(0 < pointee_size && pointee_size <= isize::MAX as usize); LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } | ------------------------------ inside `from_ptr_range::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:45:33 + ::: $DIR/forbidden_slices.rs:46:33 | LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; - | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:45:33 + | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:46:33 | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -155,13 +155,13 @@ LL | unsafe { intrinsics::offset(self, count) } LL | unsafe { self.offset(count as isize) } | --------------------------- inside `ptr::const_ptr::::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:48:25 + ::: $DIR/forbidden_slices.rs:49:25 | LL | from_ptr_range(ptr..ptr.add(2)) - | ---------- inside `R2` at $DIR/forbidden_slices.rs:48:25 + | ---------- inside `R2` at $DIR/forbidden_slices.rs:49:25 error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:50:1 + --> $DIR/forbidden_slices.rs:51:1 | LL | / pub static R4: &[u8] = unsafe { LL | | @@ -176,7 +176,7 @@ LL | | }; } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:55:1 + --> $DIR/forbidden_slices.rs:56:1 | LL | / pub static R5: &[u8] = unsafe { LL | | @@ -191,7 +191,7 @@ LL | | }; } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:60:1 + --> $DIR/forbidden_slices.rs:61:1 | LL | / pub static R6: &[bool] = unsafe { LL | | @@ -206,7 +206,7 @@ LL | | }; } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:65:1 + --> $DIR/forbidden_slices.rs:66:1 | LL | / pub static R7: &[u16] = unsafe { LL | | @@ -232,10 +232,10 @@ LL | unsafe { intrinsics::offset(self, count) } LL | unsafe { self.offset(count as isize) } | --------------------------- inside `ptr::const_ptr::::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:72:25 + ::: $DIR/forbidden_slices.rs:73:25 | LL | from_ptr_range(ptr..ptr.add(1)) - | ---------- inside `R8` at $DIR/forbidden_slices.rs:72:25 + | ---------- inside `R8` at $DIR/forbidden_slices.rs:73:25 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -251,10 +251,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:77:34 + ::: $DIR/forbidden_slices.rs:78:34 | LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) }; - | ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:77:34 + | ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:78:34 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -270,10 +270,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } | ------------------------------ inside `from_ptr_range::` at $SRC_DIR/core/src/slice/raw.rs:LL:COL | - ::: $DIR/forbidden_slices.rs:78:35 + ::: $DIR/forbidden_slices.rs:79:35 | LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) }; - | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:78:35 + | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:79:35 error: aborting due to 18 previous errors diff --git a/src/test/ui/const-ptr/forbidden_slices.rs b/src/test/ui/const-ptr/forbidden_slices.rs index e76b9749ee199..b29671b7f008d 100644 --- a/src/test/ui/const-ptr/forbidden_slices.rs +++ b/src/test/ui/const-ptr/forbidden_slices.rs @@ -1,3 +1,4 @@ +// stderr-per-bitwidth // error-pattern: could not evaluate static initializer #![feature( const_slice_from_raw_parts, From 6b4191f5e03db60f7cae7d892af368ce498412c6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 May 2022 21:38:52 +0200 Subject: [PATCH 09/17] Add "no-const-assign" eslint rule --- src/librustdoc/html/static/.eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/static/.eslintrc.js b/src/librustdoc/html/static/.eslintrc.js index 997def1657fa0..651aacec5131b 100644 --- a/src/librustdoc/html/static/.eslintrc.js +++ b/src/librustdoc/html/static/.eslintrc.js @@ -63,5 +63,6 @@ module.exports = { } ], "eqeqeq": "error", + "no-const-assign": "error", } }; From 4bb728da799f9d2332d14a3790637366bb269d5a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 May 2022 21:39:44 +0200 Subject: [PATCH 10/17] Add "no-debugger" eslint rule --- src/librustdoc/html/static/.eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/static/.eslintrc.js b/src/librustdoc/html/static/.eslintrc.js index 651aacec5131b..7d7affded7efb 100644 --- a/src/librustdoc/html/static/.eslintrc.js +++ b/src/librustdoc/html/static/.eslintrc.js @@ -64,5 +64,6 @@ module.exports = { ], "eqeqeq": "error", "no-const-assign": "error", + "no-debugger": "error", } }; From 9f79a03ca507e2aa319f3dd9c9dac6faa568bc22 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 May 2022 21:40:16 +0200 Subject: [PATCH 11/17] Add "no-dup-args" eslint rule --- src/librustdoc/html/static/.eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/static/.eslintrc.js b/src/librustdoc/html/static/.eslintrc.js index 7d7affded7efb..b83c1a2459a81 100644 --- a/src/librustdoc/html/static/.eslintrc.js +++ b/src/librustdoc/html/static/.eslintrc.js @@ -65,5 +65,6 @@ module.exports = { "eqeqeq": "error", "no-const-assign": "error", "no-debugger": "error", + "no-dupe-args": "error", } }; From 397ad826b07576c5ce928bc63065a5758db12d54 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 May 2022 21:41:05 +0200 Subject: [PATCH 12/17] Add "no-dupe-else-if" eslint rule --- src/librustdoc/html/static/.eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/static/.eslintrc.js b/src/librustdoc/html/static/.eslintrc.js index b83c1a2459a81..35a1a27db9165 100644 --- a/src/librustdoc/html/static/.eslintrc.js +++ b/src/librustdoc/html/static/.eslintrc.js @@ -66,5 +66,6 @@ module.exports = { "no-const-assign": "error", "no-debugger": "error", "no-dupe-args": "error", + "no-dupe-else-if": "error", } }; From 88c3d707a6e7869d2a80dbcdc923a535f095641f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 May 2022 21:42:11 +0200 Subject: [PATCH 13/17] Add "no-dupe-keys" eslint rule --- src/librustdoc/html/static/.eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/static/.eslintrc.js b/src/librustdoc/html/static/.eslintrc.js index 35a1a27db9165..f98a6c01fe96a 100644 --- a/src/librustdoc/html/static/.eslintrc.js +++ b/src/librustdoc/html/static/.eslintrc.js @@ -67,5 +67,6 @@ module.exports = { "no-debugger": "error", "no-dupe-args": "error", "no-dupe-else-if": "error", + "no-dupe-keys": "error", } }; From d39703a6c53a78d477360a15ffd36921449fee93 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 May 2022 21:43:37 +0200 Subject: [PATCH 14/17] Add "no-duplicate-case" eslint rule --- src/librustdoc/html/static/.eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/static/.eslintrc.js b/src/librustdoc/html/static/.eslintrc.js index f98a6c01fe96a..f153c9a714f8e 100644 --- a/src/librustdoc/html/static/.eslintrc.js +++ b/src/librustdoc/html/static/.eslintrc.js @@ -68,5 +68,6 @@ module.exports = { "no-dupe-args": "error", "no-dupe-else-if": "error", "no-dupe-keys": "error", + "no-duplicate-case": "error", } }; From f1a95b834c2d852428e72bf1a17ff99c2cfe605b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 May 2022 21:44:10 +0200 Subject: [PATCH 15/17] Add "no-ex-assign" eslint rule --- src/librustdoc/html/static/.eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/static/.eslintrc.js b/src/librustdoc/html/static/.eslintrc.js index f153c9a714f8e..f66ecbf78af7b 100644 --- a/src/librustdoc/html/static/.eslintrc.js +++ b/src/librustdoc/html/static/.eslintrc.js @@ -69,5 +69,6 @@ module.exports = { "no-dupe-else-if": "error", "no-dupe-keys": "error", "no-duplicate-case": "error", + "no-ex-assign": "error", } }; From 46d34cc9225fd11810d143ba3c78476bfe30e63d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 28 May 2022 13:34:54 -0700 Subject: [PATCH 16/17] Use type_is_copy_modulo_regions check in intrisicck --- compiler/rustc_typeck/src/check/intrinsicck.rs | 3 ++- src/test/ui/asm/issue-97490.rs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/asm/issue-97490.rs diff --git a/compiler/rustc_typeck/src/check/intrinsicck.rs b/compiler/rustc_typeck/src/check/intrinsicck.rs index 027868be8bb0d..05a30d76d4e29 100644 --- a/compiler/rustc_typeck/src/check/intrinsicck.rs +++ b/compiler/rustc_typeck/src/check/intrinsicck.rs @@ -9,6 +9,7 @@ use rustc_session::lint; use rustc_span::{Span, Symbol, DUMMY_SP}; use rustc_target::abi::{Pointer, VariantIdx}; use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType}; +use rustc_trait_selection::infer::InferCtxtExt; use super::FnCtxt; @@ -210,7 +211,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Check that the type implements Copy. The only case where this can // possibly fail is for SIMD types which don't #[derive(Copy)]. - if !ty.is_copy_modulo_regions(self.tcx.at(DUMMY_SP), self.param_env) { + if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, DUMMY_SP) { let msg = "arguments for inline assembly must be copyable"; let mut err = self.tcx.sess.struct_span_err(expr.span, msg); err.note(&format!("`{ty}` does not implement the Copy trait")); diff --git a/src/test/ui/asm/issue-97490.rs b/src/test/ui/asm/issue-97490.rs new file mode 100644 index 0000000000000..37862cf349cfc --- /dev/null +++ b/src/test/ui/asm/issue-97490.rs @@ -0,0 +1,12 @@ +// check-pass +// only-x86_64 +// needs-asm-support + +pub type Yes = extern "sysv64" fn(&'static u8) -> !; + +fn main() { + unsafe { + let yes = &6 as *const _ as *const Yes; + core::arch::asm!("call {}", in(reg) yes, options(noreturn)); + } +} From 311aacf0d0bcdbb9874d83e1f8fe0fb76aeecb96 Mon Sep 17 00:00:00 2001 From: est31 Date: Sun, 29 May 2022 23:30:54 +0200 Subject: [PATCH 17/17] Remove unused lifetimes from expand_macro The function doesn't need the lifetimes of the two arguments be bound together. --- compiler/rustc_expand/src/mbe/macro_rules.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index b16fa7111c519..b86304ba6b1b1 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -204,7 +204,7 @@ fn trace_macros_note(cx_expansions: &mut FxHashMap>, sp: Span, /// Expands the rules based macro defined by `lhses` and `rhses` for a given /// input `arg`. -fn expand_macro<'cx, 'tt>( +fn expand_macro<'cx>( cx: &'cx mut ExtCtxt<'_>, sp: Span, def_span: Span, @@ -212,8 +212,8 @@ fn expand_macro<'cx, 'tt>( name: Ident, transparency: Transparency, arg: TokenStream, - lhses: &'tt [Vec], - rhses: &'tt [mbe::TokenTree], + lhses: &[Vec], + rhses: &[mbe::TokenTree], ) -> Box { let sess = &cx.sess.parse_sess; // Macros defined in the current crate have a real node id,