Skip to content

Commit 15a6861

Browse files
committed
Only check packed ADT.
1 parent 1c5f176 commit 15a6861

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

compiler/rustc_const_eval/src/util/alignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ where
4040
}
4141
}
4242

43-
fn is_within_packed<'tcx, L>(
43+
pub fn is_within_packed<'tcx, L>(
4444
tcx: TyCtxt<'tcx>,
4545
local_decls: &L,
4646
place: Place<'tcx>,

compiler/rustc_const_eval/src/util/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod check_validity_requirement;
55
mod compare_types;
66
mod type_name;
77

8-
pub use self::alignment::is_disaligned;
8+
pub use self::alignment::{is_disaligned, is_within_packed};
99
pub use self::check_validity_requirement::check_validity_requirement;
1010
pub use self::compare_types::{is_equal_up_to_subtyping, is_subtype};
1111
pub use self::type_name::type_name;

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! will still not cause any further changes.
1313
//!
1414
15-
use crate::util::is_disaligned;
15+
use crate::util::is_within_packed;
1616
use rustc_index::bit_set::BitSet;
1717
use rustc_middle::mir::visit::Visitor;
1818
use rustc_middle::mir::*;
@@ -32,8 +32,6 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
3232
.iterate_to_fixpoint()
3333
.into_results_cursor(body);
3434

35-
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
36-
3735
// For blocks with a call terminator, if an argument copy can be turned into a move,
3836
// record it as (block, argument index).
3937
let mut call_operands_to_move = Vec::new();
@@ -52,7 +50,11 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
5250
&& !place.is_indirect()
5351
&& !borrowed.contains(place.local)
5452
&& !state.contains(place.local)
55-
&& !is_disaligned(tcx, body, param_env, place)
53+
// If `place` is a projection of a disaligned field in a packed ADT,
54+
// the move may be codegened as a pointer to that field.
55+
// Using that disaligned pointer may trigger UB in the callee,
56+
// so do nothing.
57+
&& is_within_packed(tcx, body, place).is_none()
5658
{
5759
call_operands_to_move.push((bb, index));
5860
}

0 commit comments

Comments
 (0)