Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

References to ZSTs may be at arbitrary aligned addresses #77360

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions compiler/rustc_mir/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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())
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/match/const_non_normal_zst_ref_pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// check-pass

const FOO: isize = 10;
const ZST: &() = unsafe { std::mem::transmute(FOO) };
fn main() {
match &() {
ZST => 9,
};
}