Skip to content

Add fn on types for layout of variants #75552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc_middle/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ rustc_queries! {
query is_sized_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
desc { "computing whether `{}` is `Sized`", env.value }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the chapter on the rustc-dev-guide on adding queries - try grepping for an existing query (e.g. unused_generic_params since I know that one isn't used in many places) and copy the changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you're probably right, I do want to add this with what I want to end up doing, so for now I'll hold off. I'll try replicating that query when I do so, as the other one I was replicating was scattered all about and hard to follow.


/// Query backing `TyS::is_freeze`.
query is_freeze_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
desc { "computing whether `{}` is freeze", env.value }
Expand Down
20 changes: 19 additions & 1 deletion src/librustc_middle/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_hir::def_id::DefId;
use rustc_index::vec::Idx;
use rustc_macros::HashStable;
use rustc_span::symbol::{kw, Ident, Symbol};
use rustc_target::abi::VariantIdx;
use rustc_target::abi::{Layout, VariantIdx};
use rustc_target::spec::abi;
use std::borrow::Cow;
use std::cmp::Ordering;
Expand Down Expand Up @@ -2251,4 +2251,22 @@ impl<'tcx> TyS<'tcx> {
pub fn is_zst(&'tcx self, tcx: TyCtxt<'tcx>, did: DefId) -> bool {
tcx.layout_of(tcx.param_env(did).and(self)).map(|layout| layout.is_zst()).unwrap_or(false)
}

/// Returns an iterator over the sized layouts of the variants of a type given context and
/// specific definition. If the type is not an ADT, returns None.
pub fn layout_of_variants(
&'tcx self,
tcx: TyCtxt<'tcx>,
) -> Option<impl Iterator<Item = &'tcx Layout> + 'tcx> {
let iter = match self.kind {
ty::Adt(def, _substs) => def.variants.iter().flat_map(move |var_def| {
tcx.layout_of(tcx.param_env(var_def.def_id).and(self))
.ok()
.filter(|ty_and_layout| !ty_and_layout.is_unsized())
.map(|ty_and_layout| ty_and_layout.layout)
}),
_ => return None,
};
Some(iter)
}
}