Skip to content

Commit 004208f

Browse files
committed
Move recursion check for zsts back to read site instead of access check site.
1 parent fd61d06 commit 004208f

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/librustc_mir/interpret/memory.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
400400

401401
// We can still be zero-sized in this branch, in which case we have to
402402
// return `None`.
403-
if size.bytes() == 0 {
404-
// We may be reading from a static.
405-
// In order to ensure that `static FOO: Type = FOO;` causes a cycle error
406-
// instead of magically pulling *any* ZST value from the ether, we need to
407-
// actually access the referenced allocation. The caller is likely
408-
// to short-circuit on `None`, so we trigger the access here to
409-
// make sure it happens.
410-
self.get_raw(ptr.alloc_id)?;
411-
None
412-
} else {
413-
Some(ptr)
414-
}
403+
if size.bytes() == 0 { None } else { Some(ptr) }
415404
}
416405
})
417406
}

src/librustc_mir/interpret/operand.rs

+9
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
240240
{
241241
Some(ptr) => ptr,
242242
None => {
243+
if let Scalar::Ptr(ptr) = mplace.ptr {
244+
// We may be reading from a static.
245+
// In order to ensure that `static FOO: Type = FOO;` causes a cycle error
246+
// instead of magically pulling *any* ZST value from the ether, we need to
247+
// actually access the referenced allocation. The caller is likely
248+
// to short-circuit on `None`, so we trigger the access here to
249+
// make sure it happens.
250+
self.memory.get_raw(ptr.alloc_id)?;
251+
}
243252
return Ok(Some(ImmTy {
244253
// zero-sized type
245254
imm: Scalar::zst().into(),

src/test/ui/consts/static-ice.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// check-pass
2+
3+
#[derive(Copy, Clone)]
4+
pub struct Glfw;
5+
6+
static mut GLFW: Option<Glfw> = None;
7+
pub fn new() -> Glfw {
8+
unsafe {
9+
if let Some(glfw) = GLFW {
10+
return glfw;
11+
} else {
12+
todo!()
13+
}
14+
};
15+
}
16+
17+
extern "C" {
18+
static _dispatch_queue_attr_concurrent: [u8; 0];
19+
}
20+
21+
static DISPATCH_QUEUE_CONCURRENT: &'static [u8; 0] =
22+
unsafe { &_dispatch_queue_attr_concurrent };
23+
24+
fn main() {
25+
*DISPATCH_QUEUE_CONCURRENT;
26+
new();
27+
}

0 commit comments

Comments
 (0)