Skip to content

Commit 178790f

Browse files
committed
check_align: we can still check low alignments on MSVC
1 parent d4ce381 commit 178790f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

compiler/rustc_mir_transform/src/check_alignment.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ pub(super) struct CheckAlignment;
1212

1313
impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
1414
fn is_enabled(&self, sess: &Session) -> bool {
15-
if sess.target.max_reliable_alignment() < Align::MAX {
16-
return false;
17-
}
1815
sess.ub_checks()
1916
}
2017

@@ -87,6 +84,29 @@ fn insert_alignment_check<'tcx>(
8784
))),
8885
});
8986

87+
// If this target does not have reliable alignment, further limit the mask.
88+
#[allow(irrefutable_let_patterns)]
89+
if let max_align = tcx.sess.target.max_reliable_alignment()
90+
&& max_align < Align::MAX
91+
{
92+
let max_mask = max_align.bytes() - 1;
93+
let max_mask = Operand::Constant(Box::new(ConstOperand {
94+
span: source_info.span,
95+
user_ty: None,
96+
const_: Const::Val(
97+
ConstValue::Scalar(Scalar::from_target_usize(max_mask, &tcx)),
98+
tcx.types.usize,
99+
),
100+
}));
101+
stmts.push(Statement {
102+
source_info,
103+
kind: StatementKind::Assign(Box::new((
104+
alignment_mask,
105+
Rvalue::BinaryOp(BinOp::Sub, Box::new((Operand::Copy(alignment_mask), max_mask))),
106+
))),
107+
});
108+
}
109+
90110
// BitAnd the alignment mask with the pointer
91111
let alignment_bits =
92112
local_decls.push(LocalDecl::with_source_info(tcx.types.usize, source_info)).into();

0 commit comments

Comments
 (0)