Skip to content

Commit ce6c25d

Browse files
committed
References to ZSTs may be at arbitrary aligned addresses
1 parent 4d52dc4 commit ce6c25d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

compiler/rustc_mir/src/const_eval/eval_queries.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
1414
use rustc_middle::ty::{self, subst::Subst, TyCtxt};
1515
use rustc_span::source_map::Span;
1616
use rustc_target::abi::{Abi, LayoutOf};
17-
use std::convert::TryInto;
17+
use std::convert::{TryFrom, TryInto};
1818

1919
pub fn note_on_undefined_behavior_error() -> &'static str {
2020
"The rules on what exactly is undefined behavior aren't clear, \
@@ -148,10 +148,10 @@ pub(super) fn op_to_const<'tcx>(
148148
Scalar::Raw { data, .. } => {
149149
assert!(mplace.layout.is_zst());
150150
assert_eq!(
151-
data,
152-
mplace.layout.align.abi.bytes().into(),
153-
"this MPlaceTy must come from `try_as_mplace` being used on a zst, so we know what
154-
value this integer address must have",
151+
u64::try_from(data).unwrap() % mplace.layout.align.abi.bytes(),
152+
0,
153+
"this MPlaceTy must come from a validated constant, thus we can assume the \
154+
alignment is correct",
155155
);
156156
ConstValue::Scalar(Scalar::zst())
157157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
3+
const FOO: isize = 10;
4+
const ZST: &() = unsafe { std::mem::transmute(FOO) };
5+
fn main() {
6+
match &() {
7+
ZST => 9,
8+
};
9+
}

0 commit comments

Comments
 (0)