From a1507b80c2d438cc7e72bd164cccb456408c0ac7 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 15 May 2021 15:04:41 +0200 Subject: [PATCH] handle pointers in str --- compiler/rustc_mir/src/interpret/validity.rs | 1 + src/test/ui/consts/issue-83182.rs | 6 ++++++ src/test/ui/consts/issue-83182.stderr | 14 ++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 src/test/ui/consts/issue-83182.rs create mode 100644 src/test/ui/consts/issue-83182.stderr diff --git a/compiler/rustc_mir/src/interpret/validity.rs b/compiler/rustc_mir/src/interpret/validity.rs index 6e6e64d25ac3..83b0d0528f77 100644 --- a/compiler/rustc_mir/src/interpret/validity.rs +++ b/compiler/rustc_mir/src/interpret/validity.rs @@ -833,6 +833,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> self.ecx.memory.read_bytes(mplace.ptr, Size::from_bytes(len)), self.path, err_ub!(InvalidUninitBytes(..)) => { "uninitialized data in `str`" }, + err_unsup!(ReadPointerAsBytes) => { "a pointer in `str`" }, ); } ty::Array(tys, ..) | ty::Slice(tys) diff --git a/src/test/ui/consts/issue-83182.rs b/src/test/ui/consts/issue-83182.rs new file mode 100644 index 000000000000..13c6e94f101c --- /dev/null +++ b/src/test/ui/consts/issue-83182.rs @@ -0,0 +1,6 @@ +use std::mem; +struct MyStr(str); +const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) }; +//~^ ERROR: it is undefined behavior to use this value +//~| type validation failed: encountered a pointer in `str` +fn main() {} diff --git a/src/test/ui/consts/issue-83182.stderr b/src/test/ui/consts/issue-83182.stderr new file mode 100644 index 000000000000..f7ee6a21ab29 --- /dev/null +++ b/src/test/ui/consts/issue-83182.stderr @@ -0,0 +1,14 @@ +error[E0080]: it is undefined behavior to use this value + --> $DIR/issue-83182.rs:3:1 + | +LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer in `str` at ..0 + | + = 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) { + ╾───────alloc3────────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........ + } + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0080`.