diff options
| author | Jack Wrenn <jack@wrenn.fyi> | 2024-08-14 20:10:28 +0000 |
|---|---|---|
| committer | Jack Wrenn <jack@wrenn.fyi> | 2024-08-18 18:31:06 +0000 |
| commit | 17995d5cc2e75b65b8bd6b77ad30b0c764500c0c (patch) | |
| tree | 27bbb55672e96182eedb86e9f26229eaa5214606 /compiler/rustc_transmute/src/layout/mod.rs | |
| parent | 0f442e265c165c0a78633bef98de18517815150c (diff) | |
| download | rust-17995d5cc2e75b65b8bd6b77ad30b0c764500c0c.tar.gz rust-17995d5cc2e75b65b8bd6b77ad30b0c764500c0c.zip | |
safe transmute: forbid reference lifetime extension
Modifies `BikeshedIntrinsicFrom` to forbid lifetime extensions on references. This static check can be opted out of with the `Assume::lifetimes` flag. Fixes #129097
Diffstat (limited to 'compiler/rustc_transmute/src/layout/mod.rs')
| -rw-r--r-- | compiler/rustc_transmute/src/layout/mod.rs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/compiler/rustc_transmute/src/layout/mod.rs b/compiler/rustc_transmute/src/layout/mod.rs index bbf155581f9..1cf9e0b9b70 100644 --- a/compiler/rustc_transmute/src/layout/mod.rs +++ b/compiler/rustc_transmute/src/layout/mod.rs @@ -63,7 +63,9 @@ pub mod rustc { use std::fmt::{self, Write}; use rustc_middle::mir::Mutability; - use rustc_middle::ty::{self, Ty}; + use rustc_middle::ty::layout::{LayoutCx, LayoutError}; + use rustc_middle::ty::{self, Ty, TyCtxt}; + use rustc_target::abi::Layout; /// A reference in the layout. #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)] @@ -120,4 +122,13 @@ pub mod rustc { self != &Self::Primitive } } + + pub(crate) fn layout_of<'tcx>( + cx: LayoutCx<'tcx, TyCtxt<'tcx>>, + ty: Ty<'tcx>, + ) -> Result<Layout<'tcx>, &'tcx LayoutError<'tcx>> { + use rustc_middle::ty::layout::LayoutOf; + let ty = cx.tcx.erase_regions(ty); + cx.layout_of(ty).map(|tl| tl.layout) + } } |
