Repository navigation
Make [$expr; 0] equivalent to { drop($expr); [] } - #165986
Jules-Bertholet wants to merge 2 commits into
Conversation
|
This PR changes MIR cc @oli-obk, @RalfJung, @JakobDegen, @vakaras |
|
r? @jieyouxu rustbot has assigned @jieyouxu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
[$expr; 0] equivalent to { let _ = $expr; [] }
|
Does this PR change temporary lifetime extension behavior? |
This comment has been minimized.
This comment has been minimized.
Are you following the same mental model for const blocks? If so why would |
I'm not a fan. This is a value expression context, not a place expression context, so we should not apply place expression rules. |
|
This PR introduces a discrepancy between arrays with a generic struct LoudDrop;
impl Drop for LoudDrop {
fn drop(&mut self) {
println!("dropped");
}
}
const C: LoudDrop = LoudDrop;
fn foo<const N: usize>() {
let _ = [C; N];
}
fn bar() {
let _ = [C; 0];
}
fn main() {
println!("foo");
foo::<0>();
println!("bar");
bar();
}Output on nightly: Output with this PR: |
|
@theemathas whoops… I guess this is harder to implement than I thought. Marking as draft for now |
|
@theemathas Your example works properly now. We just generate an
Because we special-case it to work. Copying non-
We can say that it is a place expression context after all. We get to decide that. |
There's no rhyme or reason to this though, as far as I can see. Place expression contexts are those that actually need a place. This here only needs a value. What's the motivation for making it a place expression, other than "it's a hack that makes the delta to the previous behavior smaller"? We haven't yet seen evidence that going all the way to consistent value-based behavior would be problematic, so I don't consider that argument convincing. I think this PR is the wrong fix for the problem; I described my preferred fix here. |
218ed2c to
2c797ec
Compare
It doesn't, actually, it makes the delta bigger. I think that is worth it for a more consistent mental model (conditional on Crater, of course).
… sometimes. Sometimes (when the length is 0) we don't need the value at all. IMO, it's natural for the semantics to reflect that. (In your comment, you seem to imply that needing "only" a value is a weaker requirement than needing a place. IMO it's the other way around. A value can always be turned into a place (make a temporary), but making a value from a place is not always possible.) |
2c797ec to
61e1ceb
Compare
61e1ceb to
16e084a
Compare
It shouldn't—added fixes and tests for this. |
|
@rustbot reroll |
|
In my eyes this PR makes the mental model strictly less consistent than my suggestion.
It is up to us to define what we need and don't need. If we are talking about what the semantics strictly need, we need a place if we want to create a pointer or mutate. Neither happens here, so creating a place makes no sense whatsoever. Values are the more natural choice for an expression to evaluate to, so IMO picking a place instead always needs justification. So far the Rust semantics follows the principle that we only evaluate to a place if we actually need a place. I am opposed to this PR because it gives up on that principle for no good reason. In |
I am not implying anything of the sort. If anything I am implying the opposite, I guessed that you made this PR because you consider "only" evaluating a place to be a worthy hack to make the semantics change less invasive. It is the only explanation I can imagine for why one would want to add a new place context here. If that's not the explanation then I am even more strongly opposed to this PR, it feels just fully arbitrary.^^ |
|
With value semantics, dropping the value on |
6c55b59 to
16e084a
Compare
[$expr; 0] equivalent to { let _ = $expr; [] }[$expr; 0] equivalent to { drop($expr); [] }
|
@RalfJung I've come around to your position. What changed my mind is code like this: let mut x = [42i32; N];
x = [*&x[0]; N];This needs to go through an intermediate temporary somewhere. Always making that temporary is more consistent. |
|
I don't think I quite understand the significance of the example... but I am happy if we can agree on the semantics. :)
It's kind of the same special case as {
let x = <expr>;
if N > 0 { /* move out of x */ }
// drop x if N = 0
}so I don't think it is comparable in specialness to using place evaluation here. |
Fixes #165323.
Potentially changes runtime behavior of existing code!!! Needs a crater test run.
@rustbot label A-array T-lang needs-fcp