Skip to content

Commit 8211ebe

Browse files
committed
rustc_target: route LayoutOf through TyAbiInterface.
1 parent 9556d7a commit 8211ebe

File tree

20 files changed

+280
-157
lines changed

20 files changed

+280
-157
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub(crate) fn codegen_fn<'tcx>(
7979
let arg_uninhabited = fx
8080
.mir
8181
.args_iter()
82-
.any(|arg| fx.layout_of(fx.monomorphize(&fx.mir.local_decls[arg].ty)).abi.is_uninhabited());
82+
.any(|arg| fx.layout_of(fx.monomorphize(fx.mir.local_decls[arg].ty)).abi.is_uninhabited());
8383

8484
if !crate::constant::check_constants(&mut fx) {
8585
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
@@ -835,7 +835,7 @@ pub(crate) fn codegen_place<'tcx>(
835835
let from: u64 = from;
836836
let to: u64 = to;
837837

838-
match cplace.layout().ty.kind() {
838+
match *cplace.layout().ty.kind() {
839839
ty::Array(elem_ty, _len) => {
840840
assert!(!from_end, "array subslices are never `from_end`");
841841
let elem_layout = fx.layout_of(elem_ty);

compiler/rustc_codegen_cranelift/src/common.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_index::vec::IndexVec;
2+
use rustc_middle::ty::layout::LayoutError;
23
use rustc_middle::ty::SymbolName;
34
use rustc_target::abi::call::FnAbi;
45
use rustc_target::abi::{Integer, Primitive};
@@ -256,15 +257,6 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
256257
pub(crate) inline_asm_index: u32,
257258
}
258259

259-
impl<'tcx> LayoutOf<'tcx> for FunctionCx<'_, '_, 'tcx> {
260-
type Ty = Ty<'tcx>;
261-
type TyAndLayout = TyAndLayout<'tcx>;
262-
263-
fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> {
264-
RevealAllLayoutCx(self.tcx).layout_of(ty)
265-
}
266-
}
267-
268260
impl<'tcx> layout::HasTyCtxt<'tcx> for FunctionCx<'_, '_, 'tcx> {
269261
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
270262
self.tcx
@@ -289,6 +281,15 @@ impl<'tcx> HasTargetSpec for FunctionCx<'_, '_, 'tcx> {
289281
}
290282
}
291283

284+
impl<'tcx> ty::layout::IsLayoutCx<'tcx> for FunctionCx<'_, '_, 'tcx> {
285+
type LayoutOfResult = TyAndLayout<'tcx>;
286+
287+
#[inline]
288+
fn map_err_for_layout_of(err: LayoutError<'tcx>, cx: &Self, span: Span, ty: Ty<'tcx>) -> ! {
289+
RevealAllLayoutCx::map_err_for_layout_of(err, &RevealAllLayoutCx(cx.tcx), span, ty)
290+
}
291+
}
292+
292293
impl<'tcx> FunctionCx<'_, '_, 'tcx> {
293294
pub(crate) fn monomorphize<T>(&self, value: T) -> T
294295
where
@@ -364,22 +365,6 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
364365

365366
pub(crate) struct RevealAllLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>);
366367

367-
impl<'tcx> LayoutOf<'tcx> for RevealAllLayoutCx<'tcx> {
368-
type Ty = Ty<'tcx>;
369-
type TyAndLayout = TyAndLayout<'tcx>;
370-
371-
fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> {
372-
assert!(!ty.still_further_specializable());
373-
self.0.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap_or_else(|e| {
374-
if let layout::LayoutError::SizeOverflow(_) = e {
375-
self.0.sess.fatal(&e.to_string())
376-
} else {
377-
bug!("failed to get layout for `{}`: {}", ty, e)
378-
}
379-
})
380-
}
381-
}
382-
383368
impl<'tcx> layout::HasTyCtxt<'tcx> for RevealAllLayoutCx<'tcx> {
384369
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
385370
self.0
@@ -403,3 +388,16 @@ impl<'tcx> HasTargetSpec for RevealAllLayoutCx<'tcx> {
403388
&self.0.sess.target
404389
}
405390
}
391+
392+
impl<'tcx> ty::layout::IsLayoutCx<'tcx> for RevealAllLayoutCx<'tcx> {
393+
type LayoutOfResult = TyAndLayout<'tcx>;
394+
395+
#[inline]
396+
fn map_err_for_layout_of(err: LayoutError<'tcx>, cx: &Self, span: Span, ty: Ty<'tcx>) -> ! {
397+
if let layout::LayoutError::SizeOverflow(_) = err {
398+
cx.0.sess.span_fatal(span, &err.to_string())
399+
} else {
400+
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
401+
}
402+
}
403+
}

compiler/rustc_codegen_cranelift/src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
489489
return None;
490490
}
491491
let const_val = mir_operand_get_const_val(fx, operand)?;
492-
if fx.layout_of(ty).size
492+
if fx.layout_of(*ty).size
493493
!= const_val.try_to_scalar_int()?.size()
494494
{
495495
return None;

compiler/rustc_codegen_cranelift/src/value_and_place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ impl<'tcx> CPlace<'tcx> {
678678
fx: &mut FunctionCx<'_, '_, 'tcx>,
679679
index: Value,
680680
) -> CPlace<'tcx> {
681-
let (elem_layout, ptr) = match self.layout().ty.kind() {
681+
let (elem_layout, ptr) = match *self.layout().ty.kind() {
682682
ty::Array(elem_ty, _) => (fx.layout_of(elem_ty), self.to_ptr()),
683683
ty::Slice(elem_ty) => (fx.layout_of(elem_ty), self.to_ptr_maybe_unsized().0),
684684
_ => bug!("place_index({:?})", self.layout().ty),

compiler/rustc_codegen_llvm/src/builder.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_codegen_ssa::traits::*;
1515
use rustc_codegen_ssa::MemFlags;
1616
use rustc_data_structures::small_c_str::SmallCStr;
1717
use rustc_hir::def_id::DefId;
18-
use rustc_middle::ty::layout::TyAndLayout;
18+
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
1919
use rustc_middle::ty::{self, Ty, TyCtxt};
2020
use rustc_span::Span;
2121
use rustc_target::abi::{self, Align, Size};
@@ -88,12 +88,12 @@ impl HasTargetSpec for Builder<'_, '_, 'tcx> {
8888
}
8989
}
9090

91-
impl abi::LayoutOf<'tcx> for Builder<'_, '_, 'tcx> {
92-
type Ty = Ty<'tcx>;
93-
type TyAndLayout = TyAndLayout<'tcx>;
91+
impl ty::layout::IsLayoutCx<'tcx> for Builder<'_, '_, 'tcx> {
92+
type LayoutOfResult = TyAndLayout<'tcx>;
9493

95-
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
96-
self.cx.layout_of(ty)
94+
#[inline]
95+
fn map_err_for_layout_of(err: LayoutError<'tcx>, bx: &Self, span: Span, ty: Ty<'tcx>) -> ! {
96+
CodegenCx::map_err_for_layout_of(err, bx.cx, span, ty)
9797
}
9898
}
9999

compiler/rustc_codegen_llvm/src/context.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ use rustc_codegen_ssa::traits::*;
1414
use rustc_data_structures::base_n;
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::small_c_str::SmallCStr;
17-
use rustc_middle::bug;
1817
use rustc_middle::mir::mono::CodegenUnit;
1918
use rustc_middle::ty::layout::{HasParamEnv, LayoutError, TyAndLayout};
2019
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
20+
use rustc_middle::{bug, span_bug};
2121
use rustc_session::config::{CFGuard, CrateType, DebugInfo};
2222
use rustc_session::Session;
23-
use rustc_span::source_map::{Span, DUMMY_SP};
23+
use rustc_span::source_map::Span;
2424
use rustc_span::symbol::Symbol;
25-
use rustc_target::abi::{HasDataLayout, LayoutOf, PointeeInfo, Size, TargetDataLayout, VariantIdx};
25+
use rustc_target::abi::{HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
2626
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
2727
use smallvec::SmallVec;
2828

@@ -835,27 +835,21 @@ impl ty::layout::HasTyCtxt<'tcx> for CodegenCx<'ll, 'tcx> {
835835
}
836836
}
837837

838-
impl LayoutOf<'tcx> for CodegenCx<'ll, 'tcx> {
839-
type Ty = Ty<'tcx>;
840-
type TyAndLayout = TyAndLayout<'tcx>;
841-
842-
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
843-
self.spanned_layout_of(ty, DUMMY_SP)
844-
}
845-
846-
fn spanned_layout_of(&self, ty: Ty<'tcx>, span: Span) -> Self::TyAndLayout {
847-
self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).unwrap_or_else(|e| {
848-
if let LayoutError::SizeOverflow(_) = e {
849-
self.sess().span_fatal(span, &e.to_string())
850-
} else {
851-
bug!("failed to get layout for `{}`: {}", ty, e)
852-
}
853-
})
854-
}
855-
}
856-
857838
impl<'tcx, 'll> HasParamEnv<'tcx> for CodegenCx<'ll, 'tcx> {
858839
fn param_env(&self) -> ty::ParamEnv<'tcx> {
859840
ty::ParamEnv::reveal_all()
860841
}
861842
}
843+
844+
impl ty::layout::IsLayoutCx<'tcx> for CodegenCx<'ll, 'tcx> {
845+
type LayoutOfResult = TyAndLayout<'tcx>;
846+
847+
#[inline]
848+
fn map_err_for_layout_of(err: LayoutError<'tcx>, cx: &Self, span: Span, ty: Ty<'tcx>) -> ! {
849+
if let LayoutError::SizeOverflow(_) = err {
850+
cx.sess().span_fatal(span, &err.to_string())
851+
} else {
852+
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
853+
}
854+
}
855+
}

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
401401
let t = arg.layout.ty;
402402
let t = match t.kind() {
403403
ty::Array(ct, _)
404-
if (*ct == cx.tcx.types.u8) || cx.layout_of(ct).is_zst() =>
404+
if (*ct == cx.tcx.types.u8) || cx.layout_of(*ct).is_zst() =>
405405
{
406406
cx.tcx.mk_imm_ptr(ct)
407407
}

compiler/rustc_codegen_ssa/src/traits/backend.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
1010
use rustc_middle::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
1111
use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
1212
use rustc_middle::ty::query::Providers;
13-
use rustc_middle::ty::{Ty, TyCtxt};
13+
use rustc_middle::ty::{self, TyCtxt};
1414
use rustc_session::{
1515
config::{self, OutputFilenames, PrintRequest},
1616
Session,
1717
};
1818
use rustc_span::symbol::Symbol;
19-
use rustc_target::abi::LayoutOf;
2019
use rustc_target::spec::Target;
2120

2221
pub use rustc_data_structures::sync::MetadataRef;
@@ -42,14 +41,14 @@ pub trait Backend<'tcx>:
4241
Sized
4342
+ BackendTypes
4443
+ HasTyCtxt<'tcx>
45-
+ LayoutOf<'tcx, Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
44+
+ ty::layout::IsLayoutCx<'tcx, LayoutOfResult = TyAndLayout<'tcx>>
4645
{
4746
}
4847

4948
impl<'tcx, T> Backend<'tcx> for T where
5049
Self: BackendTypes
5150
+ HasTyCtxt<'tcx>
52-
+ LayoutOf<'tcx, Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
51+
+ ty::layout::IsLayoutCx<'tcx, LayoutOfResult = TyAndLayout<'tcx>>
5352
{
5453
}
5554

compiler/rustc_codegen_ssa/src/traits/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ pub use self::type_::{
4444
};
4545
pub use self::write::{ModuleBufferMethods, ThinBufferMethods, WriteBackendMethods};
4646

47+
// HACK(eddyb) this means that `traits::*` imports will also import `LayoutOf`,
48+
// which wasn't needed previously because `Backend` itself having `LayoutOf` as
49+
// a supertrait meant that methods from `LayoutOf` were always available.
50+
pub use rustc_target::abi::LayoutOf as _;
51+
4752
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt};
4853
use rustc_target::spec::HasTargetSpec;
4954

compiler/rustc_lint/src/builtin.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -2830,18 +2830,19 @@ impl ClashingExternDeclarations {
28302830
let a_kind = a.kind();
28312831
let b_kind = b.kind();
28322832

2833-
let compare_layouts = |a, b| -> Result<bool, LayoutError<'tcx>> {
2834-
debug!("compare_layouts({:?}, {:?})", a, b);
2835-
let a_layout = &cx.layout_of(a)?.layout.abi;
2836-
let b_layout = &cx.layout_of(b)?.layout.abi;
2837-
debug!(
2838-
"comparing layouts: {:?} == {:?} = {}",
2839-
a_layout,
2840-
b_layout,
2841-
a_layout == b_layout
2842-
);
2843-
Ok(a_layout == b_layout)
2844-
};
2833+
let compare_layouts =
2834+
|a: Ty<'tcx>, b: Ty<'tcx>| -> Result<bool, LayoutError<'tcx>> {
2835+
debug!("compare_layouts({:?}, {:?})", a, b);
2836+
let a_layout = &cx.layout_of(a)?.layout.abi;
2837+
let b_layout = &cx.layout_of(b)?.layout.abi;
2838+
debug!(
2839+
"comparing layouts: {:?} == {:?} = {}",
2840+
a_layout,
2841+
b_layout,
2842+
a_layout == b_layout
2843+
);
2844+
Ok(a_layout == b_layout)
2845+
};
28452846

28462847
#[allow(rustc::usage_of_ty_tykind)]
28472848
let is_primitive_or_pointer = |kind: &ty::TyKind<'_>| {

compiler/rustc_lint/src/context.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_session::Session;
4141
use rustc_session::SessionLintStore;
4242
use rustc_span::lev_distance::find_best_match_for_name;
4343
use rustc_span::{symbol::Symbol, MultiSpan, Span, DUMMY_SP};
44-
use rustc_target::abi::{self, LayoutOf};
44+
use rustc_target::abi;
4545
use tracing::debug;
4646

4747
use std::cell::Cell;
@@ -1080,12 +1080,17 @@ impl<'tcx> ty::layout::HasParamEnv<'tcx> for LateContext<'tcx> {
10801080
}
10811081
}
10821082

1083-
impl<'tcx> LayoutOf<'tcx> for LateContext<'tcx> {
1084-
type Ty = Ty<'tcx>;
1085-
type TyAndLayout = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
1083+
impl<'tcx> ty::layout::IsLayoutCx<'tcx> for LateContext<'tcx> {
1084+
type LayoutOfResult = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
10861085

1087-
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
1088-
self.tcx.layout_of(self.param_env.and(ty))
1086+
#[inline]
1087+
fn map_err_for_layout_of(
1088+
err: LayoutError<'tcx>,
1089+
_cx: &Self,
1090+
_span: Span,
1091+
_ty: Ty<'tcx>,
1092+
) -> LayoutError<'tcx> {
1093+
err
10891094
}
10901095
}
10911096

0 commit comments

Comments
 (0)