From cf30ac847decca2caaaaf8a2591552bce380c14d Mon Sep 17 00:00:00 2001 From: DeveloperC Date: Fri, 8 Oct 2021 22:02:31 +0100 Subject: [PATCH 01/16] refactor: VecDeques Iter fields to private Made the fields of VecDeque's Iter private by creating a Iter::new(...) function to create a new instance of Iter and migrating usage to use Iter::new(...). --- .../alloc/src/collections/vec_deque/iter.rs | 12 ++++++++--- .../alloc/src/collections/vec_deque/mod.rs | 21 +++++++------------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/iter.rs b/library/alloc/src/collections/vec_deque/iter.rs index e8290809276fb..5139db7f451a5 100644 --- a/library/alloc/src/collections/vec_deque/iter.rs +++ b/library/alloc/src/collections/vec_deque/iter.rs @@ -13,9 +13,15 @@ use super::{count, wrap_index, RingSlices}; /// [`iter`]: super::VecDeque::iter #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, T: 'a> { - pub(crate) ring: &'a [MaybeUninit], - pub(crate) tail: usize, - pub(crate) head: usize, + ring: &'a [MaybeUninit], + tail: usize, + head: usize, +} + +impl<'a, T> Iter<'a, T> { + pub(super) fn new(ring: &'a [MaybeUninit], tail: usize, head: usize) -> Self { + Iter { ring, tail, head } + } } #[stable(feature = "collection_debug", since = "1.17.0")] diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 63280e56332cb..e1e0276fc8d86 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -1009,7 +1009,7 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn iter(&self) -> Iter<'_, T> { - Iter { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_slice() } } + Iter::new(unsafe { self.buffer_as_slice() }, self.tail, self.head) } /// Returns a front-to-back iterator that returns mutable references. @@ -1188,12 +1188,8 @@ impl VecDeque { R: RangeBounds, { let (tail, head) = self.range_tail_head(range); - Iter { - tail, - head, - // The shared reference we have in &self is maintained in the '_ of Iter. - ring: unsafe { self.buffer_as_slice() }, - } + // The shared reference we have in &self is maintained in the '_ of Iter. + Iter::new(unsafe { self.buffer_as_slice() }, tail, head) } /// Creates an iterator that covers the specified mutable range in the deque. @@ -1309,16 +1305,15 @@ impl VecDeque { self.head = drain_tail; let deque = NonNull::from(&mut *self); - let iter = Iter { - tail: drain_tail, - head: drain_head, + unsafe { // Crucially, we only create shared references from `self` here and read from // it. We do not write to `self` nor reborrow to a mutable reference. // Hence the raw pointer we created above, for `deque`, remains valid. - ring: unsafe { self.buffer_as_slice() }, - }; + let ring = self.buffer_as_slice(); + let iter = Iter::new(ring, drain_tail, drain_head); - unsafe { Drain::new(drain_head, head, iter, deque) } + Drain::new(drain_head, head, iter, deque) + } } /// Clears the deque, removing all values. From d7a2d9ae0e7e4b3c5811bdfd4809cfc772062140 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 25 May 2022 14:29:46 +0200 Subject: [PATCH 02/16] Miri call ABI check: ensure type size+align stay the same --- compiler/rustc_const_eval/src/interpret/terminator.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_const_eval/src/interpret/terminator.rs b/compiler/rustc_const_eval/src/interpret/terminator.rs index a5c7d4c8e20dc..10da2f803afe7 100644 --- a/compiler/rustc_const_eval/src/interpret/terminator.rs +++ b/compiler/rustc_const_eval/src/interpret/terminator.rs @@ -185,7 +185,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // No question return true; } - // Compare layout + if caller_abi.layout.size != callee_abi.layout.size + || caller_abi.layout.align.abi != callee_abi.layout.align.abi + { + // This cannot go well... + // FIXME: What about unsized types? + return false; + } + // The rest *should* be okay, but we are extra conservative. match (caller_abi.layout.abi, callee_abi.layout.abi) { // Different valid ranges are okay (once we enforce validity, // that will take care to make it UB to leave the range, just From 0be2ca96fa7d723db870fb2f96df0f07d32c0774 Mon Sep 17 00:00:00 2001 From: SparrowLii Date: Mon, 30 May 2022 15:56:43 +0800 Subject: [PATCH 03/16] Optimize the diagnostic generation for `extern unsafe` --- compiler/rustc_parse/src/parser/item.rs | 41 +++++++------------ src/test/ui/parser/issues/issue-19398.stderr | 7 +--- src/test/ui/parser/unsafe-foreign-mod-2.rs | 8 ++++ .../ui/parser/unsafe-foreign-mod-2.stderr | 28 +++++++++++++ 4 files changed, 52 insertions(+), 32 deletions(-) create mode 100644 src/test/ui/parser/unsafe-foreign-mod-2.rs create mode 100644 src/test/ui/parser/unsafe-foreign-mod-2.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 0f940cffcc463..5c9943b270fde 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -997,35 +997,24 @@ impl<'a> Parser<'a> { fn parse_item_foreign_mod( &mut self, attrs: &mut Vec, - unsafety: Unsafe, + mut unsafety: Unsafe, ) -> PResult<'a, ItemInfo> { - let sp_start = self.prev_token.span; let abi = self.parse_abi(); // ABI? - match self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No)) { - Ok(items) => { - let module = ast::ForeignMod { unsafety, abi, items }; - Ok((Ident::empty(), ItemKind::ForeignMod(module))) - } - Err(mut err) => { - let current_qual_sp = self.prev_token.span; - let current_qual_sp = current_qual_sp.to(sp_start); - if let Ok(current_qual) = self.span_to_snippet(current_qual_sp) { - // FIXME(davidtwco): avoid depending on the error message text - if err.message[0].0.expect_str() == "expected `{`, found keyword `unsafe`" { - let invalid_qual_sp = self.token.uninterpolated_span(); - let invalid_qual = self.span_to_snippet(invalid_qual_sp).unwrap(); - - err.span_suggestion( - current_qual_sp.to(invalid_qual_sp), - &format!("`{}` must come before `{}`", invalid_qual, current_qual), - format!("{} {}", invalid_qual, current_qual), - Applicability::MachineApplicable, - ).note("keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`"); - } - } - Err(err) - } + if unsafety == Unsafe::No + && self.token.is_keyword(kw::Unsafe) + && self.look_ahead(1, |t| t.kind == token::OpenDelim(Delimiter::Brace)) + { + let mut err = self.expect(&token::OpenDelim(Delimiter::Brace)).unwrap_err(); + err.emit(); + unsafety = Unsafe::Yes(self.token.span); + self.eat_keyword(kw::Unsafe); } + let module = ast::ForeignMod { + unsafety, + abi, + items: self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?, + }; + Ok((Ident::empty(), ItemKind::ForeignMod(module))) } /// Parses a foreign item (one in an `extern { ... }` block). diff --git a/src/test/ui/parser/issues/issue-19398.stderr b/src/test/ui/parser/issues/issue-19398.stderr index bbd85374b4bcf..1da00960adfe4 100644 --- a/src/test/ui/parser/issues/issue-19398.stderr +++ b/src/test/ui/parser/issues/issue-19398.stderr @@ -4,15 +4,10 @@ error: expected `{`, found keyword `unsafe` LL | trait T { | - while parsing this item list starting here LL | extern "Rust" unsafe fn foo(); - | --------------^^^^^^ - | | | - | | expected `{` - | help: `unsafe` must come before `extern "Rust"`: `unsafe extern "Rust"` + | ^^^^^^ expected `{` LL | LL | } | - the item list ends here - | - = note: keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern` error: aborting due to previous error diff --git a/src/test/ui/parser/unsafe-foreign-mod-2.rs b/src/test/ui/parser/unsafe-foreign-mod-2.rs new file mode 100644 index 0000000000000..77856fb67340e --- /dev/null +++ b/src/test/ui/parser/unsafe-foreign-mod-2.rs @@ -0,0 +1,8 @@ +extern "C" unsafe { + //~^ ERROR expected `{`, found keyword `unsafe` + //~| ERROR extern block cannot be declared unsafe + unsafe fn foo(); + //~^ ERROR functions in `extern` blocks cannot have qualifiers +} + +fn main() {} diff --git a/src/test/ui/parser/unsafe-foreign-mod-2.stderr b/src/test/ui/parser/unsafe-foreign-mod-2.stderr new file mode 100644 index 0000000000000..7cc2de141ae14 --- /dev/null +++ b/src/test/ui/parser/unsafe-foreign-mod-2.stderr @@ -0,0 +1,28 @@ +error: expected `{`, found keyword `unsafe` + --> $DIR/unsafe-foreign-mod-2.rs:1:12 + | +LL | extern "C" unsafe { + | ^^^^^^ expected `{` + +error: extern block cannot be declared unsafe + --> $DIR/unsafe-foreign-mod-2.rs:1:12 + | +LL | extern "C" unsafe { + | ^^^^^^ + +error: functions in `extern` blocks cannot have qualifiers + --> $DIR/unsafe-foreign-mod-2.rs:4:15 + | +LL | extern "C" unsafe { + | ----------------- in this `extern` block +... +LL | unsafe fn foo(); + | ^^^ + | +help: remove the qualifiers + | +LL | fn foo(); + | ~~ + +error: aborting due to 3 previous errors + From 19caa8c89bc0cec7e34c4e98d12e524c5b3bbcfe Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 26 May 2022 13:06:29 +0400 Subject: [PATCH 04/16] 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 ff9efd8a558a9b279812f07eced7955e22830d8e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 27 May 2022 18:37:33 +0400 Subject: [PATCH 05/16] 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 a63a83a8b6cb2ad5435a798392fe16beb49b65be Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 27 May 2022 19:08:22 +0400 Subject: [PATCH 06/16] 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 10ee6f8e0655c72ff56f9c6781c15fbada09e45e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 27 May 2022 19:09:46 +0400 Subject: [PATCH 07/16] 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 0cca47ea1ad1e966ad8d16f3100a83fe07336916 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 29 May 2022 13:27:05 +0400 Subject: [PATCH 08/16] 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 6d102173826eea9aea770013132e4046784fd37b Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 29 May 2022 13:43:01 +0400 Subject: [PATCH 09/16] --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 1f8a6410bf1f19a427429568e474d5f75c0ba1eb Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 29 May 2022 20:31:51 +0400 Subject: [PATCH 10/16] 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 f669b78ffc9db8352c859d8c83c244975dbf0397 Mon Sep 17 00:00:00 2001 From: David Wood Date: Tue, 24 May 2022 15:09:47 +0100 Subject: [PATCH 11/16] errors: simplify referring to fluent attributes To render the message of a Fluent attribute, the identifier of the Fluent message must be known. `DiagnosticMessage::FluentIdentifier` contains both the message's identifier and optionally the identifier of an attribute. Generated constants for each attribute would therefore need to be named uniquely (amongst all error messages) or be able to refer to only the attribute identifier which will be combined with a message identifier later. In this commit, the latter strategy is implemented as part of the `Diagnostic` type's functions for adding subdiagnostics of various kinds. Signed-off-by: David Wood --- compiler/rustc_error_messages/src/lib.rs | 72 ++++++++++++++-- compiler/rustc_errors/src/diagnostic.rs | 86 +++++++++++-------- .../rustc_errors/src/diagnostic_builder.rs | 47 +++++----- compiler/rustc_errors/src/lib.rs | 3 +- .../src/diagnostics/diagnostic.rs | 36 ++------ .../rustc_macros/src/diagnostics/fluent.rs | 20 +++-- .../src/diagnostics/subdiagnostic.rs | 2 +- .../rustc_parse/src/parser/diagnostics.rs | 11 +-- compiler/rustc_typeck/src/errors.rs | 11 +-- src/test/ui-fulldeps/fluent-messages/test.rs | 6 ++ .../ui-fulldeps/fluent-messages/test.stderr | 10 +-- .../session-diagnostic/diagnostic-derive.rs | 4 - .../diagnostic-derive.stderr | 32 +------ 13 files changed, 190 insertions(+), 150 deletions(-) diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 7faf14a247241..02d076c95ca52 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -234,6 +234,48 @@ pub fn fallback_fluent_bundle( /// Identifier for the Fluent message/attribute corresponding to a diagnostic message. type FluentId = Cow<'static, str>; +/// Abstraction over a message in a subdiagnostic (i.e. label, note, help, etc) to support both +/// translatable and non-translatable diagnostic messages. +/// +/// Translatable messages for subdiagnostics are typically attributes attached to a larger Fluent +/// message so messages of this type must be combined with a `DiagnosticMessage` (using +/// `DiagnosticMessage::with_subdiagnostic_message`) before rendering. However, subdiagnostics from +/// the `SessionSubdiagnostic` derive refer to Fluent identifiers directly. +pub enum SubdiagnosticMessage { + /// Non-translatable diagnostic message. + // FIXME(davidtwco): can a `Cow<'static, str>` be used here? + Str(String), + /// Identifier of a Fluent message. Instances of this variant are generated by the + /// `SessionSubdiagnostic` derive. + FluentIdentifier(FluentId), + /// Attribute of a Fluent message. Needs to be combined with a Fluent identifier to produce an + /// actual translated message. Instances of this variant are generated by the `fluent_messages` + /// macro. + /// + /// + FluentAttr(FluentId), +} + +impl SubdiagnosticMessage { + /// Create a `SubdiagnosticMessage` for the provided Fluent attribute. + pub fn attr(id: impl Into) -> Self { + SubdiagnosticMessage::FluentAttr(id.into()) + } + + /// Create a `SubdiagnosticMessage` for the provided Fluent identifier. + pub fn message(id: impl Into) -> Self { + SubdiagnosticMessage::FluentIdentifier(id.into()) + } +} + +/// `From` impl that enables existing diagnostic calls to functions which now take +/// `impl Into` to continue to work as before. +impl> From for SubdiagnosticMessage { + fn from(s: S) -> Self { + SubdiagnosticMessage::Str(s.into()) + } +} + /// Abstraction over a message in a diagnostic to support both translatable and non-translatable /// diagnostic messages. /// @@ -252,6 +294,29 @@ pub enum DiagnosticMessage { } impl DiagnosticMessage { + /// Given a `SubdiagnosticMessage` which may contain a Fluent attribute, create a new + /// `DiagnosticMessage` that combines that attribute with the Fluent identifier of `self`. + /// + /// - If the `SubdiagnosticMessage` is non-translatable then return the message as a + /// `DiagnosticMessage`. + /// - If `self` is non-translatable then return `self`'s message. + pub fn with_subdiagnostic_message(&self, sub: SubdiagnosticMessage) -> Self { + let attr = match sub { + SubdiagnosticMessage::Str(s) => return DiagnosticMessage::Str(s.clone()), + SubdiagnosticMessage::FluentIdentifier(id) => { + return DiagnosticMessage::FluentIdentifier(id, None); + } + SubdiagnosticMessage::FluentAttr(attr) => attr, + }; + + match self { + DiagnosticMessage::Str(s) => DiagnosticMessage::Str(s.clone()), + DiagnosticMessage::FluentIdentifier(id, _) => { + DiagnosticMessage::FluentIdentifier(id.clone(), Some(attr)) + } + } + } + /// Returns the `String` contained within the `DiagnosticMessage::Str` variant, assuming that /// this diagnostic message is of the legacy, non-translatable variety. Panics if this /// assumption does not hold. @@ -266,14 +331,9 @@ impl DiagnosticMessage { } /// Create a `DiagnosticMessage` for the provided Fluent identifier. - pub fn fluent(id: impl Into) -> Self { + pub fn new(id: impl Into) -> Self { DiagnosticMessage::FluentIdentifier(id.into(), None) } - - /// Create a `DiagnosticMessage` for the provided Fluent identifier and attribute. - pub fn fluent_attr(id: impl Into, attr: impl Into) -> Self { - DiagnosticMessage::FluentIdentifier(id.into(), Some(attr.into())) - } } /// `From` impl that enables existing diagnostic calls to functions which now take diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index f130b5aa9a6b1..643f3c12134c9 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -1,7 +1,7 @@ use crate::snippet::Style; use crate::{ - CodeSuggestion, DiagnosticMessage, Level, MultiSpan, Substitution, SubstitutionPart, - SuggestionStyle, + CodeSuggestion, DiagnosticMessage, Level, MultiSpan, SubdiagnosticMessage, Substitution, + SubstitutionPart, SuggestionStyle, }; use rustc_data_structures::stable_map::FxHashMap; use rustc_error_messages::FluentValue; @@ -283,8 +283,8 @@ impl Diagnostic { /// /// This span is *not* considered a ["primary span"][`MultiSpan`]; only /// the `Span` supplied when creating the diagnostic is primary. - pub fn span_label(&mut self, span: Span, label: impl Into) -> &mut Self { - self.span.push_span_label(span, label.into()); + pub fn span_label(&mut self, span: Span, label: impl Into) -> &mut Self { + self.span.push_span_label(span, self.subdiagnostic_message_to_diagnostic_message(label)); self } @@ -401,12 +401,12 @@ impl Diagnostic { } /// Add a note attached to this diagnostic. - pub fn note(&mut self, msg: impl Into) -> &mut Self { + pub fn note(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::Note, msg, MultiSpan::new(), None); self } - pub fn highlighted_note>( + pub fn highlighted_note>( &mut self, msg: Vec<(M, Style)>, ) -> &mut Self { @@ -416,7 +416,7 @@ impl Diagnostic { /// Prints the span with a note above it. /// This is like [`Diagnostic::note()`], but it gets its own span. - pub fn note_once(&mut self, msg: impl Into) -> &mut Self { + pub fn note_once(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::OnceNote, msg, MultiSpan::new(), None); self } @@ -426,7 +426,7 @@ impl Diagnostic { pub fn span_note>( &mut self, sp: S, - msg: impl Into, + msg: impl Into, ) -> &mut Self { self.sub(Level::Note, msg, sp.into(), None); self @@ -437,14 +437,14 @@ impl Diagnostic { pub fn span_note_once>( &mut self, sp: S, - msg: impl Into, + msg: impl Into, ) -> &mut Self { self.sub(Level::OnceNote, msg, sp.into(), None); self } /// Add a warning attached to this diagnostic. - pub fn warn(&mut self, msg: impl Into) -> &mut Self { + pub fn warn(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::Warning, msg, MultiSpan::new(), None); self } @@ -454,14 +454,14 @@ impl Diagnostic { pub fn span_warn>( &mut self, sp: S, - msg: impl Into, + msg: impl Into, ) -> &mut Self { self.sub(Level::Warning, msg, sp.into(), None); self } /// Add a help message attached to this diagnostic. - pub fn help(&mut self, msg: impl Into) -> &mut Self { + pub fn help(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::Help, msg, MultiSpan::new(), None); self } @@ -477,7 +477,7 @@ impl Diagnostic { pub fn span_help>( &mut self, sp: S, - msg: impl Into, + msg: impl Into, ) -> &mut Self { self.sub(Level::Help, msg, sp.into(), None); self @@ -514,7 +514,7 @@ impl Diagnostic { /// In other words, multiple changes need to be applied as part of this suggestion. pub fn multipart_suggestion( &mut self, - msg: impl Into, + msg: impl Into, suggestion: Vec<(Span, String)>, applicability: Applicability, ) -> &mut Self { @@ -530,7 +530,7 @@ impl Diagnostic { /// In other words, multiple changes need to be applied as part of this suggestion. pub fn multipart_suggestion_verbose( &mut self, - msg: impl Into, + msg: impl Into, suggestion: Vec<(Span, String)>, applicability: Applicability, ) -> &mut Self { @@ -544,7 +544,7 @@ impl Diagnostic { /// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`]. pub fn multipart_suggestion_with_style( &mut self, - msg: impl Into, + msg: impl Into, suggestion: Vec<(Span, String)>, applicability: Applicability, style: SuggestionStyle, @@ -557,7 +557,7 @@ impl Diagnostic { .map(|(span, snippet)| SubstitutionPart { snippet, span }) .collect(), }], - msg: msg.into(), + msg: self.subdiagnostic_message_to_diagnostic_message(msg), style, applicability, }); @@ -572,7 +572,7 @@ impl Diagnostic { /// improve understandability. pub fn tool_only_multipart_suggestion( &mut self, - msg: impl Into, + msg: impl Into, suggestion: Vec<(Span, String)>, applicability: Applicability, ) -> &mut Self { @@ -584,7 +584,7 @@ impl Diagnostic { .map(|(span, snippet)| SubstitutionPart { snippet, span }) .collect(), }], - msg: msg.into(), + msg: self.subdiagnostic_message_to_diagnostic_message(msg), style: SuggestionStyle::CompletelyHidden, applicability, }); @@ -611,7 +611,7 @@ impl Diagnostic { pub fn span_suggestion( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self { @@ -629,7 +629,7 @@ impl Diagnostic { pub fn span_suggestion_with_style( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, style: SuggestionStyle, @@ -638,7 +638,7 @@ impl Diagnostic { substitutions: vec![Substitution { parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }], }], - msg: msg.into(), + msg: self.subdiagnostic_message_to_diagnostic_message(msg), style, applicability, }); @@ -649,7 +649,7 @@ impl Diagnostic { pub fn span_suggestion_verbose( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self { @@ -668,7 +668,7 @@ impl Diagnostic { pub fn span_suggestions( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestions: impl Iterator, applicability: Applicability, ) -> &mut Self { @@ -680,7 +680,7 @@ impl Diagnostic { .collect(); self.push_suggestion(CodeSuggestion { substitutions, - msg: msg.into(), + msg: self.subdiagnostic_message_to_diagnostic_message(msg), style: SuggestionStyle::ShowCode, applicability, }); @@ -691,7 +691,7 @@ impl Diagnostic { /// See also [`Diagnostic::span_suggestion()`]. pub fn multipart_suggestions( &mut self, - msg: impl Into, + msg: impl Into, suggestions: impl Iterator>, applicability: Applicability, ) -> &mut Self { @@ -704,7 +704,7 @@ impl Diagnostic { .collect(), }) .collect(), - msg: msg.into(), + msg: self.subdiagnostic_message_to_diagnostic_message(msg), style: SuggestionStyle::ShowCode, applicability, }); @@ -717,7 +717,7 @@ impl Diagnostic { pub fn span_suggestion_short( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self { @@ -740,7 +740,7 @@ impl Diagnostic { pub fn span_suggestion_hidden( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self { @@ -761,7 +761,7 @@ impl Diagnostic { pub fn tool_only_span_suggestion( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self { @@ -831,6 +831,18 @@ impl Diagnostic { &self.message } + /// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by + /// combining it with the primary message of the diagnostic (if translatable, otherwise it just + /// passes the user's string along). + fn subdiagnostic_message_to_diagnostic_message( + &self, + attr: impl Into, + ) -> DiagnosticMessage { + let msg = + self.message.iter().map(|(msg, _)| msg).next().expect("diagnostic with no messages"); + msg.with_subdiagnostic_message(attr.into()) + } + /// Convenience function for internal use, clients should use one of the /// public methods above. /// @@ -838,13 +850,16 @@ impl Diagnostic { pub fn sub( &mut self, level: Level, - message: impl Into, + message: impl Into, span: MultiSpan, render_span: Option, ) { let sub = SubDiagnostic { level, - message: vec![(message.into(), Style::NoStyle)], + message: vec![( + self.subdiagnostic_message_to_diagnostic_message(message), + Style::NoStyle, + )], span, render_span, }; @@ -853,14 +868,17 @@ impl Diagnostic { /// Convenience function for internal use, clients should use one of the /// public methods above. - fn sub_with_highlights>( + fn sub_with_highlights>( &mut self, level: Level, mut message: Vec<(M, Style)>, span: MultiSpan, render_span: Option, ) { - let message = message.drain(..).map(|m| (m.0.into(), m.1)).collect(); + let message = message + .drain(..) + .map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.0), m.1)) + .collect(); let sub = SubDiagnostic { level, message, span, render_span }; self.children.push(sub); } diff --git a/compiler/rustc_errors/src/diagnostic_builder.rs b/compiler/rustc_errors/src/diagnostic_builder.rs index 6ef2c832c6526..9e0a99849a3f4 100644 --- a/compiler/rustc_errors/src/diagnostic_builder.rs +++ b/compiler/rustc_errors/src/diagnostic_builder.rs @@ -1,5 +1,8 @@ use crate::diagnostic::IntoDiagnosticArg; -use crate::{Diagnostic, DiagnosticId, DiagnosticMessage, DiagnosticStyledString, ErrorGuaranteed}; +use crate::{ + Diagnostic, DiagnosticId, DiagnosticMessage, DiagnosticStyledString, ErrorGuaranteed, + SubdiagnosticMessage, +}; use crate::{Handler, Level, MultiSpan, StashKey}; use rustc_lint_defs::Applicability; @@ -395,7 +398,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> { /// the diagnostic was constructed. However, the label span is *not* considered a /// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is /// primary. - pub fn span_label(&mut self, span: Span, label: impl Into) -> &mut Self); + pub fn span_label(&mut self, span: Span, label: impl Into) -> &mut Self); forward!( /// Labels all the given spans with the provided label. @@ -430,25 +433,29 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> { found: DiagnosticStyledString, ) -> &mut Self); - forward!(pub fn note(&mut self, msg: impl Into) -> &mut Self); - forward!(pub fn note_once(&mut self, msg: impl Into) -> &mut Self); + forward!(pub fn note(&mut self, msg: impl Into) -> &mut Self); + forward!(pub fn note_once(&mut self, msg: impl Into) -> &mut Self); forward!(pub fn span_note( &mut self, sp: impl Into, - msg: impl Into, + msg: impl Into, ) -> &mut Self); forward!(pub fn span_note_once( &mut self, sp: impl Into, - msg: impl Into, + msg: impl Into, ) -> &mut Self); - forward!(pub fn warn(&mut self, msg: impl Into) -> &mut Self); - forward!(pub fn span_warn(&mut self, sp: impl Into, msg: &str) -> &mut Self); - forward!(pub fn help(&mut self, msg: impl Into) -> &mut Self); + forward!(pub fn warn(&mut self, msg: impl Into) -> &mut Self); + forward!(pub fn span_warn( + &mut self, + sp: impl Into, + msg: impl Into, + ) -> &mut Self); + forward!(pub fn help(&mut self, msg: impl Into) -> &mut Self); forward!(pub fn span_help( &mut self, sp: impl Into, - msg: impl Into, + msg: impl Into, ) -> &mut Self); forward!(pub fn help_use_latest_edition(&mut self,) -> &mut Self); forward!(pub fn set_is_lint(&mut self,) -> &mut Self); @@ -457,67 +464,67 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> { forward!(pub fn multipart_suggestion( &mut self, - msg: impl Into, + msg: impl Into, suggestion: Vec<(Span, String)>, applicability: Applicability, ) -> &mut Self); forward!(pub fn multipart_suggestion_verbose( &mut self, - msg: impl Into, + msg: impl Into, suggestion: Vec<(Span, String)>, applicability: Applicability, ) -> &mut Self); forward!(pub fn tool_only_multipart_suggestion( &mut self, - msg: impl Into, + msg: impl Into, suggestion: Vec<(Span, String)>, applicability: Applicability, ) -> &mut Self); forward!(pub fn span_suggestion( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self); forward!(pub fn span_suggestions( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestions: impl Iterator, applicability: Applicability, ) -> &mut Self); forward!(pub fn multipart_suggestions( &mut self, - msg: impl Into, + msg: impl Into, suggestions: impl Iterator>, applicability: Applicability, ) -> &mut Self); forward!(pub fn span_suggestion_short( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self); forward!(pub fn span_suggestion_verbose( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self); forward!(pub fn span_suggestion_hidden( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self); forward!(pub fn tool_only_span_suggestion( &mut self, sp: Span, - msg: impl Into, + msg: impl Into, suggestion: impl ToString, applicability: Applicability, ) -> &mut Self); diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 5b9b65da34364..fb02f1d68ebc9 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -32,7 +32,8 @@ use rustc_data_structures::sync::{self, Lock, Lrc}; use rustc_data_structures::AtomicRef; pub use rustc_error_messages::{ fallback_fluent_bundle, fluent, fluent_bundle, DiagnosticMessage, FluentBundle, - LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel, DEFAULT_LOCALE_RESOURCES, + LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel, SubdiagnosticMessage, + DEFAULT_LOCALE_RESOURCES, }; pub use rustc_lint_defs::{pluralize, Applicability}; use rustc_span::source_map::SourceMap; diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic.rs b/compiler/rustc_macros/src/diagnostics/diagnostic.rs index d7daee64d791e..95ee0d4a060d2 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic.rs @@ -126,14 +126,14 @@ impl<'a> SessionDiagnosticDerive<'a> { (Some((SessionDiagnosticKind::Error, _)), Some((slug, _))) => { quote! { let mut #diag = #sess.struct_err( - rustc_errors::DiagnosticMessage::fluent(#slug), + rustc_errors::DiagnosticMessage::new(#slug), ); } } (Some((SessionDiagnosticKind::Warn, _)), Some((slug, _))) => { quote! { let mut #diag = #sess.struct_warn( - rustc_errors::DiagnosticMessage::fluent(#slug), + rustc_errors::DiagnosticMessage::new(#slug), ); } } @@ -254,21 +254,6 @@ impl SessionDiagnosticDeriveBuilder { if matches!(name, "help" | "note") && matches!(meta, Meta::Path(_) | Meta::NameValue(_)) { let diag = &self.diag; - let slug = match &self.slug { - Some((slug, _)) => slug.as_str(), - None => throw_span_err!( - span, - &format!( - "`#[{}{}]` must come after `#[error(..)]` or `#[warn(..)]`", - name, - match meta { - Meta::Path(_) => "", - Meta::NameValue(_) => " = ...", - _ => unreachable!(), - } - ) - ), - }; let id = match meta { Meta::Path(..) => quote! { #name }, Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => { @@ -279,7 +264,7 @@ impl SessionDiagnosticDeriveBuilder { let fn_name = proc_macro2::Ident::new(name, attr.span()); return Ok(quote! { - #diag.#fn_name(rustc_errors::DiagnosticMessage::fluent_attr(#slug, #id)); + #diag.#fn_name(rustc_errors::SubdiagnosticMessage::attr(#id)); }); } @@ -525,13 +510,8 @@ impl SessionDiagnosticDeriveBuilder { let method = format_ident!("span_{}", name); - let slug = self - .slug - .as_ref() - .map(|(slug, _)| slug.as_str()) - .unwrap_or_else(|| "missing-slug"); let msg = msg.as_deref().unwrap_or("suggestion"); - let msg = quote! { rustc_errors::DiagnosticMessage::fluent_attr(#slug, #msg) }; + let msg = quote! { rustc_errors::SubdiagnosticMessage::attr(#msg) }; let code = code.unwrap_or_else(|| quote! { String::new() }); Ok(quote! { #diag.#method(#span_field, #msg, #code, #applicability); }) @@ -549,14 +529,11 @@ impl SessionDiagnosticDeriveBuilder { fluent_attr_identifier: &str, ) -> TokenStream { let diag = &self.diag; - - let slug = - self.slug.as_ref().map(|(slug, _)| slug.as_str()).unwrap_or_else(|| "missing-slug"); let fn_name = format_ident!("span_{}", kind); quote! { #diag.#fn_name( #field_binding, - rustc_errors::DiagnosticMessage::fluent_attr(#slug, #fluent_attr_identifier) + rustc_errors::SubdiagnosticMessage::attr(#fluent_attr_identifier) ); } } @@ -565,9 +542,8 @@ impl SessionDiagnosticDeriveBuilder { /// and `fluent_attr_identifier`. fn add_subdiagnostic(&self, kind: &Ident, fluent_attr_identifier: &str) -> TokenStream { let diag = &self.diag; - let slug = self.slug.as_ref().map(|(slug, _)| slug.as_str()).unwrap_or("missing-slug"); quote! { - #diag.#kind(rustc_errors::DiagnosticMessage::fluent_attr(#slug, #fluent_attr_identifier)); + #diag.#kind(rustc_errors::SubdiagnosticMessage::attr(#fluent_attr_identifier)); } } diff --git a/compiler/rustc_macros/src/diagnostics/fluent.rs b/compiler/rustc_macros/src/diagnostics/fluent.rs index 8523d7fa9f988..42a9bf477a41c 100644 --- a/compiler/rustc_macros/src/diagnostics/fluent.rs +++ b/compiler/rustc_macros/src/diagnostics/fluent.rs @@ -11,7 +11,7 @@ use proc_macro::{Diagnostic, Level, Span}; use proc_macro2::TokenStream; use quote::quote; use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, fs::File, io::Read, path::{Path, PathBuf}, @@ -100,6 +100,10 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok let ident_span = res.ident.span().unwrap(); let path_span = res.resource.span().unwrap(); + // Set of Fluent attribute names already output, to avoid duplicate type errors - any given + // constant created for a given attribute is the same. + let mut previous_attrs = HashSet::new(); + let relative_ftl_path = res.resource.value(); let absolute_ftl_path = invocation_relative_path_to_absolute(ident_span, &relative_ftl_path); @@ -199,13 +203,15 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok }); for Attribute { id: Identifier { name: attr_name }, .. } in attributes { - let attr_snake_name = attr_name.replace("-", "_"); - let snake_name = Ident::new(&format!("{snake_name}_{attr_snake_name}"), span); + let snake_name = Ident::new(&attr_name.replace("-", "_"), span); + if !previous_attrs.insert(snake_name.clone()) { + continue; + } + constants.extend(quote! { - pub const #snake_name: crate::DiagnosticMessage = - crate::DiagnosticMessage::FluentIdentifier( - std::borrow::Cow::Borrowed(#name), - Some(std::borrow::Cow::Borrowed(#attr_name)) + pub const #snake_name: crate::SubdiagnosticMessage = + crate::SubdiagnosticMessage::FluentAttr( + std::borrow::Cow::Borrowed(#attr_name) ); }); } diff --git a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs index df01419c82a8e..9aeb484bfd52d 100644 --- a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs @@ -397,7 +397,7 @@ impl<'a> SessionSubdiagnosticDeriveBuilder<'a> { let diag = &self.diag; let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind); - let message = quote! { rustc_errors::DiagnosticMessage::fluent(#slug) }; + let message = quote! { rustc_errors::SubdiagnosticMessage::message(#slug) }; let call = if matches!(kind, SubdiagnosticKind::Suggestion(..)) { if let Some(span) = span_field { quote! { #diag.#name(#span, #message, #code, #applicability); } diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index c9da58aae5c86..12302315e9044 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -17,10 +17,10 @@ use rustc_ast::{ }; use rustc_ast_pretty::pprust; use rustc_data_structures::fx::FxHashSet; -use rustc_errors::{pluralize, struct_span_err, Diagnostic, EmissionGuarantee, ErrorGuaranteed}; use rustc_errors::{ - Applicability, DiagnosticBuilder, DiagnosticMessage, Handler, MultiSpan, PResult, + fluent, Applicability, DiagnosticBuilder, DiagnosticMessage, Handler, MultiSpan, PResult, }; +use rustc_errors::{pluralize, struct_span_err, Diagnostic, EmissionGuarantee, ErrorGuaranteed}; use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, Ident}; @@ -658,13 +658,10 @@ impl<'a> Parser<'a> { err.delay_as_bug(); self.struct_span_err( expr.span, - DiagnosticMessage::fluent("parser-struct-literal-body-without-path"), + fluent::parser::struct_literal_body_without_path, ) .multipart_suggestion( - DiagnosticMessage::fluent_attr( - "parser-struct-literal-body-without-path", - "suggestion", - ), + fluent::parser::suggestion, vec![ (expr.span.shrink_to_lo(), "{ SomeStruct ".to_string()), (expr.span.shrink_to_hi(), " }".to_string()), diff --git a/compiler/rustc_typeck/src/errors.rs b/compiler/rustc_typeck/src/errors.rs index d9c9f2920b079..a7f736fed14a3 100644 --- a/compiler/rustc_typeck/src/errors.rs +++ b/compiler/rustc_typeck/src/errors.rs @@ -277,7 +277,7 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams { .join(", "), ); - err.span_label(self.def_span, rustc_errors::fluent::typeck::missing_type_params_label); + err.span_label(self.def_span, rustc_errors::fluent::typeck::label); let mut suggested = false; if let (Ok(snippet), true) = ( @@ -295,7 +295,7 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams { // least we can clue them to the correct syntax `Iterator`. err.span_suggestion( self.span, - rustc_errors::fluent::typeck::missing_type_params_suggestion, + rustc_errors::fluent::typeck::suggestion, format!("{}<{}>", snippet, self.missing_type_params.join(", ")), Applicability::HasPlaceholders, ); @@ -303,13 +303,10 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams { } } if !suggested { - err.span_label( - self.span, - rustc_errors::fluent::typeck::missing_type_params_no_suggestion_label, - ); + err.span_label(self.span, rustc_errors::fluent::typeck::no_suggestion_label); } - err.note(rustc_errors::fluent::typeck::missing_type_params_note); + err.note(rustc_errors::fluent::typeck::note); err } } diff --git a/src/test/ui-fulldeps/fluent-messages/test.rs b/src/test/ui-fulldeps/fluent-messages/test.rs index b05d3d08ccb09..0390a07850b41 100644 --- a/src/test/ui-fulldeps/fluent-messages/test.rs +++ b/src/test/ui-fulldeps/fluent-messages/test.rs @@ -12,6 +12,12 @@ pub enum DiagnosticMessage { FluentIdentifier(std::borrow::Cow<'static, str>, Option>), } +/// Copy of the relevant `SubdiagnosticMessage` variant constructed by `fluent_messages` as it +/// expects `crate::SubdiagnosticMessage` to exist. +pub enum SubdiagnosticMessage { + FluentAttr(std::borrow::Cow<'static, str>), +} + mod missing_absolute { use super::fluent_messages; diff --git a/src/test/ui-fulldeps/fluent-messages/test.stderr b/src/test/ui-fulldeps/fluent-messages/test.stderr index f88d09bee6e88..526bca43f694d 100644 --- a/src/test/ui-fulldeps/fluent-messages/test.stderr +++ b/src/test/ui-fulldeps/fluent-messages/test.stderr @@ -1,5 +1,5 @@ error: could not open Fluent resource - --> $DIR/test.rs:19:29 + --> $DIR/test.rs:25:29 | LL | missing_absolute => "/definitely_does_not_exist.ftl", | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -7,7 +7,7 @@ LL | missing_absolute => "/definitely_does_not_exist.ftl", = note: os-specific message error: could not open Fluent resource - --> $DIR/test.rs:28:29 + --> $DIR/test.rs:34:29 | LL | missing_relative => "../definitely_does_not_exist.ftl", | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -15,7 +15,7 @@ LL | missing_relative => "../definitely_does_not_exist.ftl", = note: os-specific message error: could not parse Fluent resource - --> $DIR/test.rs:37:28 + --> $DIR/test.rs:43:28 | LL | missing_message => "./missing-message.ftl", | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -30,13 +30,13 @@ error: expected a message field for "missing-message" | error: overrides existing message: `key` - --> $DIR/test.rs:47:9 + --> $DIR/test.rs:53:9 | LL | b => "./duplicate-b.ftl", | ^ | help: previously defined in this resource - --> $DIR/test.rs:46:9 + --> $DIR/test.rs:52:9 | LL | a => "./duplicate-a.ftl", | ^ diff --git a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs index 1cdc5d18c2898..84d5de173091b 100644 --- a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs +++ b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs @@ -404,7 +404,6 @@ struct ErrorWithHelpCustom { #[derive(SessionDiagnostic)] #[help] -//~^ ERROR `#[help]` must come after `#[error(..)]` or `#[warn(..)]` #[error(code = "E0123", slug = "foo")] struct ErrorWithHelpWrongOrder { val: String, @@ -412,7 +411,6 @@ struct ErrorWithHelpWrongOrder { #[derive(SessionDiagnostic)] #[help = "bar"] -//~^ ERROR `#[help = ...]` must come after `#[error(..)]` or `#[warn(..)]` #[error(code = "E0123", slug = "foo")] struct ErrorWithHelpCustomWrongOrder { val: String, @@ -420,7 +418,6 @@ struct ErrorWithHelpCustomWrongOrder { #[derive(SessionDiagnostic)] #[note] -//~^ ERROR `#[note]` must come after `#[error(..)]` or `#[warn(..)]` #[error(code = "E0123", slug = "foo")] struct ErrorWithNoteWrongOrder { val: String, @@ -428,7 +425,6 @@ struct ErrorWithNoteWrongOrder { #[derive(SessionDiagnostic)] #[note = "bar"] -//~^ ERROR `#[note = ...]` must come after `#[error(..)]` or `#[warn(..)]` #[error(code = "E0123", slug = "foo")] struct ErrorWithNoteCustomWrongOrder { val: String, diff --git a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr index 2583363120a29..85ea44ec278c0 100644 --- a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr +++ b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr @@ -301,38 +301,14 @@ LL | #[label("bar")] | = help: only `suggestion{,_short,_hidden,_verbose}` are valid field attributes -error: `#[help]` must come after `#[error(..)]` or `#[warn(..)]` - --> $DIR/diagnostic-derive.rs:406:1 - | -LL | #[help] - | ^^^^^^^ - -error: `#[help = ...]` must come after `#[error(..)]` or `#[warn(..)]` - --> $DIR/diagnostic-derive.rs:414:1 - | -LL | #[help = "bar"] - | ^^^^^^^^^^^^^^^ - -error: `#[note]` must come after `#[error(..)]` or `#[warn(..)]` - --> $DIR/diagnostic-derive.rs:422:1 - | -LL | #[note] - | ^^^^^^^ - -error: `#[note = ...]` must come after `#[error(..)]` or `#[warn(..)]` - --> $DIR/diagnostic-derive.rs:430:1 - | -LL | #[note = "bar"] - | ^^^^^^^^^^^^^^^ - error: applicability cannot be set in both the field and attribute - --> $DIR/diagnostic-derive.rs:440:49 + --> $DIR/diagnostic-derive.rs:436:49 | LL | #[suggestion(message = "bar", code = "...", applicability = "maybe-incorrect")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: invalid applicability - --> $DIR/diagnostic-derive.rs:448:49 + --> $DIR/diagnostic-derive.rs:444:49 | LL | #[suggestion(message = "bar", code = "...", applicability = "batman")] | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -363,12 +339,12 @@ LL | #[derive(SessionDiagnostic)] rustc_middle::ty::Ty<'tcx> usize note: required by a bound in `DiagnosticBuilder::<'a, G>::set_arg` - --> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:531:19 + --> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:538:19 | LL | arg: impl IntoDiagnosticArg, | ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg` = note: this error originates in the derive macro `SessionDiagnostic` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 43 previous errors +error: aborting due to 39 previous errors For more information about this error, try `rustc --explain E0277`. From 1e0747f8d25a76f83723990d2c11a7ce40f97258 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 30 May 2022 18:27:07 +0400 Subject: [PATCH 12/16] normalize forbidden slices --- .../const-ptr/forbidden_slices.32bit.stderr | 76 +++++++++--------- .../const-ptr/forbidden_slices.64bit.stderr | 80 +++++++++---------- src/test/ui/const-ptr/forbidden_slices.rs | 1 + 3 files changed, 79 insertions(+), 78 deletions(-) diff --git a/src/test/ui/const-ptr/forbidden_slices.32bit.stderr b/src/test/ui/const-ptr/forbidden_slices.32bit.stderr index d376548b9b925..4d12f59208616 100644 --- a/src/test/ui/const-ptr/forbidden_slices.32bit.stderr +++ b/src/test/ui/const-ptr/forbidden_slices.32bit.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:17:34 + ::: $DIR/forbidden_slices.rs:18:34 | LL | pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) }; - | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:17:34 + | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:18: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:18:33 + ::: $DIR/forbidden_slices.rs:19:33 | LL | pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) }; - | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:18:33 + | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:19:33 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/slice/raw.rs:LL:COL @@ -32,49 +32,49 @@ error[E0080]: could not evaluate static initializer 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 + | dereferencing pointer failed: ALLOC_ID 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 + ::: $DIR/forbidden_slices.rs:22:34 | LL | pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) }; - | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:21:34 + | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:22:34 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 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 │ ╾──╼.... + ╾─ALLOC_ID─╼ 01 00 00 00 │ ╾──╼.... } 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 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 │ ╾──╼.... + ╾─ALLOC_ID─╼ 04 00 00 00 │ ╾──╼.... } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:28:1 + --> $DIR/forbidden_slices.rs:29: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 │ ╾──╼.... + ╾─ALLOC_ID─╼ 04 00 00 00 │ ╾──╼.... } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:31:1 + --> $DIR/forbidden_slices.rs:32:1 | LL | / pub static S7: &[u16] = unsafe { LL | | @@ -95,13 +95,13 @@ error[E0080]: could not evaluate static initializer 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 + | dereferencing pointer failed: ALLOC_ID 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 + ::: $DIR/forbidden_slices.rs:43:5 | LL | from_raw_parts(ptr, 1) - | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:42:5 + | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:43: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:45:34 + ::: $DIR/forbidden_slices.rs:46:34 | LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; - | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:45:34 + | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:46: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:46:33 + ::: $DIR/forbidden_slices.rs:47:33 | LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; - | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:46:33 + | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:47:33 | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -149,19 +149,19 @@ error[E0080]: could not evaluate static initializer 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 + | pointer arithmetic failed: ALLOC_ID 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 + ::: $DIR/forbidden_slices.rs:50:25 | LL | from_ptr_range(ptr..ptr.add(2)) - | ---------- inside `R2` at $DIR/forbidden_slices.rs:49:25 + | ---------- inside `R2` at $DIR/forbidden_slices.rs:50:25 error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:51:1 + --> $DIR/forbidden_slices.rs:52:1 | LL | / pub static R4: &[u8] = unsafe { LL | | @@ -172,11 +172,11 @@ LL | | }; | = 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 │ ╾──╼.... + ╾ALLOC_ID─╼ 01 00 00 00 │ ╾──╼.... } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:56:1 + --> $DIR/forbidden_slices.rs:57:1 | LL | / pub static R5: &[u8] = unsafe { LL | | @@ -187,11 +187,11 @@ LL | | }; | = 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 │ ╾──╼.... + ╾ALLOC_ID─╼ 04 00 00 00 │ ╾──╼.... } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:61:1 + --> $DIR/forbidden_slices.rs:62:1 | LL | / pub static R6: &[bool] = unsafe { LL | | @@ -202,11 +202,11 @@ LL | | }; | = 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 │ ╾──╼.... + ╾ALLOC_ID─╼ 04 00 00 00 │ ╾──╼.... } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:66:1 + --> $DIR/forbidden_slices.rs:67:1 | LL | / pub static R7: &[u16] = unsafe { LL | | @@ -226,16 +226,16 @@ error[E0080]: could not evaluate static initializer 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 + | pointer arithmetic failed: ALLOC_ID 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 + ::: $DIR/forbidden_slices.rs:74:25 | LL | from_ptr_range(ptr..ptr.add(1)) - | ---------- inside `R8` at $DIR/forbidden_slices.rs:73:25 + | ---------- inside `R8` at $DIR/forbidden_slices.rs:74: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:78:34 + ::: $DIR/forbidden_slices.rs:79: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 + | ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:79: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:79:35 + ::: $DIR/forbidden_slices.rs:80:35 | LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) }; - | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:79:35 + | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:80:35 error: aborting due to 18 previous errors diff --git a/src/test/ui/const-ptr/forbidden_slices.64bit.stderr b/src/test/ui/const-ptr/forbidden_slices.64bit.stderr index cb294b16310a9..d9d4dbc34b1f2 100644 --- a/src/test/ui/const-ptr/forbidden_slices.64bit.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:17:34 + ::: $DIR/forbidden_slices.rs:18:34 | LL | pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) }; - | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:17:34 + | ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:18: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:18:33 + ::: $DIR/forbidden_slices.rs:19:33 | LL | pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) }; - | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:18:33 + | ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:19:33 error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/slice/raw.rs:LL:COL @@ -32,49 +32,49 @@ error[E0080]: could not evaluate static initializer 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 + | dereferencing pointer failed: ALLOC_ID 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 + ::: $DIR/forbidden_slices.rs:22:34 | LL | pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) }; - | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:21:34 + | ---------------------- inside `S2` at $DIR/forbidden_slices.rs:22:34 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 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 │ ╾──────╼........ + ╾───────ALLOC_ID───────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........ } 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 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 │ ╾──────╼........ + ╾───────ALLOC_ID───────╼ 08 00 00 00 00 00 00 00 │ ╾──────╼........ } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:28:1 + --> $DIR/forbidden_slices.rs:29: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 │ ╾──────╼........ + ╾───────ALLOC_ID───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:31:1 + --> $DIR/forbidden_slices.rs:32:1 | LL | / pub static S7: &[u16] = unsafe { LL | | @@ -86,7 +86,7 @@ LL | | }; | = 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 │ ╾──────╼........ + ╾─────ALLOC_ID+0x1─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ } error[E0080]: could not evaluate static initializer @@ -95,13 +95,13 @@ error[E0080]: could not evaluate static initializer 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 + | dereferencing pointer failed: ALLOC_ID 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 + ::: $DIR/forbidden_slices.rs:43:5 | LL | from_raw_parts(ptr, 1) - | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:42:5 + | ---------------------- inside `S8` at $DIR/forbidden_slices.rs:43: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:45:34 + ::: $DIR/forbidden_slices.rs:46:34 | LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; - | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:45:34 + | ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:46: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:46:33 + ::: $DIR/forbidden_slices.rs:47:33 | LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; - | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:46:33 + | ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:47:33 | = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -149,19 +149,19 @@ error[E0080]: could not evaluate static initializer 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 + | pointer arithmetic failed: ALLOC_ID 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 + ::: $DIR/forbidden_slices.rs:50:25 | LL | from_ptr_range(ptr..ptr.add(2)) - | ---------- inside `R2` at $DIR/forbidden_slices.rs:49:25 + | ---------- inside `R2` at $DIR/forbidden_slices.rs:50:25 error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:51:1 + --> $DIR/forbidden_slices.rs:52:1 | LL | / pub static R4: &[u8] = unsafe { LL | | @@ -172,11 +172,11 @@ LL | | }; | = 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 │ ╾──────╼........ + ╾──────ALLOC_ID───────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........ } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:56:1 + --> $DIR/forbidden_slices.rs:57:1 | LL | / pub static R5: &[u8] = unsafe { LL | | @@ -187,11 +187,11 @@ LL | | }; | = 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 │ ╾──────╼........ + ╾──────ALLOC_ID───────╼ 08 00 00 00 00 00 00 00 │ ╾──────╼........ } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:61:1 + --> $DIR/forbidden_slices.rs:62:1 | LL | / pub static R6: &[bool] = unsafe { LL | | @@ -202,11 +202,11 @@ LL | | }; | = 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 │ ╾──────╼........ + ╾──────ALLOC_ID───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ } error[E0080]: it is undefined behavior to use this value - --> $DIR/forbidden_slices.rs:66:1 + --> $DIR/forbidden_slices.rs:67:1 | LL | / pub static R7: &[u16] = unsafe { LL | | @@ -217,7 +217,7 @@ LL | | }; | = 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 │ ╾──────╼........ + ╾────ALLOC_ID+0x1─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ } error[E0080]: could not evaluate static initializer @@ -226,16 +226,16 @@ error[E0080]: could not evaluate static initializer 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 + | pointer arithmetic failed: ALLOC_ID 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 + ::: $DIR/forbidden_slices.rs:74:25 | LL | from_ptr_range(ptr..ptr.add(1)) - | ---------- inside `R8` at $DIR/forbidden_slices.rs:73:25 + | ---------- inside `R8` at $DIR/forbidden_slices.rs:74: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:78:34 + ::: $DIR/forbidden_slices.rs:79: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 + | ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:79: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:79:35 + ::: $DIR/forbidden_slices.rs:80:35 | LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) }; - | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:79:35 + | ------------------------ inside `R10` at $DIR/forbidden_slices.rs:80: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 b29671b7f008d..dbd0b128e56ab 100644 --- a/src/test/ui/const-ptr/forbidden_slices.rs +++ b/src/test/ui/const-ptr/forbidden_slices.rs @@ -1,4 +1,5 @@ // stderr-per-bitwidth +// normalize-stderr-test "alloc[0-9]+" -> "ALLOC_ID" // error-pattern: could not evaluate static initializer #![feature( const_slice_from_raw_parts, From f3eae89b33fbd729fb6c401b42daa672bdd1751c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 30 May 2022 16:53:24 +0200 Subject: [PATCH 13/16] Fix invalid line number computation when clicking on something else than a line number --- src/librustdoc/html/static/js/source-script.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js index aaac878d3a37b..58c036e0b3ca3 100644 --- a/src/librustdoc/html/static/js/source-script.js +++ b/src/librustdoc/html/static/js/source-script.js @@ -205,6 +205,10 @@ const handleSourceHighlight = (function() { return ev => { let cur_line_id = parseInt(ev.target.id, 10); + // It can happen when clicking not on a line number span. + if (isNaN(cur_line_id)) { + return; + } ev.preventDefault(); if (ev.shiftKey && prev_line_id) { From 16d5cdc570b45a17a9c85953244ef6058929f608 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 30 May 2022 17:07:21 +0200 Subject: [PATCH 14/16] Improve source-code-page.goml GUI test code --- src/test/rustdoc-gui/source-code-page.goml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml index ad7080c39b842..f31acc80f7e55 100644 --- a/src/test/rustdoc-gui/source-code-page.goml +++ b/src/test/rustdoc-gui/source-code-page.goml @@ -2,9 +2,9 @@ goto: file://|DOC_PATH|/src/test_docs/lib.rs.html // Check that we can click on the line number. click: ".line-numbers > span:nth-child(4)" // This is the span for line 4. -// Unfortunately, "#4" isn't a valid query selector, so we have to go around that limitation -// by instead getting the nth span. -assert-attribute: (".line-numbers > span:nth-child(4)", {"class": "line-highlighted"}) +// Ensure that the page URL was updated. +assert-document-property: ({"URL": "#4"}, ENDS_WITH) +assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"}) // We now check that the good spans are highlighted goto: file://|DOC_PATH|/src/test_docs/lib.rs.html#4-6 assert-attribute-false: (".line-numbers > span:nth-child(3)", {"class": "line-highlighted"}) From d286df1402f0e3a96e9ec11748e80c8be4bca92f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 30 May 2022 17:14:46 +0200 Subject: [PATCH 15/16] Add line number click GUI test --- src/test/rustdoc-gui/source-code-page.goml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml index f31acc80f7e55..509739c9f2951 100644 --- a/src/test/rustdoc-gui/source-code-page.goml +++ b/src/test/rustdoc-gui/source-code-page.goml @@ -3,7 +3,7 @@ goto: file://|DOC_PATH|/src/test_docs/lib.rs.html // Check that we can click on the line number. click: ".line-numbers > span:nth-child(4)" // This is the span for line 4. // Ensure that the page URL was updated. -assert-document-property: ({"URL": "#4"}, ENDS_WITH) +assert-document-property: ({"URL": "lib.rs.html#4"}, ENDS_WITH) assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"}) // We now check that the good spans are highlighted goto: file://|DOC_PATH|/src/test_docs/lib.rs.html#4-6 @@ -17,3 +17,13 @@ compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y")) // Assert that the line numbers text is aligned to the right. assert-css: (".line-numbers", {"text-align": "right"}) + +// Now let's check that clicking on something else than the line number doesn't +// do anything (and certainly not add a `#NaN` to the URL!). +show-text: true +goto: file://|DOC_PATH|/src/test_docs/lib.rs.html +// We use this assert-position to know where we will click. +assert-position: ("//*[@id='1']", {"x": 104, "y": 103}) +// We click on the left of the "1" span but still in the "line-number" `
`.
+click: (103, 103)
+assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)

From 56662bcdff2123702c131b4b1a9dc574dea3e402 Mon Sep 17 00:00:00 2001
From: Tobias Stoeckmann 
Date: Mon, 30 May 2022 21:21:32 +0200
Subject: [PATCH 16/16] Fix typos in comment

---
 compiler/rustc_data_structures/src/stable_hasher.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler/rustc_data_structures/src/stable_hasher.rs b/compiler/rustc_data_structures/src/stable_hasher.rs
index c8bb4fc5e6af6..a915a4daa9541 100644
--- a/compiler/rustc_data_structures/src/stable_hasher.rs
+++ b/compiler/rustc_data_structures/src/stable_hasher.rs
@@ -632,10 +632,10 @@ fn stable_hash_reduce(
     }
 }
 
-/// Controls what data we do or not not hash.
+/// Controls what data we do or do not hash.
 /// Whenever a `HashStable` implementation caches its
 /// result, it needs to include `HashingControls` as part
-/// of the key, to ensure that is does not produce an incorrect
+/// of the key, to ensure that it does not produce an incorrect
 /// result (for example, using a `Fingerprint` produced while
 /// hashing `Span`s when a `Fingerprint` without `Span`s is
 /// being requested)