2022-12-22 16:40:50 +01:00
|
|
|
// Strip out raw byte dumps to make comparison platform-independent:
|
2024-12-25 22:12:17 +11:00
|
|
|
//@ normalize-stderr: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
|
|
|
|
|
//@ normalize-stderr: "([0-9a-f][0-9a-f] |╾─*A(LLOC)?[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
|
2023-10-15 17:00:11 +00:00
|
|
|
|
2022-05-27 19:08:22 +04:00
|
|
|
#![feature(
|
|
|
|
|
slice_from_ptr_range,
|
2022-05-27 19:09:46 +04:00
|
|
|
const_slice_from_ptr_range,
|
2022-05-27 19:08:22 +04:00
|
|
|
)]
|
|
|
|
|
use std::{
|
|
|
|
|
mem::{size_of, MaybeUninit},
|
|
|
|
|
ptr,
|
|
|
|
|
slice::{from_ptr_range, from_raw_parts},
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-09 12:35:11 +02:00
|
|
|
// Null is never valid for references
|
2022-05-27 19:08:22 +04:00
|
|
|
pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) };
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: null reference
|
2022-05-27 19:08:22 +04:00
|
|
|
pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) };
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: null reference
|
2022-05-27 19:08:22 +04:00
|
|
|
|
|
|
|
|
// Out of bounds
|
|
|
|
|
pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) };
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: dangling reference (going beyond the bounds of its allocation)
|
2022-05-27 19:08:22 +04:00
|
|
|
|
|
|
|
|
// Reading uninitialized data
|
2025-05-28 10:29:08 +00:00
|
|
|
pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) }; //~ ERROR: uninitialized memory
|
2022-05-27 19:08:22 +04:00
|
|
|
// Reinterpret pointers as integers (UB in CTFE.)
|
2025-05-28 10:29:08 +00:00
|
|
|
pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) }; //~ ERROR: pointer, but expected an integer
|
2022-05-27 19:08:22 +04:00
|
|
|
// Layout mismatch
|
2025-05-28 10:29:08 +00:00
|
|
|
pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; //~ ERROR: 0x11, but expected a boolean
|
2022-05-27 19:08:22 +04:00
|
|
|
|
|
|
|
|
// Reading padding is not ok
|
|
|
|
|
pub static S7: &[u16] = unsafe {
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: uninitialized memory
|
2022-11-20 09:54:45 +01:00
|
|
|
let ptr = (&D2 as *const Struct as *const u16).add(1);
|
2022-05-27 19:08:22 +04:00
|
|
|
|
|
|
|
|
from_raw_parts(ptr, 4)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Unaligned read
|
|
|
|
|
pub static S8: &[u64] = unsafe {
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: dangling reference (going beyond the bounds of its allocation)
|
2022-05-27 19:08:22 +04:00
|
|
|
let ptr = (&D4 as *const [u32; 2] as *const u32).byte_add(1).cast::<u64>();
|
|
|
|
|
|
|
|
|
|
from_raw_parts(ptr, 1)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR encountered a null reference
|
2024-05-09 12:35:11 +02:00
|
|
|
pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; // errors inside libcore
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR 0 < pointee_size && pointee_size <= isize::MAX as usize
|
2022-05-27 19:08:22 +04:00
|
|
|
pub static R2: &[u32] = unsafe {
|
|
|
|
|
let ptr = &D0 as *const u32;
|
2024-05-09 12:35:11 +02:00
|
|
|
from_ptr_range(ptr..ptr.add(2)) // errors inside libcore
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR in-bounds pointer arithmetic failed
|
2022-05-27 19:08:22 +04:00
|
|
|
};
|
|
|
|
|
pub static R4: &[u8] = unsafe {
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: encountered uninitialized memory, but expected an integer
|
2022-05-27 19:08:22 +04:00
|
|
|
let ptr = (&D1) as *const MaybeUninit<&u32> as *const u8;
|
|
|
|
|
from_ptr_range(ptr..ptr.add(1))
|
|
|
|
|
};
|
|
|
|
|
pub static R5: &[u8] = unsafe {
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: encountered a pointer, but expected an integer
|
2022-05-27 19:08:22 +04:00
|
|
|
let ptr = &D3 as *const &u32;
|
|
|
|
|
from_ptr_range(ptr.cast()..ptr.add(1).cast())
|
|
|
|
|
};
|
|
|
|
|
pub static R6: &[bool] = unsafe {
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: 0x11, but expected a boolean
|
2022-05-27 19:08:22 +04:00
|
|
|
let ptr = &D0 as *const u32 as *const bool;
|
|
|
|
|
from_ptr_range(ptr..ptr.add(4))
|
|
|
|
|
};
|
|
|
|
|
pub static R7: &[u16] = unsafe {
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR: unaligned reference (required 2 byte alignment but found 1)
|
2022-05-27 19:08:22 +04:00
|
|
|
let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
|
2023-08-01 17:11:00 +02:00
|
|
|
from_ptr_range(ptr..ptr.add(4))
|
2022-05-27 19:08:22 +04:00
|
|
|
};
|
|
|
|
|
pub static R8: &[u64] = unsafe {
|
|
|
|
|
let ptr = (&D4 as *const [u32; 2] as *const u32).byte_add(1).cast::<u64>();
|
Shorten span of panic failures in const context
Previously, we included a redundant prefix on the panic message and a postfix of the location of the panic. The prefix didn't carry any additional information beyond "something failed", and the location of the panic is redundant with the diagnostic's span, which gets printed out even if its code is not shown.
```
error[E0080]: evaluation of constant value failed
--> $DIR/assert-type-intrinsics.rs:11:9
|
LL | MaybeUninit::<!>::uninit().assume_init();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation panicked: aborted execution: attempted to instantiate uninhabited type `!`
```
```
error[E0080]: evaluation of `Fail::<i32>::C` failed
--> $DIR/collect-in-dead-closure.rs:9:19
|
LL | const C: () = panic!();
| ^^^^^^^^ evaluation panicked: explicit panic
|
= note: this error originates in the macro
`$crate::panic::panic_2015` which comes from the expansion of the macro
`panic` (in Nightly builds, run with -Z macro-backtrace for more info)
```
```
error[E0080]: evaluation of constant value failed
--> $DIR/uninhabited.rs:41:9
|
LL | assert!(false);
| ^^^^^^^^^^^^^^ evaluation panicked: assertion failed: false
|
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
```
---
When the primary span for a const error is the same as the first frame in the const error report, skip it.
```
error[E0080]: evaluation of constant value failed
--> $DIR/issue-88434-removal-index-should-be-less.rs:3:24
|
LL | const _CONST: &[u8] = &f(&[], |_| {});
| ^^^^^^^^^^^^^^ evaluation panicked: explicit panic
|
note: inside `f::<{closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34}>`
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
|
LL | panic!()
| ^^^^^^^^ the failure occurred here
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
```
instead of
```
error[E0080]: evaluation of constant value failed
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
|
LL | panic!()
| ^^^^^^^^ explicit panic
|
note: inside `f::<{closure@$DIR/issue-88434-removal-index-should-be-less.rs:3:31: 3:34}>`
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
|
LL | panic!()
| ^^^^^^^^
note: inside `_CONST`
--> $DIR/issue-88434-removal-index-should-be-less.rs:3:24
|
LL | const _CONST: &[u8] = &f(&[], |_| {});
| ^^^^^^^^^^^^^^
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
```
---
Revert order of constant evaluation errors
Point at the code the user wrote first and std functions last.
```
error[E0080]: evaluation of constant value failed
--> $DIR/const-errs-dont-conflict-103369.rs:5:25
|
LL | impl ConstGenericTrait<{my_fn(1)}> for () {}
| ^^^^^^^^ evaluation panicked: Some error occurred
|
note: called from `my_fn`
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
|
LL | panic!("Some error occurred");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
```
instead of
```
error[E0080]: evaluation of constant value failed
--> $DIR/const-errs-dont-conflict-103369.rs:10:5
|
LL | panic!("Some error occurred");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Some error occurred
|
note: called from `<() as ConstGenericTrait<{my_fn(1)}>>::{constant#0}`
--> $DIR/const-errs-dont-conflict-103369.rs:5:25
|
LL | impl ConstGenericTrait<{my_fn(1)}> for () {}
| ^^^^^^^^
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
```
2025-02-03 18:43:55 +00:00
|
|
|
from_ptr_range(ptr..ptr.add(1))
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR in-bounds pointer arithmetic failed
|
2022-05-27 19:08:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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)) };
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR not both derived from the same allocation
|
2022-05-27 19:08:22 +04:00
|
|
|
pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) };
|
2025-05-28 10:29:08 +00:00
|
|
|
//~^ ERROR not both derived from the same allocation
|
2022-05-27 19:08:22 +04:00
|
|
|
|
2022-12-22 16:40:50 +01:00
|
|
|
const D0: u32 = 0x11111111; // Constant chosen for endianness-independent behavior.
|
2022-05-27 19:08:22 +04:00
|
|
|
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<u8>; 3]
|
|
|
|
|
b: u32,
|
|
|
|
|
c: u16,
|
|
|
|
|
d: u8,
|
|
|
|
|
// _pad: [MaybeUninit<u8>; 1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {}
|