Skip to content

Commit

Permalink
Move recursion check for zsts back to read site instead of access che…
Browse files Browse the repository at this point in the history
…ck site.
  • Loading branch information
oli-obk committed May 1, 2020
1 parent fd61d06 commit 004208f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
13 changes: 1 addition & 12 deletions src/librustc_mir/interpret/memory.rs
Expand Up @@ -400,18 +400,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

// We can still be zero-sized in this branch, in which case we have to
// return `None`.
if size.bytes() == 0 {
// We may be reading from a static.
// In order to ensure that `static FOO: Type = FOO;` causes a cycle error
// instead of magically pulling *any* ZST value from the ether, we need to
// actually access the referenced allocation. The caller is likely
// to short-circuit on `None`, so we trigger the access here to
// make sure it happens.
self.get_raw(ptr.alloc_id)?;
None
} else {
Some(ptr)
}
if size.bytes() == 0 { None } else { Some(ptr) }
}
})
}
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_mir/interpret/operand.rs
Expand Up @@ -240,6 +240,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
{
Some(ptr) => ptr,
None => {
if let Scalar::Ptr(ptr) = mplace.ptr {
// We may be reading from a static.
// In order to ensure that `static FOO: Type = FOO;` causes a cycle error
// instead of magically pulling *any* ZST value from the ether, we need to
// actually access the referenced allocation. The caller is likely
// to short-circuit on `None`, so we trigger the access here to
// make sure it happens.
self.memory.get_raw(ptr.alloc_id)?;
}
return Ok(Some(ImmTy {
// zero-sized type
imm: Scalar::zst().into(),
Expand Down
27 changes: 27 additions & 0 deletions src/test/ui/consts/static-ice.rs
@@ -0,0 +1,27 @@
// check-pass

#[derive(Copy, Clone)]
pub struct Glfw;

static mut GLFW: Option<Glfw> = None;
pub fn new() -> Glfw {
unsafe {
if let Some(glfw) = GLFW {
return glfw;
} else {
todo!()
}
};
}

extern "C" {
static _dispatch_queue_attr_concurrent: [u8; 0];
}

static DISPATCH_QUEUE_CONCURRENT: &'static [u8; 0] =
unsafe { &_dispatch_queue_attr_concurrent };

fn main() {
*DISPATCH_QUEUE_CONCURRENT;
new();
}

0 comments on commit 004208f

Please sign in to comment.