2024-02-16 14:02:50 -06:00
|
|
|
//@ check-pass
|
|
|
|
//@ compile-flags: -Z validate-mir
|
Remove drop order twist of && and || and make them associative
Previously a short circuiting && chain would drop the
first element after all the other elements, and otherwise
follow evaluation order, so code like:
f(1).g() && f(2).g() && f(3).g() && f(4).g()
would drop the temporaries in the order 2,3,4,1. This made
&& and || non-associative regarding drop order, so
adding ()'s to the expression would change drop order:
f(1).g() && (f(2).g() && f(3).g()) && f(4).g()
for example would drop in the order 3,2,4,1.
As, except for the bool result, there is no data returned
by the sub-expressions of the short circuiting binops,
we can safely discard of any temporaries created by the
sub-expr. Previously, code was already putting the rhs's
into terminating scopes, but missed it for the lhs's.
This commit addresses this "twist". In the expression,
we now also put the lhs into a terminating scope.
The drop order for the above expressions is 1,2,3,4
now.
2022-10-20 02:50:32 -05:00
|
|
|
|
|
|
|
struct Foo<'a>(&'a mut u32);
|
|
|
|
|
|
|
|
impl<'a> Drop for Foo<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
*self.0 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn and() {
|
|
|
|
let mut foo = 0;
|
|
|
|
// This used to compile also before the fix
|
|
|
|
if true && *Foo(&mut foo).0 == 0 && ({ foo = 0; true}) {}
|
|
|
|
|
|
|
|
// This used to fail before the fix
|
|
|
|
if *Foo(&mut foo).0 == 0 && ({ foo = 0; true}) {}
|
|
|
|
|
|
|
|
println!("{foo}");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn or() {
|
|
|
|
let mut foo = 0;
|
|
|
|
// This used to compile also before the fix
|
|
|
|
if false || *Foo(&mut foo).0 == 1 || ({ foo = 0; true}) {}
|
|
|
|
|
|
|
|
// This used to fail before the fix
|
|
|
|
if *Foo(&mut foo).0 == 1 || ({ foo = 0; true}) {}
|
|
|
|
|
|
|
|
println!("{foo}");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
and();
|
|
|
|
or();
|
|
|
|
}
|