3 /// Context necessary to answer the question "Are these types transmutable?".
4 pub(crate) trait QueryContext {
9 /// Is `def` accessible from the defining module of `scope`?
10 fn is_accessible_from(&self, def: Self::Def, scope: Self::Scope) -> bool;
12 fn min_align(&self, reference: Self::Ref) -> usize;
17 use super::QueryContext;
19 pub(crate) struct UltraMinimal;
21 #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
27 impl crate::layout::Def for Def {}
29 impl QueryContext for UltraMinimal {
34 fn is_accessible_from(&self, def: Def, scope: ()) -> bool {
35 matches!(Def::Visible, def)
38 fn min_align(&self, reference: !) -> usize {
44 #[cfg(feature = "rustc")]
47 use rustc_middle::ty::{Ty, TyCtxt};
49 impl<'tcx> super::QueryContext for TyCtxt<'tcx> {
50 type Def = layout::rustc::Def<'tcx>;
51 type Ref = layout::rustc::Ref<'tcx>;
53 type Scope = Ty<'tcx>;
55 #[instrument(level = "debug", skip(self))]
56 fn is_accessible_from(&self, def: Self::Def, scope: Self::Scope) -> bool {
57 use layout::rustc::Def;
60 let parent = if let ty::Adt(adt_def, ..) = scope.kind() {
61 use rustc_middle::ty::DefIdTree;
62 let parent = self.parent(adt_def.did());
65 // Is this always how we want to handle a non-ADT scope?
69 let def_id = match def {
70 Def::Adt(adt_def) => adt_def.did(),
71 Def::Variant(variant_def) => variant_def.def_id,
72 Def::Field(field_def) => field_def.did,
74 // primitives do not have a def_id, but they're always accessible
79 let ret = if self.visibility(def_id).is_accessible_from(parent, *self) {
89 fn min_align(&self, reference: Self::Ref) -> usize {