Skip to content

Commit 849b6d2

Browse files
author
Lukas Markeffsky
committed
make LayoutCx not generic
1 parent 842d6fc commit 849b6d2

File tree

8 files changed

+39
-68
lines changed

8 files changed

+39
-68
lines changed

compiler/rustc_const_eval/src/util/check_validity_requirement.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn check_validity_requirement<'tcx>(
4242
/// details.
4343
fn might_permit_raw_init_strict<'tcx>(
4444
ty: TyAndLayout<'tcx>,
45-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
45+
cx: &LayoutCx<'tcx>,
4646
kind: ValidityRequirement,
4747
) -> Result<bool, &'tcx LayoutError<'tcx>> {
4848
let machine = CompileTimeMachine::new(CanAccessMutGlobal::No, CheckAlignment::Error);
@@ -74,7 +74,7 @@ fn might_permit_raw_init_strict<'tcx>(
7474
/// details.
7575
fn might_permit_raw_init_lax<'tcx>(
7676
this: TyAndLayout<'tcx>,
77-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
77+
cx: &LayoutCx<'tcx>,
7878
init_kind: ValidityRequirement,
7979
) -> Result<bool, &'tcx LayoutError<'tcx>> {
8080
let scalar_allows_raw_init = move |s: Scalar| -> bool {

compiler/rustc_middle/src/ty/layout.rs

+9-28
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,12 @@ impl<'tcx> IntoDiagArg for LayoutError<'tcx> {
286286
}
287287

288288
#[derive(Clone, Copy)]
289-
pub struct LayoutCx<'tcx, C> {
290-
pub tcx: C,
289+
pub struct LayoutCx<'tcx> {
290+
pub tcx: TyCtxt<'tcx>,
291291
pub param_env: ty::ParamEnv<'tcx>,
292292
}
293293

294-
impl<'tcx> LayoutCalculator for LayoutCx<'tcx, TyCtxt<'tcx>> {
294+
impl<'tcx> LayoutCalculator for LayoutCx<'tcx> {
295295
type TargetDataLayoutRef = &'tcx TargetDataLayout;
296296

297297
fn delayed_bug(&self, txt: impl Into<Cow<'static, str>>) {
@@ -568,31 +568,31 @@ impl<'tcx> HasTyCtxt<'tcx> for TyCtxtAt<'tcx> {
568568
}
569569
}
570570

571-
impl<'tcx, C> HasParamEnv<'tcx> for LayoutCx<'tcx, C> {
571+
impl<'tcx> HasParamEnv<'tcx> for LayoutCx<'tcx> {
572572
fn param_env(&self) -> ty::ParamEnv<'tcx> {
573573
self.param_env
574574
}
575575
}
576576

577-
impl<'tcx, T: HasDataLayout> HasDataLayout for LayoutCx<'tcx, T> {
577+
impl<'tcx> HasDataLayout for LayoutCx<'tcx> {
578578
fn data_layout(&self) -> &TargetDataLayout {
579579
self.tcx.data_layout()
580580
}
581581
}
582582

583-
impl<'tcx, T: HasTargetSpec> HasTargetSpec for LayoutCx<'tcx, T> {
583+
impl<'tcx> HasTargetSpec for LayoutCx<'tcx> {
584584
fn target_spec(&self) -> &Target {
585585
self.tcx.target_spec()
586586
}
587587
}
588588

589-
impl<'tcx, T: HasWasmCAbiOpt> HasWasmCAbiOpt for LayoutCx<'tcx, T> {
589+
impl<'tcx> HasWasmCAbiOpt for LayoutCx<'tcx> {
590590
fn wasm_c_abi_opt(&self) -> WasmCAbi {
591591
self.tcx.wasm_c_abi_opt()
592592
}
593593
}
594594

595-
impl<'tcx, T: HasTyCtxt<'tcx>> HasTyCtxt<'tcx> for LayoutCx<'tcx, T> {
595+
impl<'tcx> HasTyCtxt<'tcx> for LayoutCx<'tcx> {
596596
fn tcx(&self) -> TyCtxt<'tcx> {
597597
self.tcx.tcx()
598598
}
@@ -685,7 +685,7 @@ pub trait LayoutOf<'tcx>: LayoutOfHelpers<'tcx> {
685685

686686
impl<'tcx, C: LayoutOfHelpers<'tcx>> LayoutOf<'tcx> for C {}
687687

688-
impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxt<'tcx>> {
688+
impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx> {
689689
type LayoutOfResult = Result<TyAndLayout<'tcx>, &'tcx LayoutError<'tcx>>;
690690

691691
#[inline]
@@ -699,25 +699,6 @@ impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxt<'tcx>> {
699699
}
700700
}
701701

702-
impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxtAt<'tcx>> {
703-
type LayoutOfResult = Result<TyAndLayout<'tcx>, &'tcx LayoutError<'tcx>>;
704-
705-
#[inline]
706-
fn layout_tcx_at_span(&self) -> Span {
707-
self.tcx.span
708-
}
709-
710-
#[inline]
711-
fn handle_layout_err(
712-
&self,
713-
err: LayoutError<'tcx>,
714-
_: Span,
715-
_: Ty<'tcx>,
716-
) -> &'tcx LayoutError<'tcx> {
717-
self.tcx.arena.alloc(err)
718-
}
719-
}
720-
721702
impl<'tcx, C> TyAbiInterface<'tcx, C> for Ty<'tcx>
722703
where
723704
C: HasTyCtxt<'tcx> + HasParamEnv<'tcx>,

compiler/rustc_transmute/src/layout/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub mod rustc {
6464

6565
use rustc_middle::mir::Mutability;
6666
use rustc_middle::ty::layout::{LayoutCx, LayoutError};
67-
use rustc_middle::ty::{self, Ty, TyCtxt};
67+
use rustc_middle::ty::{self, Ty};
6868
use rustc_target::abi::Layout;
6969

7070
/// A reference in the layout.
@@ -124,7 +124,7 @@ pub mod rustc {
124124
}
125125

126126
pub(crate) fn layout_of<'tcx>(
127-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
127+
cx: LayoutCx<'tcx>,
128128
ty: Ty<'tcx>,
129129
) -> Result<Layout<'tcx>, &'tcx LayoutError<'tcx>> {
130130
use rustc_middle::ty::layout::LayoutOf;

compiler/rustc_transmute/src/layout/tree.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub(crate) mod rustc {
204204
}
205205

206206
impl<'tcx> Tree<Def<'tcx>, Ref<'tcx>> {
207-
pub(crate) fn from_ty(ty: Ty<'tcx>, cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, Err> {
207+
pub(crate) fn from_ty(ty: Ty<'tcx>, cx: LayoutCx<'tcx>) -> Result<Self, Err> {
208208
use rustc_target::abi::HasDataLayout;
209209
let layout = layout_of(cx, ty)?;
210210

@@ -274,7 +274,7 @@ pub(crate) mod rustc {
274274
fn from_tuple(
275275
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
276276
members: &'tcx List<Ty<'tcx>>,
277-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
277+
cx: LayoutCx<'tcx>,
278278
) -> Result<Self, Err> {
279279
match &layout.fields {
280280
FieldsShape::Primitive => {
@@ -299,7 +299,7 @@ pub(crate) mod rustc {
299299
fn from_struct(
300300
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
301301
def: AdtDef<'tcx>,
302-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
302+
cx: LayoutCx<'tcx>,
303303
) -> Result<Self, Err> {
304304
assert!(def.is_struct());
305305
let def = Def::Adt(def);
@@ -314,7 +314,7 @@ pub(crate) mod rustc {
314314
fn from_enum(
315315
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
316316
def: AdtDef<'tcx>,
317-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
317+
cx: LayoutCx<'tcx>,
318318
) -> Result<Self, Err> {
319319
assert!(def.is_enum());
320320

@@ -383,7 +383,7 @@ pub(crate) mod rustc {
383383
tag: Option<ScalarInt>,
384384
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
385385
total_size: Size,
386-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
386+
cx: LayoutCx<'tcx>,
387387
) -> Result<Self, Err> {
388388
// This constructor does not support non-`FieldsShape::Arbitrary`
389389
// layouts.
@@ -455,7 +455,7 @@ pub(crate) mod rustc {
455455
fn from_union(
456456
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
457457
def: AdtDef<'tcx>,
458-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
458+
cx: LayoutCx<'tcx>,
459459
) -> Result<Self, Err> {
460460
assert!(def.is_union());
461461

@@ -485,7 +485,7 @@ pub(crate) mod rustc {
485485
}
486486

487487
fn ty_field<'tcx>(
488-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
488+
cx: LayoutCx<'tcx>,
489489
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
490490
i: FieldIdx,
491491
) -> Ty<'tcx> {
@@ -512,7 +512,7 @@ pub(crate) mod rustc {
512512
}
513513

514514
fn ty_variant<'tcx>(
515-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
515+
cx: LayoutCx<'tcx>,
516516
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
517517
i: VariantIdx,
518518
) -> Layout<'tcx> {

compiler/rustc_ty_utils/src/abi.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ fn fn_abi_of_instance<'tcx>(
358358

359359
// Handle safe Rust thin and fat pointers.
360360
fn adjust_for_rust_scalar<'tcx>(
361-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
361+
cx: LayoutCx<'tcx>,
362362
attrs: &mut ArgAttributes,
363363
scalar: Scalar,
364364
layout: TyAndLayout<'tcx>,
@@ -448,12 +448,12 @@ fn adjust_for_rust_scalar<'tcx>(
448448

449449
/// Ensure that the ABI makes basic sense.
450450
fn fn_abi_sanity_check<'tcx>(
451-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
451+
cx: &LayoutCx<'tcx>,
452452
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
453453
spec_abi: SpecAbi,
454454
) {
455455
fn fn_arg_sanity_check<'tcx>(
456-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
456+
cx: &LayoutCx<'tcx>,
457457
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
458458
spec_abi: SpecAbi,
459459
arg: &ArgAbi<'tcx, Ty<'tcx>>,
@@ -538,7 +538,7 @@ fn fn_abi_sanity_check<'tcx>(
538538
// arguments of this method, into a separate `struct`.
539539
#[tracing::instrument(level = "debug", skip(cx, caller_location, fn_def_id, force_thin_self_ptr))]
540540
fn fn_abi_new_uncached<'tcx>(
541-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
541+
cx: &LayoutCx<'tcx>,
542542
sig: ty::PolyFnSig<'tcx>,
543543
extra_args: &[Ty<'tcx>],
544544
caller_location: Option<Ty<'tcx>>,
@@ -643,7 +643,7 @@ fn fn_abi_new_uncached<'tcx>(
643643

644644
#[tracing::instrument(level = "trace", skip(cx))]
645645
fn fn_abi_adjust_for_abi<'tcx>(
646-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
646+
cx: &LayoutCx<'tcx>,
647647
fn_abi: &mut FnAbi<'tcx, Ty<'tcx>>,
648648
abi: SpecAbi,
649649
fn_def_id: Option<DefId>,

compiler/rustc_ty_utils/src/layout.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,12 @@ fn layout_of<'tcx>(
7979
Ok(layout)
8080
}
8181

82-
fn error<'tcx>(
83-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
84-
err: LayoutError<'tcx>,
85-
) -> &'tcx LayoutError<'tcx> {
82+
fn error<'tcx>(cx: &LayoutCx<'tcx>, err: LayoutError<'tcx>) -> &'tcx LayoutError<'tcx> {
8683
cx.tcx.arena.alloc(err)
8784
}
8885

8986
fn univariant_uninterned<'tcx>(
90-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
87+
cx: &LayoutCx<'tcx>,
9188
ty: Ty<'tcx>,
9289
fields: &IndexSlice<FieldIdx, Layout<'_>>,
9390
repr: &ReprOptions,
@@ -103,7 +100,7 @@ fn univariant_uninterned<'tcx>(
103100
}
104101

105102
fn layout_of_uncached<'tcx>(
106-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
103+
cx: &LayoutCx<'tcx>,
107104
ty: Ty<'tcx>,
108105
) -> Result<Layout<'tcx>, &'tcx LayoutError<'tcx>> {
109106
// Types that reference `ty::Error` pessimistically don't have a meaningful layout.
@@ -809,7 +806,7 @@ fn coroutine_saved_local_eligibility(
809806

810807
/// Compute the full coroutine layout.
811808
fn coroutine_layout<'tcx>(
812-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
809+
cx: &LayoutCx<'tcx>,
813810
ty: Ty<'tcx>,
814811
def_id: hir::def_id::DefId,
815812
args: GenericArgsRef<'tcx>,
@@ -1011,7 +1008,7 @@ fn coroutine_layout<'tcx>(
10111008
Ok(layout)
10121009
}
10131010

1014-
fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: TyAndLayout<'tcx>) {
1011+
fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>) {
10151012
// Ignore layouts that are done with non-empty environments or
10161013
// non-monomorphic layouts, as the user only wants to see the stuff
10171014
// resulting from the final codegen session.
@@ -1062,7 +1059,7 @@ fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: T
10621059
}
10631060

10641061
fn variant_info_for_adt<'tcx>(
1065-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
1062+
cx: &LayoutCx<'tcx>,
10661063
layout: TyAndLayout<'tcx>,
10671064
adt_def: AdtDef<'tcx>,
10681065
) -> (Vec<VariantInfo>, Option<Size>) {
@@ -1134,7 +1131,7 @@ fn variant_info_for_adt<'tcx>(
11341131
}
11351132

11361133
fn variant_info_for_coroutine<'tcx>(
1137-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
1134+
cx: &LayoutCx<'tcx>,
11381135
layout: TyAndLayout<'tcx>,
11391136
def_id: DefId,
11401137
args: ty::GenericArgsRef<'tcx>,

compiler/rustc_ty_utils/src/layout_sanity_check.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ use std::assert_matches::assert_matches;
22

33
use rustc_middle::bug;
44
use rustc_middle::ty::layout::{LayoutCx, TyAndLayout};
5-
use rustc_middle::ty::TyCtxt;
65
use rustc_target::abi::*;
76

87
/// Enforce some basic invariants on layouts.
9-
pub(super) fn sanity_check_layout<'tcx>(
10-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
11-
layout: &TyAndLayout<'tcx>,
12-
) {
8+
pub(super) fn sanity_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) {
139
// Type-level uninhabitedness should always imply ABI uninhabitedness.
1410
if layout.ty.is_privately_uninhabited(cx.tcx, cx.param_env) {
1511
assert!(layout.abi.is_uninhabited());
@@ -29,7 +25,7 @@ pub(super) fn sanity_check_layout<'tcx>(
2925

3026
/// Yields non-ZST fields of the type
3127
fn non_zst_fields<'tcx, 'a>(
32-
cx: &'a LayoutCx<'tcx, TyCtxt<'tcx>>,
28+
cx: &'a LayoutCx<'tcx>,
3329
layout: &'a TyAndLayout<'tcx>,
3430
) -> impl Iterator<Item = (Size, TyAndLayout<'tcx>)> + 'a {
3531
(0..layout.layout.fields().count()).filter_map(|i| {
@@ -43,10 +39,7 @@ pub(super) fn sanity_check_layout<'tcx>(
4339
})
4440
}
4541

46-
fn skip_newtypes<'tcx>(
47-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
48-
layout: &TyAndLayout<'tcx>,
49-
) -> TyAndLayout<'tcx> {
42+
fn skip_newtypes<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) -> TyAndLayout<'tcx> {
5043
if matches!(layout.layout.variants(), Variants::Multiple { .. }) {
5144
// Definitely not a newtype of anything.
5245
return *layout;
@@ -69,7 +62,7 @@ pub(super) fn sanity_check_layout<'tcx>(
6962
*layout
7063
}
7164

72-
fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: &TyAndLayout<'tcx>) {
65+
fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) {
7366
// Verify the ABI mandated alignment and size.
7467
let align = layout.abi.inherent_align(cx).map(|align| align.abi);
7568
let size = layout.abi.inherent_size(cx);

src/tools/miri/src/machine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ pub struct PrimitiveLayouts<'tcx> {
363363
}
364364

365365
impl<'tcx> PrimitiveLayouts<'tcx> {
366-
fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, &'tcx LayoutError<'tcx>> {
366+
fn new(layout_cx: LayoutCx<'tcx>) -> Result<Self, &'tcx LayoutError<'tcx>> {
367367
let tcx = layout_cx.tcx;
368368
let mut_raw_ptr = Ty::new_mut_ptr(tcx, tcx.types.unit);
369369
let const_raw_ptr = Ty::new_imm_ptr(tcx, tcx.types.unit);
@@ -575,7 +575,7 @@ pub struct MiriMachine<'tcx> {
575575
}
576576

577577
impl<'tcx> MiriMachine<'tcx> {
578-
pub(crate) fn new(config: &MiriConfig, layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Self {
578+
pub(crate) fn new(config: &MiriConfig, layout_cx: LayoutCx<'tcx>) -> Self {
579579
let tcx = layout_cx.tcx;
580580
let local_crates = helpers::get_local_crates(tcx);
581581
let layouts =

0 commit comments

Comments
 (0)