Skip to content

Commit 621bb79

Browse files
Remove fake borrows of refs that are converted into non-refs in MakeByMoveBody
1 parent cbfdf0b commit 621bb79

4 files changed

+205
-2
lines changed

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
134134
let mut child_precise_captures =
135135
child_capture.place.projections[parent_capture.place.projections.len()..].to_vec();
136136

137+
// Skip trivial captures. These don't need to be remapped, and we save some
138+
// effort peeling and adding back a deref if the capture kind is by-ref.
139+
if parent_capture.info.capture_kind == child_capture.info.capture_kind
140+
&& child_precise_captures.is_empty()
141+
{
142+
return None;
143+
}
144+
137145
// If the parent capture is by-ref, then we need to apply an additional
138146
// deref before applying any further projections to this place.
139147
if parent_capture.is_by_ref() {
@@ -178,17 +186,18 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
178186
),
179187
};
180188

181-
(
189+
Some((
182190
FieldIdx::from_usize(child_field_idx + num_args),
183191
(
184192
FieldIdx::from_usize(parent_field_idx + num_args),
185193
parent_capture_ty,
186194
peel_deref,
187195
child_precise_captures,
188196
),
189-
)
197+
))
190198
},
191199
)
200+
.flatten()
192201
.collect();
193202

194203
if coroutine_kind == ty::ClosureKind::FnOnce {
@@ -312,10 +321,41 @@ impl<'tcx> MutVisitor<'tcx> for MakeByMoveBody<'tcx> {
312321
self.super_place(place, context, location);
313322
}
314323

324+
fn visit_statement(&mut self, statement: &mut mir::Statement<'tcx>, location: mir::Location) {
325+
// Remove fake borrows of closure captures if that capture has been
326+
// replaced with a by-move version of that capture.
327+
//
328+
// For example, imagine we capture `Foo` in the parent and `&Foo`
329+
// in the child. We will emit two fake borrows like:
330+
//
331+
// ```
332+
// _2 = &fake shallow (*(_1.0: &Foo));
333+
// _3 = &fake shallow (_1.0: &Foo);
334+
// ```
335+
//
336+
// However, since this transform is responsible for replacing
337+
// `_1.0: &Foo` with `_1.0: Foo`, that makes the second fake borrow
338+
// obsolete, and we should replace it with a nop.
339+
if let mir::StatementKind::Assign(box (_, rvalue)) = &statement.kind
340+
&& let mir::Rvalue::Ref(_, mir::BorrowKind::Fake(mir::FakeBorrowKind::Shallow), place) =
341+
rvalue
342+
&& let mir::PlaceRef {
343+
local: ty::CAPTURE_STRUCT_LOCAL,
344+
projection: [mir::ProjectionElem::Field(idx, _)],
345+
} = place.as_ref()
346+
&& let Some(&(_, _, true, _)) = self.field_remapping.get(&idx)
347+
{
348+
statement.kind = mir::StatementKind::Nop;
349+
}
350+
351+
self.super_statement(statement, location);
352+
}
353+
315354
fn visit_local_decl(&mut self, local: mir::Local, local_decl: &mut mir::LocalDecl<'tcx>) {
316355
// Replace the type of the self arg.
317356
if local == ty::CAPTURE_STRUCT_LOCAL {
318357
local_decl.ty = self.by_move_coroutine_ty;
319358
}
359+
self.super_local_decl(local, local_decl);
320360
}
321361
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// MIR for `foo::{closure#0}::{closure#0}` after built
2+
3+
fn foo::{closure#0}::{closure#0}(_1: {async closure body@$DIR/async_closure_fake_read_for_by_move.rs:12:27: 15:6}, _2: ResumeTy) -> ()
4+
yields ()
5+
{
6+
debug _task_context => _2;
7+
debug f => (*(_1.0: &&Foo));
8+
let mut _0: ();
9+
let mut _3: &Foo;
10+
let mut _4: &&Foo;
11+
let mut _5: &&&Foo;
12+
let mut _6: isize;
13+
let mut _7: bool;
14+
15+
bb0: {
16+
PlaceMention((*(_1.0: &&Foo)));
17+
_6 = discriminant((*(*(_1.0: &&Foo))));
18+
switchInt(move _6) -> [0: bb2, otherwise: bb1];
19+
}
20+
21+
bb1: {
22+
_0 = const ();
23+
goto -> bb10;
24+
}
25+
26+
bb2: {
27+
falseEdge -> [real: bb5, imaginary: bb1];
28+
}
29+
30+
bb3: {
31+
goto -> bb1;
32+
}
33+
34+
bb4: {
35+
FakeRead(ForMatchedPlace(None), (*(_1.0: &&Foo)));
36+
unreachable;
37+
}
38+
39+
bb5: {
40+
_3 = &fake shallow (*(*(_1.0: &&Foo)));
41+
_4 = &fake shallow (*(_1.0: &&Foo));
42+
_5 = &fake shallow (_1.0: &&Foo);
43+
StorageLive(_7);
44+
_7 = const true;
45+
switchInt(move _7) -> [0: bb8, otherwise: bb7];
46+
}
47+
48+
bb6: {
49+
falseEdge -> [real: bb3, imaginary: bb1];
50+
}
51+
52+
bb7: {
53+
StorageDead(_7);
54+
FakeRead(ForMatchGuard, _3);
55+
FakeRead(ForMatchGuard, _4);
56+
FakeRead(ForMatchGuard, _5);
57+
_0 = const ();
58+
goto -> bb10;
59+
}
60+
61+
bb8: {
62+
goto -> bb9;
63+
}
64+
65+
bb9: {
66+
StorageDead(_7);
67+
goto -> bb6;
68+
}
69+
70+
bb10: {
71+
drop(_1) -> [return: bb11, unwind: bb12];
72+
}
73+
74+
bb11: {
75+
return;
76+
}
77+
78+
bb12 (cleanup): {
79+
resume;
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// MIR for `foo::{closure#0}::{closure#1}` after built
2+
3+
fn foo::{closure#0}::{closure#1}(_1: {async closure body@$DIR/async_closure_fake_read_for_by_move.rs:12:27: 15:6}, _2: ResumeTy) -> ()
4+
yields ()
5+
{
6+
debug _task_context => _2;
7+
debug f => (_1.0: &Foo);
8+
let mut _0: ();
9+
let mut _3: &Foo;
10+
let mut _4: &&Foo;
11+
let mut _5: &&&Foo;
12+
let mut _6: isize;
13+
let mut _7: bool;
14+
15+
bb0: {
16+
PlaceMention((_1.0: &Foo));
17+
_6 = discriminant((*(_1.0: &Foo)));
18+
switchInt(move _6) -> [0: bb2, otherwise: bb1];
19+
}
20+
21+
bb1: {
22+
_0 = const ();
23+
goto -> bb6;
24+
}
25+
26+
bb2: {
27+
falseEdge -> [real: bb3, imaginary: bb1];
28+
}
29+
30+
bb3: {
31+
_3 = &fake shallow (*(_1.0: &Foo));
32+
_4 = &fake shallow (_1.0: &Foo);
33+
nop;
34+
StorageLive(_7);
35+
_7 = const true;
36+
switchInt(move _7) -> [0: bb5, otherwise: bb4];
37+
}
38+
39+
bb4: {
40+
StorageDead(_7);
41+
FakeRead(ForMatchGuard, _3);
42+
FakeRead(ForMatchGuard, _4);
43+
FakeRead(ForMatchGuard, _5);
44+
_0 = const ();
45+
goto -> bb6;
46+
}
47+
48+
bb5: {
49+
StorageDead(_7);
50+
falseEdge -> [real: bb1, imaginary: bb1];
51+
}
52+
53+
bb6: {
54+
drop(_1) -> [return: bb7, unwind: bb8];
55+
}
56+
57+
bb7: {
58+
return;
59+
}
60+
61+
bb8 (cleanup): {
62+
resume;
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ edition:2021
2+
// skip-filecheck
3+
4+
enum Foo {
5+
Bar,
6+
Baz,
7+
}
8+
9+
// EMIT_MIR async_closure_fake_read_for_by_move.foo-{closure#0}-{closure#0}.built.after.mir
10+
// EMIT_MIR async_closure_fake_read_for_by_move.foo-{closure#0}-{closure#1}.built.after.mir
11+
fn foo(f: &Foo) {
12+
let x = async move || match f {
13+
Foo::Bar if true => {}
14+
_ => {}
15+
};
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)