Skip to content

Commit b75dfa8

Browse files
committed
Don't access a static just for its size and alignment
1 parent eedf6ce commit b75dfa8

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/librustc_mir/interpret/memory.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,19 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
535535
id: AllocId,
536536
liveness: AllocCheck,
537537
) -> InterpResult<'static, (Size, Align)> {
538+
// Allocations of `static` items
539+
// Can't do this in the match argument, we may get cycle errors since the lock would
540+
// be held throughout the match.
541+
let alloc = self.tcx.alloc_map.lock().get(id);
542+
match alloc {
543+
Some(GlobalAlloc::Static(did)) => {
544+
// Use size and align of the type
545+
let ty = self.tcx.type_of(did);
546+
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
547+
return Ok((layout.size, layout.align.abi));
548+
}
549+
_ => {}
550+
}
538551
// Regular allocations.
539552
if let Ok(alloc) = self.get(id) {
540553
return Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align));
@@ -548,20 +561,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
548561
Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
549562
};
550563
}
551-
// Foreign statics.
552-
// Can't do this in the match argument, we may get cycle errors since the lock would
553-
// be held throughout the match.
554-
let alloc = self.tcx.alloc_map.lock().get(id);
555-
match alloc {
556-
Some(GlobalAlloc::Static(did)) => {
557-
assert!(self.tcx.is_foreign_item(did));
558-
// Use size and align of the type
559-
let ty = self.tcx.type_of(did);
560-
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
561-
return Ok((layout.size, layout.align.abi));
562-
}
563-
_ => {}
564-
}
565564
// The rest must be dead.
566565
if let AllocCheck::MaybeDead = liveness {
567566
// Deallocated pointers are allowed, we should be able to find
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
3+
struct Foo {
4+
foo: Option<&'static Foo>
5+
}
6+
7+
static FOO: Foo = Foo {
8+
foo: Some(&FOO),
9+
};
10+
11+
fn main() {}

0 commit comments

Comments
 (0)