Skip to content

Commit 7fab4e2

Browse files
committed
Add query for layout of variants of types
1 parent 668a34e commit 7fab4e2

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/librustc_middle/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ rustc_queries! {
884884
query is_sized_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
885885
desc { "computing whether `{}` is `Sized`", env.value }
886886
}
887+
887888
/// Query backing `TyS::is_freeze`.
888889
query is_freeze_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
889890
desc { "computing whether `{}` is freeze", env.value }

src/librustc_middle/ty/sty.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_hir::def_id::DefId;
1919
use rustc_index::vec::Idx;
2020
use rustc_macros::HashStable;
2121
use rustc_span::symbol::{kw, Ident, Symbol};
22-
use rustc_target::abi::VariantIdx;
22+
use rustc_target::abi::{Layout, VariantIdx};
2323
use rustc_target::spec::abi;
2424
use std::borrow::Cow;
2525
use std::cmp::Ordering;
@@ -2251,4 +2251,22 @@ impl<'tcx> TyS<'tcx> {
22512251
pub fn is_zst(&'tcx self, tcx: TyCtxt<'tcx>, did: DefId) -> bool {
22522252
tcx.layout_of(tcx.param_env(did).and(self)).map(|layout| layout.is_zst()).unwrap_or(false)
22532253
}
2254+
2255+
/// Returns an iterator over the sized layouts of the variants of a type given context and
2256+
/// specific definition. If the type is not an ADT, returns None.
2257+
pub fn layout_of_variants(
2258+
&'tcx self,
2259+
tcx: TyCtxt<'tcx>,
2260+
) -> Option<impl Iterator<Item = &'tcx Layout> + 'tcx> {
2261+
let iter = match self.kind {
2262+
ty::Adt(def, _substs) => def.variants.iter().flat_map(move |var_def| {
2263+
tcx.layout_of(tcx.param_env(var_def.def_id).and(self))
2264+
.ok()
2265+
.filter(|ty_and_layout| !ty_and_layout.is_unsized())
2266+
.map(|ty_and_layout| ty_and_layout.layout)
2267+
}),
2268+
_ => return None,
2269+
};
2270+
Some(iter)
2271+
}
22542272
}

0 commit comments

Comments
 (0)