Skip to content

Commit 541a73f

Browse files
authored
Rollup merge of rust-lang#80900 - camelid:readpointerasbytes-ice, r=oli-obk
Fix ICE with `ReadPointerAsBytes` validation error Fixes rust-lang#79690. r? `````@oli-obk`````
2 parents 544c07b + 2be1993 commit 541a73f

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

compiler/rustc_mir/src/interpret/validity.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,11 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
515515
Ok(true)
516516
}
517517
ty::Float(_) | ty::Int(_) | ty::Uint(_) => {
518-
let value = self.ecx.read_scalar(value)?;
518+
let value = try_validation!(
519+
self.ecx.read_scalar(value),
520+
self.path,
521+
err_unsup!(ReadPointerAsBytes) => { "read of part of a pointer" },
522+
);
519523
// NOTE: Keep this in sync with the array optimization for int/float
520524
// types below!
521525
if self.ctfe_mode.is_some() {

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

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ignore-32bit
2+
// This test gives a different error on 32-bit architectures.
3+
4+
union Transmute<T: Copy, U: Copy> {
5+
t: T,
6+
u: U,
7+
}
8+
trait Bar {
9+
fn bar(&self) -> u32;
10+
}
11+
struct Foo {
12+
foo: u32,
13+
bar: bool,
14+
}
15+
impl Bar for Foo {
16+
fn bar(&self) -> u32 {
17+
self.foo
18+
}
19+
}
20+
#[derive(Copy, Clone)]
21+
struct Fat<'a>(&'a Foo, &'static VTable);
22+
struct VTable {
23+
size: Foo,
24+
}
25+
const FOO: &dyn Bar = &Foo {
26+
foo: 128,
27+
bar: false,
28+
};
29+
const G: Fat = unsafe { Transmute { t: FOO }.u };
30+
//~^ ERROR it is undefined behavior to use this value
31+
32+
fn main() {}

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

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/issue-79690.rs:29:1
3+
|
4+
LL | const G: Fat = unsafe { Transmute { t: FOO }.u };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered read of part of a pointer at .1.<deref>.size.foo
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+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)