From 16be6666d4502e0e2255b9c4c1afab87db0ac50f Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Sun, 15 Sep 2024 21:59:51 +0200 Subject: [PATCH] make `LayoutCx` not generic --- .../src/interpret/validity.rs | 4 +- .../src/util/check_validity_requirement.rs | 4 +- compiler/rustc_middle/src/ty/layout.rs | 37 +++++-------------- compiler/rustc_transmute/src/layout/mod.rs | 4 +- compiler/rustc_transmute/src/layout/tree.rs | 16 ++++---- compiler/rustc_ty_utils/src/abi.rs | 10 ++--- compiler/rustc_ty_utils/src/layout.rs | 17 ++++----- .../rustc_ty_utils/src/layout_sanity_check.rs | 17 +++------ src/tools/miri/src/machine.rs | 4 +- 9 files changed, 42 insertions(+), 71 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index ca38f779256..5647bf8d3c2 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -21,7 +21,7 @@ UnsupportedOpInfo, ValidationErrorInfo, }; use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout}; -use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty}; use rustc_span::symbol::{sym, Symbol}; use rustc_target::abi::{ Abi, FieldIdx, FieldsShape, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange, @@ -949,7 +949,7 @@ fn union_data_range<'e>( /// Helper for recursive traversal: add data ranges of the given type to `out`. fn union_data_range_uncached<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>, base_offset: Size, out: &mut RangeSet, diff --git a/compiler/rustc_const_eval/src/util/check_validity_requirement.rs b/compiler/rustc_const_eval/src/util/check_validity_requirement.rs index 611a8e1a884..f5277c328ea 100644 --- a/compiler/rustc_const_eval/src/util/check_validity_requirement.rs +++ b/compiler/rustc_const_eval/src/util/check_validity_requirement.rs @@ -42,7 +42,7 @@ pub fn check_validity_requirement<'tcx>( /// for details. fn check_validity_requirement_strict<'tcx>( ty: TyAndLayout<'tcx>, - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, kind: ValidityRequirement, ) -> Result> { let machine = CompileTimeMachine::new(CanAccessMutGlobal::No, CheckAlignment::Error); @@ -80,7 +80,7 @@ fn check_validity_requirement_strict<'tcx>( /// function for details. fn check_validity_requirement_lax<'tcx>( this: TyAndLayout<'tcx>, - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, init_kind: ValidityRequirement, ) -> Result> { let scalar_allows_raw_init = move |s: Scalar| -> bool { diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 254a0119920..48eb8227022 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -286,12 +286,12 @@ fn into_diag_arg(self) -> DiagArgValue { } #[derive(Clone, Copy)] -pub struct LayoutCx<'tcx, C> { - pub tcx: C, +pub struct LayoutCx<'tcx> { + pub tcx: TyCtxt<'tcx>, pub param_env: ty::ParamEnv<'tcx>, } -impl<'tcx> LayoutCalculator for LayoutCx<'tcx, TyCtxt<'tcx>> { +impl<'tcx> LayoutCalculator for LayoutCx<'tcx> { type TargetDataLayoutRef = &'tcx TargetDataLayout; fn delayed_bug(&self, txt: impl Into>) { @@ -568,31 +568,31 @@ fn tcx(&self) -> TyCtxt<'tcx> { } } -impl<'tcx, C> HasParamEnv<'tcx> for LayoutCx<'tcx, C> { +impl<'tcx> HasParamEnv<'tcx> for LayoutCx<'tcx> { fn param_env(&self) -> ty::ParamEnv<'tcx> { self.param_env } } -impl<'tcx, T: HasDataLayout> HasDataLayout for LayoutCx<'tcx, T> { +impl<'tcx> HasDataLayout for LayoutCx<'tcx> { fn data_layout(&self) -> &TargetDataLayout { self.tcx.data_layout() } } -impl<'tcx, T: HasTargetSpec> HasTargetSpec for LayoutCx<'tcx, T> { +impl<'tcx> HasTargetSpec for LayoutCx<'tcx> { fn target_spec(&self) -> &Target { self.tcx.target_spec() } } -impl<'tcx, T: HasWasmCAbiOpt> HasWasmCAbiOpt for LayoutCx<'tcx, T> { +impl<'tcx> HasWasmCAbiOpt for LayoutCx<'tcx> { fn wasm_c_abi_opt(&self) -> WasmCAbi { self.tcx.wasm_c_abi_opt() } } -impl<'tcx, T: HasTyCtxt<'tcx>> HasTyCtxt<'tcx> for LayoutCx<'tcx, T> { +impl<'tcx> HasTyCtxt<'tcx> for LayoutCx<'tcx> { fn tcx(&self) -> TyCtxt<'tcx> { self.tcx.tcx() } @@ -685,7 +685,7 @@ fn spanned_layout_of(&self, ty: Ty<'tcx>, span: Span) -> Self::LayoutOfResult { impl<'tcx, C: LayoutOfHelpers<'tcx>> LayoutOf<'tcx> for C {} -impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxt<'tcx>> { +impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx> { type LayoutOfResult = Result, &'tcx LayoutError<'tcx>>; #[inline] @@ -699,25 +699,6 @@ fn handle_layout_err( } } -impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxtAt<'tcx>> { - type LayoutOfResult = Result, &'tcx LayoutError<'tcx>>; - - #[inline] - fn layout_tcx_at_span(&self) -> Span { - self.tcx.span - } - - #[inline] - fn handle_layout_err( - &self, - err: LayoutError<'tcx>, - _: Span, - _: Ty<'tcx>, - ) -> &'tcx LayoutError<'tcx> { - self.tcx.arena.alloc(err) - } -} - impl<'tcx, C> TyAbiInterface<'tcx, C> for Ty<'tcx> where C: HasTyCtxt<'tcx> + HasParamEnv<'tcx>, diff --git a/compiler/rustc_transmute/src/layout/mod.rs b/compiler/rustc_transmute/src/layout/mod.rs index 1cf9e0b9b70..596d80869ea 100644 --- a/compiler/rustc_transmute/src/layout/mod.rs +++ b/compiler/rustc_transmute/src/layout/mod.rs @@ -64,7 +64,7 @@ pub mod rustc { use rustc_middle::mir::Mutability; use rustc_middle::ty::layout::{LayoutCx, LayoutError}; - use rustc_middle::ty::{self, Ty, TyCtxt}; + use rustc_middle::ty::{self, Ty}; use rustc_target::abi::Layout; /// A reference in the layout. @@ -124,7 +124,7 @@ fn has_safety_invariants(&self) -> bool { } pub(crate) fn layout_of<'tcx>( - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, ty: Ty<'tcx>, ) -> Result, &'tcx LayoutError<'tcx>> { use rustc_middle::ty::layout::LayoutOf; diff --git a/compiler/rustc_transmute/src/layout/tree.rs b/compiler/rustc_transmute/src/layout/tree.rs index b5ce465a173..3b7284c1ad6 100644 --- a/compiler/rustc_transmute/src/layout/tree.rs +++ b/compiler/rustc_transmute/src/layout/tree.rs @@ -204,7 +204,7 @@ fn from(err: &LayoutError<'tcx>) -> Self { } impl<'tcx> Tree, Ref<'tcx>> { - pub(crate) fn from_ty(ty: Ty<'tcx>, cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result { + pub(crate) fn from_ty(ty: Ty<'tcx>, cx: LayoutCx<'tcx>) -> Result { use rustc_target::abi::HasDataLayout; let layout = layout_of(cx, ty)?; @@ -274,7 +274,7 @@ pub(crate) fn from_ty(ty: Ty<'tcx>, cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result< fn from_tuple( (ty, layout): (Ty<'tcx>, Layout<'tcx>), members: &'tcx List>, - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, ) -> Result { match &layout.fields { FieldsShape::Primitive => { @@ -299,7 +299,7 @@ fn from_tuple( fn from_struct( (ty, layout): (Ty<'tcx>, Layout<'tcx>), def: AdtDef<'tcx>, - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, ) -> Result { assert!(def.is_struct()); let def = Def::Adt(def); @@ -314,7 +314,7 @@ fn from_struct( fn from_enum( (ty, layout): (Ty<'tcx>, Layout<'tcx>), def: AdtDef<'tcx>, - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, ) -> Result { assert!(def.is_enum()); @@ -389,7 +389,7 @@ fn from_variant( tag: Option<(ScalarInt, VariantIdx, TagEncoding)>, (ty, layout): (Ty<'tcx>, Layout<'tcx>), total_size: Size, - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, ) -> Result { // This constructor does not support non-`FieldsShape::Arbitrary` // layouts. @@ -470,7 +470,7 @@ fn from_tag(tag: ScalarInt, tcx: TyCtxt<'tcx>) -> Self { fn from_union( (ty, layout): (Ty<'tcx>, Layout<'tcx>), def: AdtDef<'tcx>, - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, ) -> Result { assert!(def.is_union()); @@ -500,7 +500,7 @@ fn from_union( } fn ty_field<'tcx>( - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, (ty, layout): (Ty<'tcx>, Layout<'tcx>), i: FieldIdx, ) -> Ty<'tcx> { @@ -527,7 +527,7 @@ fn ty_field<'tcx>( } fn ty_variant<'tcx>( - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, (ty, layout): (Ty<'tcx>, Layout<'tcx>), i: VariantIdx, ) -> Layout<'tcx> { diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index 16cd147b7d4..0d433da3aea 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -358,7 +358,7 @@ fn fn_abi_of_instance<'tcx>( // Handle safe Rust thin and fat pointers. fn adjust_for_rust_scalar<'tcx>( - cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: LayoutCx<'tcx>, attrs: &mut ArgAttributes, scalar: Scalar, layout: TyAndLayout<'tcx>, @@ -448,12 +448,12 @@ fn adjust_for_rust_scalar<'tcx>( /// Ensure that the ABI makes basic sense. fn fn_abi_sanity_check<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, spec_abi: SpecAbi, ) { fn fn_arg_sanity_check<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, spec_abi: SpecAbi, arg: &ArgAbi<'tcx, Ty<'tcx>>, @@ -538,7 +538,7 @@ fn fn_arg_sanity_check<'tcx>( // arguments of this method, into a separate `struct`. #[tracing::instrument(level = "debug", skip(cx, caller_location, fn_def_id, force_thin_self_ptr))] fn fn_abi_new_uncached<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, sig: ty::PolyFnSig<'tcx>, extra_args: &[Ty<'tcx>], caller_location: Option>, @@ -643,7 +643,7 @@ fn fn_abi_new_uncached<'tcx>( #[tracing::instrument(level = "trace", skip(cx))] fn fn_abi_adjust_for_abi<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, fn_abi: &mut FnAbi<'tcx, Ty<'tcx>>, abi: SpecAbi, fn_def_id: Option, diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index c23a7fec5a5..2c2276ad40d 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -79,15 +79,12 @@ fn layout_of<'tcx>( Ok(layout) } -fn error<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, - err: LayoutError<'tcx>, -) -> &'tcx LayoutError<'tcx> { +fn error<'tcx>(cx: &LayoutCx<'tcx>, err: LayoutError<'tcx>) -> &'tcx LayoutError<'tcx> { cx.tcx.arena.alloc(err) } fn univariant_uninterned<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, ty: Ty<'tcx>, fields: &IndexSlice>, repr: &ReprOptions, @@ -103,7 +100,7 @@ fn univariant_uninterned<'tcx>( } fn layout_of_uncached<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, ty: Ty<'tcx>, ) -> Result, &'tcx LayoutError<'tcx>> { // Types that reference `ty::Error` pessimistically don't have a meaningful layout. @@ -809,7 +806,7 @@ fn coroutine_saved_local_eligibility( /// Compute the full coroutine layout. fn coroutine_layout<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, ty: Ty<'tcx>, def_id: hir::def_id::DefId, args: GenericArgsRef<'tcx>, @@ -1017,7 +1014,7 @@ fn coroutine_layout<'tcx>( Ok(layout) } -fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: TyAndLayout<'tcx>) { +fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>) { // Ignore layouts that are done with non-empty environments or // non-monomorphic layouts, as the user only wants to see the stuff // resulting from the final codegen session. @@ -1068,7 +1065,7 @@ fn record_layout_for_printing<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: T } fn variant_info_for_adt<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>, adt_def: AdtDef<'tcx>, ) -> (Vec, Option) { @@ -1140,7 +1137,7 @@ fn variant_info_for_adt<'tcx>( } fn variant_info_for_coroutine<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>, def_id: DefId, args: ty::GenericArgsRef<'tcx>, diff --git a/compiler/rustc_ty_utils/src/layout_sanity_check.rs b/compiler/rustc_ty_utils/src/layout_sanity_check.rs index 8378237fe2f..38fbd7a9437 100644 --- a/compiler/rustc_ty_utils/src/layout_sanity_check.rs +++ b/compiler/rustc_ty_utils/src/layout_sanity_check.rs @@ -2,14 +2,10 @@ use rustc_middle::bug; use rustc_middle::ty::layout::{LayoutCx, TyAndLayout}; -use rustc_middle::ty::TyCtxt; use rustc_target::abi::*; /// Enforce some basic invariants on layouts. -pub(super) fn sanity_check_layout<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, - layout: &TyAndLayout<'tcx>, -) { +pub(super) fn sanity_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) { // Type-level uninhabitedness should always imply ABI uninhabitedness. if layout.ty.is_privately_uninhabited(cx.tcx, cx.param_env) { assert!(layout.abi.is_uninhabited()); @@ -28,8 +24,8 @@ pub(super) fn sanity_check_layout<'tcx>( } /// Yields non-ZST fields of the type - fn non_zst_fields<'a, 'tcx>( - cx: &'a LayoutCx<'tcx, TyCtxt<'tcx>>, + fn non_zst_fields<'tcx, 'a>( + cx: &'a LayoutCx<'tcx>, layout: &'a TyAndLayout<'tcx>, ) -> impl Iterator)> + 'a { (0..layout.layout.fields().count()).filter_map(|i| { @@ -43,10 +39,7 @@ fn non_zst_fields<'a, 'tcx>( }) } - fn skip_newtypes<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, - layout: &TyAndLayout<'tcx>, - ) -> TyAndLayout<'tcx> { + fn skip_newtypes<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) -> TyAndLayout<'tcx> { if matches!(layout.layout.variants(), Variants::Multiple { .. }) { // Definitely not a newtype of anything. return *layout; @@ -69,7 +62,7 @@ fn skip_newtypes<'tcx>( *layout } - fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: &TyAndLayout<'tcx>) { + fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) { // Verify the ABI mandated alignment and size. let align = layout.abi.inherent_align(cx).map(|align| align.abi); let size = layout.abi.inherent_size(cx); diff --git a/src/tools/miri/src/machine.rs b/src/tools/miri/src/machine.rs index df55902decd..8d0a9263cb3 100644 --- a/src/tools/miri/src/machine.rs +++ b/src/tools/miri/src/machine.rs @@ -381,7 +381,7 @@ pub struct PrimitiveLayouts<'tcx> { } impl<'tcx> PrimitiveLayouts<'tcx> { - fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result> { + fn new(layout_cx: LayoutCx<'tcx>) -> Result> { let tcx = layout_cx.tcx; let mut_raw_ptr = Ty::new_mut_ptr(tcx, tcx.types.unit); let const_raw_ptr = Ty::new_imm_ptr(tcx, tcx.types.unit); @@ -596,7 +596,7 @@ pub struct MiriMachine<'tcx> { } impl<'tcx> MiriMachine<'tcx> { - pub(crate) fn new(config: &MiriConfig, layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Self { + pub(crate) fn new(config: &MiriConfig, layout_cx: LayoutCx<'tcx>) -> Self { let tcx = layout_cx.tcx; let local_crates = helpers::get_local_crates(tcx); let layouts =