Skip to content

Commit a1507b8

Browse files
committed
handle pointers in str
1 parent 75da570 commit a1507b8

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

compiler/rustc_mir/src/interpret/validity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
833833
self.ecx.memory.read_bytes(mplace.ptr, Size::from_bytes(len)),
834834
self.path,
835835
err_ub!(InvalidUninitBytes(..)) => { "uninitialized data in `str`" },
836+
err_unsup!(ReadPointerAsBytes) => { "a pointer in `str`" },
836837
);
837838
}
838839
ty::Array(tys, ..) | ty::Slice(tys)

src/test/ui/consts/issue-83182.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::mem;
2+
struct MyStr(str);
3+
const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) };
4+
//~^ ERROR: it is undefined behavior to use this value
5+
//~| type validation failed: encountered a pointer in `str`
6+
fn main() {}

src/test/ui/consts/issue-83182.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/issue-83182.rs:3:1
3+
|
4+
LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer in `str` at .<deref>.0
6+
|
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 rustc repository if you believe it should not be considered undefined behavior.
8+
= note: the raw bytes of the constant (size: 16, align: 8) {
9+
╾───────alloc3────────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........
10+
}
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)