Skip to content

Commit 047a657

Browse files
committed
Auto merge of #129970 - lukas-code:LayoutCalculator, r=compiler-errors
layout computation: gracefully handle unsized types in unexpected locations This PR reworks the layout computation to eagerly return an error when encountering an unsized field where a sized field was expected, rather than delaying a bug and attempting to recover a layout. This is required, because with trivially false where clauses like `[T]: Sized`, any field can possible be an unsized type, without causing a compile error. Since this PR removes the `delayed_bug` method from the `LayoutCalculator` trait, it essentially becomes the same as the `HasDataLayout` trait, so I've also refactored the `LayoutCalculator` to be a simple wrapper struct around a type that implements `HasDataLayout`. The majority of the diff is whitespace changes, so viewing with whitespace ignored is advised. implements rust-lang/rust#123169 (comment) r? `@compiler-errors` or compiler fixes rust-lang/rust#123134 fixes rust-lang/rust#124182 fixes rust-lang/rust#126939 fixes rust-lang/rust#127737
2 parents 692adda + d1d77b8 commit 047a657

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub fn create_ecx<'tcx>(
277277
config: &MiriConfig,
278278
) -> InterpResult<'tcx, InterpCx<'tcx, MiriMachine<'tcx>>> {
279279
let param_env = ty::ParamEnv::reveal_all();
280-
let layout_cx = LayoutCx { tcx, param_env };
280+
let layout_cx = LayoutCx::new(tcx, param_env);
281281
let mut ecx =
282282
InterpCx::new(tcx, rustc_span::DUMMY_SP, param_env, MiriMachine::new(config, layout_cx));
283283

src/machine.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::{
2121
query::TyCtxtAt,
2222
ty::{
2323
self,
24-
layout::{LayoutCx, LayoutError, LayoutOf, TyAndLayout},
24+
layout::{HasTyCtxt, LayoutCx, LayoutError, LayoutOf, TyAndLayout},
2525
Instance, Ty, TyCtxt,
2626
},
2727
};
@@ -381,8 +381,8 @@ pub struct PrimitiveLayouts<'tcx> {
381381
}
382382

383383
impl<'tcx> PrimitiveLayouts<'tcx> {
384-
fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, &'tcx LayoutError<'tcx>> {
385-
let tcx = layout_cx.tcx;
384+
fn new(layout_cx: LayoutCx<'tcx>) -> Result<Self, &'tcx LayoutError<'tcx>> {
385+
let tcx = layout_cx.tcx();
386386
let mut_raw_ptr = Ty::new_mut_ptr(tcx, tcx.types.unit);
387387
let const_raw_ptr = Ty::new_imm_ptr(tcx, tcx.types.unit);
388388
Ok(Self {
@@ -596,14 +596,13 @@ pub struct MiriMachine<'tcx> {
596596
}
597597

598598
impl<'tcx> MiriMachine<'tcx> {
599-
pub(crate) fn new(config: &MiriConfig, layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Self {
600-
let tcx = layout_cx.tcx;
599+
pub(crate) fn new(config: &MiriConfig, layout_cx: LayoutCx<'tcx>) -> Self {
600+
let tcx = layout_cx.tcx();
601601
let local_crates = helpers::get_local_crates(tcx);
602602
let layouts =
603603
PrimitiveLayouts::new(layout_cx).expect("Couldn't get layouts of primitive types");
604604
let profiler = config.measureme_out.as_ref().map(|out| {
605-
let crate_name = layout_cx
606-
.tcx
605+
let crate_name = tcx
607606
.sess
608607
.opts
609608
.crate_name
@@ -701,7 +700,7 @@ impl<'tcx> MiriMachine<'tcx> {
701700
clock: Clock::new(config.isolated_op == IsolatedOp::Allow),
702701
#[cfg(unix)]
703702
native_lib: config.native_lib.as_ref().map(|lib_file_path| {
704-
let target_triple = layout_cx.tcx.sess.opts.target_triple.triple();
703+
let target_triple = tcx.sess.opts.target_triple.triple();
705704
// Check if host target == the session target.
706705
if env!("TARGET") != target_triple {
707706
panic!(

0 commit comments

Comments
 (0)