Skip to content

Commit 16be666

Browse files
author
Lukas Markeffsky
committed
make LayoutCx not generic
1 parent 13b5a4e commit 16be666

File tree

9 files changed

+42
-71
lines changed

9 files changed

+42
-71
lines changed

Diff for: compiler/rustc_const_eval/src/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::mir::interpret::{
2121
UnsupportedOpInfo, ValidationErrorInfo,
2222
};
2323
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
24-
use rustc_middle::ty::{self, Ty, TyCtxt};
24+
use rustc_middle::ty::{self, Ty};
2525
use rustc_span::symbol::{sym, Symbol};
2626
use rustc_target::abi::{
2727
Abi, FieldIdx, FieldsShape, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange,
@@ -949,7 +949,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
949949

950950
/// Helper for recursive traversal: add data ranges of the given type to `out`.
951951
fn union_data_range_uncached<'tcx>(
952-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
952+
cx: &LayoutCx<'tcx>,
953953
layout: TyAndLayout<'tcx>,
954954
base_offset: Size,
955955
out: &mut RangeSet,

Diff for: 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
/// for details.
4343
fn check_validity_requirement_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);
@@ -80,7 +80,7 @@ fn check_validity_requirement_strict<'tcx>(
8080
/// function for details.
8181
fn check_validity_requirement_lax<'tcx>(
8282
this: TyAndLayout<'tcx>,
83-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
83+
cx: &LayoutCx<'tcx>,
8484
init_kind: ValidityRequirement,
8585
) -> Result<bool, &'tcx LayoutError<'tcx>> {
8686
let scalar_allows_raw_init = move |s: Scalar| -> bool {

Diff for: 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>,

Diff for: 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;

Diff for: 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

@@ -389,7 +389,7 @@ pub(crate) mod rustc {
389389
tag: Option<(ScalarInt, VariantIdx, TagEncoding<VariantIdx>)>,
390390
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
391391
total_size: Size,
392-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
392+
cx: LayoutCx<'tcx>,
393393
) -> Result<Self, Err> {
394394
// This constructor does not support non-`FieldsShape::Arbitrary`
395395
// layouts.
@@ -470,7 +470,7 @@ pub(crate) mod rustc {
470470
fn from_union(
471471
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
472472
def: AdtDef<'tcx>,
473-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
473+
cx: LayoutCx<'tcx>,
474474
) -> Result<Self, Err> {
475475
assert!(def.is_union());
476476

@@ -500,7 +500,7 @@ pub(crate) mod rustc {
500500
}
501501

502502
fn ty_field<'tcx>(
503-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
503+
cx: LayoutCx<'tcx>,
504504
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
505505
i: FieldIdx,
506506
) -> Ty<'tcx> {
@@ -527,7 +527,7 @@ pub(crate) mod rustc {
527527
}
528528

529529
fn ty_variant<'tcx>(
530-
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
530+
cx: LayoutCx<'tcx>,
531531
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
532532
i: VariantIdx,
533533
) -> Layout<'tcx> {

Diff for: 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>,

Diff for: 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>,
@@ -1017,7 +1014,7 @@ fn coroutine_layout<'tcx>(
10171014
Ok(layout)
10181015
}
10191016

1020-
fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: TyAndLayout<'tcx>) {
1017+
fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>) {
10211018
// Ignore layouts that are done with non-empty environments or
10221019
// non-monomorphic layouts, as the user only wants to see the stuff
10231020
// resulting from the final codegen session.
@@ -1068,7 +1065,7 @@ fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: T
10681065
}
10691066

10701067
fn variant_info_for_adt<'tcx>(
1071-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
1068+
cx: &LayoutCx<'tcx>,
10721069
layout: TyAndLayout<'tcx>,
10731070
adt_def: AdtDef<'tcx>,
10741071
) -> (Vec<VariantInfo>, Option<Size>) {
@@ -1140,7 +1137,7 @@ fn variant_info_for_adt<'tcx>(
11401137
}
11411138

11421139
fn variant_info_for_coroutine<'tcx>(
1143-
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
1140+
cx: &LayoutCx<'tcx>,
11441141
layout: TyAndLayout<'tcx>,
11451142
def_id: DefId,
11461143
args: ty::GenericArgsRef<'tcx>,

Diff for: compiler/rustc_ty_utils/src/layout_sanity_check.rs

+5-12
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());
@@ -28,8 +24,8 @@ pub(super) fn sanity_check_layout<'tcx>(
2824
}
2925

3026
/// Yields non-ZST fields of the type
31-
fn non_zst_fields<'a, 'tcx>(
32-
cx: &'a LayoutCx<'tcx, TyCtxt<'tcx>>,
27+
fn non_zst_fields<'tcx, 'a>(
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);

Diff for: src/tools/miri/src/machine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ 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>> {
384+
fn new(layout_cx: LayoutCx<'tcx>) -> Result<Self, &'tcx LayoutError<'tcx>> {
385385
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);
@@ -596,7 +596,7 @@ 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 {
599+
pub(crate) fn new(config: &MiriConfig, layout_cx: LayoutCx<'tcx>) -> Self {
600600
let tcx = layout_cx.tcx;
601601
let local_crates = helpers::get_local_crates(tcx);
602602
let layouts =

0 commit comments

Comments
 (0)