Skip to content

Move recursion check for zsts back to read site instead of access check site #71751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
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
7 changes: 7 additions & 0 deletions src/librustc_mir/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ 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.
self.memory.get_raw(ptr.alloc_id)?;
}
return Ok(Some(ImmTy {
// zero-sized type
imm: Scalar::zst().into(),
Expand Down
32 changes: 32 additions & 0 deletions src/test/ui/consts/ice-zst-static-access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// check-pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure "check" is enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the const prop one occurred during mir optimization of fn new, which will still happen in check mode, and the static one happened during evaluation of the static's initializer, which also happens in check mode.


// 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;

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();
}