-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Remove an unnecessary restriction in dest_prop
#94305
Conversation
A few things:
|
@oli-obk :
|
By the way, a useful example to illustrate the idea behind this PR is the following: // #[repr(C)]
union Foo {
a: (),
_b: u32,
}
fn main() {
unsafe {
let f: Foo = std::mem::MaybeUninit::uninit().assume_init();
dbg!(f.a)
}
} This compiles, runs, and passes MIRI (with all the hard mode flags enabled) today both with the |
Ok, that makes sense, thanks! So I guess imo we should just keep the test you removed to demonstrate how dest_prop changes this union field write. |
I've readded the test. One thing I wanted to ask also: There is an assert in the code indicating that self-assignment is UB. This seems to be specific to MIR, since obviously it's possible to self-assign in Rust. What is the benefit of this being UB? |
I'm not sure if this has been officially defined, but AFAIK they have active fields just like C unions, and Miri will check this, which I believe is what made me add this restriction. The problem is not with code like
because dest_prop will just turn that into
but with having just the union field write without the preceding initialization of If those conditions are met, what will happen is that dest_prop will remove the only store to the union, which leaves it with no active field, so future reads from any union fields are detected by Miri as UB. |
Hmm... I don't think we know about active fields at all, at least not explicitly. A ZST write will not actually do anything in miri, so I don't see how removing it could affect anything. That said, maybe it should be brought up with the UCG working group? |
StorageLive(_1); // scope 0 at $DIR/union.rs:13:9: 13:11 | ||
StorageLive(_2); // scope 0 at $DIR/union.rs:13:23: 13:28 | ||
_2 = val() -> bb1; // scope 0 at $DIR/union.rs:13:23: 13:28 | ||
- StorageLive(_1); // scope 0 at $DIR/union.rs:13:9: 13:11 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting that we're now not marking the _1
local as live anymore, but using it right below. Maybe that is what would be causing miri to appear to care about unions being not assigned to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't catch this when I saw the test. Yeah, that seems like a bug. Let me try and see if there's an easy fix for this
@jonas-schievink the example I posted above should show that this doesn't seem to be the case, at least right now. Reading a
|
Ah, wait, it might have been that Miri complained about the whole union not having been initialized (since it is never written to), not about the wrong variant being used |
Even then though, the same thing happens in my example above where I create and read from an uninitialized union just fine |
Locals without any storage markers whatsoever are considered always live. I was only wondering why the opt removes the storage markers, it's not incorrect to do so |
Storage markers are deleted because I didn't write an algorithm for merging them, since that seemed pretty nontrivial. Also, there were talks about inferring them automatically, which would remove the need to care about them. |
StorageDead is needed if any references are taken. Because the borrow may outlive the last use of the local. But for dest prop, borrows are already stopping the opt, so that's indeed ok. |
@bors r+ rollup |
📌 Commit 57c4163 has been approved by |
…askrgr Rollup of 7 pull requests Successful merges: - rust-lang#93845 (Remove in band lifetimes) - rust-lang#94155 (Extend toggle GUI test a bit) - rust-lang#94252 (don't special case `DefKind::Ctor` in encoding) - rust-lang#94305 (Remove an unnecessary restriction in `dest_prop`) - rust-lang#94343 (Miri fn ptr check: don't use conservative null check) - rust-lang#94344 (diagnostic: suggest parens when users want logical ops, but get closures) - rust-lang#94352 (Fix SGX docs build) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
I had asked about this on Zulip but didn't receive a response, so putting up this PR that makes the change I think we can. If it turns out that this is wrong, hopefully I'll find out here. Reposting my Zulip comment:
This PR just removes that comment and the associated code. Also it fixes one unrelated comment that did not match the code it was commenting on.
r? rust-lang/mir-opt