File tree Expand file tree Collapse file tree 2 files changed +17
-5
lines changed
compiler/rustc_mir_transform/src Expand file tree Collapse file tree 2 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -1391,11 +1391,15 @@ fn op_to_prop_const<'tcx>(
13911391 let ( prov, offset) = pointer. into_parts ( ) ;
13921392 let alloc_id = prov. alloc_id ( ) ;
13931393 intern_const_alloc_for_constprop ( ecx, alloc_id) . ok ( ) ?;
1394- if matches ! ( ecx. tcx. global_alloc( alloc_id) , GlobalAlloc :: Memory ( _) ) {
1395- // `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
1396- // by `GlobalAlloc::Memory`, so do fall through to copying if needed.
1397- // FIXME: find a way to treat this more uniformly
1398- // (probably by fixing codegen)
1394+
1395+ // `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
1396+ // by `GlobalAlloc::Memory`, so do fall through to copying if needed.
1397+ // FIXME: find a way to treat this more uniformly (probably by fixing codegen)
1398+ if let GlobalAlloc :: Memory ( alloc) = ecx. tcx . global_alloc ( alloc_id)
1399+ // Transmuting a constant is just an offset in the allocation. If the alignment of the
1400+ // allocation is not enough, fallback to copying into a properly aligned value.
1401+ && alloc. inner ( ) . align >= op. layout . align . abi
1402+ {
13991403 return Some ( ConstValue :: Indirect { alloc_id, offset } ) ;
14001404 }
14011405 }
Original file line number Diff line number Diff line change 1+ //@ build-pass
2+ //@ compile-flags: -Zmir-opt-level=0 -Zmir-enable-passes=+GVN
3+
4+ fn main ( ) {
5+ let variant: Option < u32 > = None ;
6+ let transmuted: u64 = unsafe { std:: mem:: transmute ( variant) } ;
7+ println ! ( "{transmuted}" ) ;
8+ }
You can’t perform that action at this time.
0 commit comments