From 004208fc46467c5e171e739559f77c0fafbbe87a Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 1 May 2020 15:52:08 +0200 Subject: [PATCH 1/3] Move recursion check for zsts back to read site instead of access check site. --- src/librustc_mir/interpret/memory.rs | 13 +------------ src/librustc_mir/interpret/operand.rs | 9 +++++++++ src/test/ui/consts/static-ice.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/test/ui/consts/static-ice.rs diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index d1881524172cc..39e428cee1d7b 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -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) } } }) } diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index b924f20ce7cc8..05844eb126c59 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -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(), diff --git a/src/test/ui/consts/static-ice.rs b/src/test/ui/consts/static-ice.rs new file mode 100644 index 0000000000000..a6d90e44e3407 --- /dev/null +++ b/src/test/ui/consts/static-ice.rs @@ -0,0 +1,27 @@ +// check-pass + +#[derive(Copy, Clone)] +pub struct Glfw; + +static mut GLFW: Option = 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(); +} From ba26df099846165de7bb24486cced1cdfb40132b Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 1 May 2020 17:45:10 +0200 Subject: [PATCH 2/3] Name test appropriately and link to the issues it regress-checks for --- .../ui/consts/{static-ice.rs => ice-zst-static-access.rs} | 5 +++++ 1 file changed, 5 insertions(+) rename src/test/ui/consts/{static-ice.rs => ice-zst-static-access.rs} (75%) diff --git a/src/test/ui/consts/static-ice.rs b/src/test/ui/consts/ice-zst-static-access.rs similarity index 75% rename from src/test/ui/consts/static-ice.rs rename to src/test/ui/consts/ice-zst-static-access.rs index a6d90e44e3407..b68e442a57c71 100644 --- a/src/test/ui/consts/static-ice.rs +++ b/src/test/ui/consts/ice-zst-static-access.rs @@ -1,5 +1,10 @@ // check-pass +// This is a regression test for ICEs from +// https://github.com/rust-lang/rust/issues/71612 +// and +// https://github.com/rust-lang/rust/issues/71709 + #[derive(Copy, Clone)] pub struct Glfw; From c64c7766787e6d9d5b80b95832155a11cadcbe40 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 1 May 2020 17:45:51 +0200 Subject: [PATCH 3/3] Remove a comment that made only sense in the original position of this cycle check. --- src/librustc_mir/interpret/operand.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index 05844eb126c59..6832701e1a5ad 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -244,9 +244,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // 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. + // actually access the referenced allocation. self.memory.get_raw(ptr.alloc_id)?; } return Ok(Some(ImmTy {