Skip to content

Commit ebf0336

Browse files
committed
Properly test for int pointers in fat pointers to str slices of zero chars
1 parent cc2f46e commit ebf0336

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
const FOO: &str = unsafe { &*(1_usize as *const [u8; 0] as *const [u8] as *const [str]) };
1+
#![feature(const_raw_ptr_deref)]
22

3-
fn main() {}
3+
const FOO: &str = unsafe { &*(1_usize as *const [u8; 0] as *const [u8] as *const str) };
4+
//~^ ERROR it is undefined behaviour to use this value
5+
6+
fn main() {}
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/int_ptr_for_zst_slices.rs:1:28
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/int_ptr_for_zst_slices.rs:3:1
33
|
4-
LL | const FOO: &str = unsafe { &*(1_usize as *const [u8; 0] as *const [u8] as *const [str]) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected str, found slice
4+
LL | const FOO: &str = unsafe { &*(1_usize as *const [u8; 0] as *const [u8] as *const str) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized or non-UTF-8 data in str at .<deref>
66
|
7-
= note: expected type `&'static str`
8-
found type `&[str]`
7+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
98

10-
error[E0277]: the size for values of type `str` cannot be known at compilation time
11-
--> $DIR/int_ptr_for_zst_slices.rs:1:75
12-
|
13-
LL | const FOO: &str = unsafe { &*(1_usize as *const [u8; 0] as *const [u8] as *const [str]) };
14-
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
15-
|
16-
= help: the trait `std::marker::Sized` is not implemented for `str`
17-
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
18-
= note: slice and array elements must have `Sized` type
19-
20-
error: aborting due to 2 previous errors
9+
error: aborting due to previous error
2110

22-
Some errors occurred: E0277, E0308.
23-
For more information about an error, try `rustc --explain E0277`.
11+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)