From ce6c25da5887dcdb446b56892ace1d2463a743f3 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Wed, 30 Sep 2020 10:40:49 +0200 Subject: [PATCH] References to ZSTs may be at arbitrary aligned addresses --- compiler/rustc_mir/src/const_eval/eval_queries.rs | 10 +++++----- src/test/ui/match/const_non_normal_zst_ref_pattern.rs | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/match/const_non_normal_zst_ref_pattern.rs diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index a0ee7fdc072ef..57aa216850aab 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -14,7 +14,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{self, subst::Subst, TyCtxt}; use rustc_span::source_map::Span; use rustc_target::abi::{Abi, LayoutOf}; -use std::convert::TryInto; +use std::convert::{TryFrom, TryInto}; pub fn note_on_undefined_behavior_error() -> &'static str { "The rules on what exactly is undefined behavior aren't clear, \ @@ -148,10 +148,10 @@ pub(super) fn op_to_const<'tcx>( Scalar::Raw { data, .. } => { assert!(mplace.layout.is_zst()); assert_eq!( - data, - mplace.layout.align.abi.bytes().into(), - "this MPlaceTy must come from `try_as_mplace` being used on a zst, so we know what - value this integer address must have", + u64::try_from(data).unwrap() % mplace.layout.align.abi.bytes(), + 0, + "this MPlaceTy must come from a validated constant, thus we can assume the \ + alignment is correct", ); ConstValue::Scalar(Scalar::zst()) } diff --git a/src/test/ui/match/const_non_normal_zst_ref_pattern.rs b/src/test/ui/match/const_non_normal_zst_ref_pattern.rs new file mode 100644 index 0000000000000..a114fafb64730 --- /dev/null +++ b/src/test/ui/match/const_non_normal_zst_ref_pattern.rs @@ -0,0 +1,9 @@ +// check-pass + +const FOO: isize = 10; +const ZST: &() = unsafe { std::mem::transmute(FOO) }; +fn main() { + match &() { + ZST => 9, + }; +}