Skip to content

Commit bf2a6c3

Browse files
author
Oliver Schneider
committed
Allow unaligned reads in constants
1 parent a272684 commit bf2a6c3

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

Diff for: src/librustc_mir/hair/pattern/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
792792
ConstVal::Value(miri) => const_val_field(
793793
self.tcx, self.param_env, instance,
794794
variant_opt, field, miri, cv.ty,
795-
).unwrap(),
795+
).expect("field access failed"),
796796
_ => bug!("{:#?} is not a valid adt", cv),
797797
};
798798
self.const_to_pat(instance, val, id, span)

Diff for: src/librustc_mir/interpret/eval_context.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1340,9 +1340,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
13401340
use syntax::ast::FloatTy;
13411341

13421342
let layout = self.layout_of(ty)?;
1343-
// do the strongest layout check of the two
1344-
let align = layout.align.max(ptr_align);
1345-
self.memory.check_align(ptr, align)?;
1343+
self.memory.check_align(ptr, ptr_align)?;
13461344

13471345
if layout.size.bytes() == 0 {
13481346
return Ok(Some(Value::ByVal(PrimVal::Undef)));

Diff for: src/test/ui/const-eval/ice-packed.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-pass
12+
#[derive(Copy, Clone, PartialEq, Eq)]
13+
#[repr(packed)]
14+
pub struct Num(u64);
15+
16+
impl Num {
17+
pub const ZERO: Self = Num(0);
18+
}
19+
20+
pub fn decrement(a: Num) -> Num {
21+
match a {
22+
Num::ZERO => Num::ZERO,
23+
a => Num(a.0 - 1)
24+
}
25+
}
26+
27+
fn main() {
28+
}

0 commit comments

Comments
 (0)