Jack Wrenn e9eae28eee transmutability: shift abstraction boundary
Previously, `rustc_transmute`'s layout representations were genericized
over `R`, a reference. Now, it's instead genericized over
representations of type and region. This allows us to move reference
transmutability logic from `rustc_trait_selection` to
`rustc_transmutability` (and thus unit test it independently of the
compiler), and — in a follow-up PR — will make it possible to support
analyzing function pointer transmutability with minimal surgery.
2025-06-09 14:08:12 +00:00

59 lines
1.3 KiB
Rust

use crate::layout;
/// Context necessary to answer the question "Are these types transmutable?".
pub(crate) trait QueryContext {
type Def: layout::Def;
type Region: layout::Region;
type Type: layout::Type;
}
#[cfg(test)]
pub(crate) mod test {
use std::marker::PhantomData;
use super::QueryContext;
pub(crate) struct UltraMinimal<R = !, T = !>(PhantomData<(R, T)>);
impl<R, T> Default for UltraMinimal<R, T> {
fn default() -> Self {
Self(PhantomData)
}
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
pub(crate) enum Def {
HasSafetyInvariants,
NoSafetyInvariants,
}
impl crate::layout::Def for Def {
fn has_safety_invariants(&self) -> bool {
self == &Self::HasSafetyInvariants
}
}
impl<R, T> QueryContext for UltraMinimal<R, T>
where
R: crate::layout::Region,
T: crate::layout::Type,
{
type Def = Def;
type Region = R;
type Type = T;
}
}
#[cfg(feature = "rustc")]
mod rustc {
use rustc_middle::ty::{Region, Ty, TyCtxt};
use super::*;
impl<'tcx> super::QueryContext for TyCtxt<'tcx> {
type Def = layout::rustc::Def<'tcx>;
type Region = Region<'tcx>;
type Type = Ty<'tcx>;
}
}