Skip to content

Commit 8cf521d

Browse files
committed
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.
1 parent cab4fd6 commit 8cf521d

File tree

3 files changed

+129
-11
lines changed

3 files changed

+129
-11
lines changed

compiler/rustc_hir_analysis/src/check/region.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,35 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
241241
// scopes, meaning that temporaries cannot outlive them.
242242
// This ensures fixed size stacks.
243243
hir::ExprKind::Binary(
244-
source_map::Spanned { node: hir::BinOpKind::And, .. },
245-
_,
244+
source_map::Spanned { node: outer @ hir::BinOpKind::And, .. },
245+
ref l,
246246
ref r,
247247
)
248248
| hir::ExprKind::Binary(
249-
source_map::Spanned { node: hir::BinOpKind::Or, .. },
250-
_,
249+
source_map::Spanned { node: outer @ hir::BinOpKind::Or, .. },
250+
ref l,
251251
ref r,
252252
) => {
253253
// For shortcircuiting operators, mark the RHS as a terminating
254254
// scope since it only executes conditionally.
255255

256+
// If the LHS is not another binop itself of the same kind as ours,
257+
// we also mark it as terminating, so that in && or || chains,
258+
// the temporaries are dropped in order instead of the very first
259+
// being dropped last. For the Let exception, see below.
260+
let terminate_lhs = match l.kind {
261+
hir::ExprKind::Let(_) => false,
262+
hir::ExprKind::Binary(source_map::Spanned { node, .. }, ..)
263+
if node == outer =>
264+
{
265+
false
266+
}
267+
_ => true,
268+
};
269+
if terminate_lhs {
270+
terminating(l.hir_id.local_id);
271+
}
272+
256273
// `Let` expressions (in a let-chain) shouldn't be terminating, as their temporaries
257274
// should live beyond the immediate expression
258275
if !matches!(r.kind, hir::ExprKind::Let(_)) {

src/test/ui/drop/drop_order.rs

+71-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl DropOrderCollector {
4343
}
4444

4545
if {
46-
if self.option_loud_drop(7).is_some() && self.option_loud_drop(6).is_some() {
46+
if self.option_loud_drop(6).is_some() && self.option_loud_drop(7).is_some() {
4747
self.loud_drop(8);
4848
true
4949
} else {
@@ -118,17 +118,71 @@ impl DropOrderCollector {
118118
}
119119
}
120120

121+
fn and_chain(&self) {
122+
// issue-103107
123+
if self.option_loud_drop(1).is_some() // 1
124+
&& self.option_loud_drop(2).is_some() // 2
125+
&& self.option_loud_drop(3).is_some() // 3
126+
&& self.option_loud_drop(4).is_some() // 4
127+
&& self.option_loud_drop(5).is_some() // 5
128+
{
129+
self.print(6); // 6
130+
}
131+
132+
let _ = self.option_loud_drop(7).is_some() // 1
133+
&& self.option_loud_drop(8).is_some() // 2
134+
&& self.option_loud_drop(9).is_some(); // 3
135+
self.print(10); // 4
136+
137+
// Test associativity
138+
if self.option_loud_drop(11).is_some() // 1
139+
&& (self.option_loud_drop(12).is_some() // 2
140+
&& self.option_loud_drop(13).is_some() // 3
141+
&& self.option_loud_drop(14).is_some()) // 4
142+
&& self.option_loud_drop(15).is_some() // 5
143+
{
144+
self.print(16); // 6
145+
}
146+
}
147+
148+
fn or_chain(&self) {
149+
// issue-103107
150+
if self.option_loud_drop(1).is_none() // 1
151+
|| self.option_loud_drop(2).is_none() // 2
152+
|| self.option_loud_drop(3).is_none() // 3
153+
|| self.option_loud_drop(4).is_none() // 4
154+
|| self.option_loud_drop(5).is_some() // 5
155+
{
156+
self.print(6); // 6
157+
}
158+
159+
let _ = self.option_loud_drop(7).is_none() // 1
160+
|| self.option_loud_drop(8).is_none() // 2
161+
|| self.option_loud_drop(9).is_none(); // 3
162+
self.print(10); // 4
163+
164+
// Test associativity
165+
if self.option_loud_drop(11).is_none() // 1
166+
|| (self.option_loud_drop(12).is_none() // 2
167+
|| self.option_loud_drop(13).is_none() // 3
168+
|| self.option_loud_drop(14).is_none()) // 4
169+
|| self.option_loud_drop(15).is_some() // 5
170+
{
171+
self.print(16); // 6
172+
}
173+
}
174+
121175
fn let_chain(&self) {
122176
// take the "then" branch
123-
if self.option_loud_drop(2).is_some() // 2
124-
&& self.option_loud_drop(1).is_some() // 1
177+
if self.option_loud_drop(1).is_some() // 1
178+
&& self.option_loud_drop(2).is_some() // 2
125179
&& let Some(_d) = self.option_loud_drop(4) { // 4
126180
self.print(3); // 3
127181
}
128182

129183
// take the "else" branch
130-
if self.option_loud_drop(6).is_some() // 2
131-
&& self.option_loud_drop(5).is_some() // 1
184+
if self.option_loud_drop(5).is_some() // 1
185+
&& self.option_loud_drop(6).is_some() // 2
132186
&& let None = self.option_loud_drop(8) { // 4
133187
unreachable!();
134188
} else {
@@ -152,8 +206,8 @@ impl DropOrderCollector {
152206
}
153207

154208
// let exprs last
155-
if self.option_loud_drop(20).is_some() // 2
156-
&& self.option_loud_drop(19).is_some() // 1
209+
if self.option_loud_drop(19).is_some() // 1
210+
&& self.option_loud_drop(20).is_some() // 2
157211
&& let Some(_d) = self.option_loud_drop(23) // 5
158212
&& let Some(_e) = self.option_loud_drop(22) { // 4
159213
self.print(21); // 3
@@ -187,6 +241,16 @@ fn main() {
187241
collector.if_();
188242
collector.assert_sorted();
189243

244+
println!("-- and chain --");
245+
let collector = DropOrderCollector::default();
246+
collector.and_chain();
247+
collector.assert_sorted();
248+
249+
println!("-- or chain --");
250+
let collector = DropOrderCollector::default();
251+
collector.or_chain();
252+
collector.assert_sorted();
253+
190254
println!("-- if let --");
191255
let collector = DropOrderCollector::default();
192256
collector.if_let();

src/test/ui/drop/issue-103107.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// check-pass
2+
// compile-flags: -Z validate-mir
3+
4+
struct Foo<'a>(&'a mut u32);
5+
6+
impl<'a> Drop for Foo<'a> {
7+
fn drop(&mut self) {
8+
*self.0 = 0;
9+
}
10+
}
11+
12+
fn and() {
13+
let mut foo = 0;
14+
// This used to compile also before the fix
15+
if true && *Foo(&mut foo).0 == 0 && ({ foo = 0; true}) {}
16+
17+
// This used to fail before the fix
18+
if *Foo(&mut foo).0 == 0 && ({ foo = 0; true}) {}
19+
20+
println!("{foo}");
21+
}
22+
23+
fn or() {
24+
let mut foo = 0;
25+
// This used to compile also before the fix
26+
if false || *Foo(&mut foo).0 == 1 || ({ foo = 0; true}) {}
27+
28+
// This used to fail before the fix
29+
if *Foo(&mut foo).0 == 1 || ({ foo = 0; true}) {}
30+
31+
println!("{foo}");
32+
}
33+
34+
fn main() {
35+
and();
36+
or();
37+
}

0 commit comments

Comments
 (0)