Skip to content

Commit 0167838

Browse files
committed
Auto merge of rust-lang#88499 - eddyb:layout-off, r=nagisa
Provide `layout_of` automatically (given tcx + param_env + error handling). After rust-lang#88337, there's no longer any uses of `LayoutOf` within `rustc_target` itself, so I realized I could move the trait to `rustc_middle::ty::layout` and redesign it a bit. This is similar to rust-lang#88338 (and supersedes it), but at no ergonomic loss, since there's no funky `C: LayoutOf<Ty = Ty>` -> `Ty: TyAbiInterface<C>` generic `impl` chain, and each `LayoutOf` still corresponds to one `impl` (of `LayoutOfHelpers`) for the specific context. After this PR, this is what's needed to get `trait LayoutOf` (with the `layout_of` method) implemented on some context type: * `TyCtxt`, via `HasTyCtxt` * `ParamEnv`, via `HasParamEnv` * a way to transform `LayoutError`s into the desired error type * an error type of `!` can be paired with having `cx.layout_of(...)` return `TyAndLayout` *without* `Result<...>` around it, such as used by codegen * this is done through a new `LayoutOfHelpers` trait (and so is specifying the type of `cx.layout_of(...)`) When going through this path (and not bypassing it with a manual `impl` of `LayoutOf`), the end result is that only the error case can be customized, the query itself and the success paths are guaranteed to be uniform. (**EDIT**: just noticed that because of the supertrait relationship, you cannot actually implement `LayoutOf` yourself, the blanket `impl` fully covers all possible context types that could ever implement it) Part of the motivation for this shape of API is that I've been working on querifying `FnAbi::of_*`, and what I want/need to introduce for that looks a lot like the setup in this PR - in particular, it's harder to express the `FnAbi` methods in `rustc_target`, since they're much more tied to `rustc` concepts. r? `@nagisa` cc `@oli-obk` `@bjorn3`
2 parents 771c2c6 + dc6c4de commit 0167838

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/common.rs

+15-17
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, LayoutOfHelpers};
23
use rustc_middle::ty::SymbolName;
34
use rustc_target::abi::call::FnAbi;
45
use rustc_target::abi::{Integer, Primitive};
@@ -256,12 +257,12 @@ 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>;
260+
impl<'tcx> LayoutOfHelpers<'tcx> for FunctionCx<'_, '_, 'tcx> {
261+
type LayoutOfResult = TyAndLayout<'tcx>;
262262

263-
fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> {
264-
RevealAllLayoutCx(self.tcx).layout_of(ty)
263+
#[inline]
264+
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
265+
RevealAllLayoutCx(self.tcx).handle_layout_err(err, span, ty)
265266
}
266267
}
267268

@@ -364,19 +365,16 @@ 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>;
368+
impl<'tcx> LayoutOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
369+
type LayoutOfResult = TyAndLayout<'tcx>;
370370

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-
})
371+
#[inline]
372+
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
373+
if let layout::LayoutError::SizeOverflow(_) = err {
374+
self.0.sess.span_fatal(span, &err.to_string())
375+
} else {
376+
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
377+
}
380378
}
381379
}
382380

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ mod prelude {
7979
pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
8080
pub(crate) use rustc_middle::bug;
8181
pub(crate) use rustc_middle::mir::{self, *};
82-
pub(crate) use rustc_middle::ty::layout::{self, TyAndLayout};
82+
pub(crate) use rustc_middle::ty::layout::{self, LayoutOf, TyAndLayout};
8383
pub(crate) use rustc_middle::ty::{
8484
self, FloatTy, Instance, InstanceDef, IntTy, ParamEnv, Ty, TyCtxt, TypeAndMut,
8585
TypeFoldable, UintTy,
8686
};
87-
pub(crate) use rustc_target::abi::{Abi, LayoutOf, Scalar, Size, VariantIdx};
87+
pub(crate) use rustc_target::abi::{Abi, Scalar, Size, VariantIdx};
8888

8989
pub(crate) use rustc_data_structures::fx::FxHashMap;
9090

0 commit comments

Comments
 (0)