Open sohide opened 3 months ago
Could you add some more context about what is going wrong vs. what you expect to happen?
Here's my understanding of what issue is being reported.
This code doesn't compile:
struct Wrap<'a>(&'a i32);
static TWO: i32 = 2;
fn main() {
let mut x;
{
let y = 1;
x = Wrap(&y);
x.0 = &TWO;
}
let _z = x;
}
But this code compiles fine:
struct Wrap<'a>(&'a i32);
static TWO: i32 = 2;
fn main() {
let mut x;
{
let y = 1;
x = Wrap(&y);
x = Wrap(&TWO);
}
let _z = x;
}
Weird. I would expect either both to compile or neither to compile.
Here's my understanding of what issue is being reported.
This code doesn't compile:
struct Wrap<'a>(&'a i32); static TWO: i32 = 2; fn main() { let mut x; { let y = 1; x = Wrap(&y); x.0 = &TWO; } let _z = x; }
Error message But this code compiles fine:
struct Wrap<'a>(&'a i32); static TWO: i32 = 2; fn main() { let mut x; { let y = 1; x = Wrap(&y); x = Wrap(&TWO); } let _z = x; }
Weird. I would expect either both to compile or neither to compile.
Yes, that's exactly what I want to say. In the first code snippet, x.0 was rebind, and won't become dangling. I expect the first can be compile without error.
I expect the first can be compile without error.
That would be UB, as it would assign a value that does not satisfy its validity invariant. So the first must be rejected.
What value and what validity invariant are you talking about? Miri doesn't complain about this code:
struct Wrap<'a>(&'a i32);
static TWO: i32 = 2;
fn main() {
let mut x;
{
let y = 1;
x = Wrap(unsafe { std::mem::transmute(&y) });
x = Wrap(&TWO);
}
let _z = x;
}
Ah sorry I misread the code, I thought it was
struct Wrap<'a>(&'a i32);
static TWO: i32 = 2;
fn main() {
let mut x;
{
let y = 1;
x = Wrap(&y);
}
let _z = x;
}
That must be rejected for sure. I should not post before I am properly awake. ;)
The actual issue may just be explained by strong (type-changing) assignments. That would also explain why it does not work when changing just the field of a type.