Skip to content

Commit 7ade6ed

Browse files
Rollup merge of #89208 - wesleywiser:rfc_2229_droporder, r=nikomatsakis
[rfc 2229] Drop fully captured upvars in the same order as the regular drop code Currently, with the new 2021 edition, if a closure captures all of the fields of an upvar, we'll drop those fields in the order they are used within the closure instead of the normal drop order (the definition order of the fields in the type). This changes that so we sort the captured fields by the definition order which causes them to drop in that same order as well. Fixes rust-lang/project-rfc-2229#42 r? `@nikomatsakis`
2 parents 80d9886 + dd91804 commit 7ade6ed

6 files changed

+485
-1
lines changed

compiler/rustc_typeck/src/check/upvar.rs

+72-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,78 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
602602
}
603603
}
604604

605-
debug!("For closure={:?}, min_captures={:#?}", closure_def_id, root_var_min_capture_list);
605+
debug!(
606+
"For closure={:?}, min_captures before sorting={:?}",
607+
closure_def_id, root_var_min_capture_list
608+
);
609+
610+
// Now that we have the minimized list of captures, sort the captures by field id.
611+
// This causes the closure to capture the upvars in the same order as the fields are
612+
// declared which is also the drop order. Thus, in situations where we capture all the
613+
// fields of some type, the obserable drop order will remain the same as it previously
614+
// was even though we're dropping each capture individually.
615+
// See https://github.com/rust-lang/project-rfc-2229/issues/42 and
616+
// `src/test/ui/closures/2229_closure_analysis/preserve_field_drop_order.rs`.
617+
for (_, captures) in &mut root_var_min_capture_list {
618+
captures.sort_by(|capture1, capture2| {
619+
for (p1, p2) in capture1.place.projections.iter().zip(&capture2.place.projections) {
620+
// We do not need to look at the `Projection.ty` fields here because at each
621+
// step of the iteration, the projections will either be the same and therefore
622+
// the types must be as well or the current projection will be different and
623+
// we will return the result of comparing the field indexes.
624+
match (p1.kind, p2.kind) {
625+
// Paths are the same, continue to next loop.
626+
(ProjectionKind::Deref, ProjectionKind::Deref) => {}
627+
(ProjectionKind::Field(i1, _), ProjectionKind::Field(i2, _))
628+
if i1 == i2 => {}
629+
630+
// Fields are different, compare them.
631+
(ProjectionKind::Field(i1, _), ProjectionKind::Field(i2, _)) => {
632+
return i1.cmp(&i2);
633+
}
634+
635+
// We should have either a pair of `Deref`s or a pair of `Field`s.
636+
// Anything else is a bug.
637+
(
638+
l @ (ProjectionKind::Deref | ProjectionKind::Field(..)),
639+
r @ (ProjectionKind::Deref | ProjectionKind::Field(..)),
640+
) => bug!(
641+
"ProjectionKinds Deref and Field were mismatched: ({:?}, {:?})",
642+
l,
643+
r
644+
),
645+
(
646+
l
647+
@
648+
(ProjectionKind::Index
649+
| ProjectionKind::Subslice
650+
| ProjectionKind::Deref
651+
| ProjectionKind::Field(..)),
652+
r
653+
@
654+
(ProjectionKind::Index
655+
| ProjectionKind::Subslice
656+
| ProjectionKind::Deref
657+
| ProjectionKind::Field(..)),
658+
) => bug!(
659+
"ProjectionKinds Index or Subslice were unexpected: ({:?}, {:?})",
660+
l,
661+
r
662+
),
663+
}
664+
}
665+
666+
unreachable!(
667+
"we captured two identical projections: capture1 = {:?}, capture2 = {:?}",
668+
capture1, capture2
669+
);
670+
});
671+
}
672+
673+
debug!(
674+
"For closure={:?}, min_captures after sorting={:#?}",
675+
closure_def_id, root_var_min_capture_list
676+
);
606677
typeck_results.closure_min_captures.insert(closure_def_id, root_var_min_capture_list);
607678
}
608679

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// edition:2021
2+
3+
// Tests that in cases where we individually capture all the fields of a type,
4+
// we still drop them in the order they would have been dropped in the 2018 edition.
5+
6+
// NOTE: It is *critical* that the order of the min capture NOTES in the stderr output
7+
// does *not* change!
8+
9+
#![feature(rustc_attrs)]
10+
11+
#[derive(Debug)]
12+
struct HasDrop;
13+
impl Drop for HasDrop {
14+
fn drop(&mut self) {
15+
println!("dropped");
16+
}
17+
}
18+
19+
fn test_one() {
20+
let a = (HasDrop, HasDrop);
21+
let b = (HasDrop, HasDrop);
22+
23+
let c = #[rustc_capture_analysis]
24+
//~^ ERROR: attributes on expressions are experimental
25+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
26+
|| {
27+
//~^ ERROR: Min Capture analysis includes:
28+
//~| ERROR
29+
println!("{:?}", a.0);
30+
//~^ NOTE: Min Capture a[(0, 0)] -> ImmBorrow
31+
//~| NOTE
32+
println!("{:?}", a.1);
33+
//~^ NOTE: Min Capture a[(1, 0)] -> ImmBorrow
34+
//~| NOTE
35+
36+
println!("{:?}", b.0);
37+
//~^ NOTE: Min Capture b[(0, 0)] -> ImmBorrow
38+
//~| NOTE
39+
println!("{:?}", b.1);
40+
//~^ NOTE: Min Capture b[(1, 0)] -> ImmBorrow
41+
//~| NOTE
42+
};
43+
}
44+
45+
fn test_two() {
46+
let a = (HasDrop, HasDrop);
47+
let b = (HasDrop, HasDrop);
48+
49+
let c = #[rustc_capture_analysis]
50+
//~^ ERROR: attributes on expressions are experimental
51+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
52+
|| {
53+
//~^ ERROR: Min Capture analysis includes:
54+
//~| ERROR
55+
println!("{:?}", a.1);
56+
//~^ NOTE: Min Capture a[(1, 0)] -> ImmBorrow
57+
//~| NOTE
58+
println!("{:?}", a.0);
59+
//~^ NOTE: Min Capture a[(0, 0)] -> ImmBorrow
60+
//~| NOTE
61+
62+
println!("{:?}", b.1);
63+
//~^ NOTE: Min Capture b[(1, 0)] -> ImmBorrow
64+
//~| NOTE
65+
println!("{:?}", b.0);
66+
//~^ NOTE: Min Capture b[(0, 0)] -> ImmBorrow
67+
//~| NOTE
68+
};
69+
}
70+
71+
fn test_three() {
72+
let a = (HasDrop, HasDrop);
73+
let b = (HasDrop, HasDrop);
74+
75+
let c = #[rustc_capture_analysis]
76+
//~^ ERROR: attributes on expressions are experimental
77+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
78+
|| {
79+
//~^ ERROR: Min Capture analysis includes:
80+
//~| ERROR
81+
println!("{:?}", b.1);
82+
//~^ NOTE: Min Capture b[(1, 0)] -> ImmBorrow
83+
//~| NOTE
84+
println!("{:?}", a.1);
85+
//~^ NOTE: Min Capture a[(1, 0)] -> ImmBorrow
86+
//~| NOTE
87+
println!("{:?}", a.0);
88+
//~^ NOTE: Min Capture a[(0, 0)] -> ImmBorrow
89+
//~| NOTE
90+
91+
println!("{:?}", b.0);
92+
//~^ NOTE: Min Capture b[(0, 0)] -> ImmBorrow
93+
//~| NOTE
94+
};
95+
}
96+
97+
fn main() {
98+
test_one();
99+
test_two();
100+
test_three();
101+
}

0 commit comments

Comments
 (0)