Skip to content

Commit

Permalink
Stop SRoA'ing DynMetadata in MIR
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed May 25, 2024
1 parent 213ad10 commit d7248d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_mir_transform/src/sroa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ fn escaping_locals<'tcx>(
// Exclude #[repr(simd)] types so that they are not de-optimized into an array
return true;
}
if Some(def.did()) == tcx.lang_items().dyn_metadata() {
// codegen wants to see the `DynMetadata<T>`,
// not the inner reference-to-opaque-type.
return true;
}
// We already excluded unions and enums, so this ADT must have one variant
let variant = def.variant(FIRST_VARIANT);
if variant.fields.len() > 1 {
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/mir/dyn_metadata_sroa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@ run-pass
//@ compile-flags: -Zmir-opt-level=5 -Zvalidate-mir

#![feature(ptr_metadata)]

// Regression for <https://github.com/rust-lang/rust/issues/125506>,
// which failed because of SRoA would project into `DynMetadata`.

trait Foo {}

struct Bar;

impl Foo for Bar {}

fn main() {
let a: *mut dyn Foo = &mut Bar;

let _d = a.to_raw_parts().0 as usize;
}

0 comments on commit d7248d7

Please sign in to comment.