Skip to content

Commit

Permalink
make LayoutCx not generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Sep 4, 2024
1 parent 842d6fc commit 849b6d2
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn check_validity_requirement<'tcx>(
/// details.
fn might_permit_raw_init_strict<'tcx>(
ty: TyAndLayout<'tcx>,
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: &LayoutCx<'tcx>,
kind: ValidityRequirement,
) -> Result<bool, &'tcx LayoutError<'tcx>> {
let machine = CompileTimeMachine::new(CanAccessMutGlobal::No, CheckAlignment::Error);
Expand Down Expand Up @@ -74,7 +74,7 @@ fn might_permit_raw_init_strict<'tcx>(
/// details.
fn might_permit_raw_init_lax<'tcx>(
this: TyAndLayout<'tcx>,
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: &LayoutCx<'tcx>,
init_kind: ValidityRequirement,
) -> Result<bool, &'tcx LayoutError<'tcx>> {
let scalar_allows_raw_init = move |s: Scalar| -> bool {
Expand Down
37 changes: 9 additions & 28 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,12 @@ impl<'tcx> IntoDiagArg for LayoutError<'tcx> {
}

#[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<Cow<'static, str>>) {
Expand Down Expand Up @@ -568,31 +568,31 @@ impl<'tcx> HasTyCtxt<'tcx> for TyCtxtAt<'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()
}
Expand Down Expand Up @@ -685,7 +685,7 @@ pub trait LayoutOf<'tcx>: LayoutOfHelpers<'tcx> {

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<TyAndLayout<'tcx>, &'tcx LayoutError<'tcx>>;

#[inline]
Expand All @@ -699,25 +699,6 @@ impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxt<'tcx>> {
}
}

impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxtAt<'tcx>> {
type LayoutOfResult = Result<TyAndLayout<'tcx>, &'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>,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_transmute/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -124,7 +124,7 @@ pub mod rustc {
}

pub(crate) fn layout_of<'tcx>(
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: LayoutCx<'tcx>,
ty: Ty<'tcx>,
) -> Result<Layout<'tcx>, &'tcx LayoutError<'tcx>> {
use rustc_middle::ty::layout::LayoutOf;
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_transmute/src/layout/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub(crate) mod rustc {
}

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

Expand Down Expand Up @@ -274,7 +274,7 @@ pub(crate) mod rustc {
fn from_tuple(
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
members: &'tcx List<Ty<'tcx>>,
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: LayoutCx<'tcx>,
) -> Result<Self, Err> {
match &layout.fields {
FieldsShape::Primitive => {
Expand All @@ -299,7 +299,7 @@ pub(crate) mod rustc {
fn from_struct(
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
def: AdtDef<'tcx>,
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: LayoutCx<'tcx>,
) -> Result<Self, Err> {
assert!(def.is_struct());
let def = Def::Adt(def);
Expand All @@ -314,7 +314,7 @@ pub(crate) mod rustc {
fn from_enum(
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
def: AdtDef<'tcx>,
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: LayoutCx<'tcx>,
) -> Result<Self, Err> {
assert!(def.is_enum());

Expand Down Expand Up @@ -383,7 +383,7 @@ pub(crate) mod rustc {
tag: Option<ScalarInt>,
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
total_size: Size,
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: LayoutCx<'tcx>,
) -> Result<Self, Err> {
// This constructor does not support non-`FieldsShape::Arbitrary`
// layouts.
Expand Down Expand Up @@ -455,7 +455,7 @@ pub(crate) mod rustc {
fn from_union(
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
def: AdtDef<'tcx>,
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: LayoutCx<'tcx>,
) -> Result<Self, Err> {
assert!(def.is_union());

Expand Down Expand Up @@ -485,7 +485,7 @@ pub(crate) mod rustc {
}

fn ty_field<'tcx>(
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: LayoutCx<'tcx>,
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
i: FieldIdx,
) -> Ty<'tcx> {
Expand All @@ -512,7 +512,7 @@ pub(crate) mod rustc {
}

fn ty_variant<'tcx>(
cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: LayoutCx<'tcx>,
(ty, layout): (Ty<'tcx>, Layout<'tcx>),
i: VariantIdx,
) -> Layout<'tcx> {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ty_utils/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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>>,
Expand Down Expand Up @@ -538,7 +538,7 @@ fn fn_abi_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<Ty<'tcx>>,
Expand Down Expand Up @@ -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<DefId>,
Expand Down
17 changes: 7 additions & 10 deletions compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldIdx, Layout<'_>>,
repr: &ReprOptions,
Expand All @@ -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<Layout<'tcx>, &'tcx LayoutError<'tcx>> {
// Types that reference `ty::Error` pessimistically don't have a meaningful layout.
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -1011,7 +1008,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.
Expand Down Expand Up @@ -1062,7 +1059,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<VariantInfo>, Option<Size>) {
Expand Down Expand Up @@ -1134,7 +1131,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>,
Expand Down
15 changes: 4 additions & 11 deletions compiler/rustc_ty_utils/src/layout_sanity_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ use std::assert_matches::assert_matches;

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());
Expand All @@ -29,7 +25,7 @@ pub(super) fn sanity_check_layout<'tcx>(

/// Yields non-ZST fields of the type
fn non_zst_fields<'tcx, 'a>(
cx: &'a LayoutCx<'tcx, TyCtxt<'tcx>>,
cx: &'a LayoutCx<'tcx>,
layout: &'a TyAndLayout<'tcx>,
) -> impl Iterator<Item = (Size, TyAndLayout<'tcx>)> + 'a {
(0..layout.layout.fields().count()).filter_map(|i| {
Expand All @@ -43,10 +39,7 @@ pub(super) fn sanity_check_layout<'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;
Expand All @@ -69,7 +62,7 @@ pub(super) fn sanity_check_layout<'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);
Expand Down
4 changes: 2 additions & 2 deletions src/tools/miri/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ pub struct PrimitiveLayouts<'tcx> {
}

impl<'tcx> PrimitiveLayouts<'tcx> {
fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, &'tcx LayoutError<'tcx>> {
fn new(layout_cx: LayoutCx<'tcx>) -> Result<Self, &'tcx LayoutError<'tcx>> {
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);
Expand Down Expand Up @@ -575,7 +575,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 =
Expand Down

0 comments on commit 849b6d2

Please sign in to comment.