Skip to content

Commit ccd9bb1

Browse files
committed
Generate the right MIR for by use closures
1 parent c0935bf commit ccd9bb1

File tree

10 files changed

+65
-47
lines changed

10 files changed

+65
-47
lines changed

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
10711071
);
10721072

10731073
match capture_info.capture_kind {
1074-
ty::UpvarCapture::ByValue => {
1074+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {
10751075
self.consume_or_copy(&place_with_id, place_with_id.hir_id);
10761076
}
10771077
ty::UpvarCapture::ByRef(upvar_borrow) => {

compiler/rustc_hir_typeck/src/upvar.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
666666
origin = updated.1;
667667

668668
let (place, capture_kind) = match capture_clause {
669-
hir::CaptureBy::Value { .. } | hir::CaptureBy::Use { .. } => {
670-
adjust_for_move_closure(place, capture_kind)
671-
}
669+
hir::CaptureBy::Value { .. } => adjust_for_move_closure(place, capture_kind),
670+
hir::CaptureBy::Use { .. } => adjust_for_use_closure(place, capture_kind),
672671
hir::CaptureBy::Ref => adjust_for_non_move_closure(place, capture_kind),
673672
};
674673

@@ -1303,7 +1302,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13031302
for captured_place in root_var_min_capture_list.iter() {
13041303
match captured_place.info.capture_kind {
13051304
// Only care about captures that are moved into the closure
1306-
ty::UpvarCapture::ByValue => {
1305+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {
13071306
projections_list.push(captured_place.place.projections.as_slice());
13081307
diagnostics_info.insert(UpvarMigrationInfo::CapturingPrecise {
13091308
source_expr: captured_place.info.path_expr_id,
@@ -1927,7 +1926,7 @@ fn apply_capture_kind_on_capture_ty<'tcx>(
19271926
region: ty::Region<'tcx>,
19281927
) -> Ty<'tcx> {
19291928
match capture_kind {
1930-
ty::UpvarCapture::ByValue => ty,
1929+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => ty,
19311930
ty::UpvarCapture::ByRef(kind) => Ty::new_ref(tcx, region, ty, kind.to_mutbl_lossy()),
19321931
}
19331932
}
@@ -2157,6 +2156,20 @@ fn adjust_for_move_closure(
21572156
(place, ty::UpvarCapture::ByValue)
21582157
}
21592158

2159+
/// Truncate deref of any reference.
2160+
fn adjust_for_use_closure(
2161+
mut place: Place<'_>,
2162+
mut kind: ty::UpvarCapture,
2163+
) -> (Place<'_>, ty::UpvarCapture) {
2164+
let first_deref = place.projections.iter().position(|proj| proj.kind == ProjectionKind::Deref);
2165+
2166+
if let Some(idx) = first_deref {
2167+
truncate_place_to_len_and_update_capture_kind(&mut place, &mut kind, idx);
2168+
}
2169+
2170+
(place, ty::UpvarCapture::ByUse)
2171+
}
2172+
21602173
/// Adjust closure capture just that if taking ownership of data, only move data
21612174
/// from enclosing stack frame.
21622175
fn adjust_for_non_move_closure(
@@ -2167,7 +2180,7 @@ fn adjust_for_non_move_closure(
21672180
place.projections.iter().position(|proj| proj.kind == ProjectionKind::Deref);
21682181

21692182
match kind {
2170-
ty::UpvarCapture::ByValue => {
2183+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {
21712184
if let Some(idx) = contains_deref {
21722185
truncate_place_to_len_and_update_capture_kind(&mut place, &mut kind, idx);
21732186
}
@@ -2212,6 +2225,7 @@ fn construct_capture_kind_reason_string<'tcx>(
22122225

22132226
let capture_kind_str = match capture_info.capture_kind {
22142227
ty::UpvarCapture::ByValue => "ByValue".into(),
2228+
ty::UpvarCapture::ByUse => "ByUse".into(),
22152229
ty::UpvarCapture::ByRef(kind) => format!("{kind:?}"),
22162230
};
22172231

@@ -2233,6 +2247,7 @@ fn construct_capture_info_string<'tcx>(
22332247

22342248
let capture_kind_str = match capture_info.capture_kind {
22352249
ty::UpvarCapture::ByValue => "ByValue".into(),
2250+
ty::UpvarCapture::ByUse => "ByUse".into(),
22362251
ty::UpvarCapture::ByRef(kind) => format!("{kind:?}"),
22372252
};
22382253
format!("{place_str} -> {capture_kind_str}")
@@ -2328,8 +2343,11 @@ fn determine_capture_info(
23282343
// expressions.
23292344
let eq_capture_kind = match (capture_info_a.capture_kind, capture_info_b.capture_kind) {
23302345
(ty::UpvarCapture::ByValue, ty::UpvarCapture::ByValue) => true,
2346+
(ty::UpvarCapture::ByUse, ty::UpvarCapture::ByUse) => true,
23312347
(ty::UpvarCapture::ByRef(ref_a), ty::UpvarCapture::ByRef(ref_b)) => ref_a == ref_b,
2332-
(ty::UpvarCapture::ByValue, _) | (ty::UpvarCapture::ByRef(_), _) => false,
2348+
(ty::UpvarCapture::ByValue, _)
2349+
| (ty::UpvarCapture::ByUse, _)
2350+
| (ty::UpvarCapture::ByRef(_), _) => false,
23332351
};
23342352

23352353
if eq_capture_kind {
@@ -2339,8 +2357,10 @@ fn determine_capture_info(
23392357
}
23402358
} else {
23412359
// We select the CaptureKind which ranks higher based the following priority order:
2342-
// ByValue > MutBorrow > UniqueImmBorrow > ImmBorrow
2360+
// ByUse > ByValue > MutBorrow > UniqueImmBorrow > ImmBorrow
23432361
match (capture_info_a.capture_kind, capture_info_b.capture_kind) {
2362+
(ty::UpvarCapture::ByUse, _) => capture_info_a,
2363+
(_, ty::UpvarCapture::ByUse) => capture_info_b,
23442364
(ty::UpvarCapture::ByValue, _) => capture_info_a,
23452365
(_, ty::UpvarCapture::ByValue) => capture_info_b,
23462366
(ty::UpvarCapture::ByRef(ref_a), ty::UpvarCapture::ByRef(ref_b)) => {
@@ -2394,7 +2414,7 @@ fn truncate_place_to_len_and_update_capture_kind<'tcx>(
23942414
}
23952415

23962416
ty::UpvarCapture::ByRef(..) => {}
2397-
ty::UpvarCapture::ByValue => {}
2417+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {}
23982418
}
23992419

24002420
place.projections.truncate(len);

compiler/rustc_middle/src/ty/closure.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub enum UpvarCapture {
5252
/// depending on inference.
5353
ByValue,
5454

55+
/// Upvar is captured by use. This is true when the closure is labeled `use`.
56+
ByUse,
57+
5558
/// Upvar is captured by reference.
5659
ByRef(BorrowKind),
5760
}
@@ -179,7 +182,7 @@ impl<'tcx> CapturedPlace<'tcx> {
179182

180183
pub fn is_by_ref(&self) -> bool {
181184
match self.info.capture_kind {
182-
ty::UpvarCapture::ByValue => false,
185+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => false,
183186
ty::UpvarCapture::ByRef(..) => true,
184187
}
185188
}
@@ -215,7 +218,7 @@ impl<'tcx> TyCtxt<'tcx> {
215218
pub fn closure_captures(self, def_id: LocalDefId) -> &'tcx [&'tcx ty::CapturedPlace<'tcx>] {
216219
if !self.is_closure_like(def_id.to_def_id()) {
217220
return &[];
218-
};
221+
}
219222
self.closure_typeinfo(def_id).captures
220223
}
221224
}

compiler/rustc_mir_build/src/builder/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
3939
.map(|captured_place| {
4040
let name = captured_place.to_symbol();
4141
match captured_place.info.capture_kind {
42-
ty::UpvarCapture::ByValue => name,
42+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => name,
4343
ty::UpvarCapture::ByRef(..) => Symbol::intern(&format!("_ref__{name}")),
4444
}
4545
})
@@ -884,7 +884,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
884884
let mut projs = closure_env_projs.clone();
885885
projs.push(ProjectionElem::Field(FieldIdx::new(i), ty));
886886
match capture {
887-
ty::UpvarCapture::ByValue => {}
887+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {}
888888
ty::UpvarCapture::ByRef(..) => {
889889
projs.push(ProjectionElem::Deref);
890890
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ impl<'tcx> Cx<'tcx> {
678678
}
679679
},
680680

681-
hir::ExprKind::Closure { .. } => {
681+
hir::ExprKind::Closure(hir::Closure { .. }) => {
682682
let closure_ty = self.typeck_results().expr_ty(expr);
683683
let (def_id, args, movability) = match *closure_ty.kind() {
684684
ty::Closure(def_id, args) => (def_id, UpvarArgs::Closure(args), None),
@@ -1269,6 +1269,17 @@ impl<'tcx> Cx<'tcx> {
12691269

12701270
match upvar_capture {
12711271
ty::UpvarCapture::ByValue => captured_place_expr,
1272+
ty::UpvarCapture::ByUse => {
1273+
let span = captured_place_expr.span;
1274+
let expr_id = self.thir.exprs.push(captured_place_expr);
1275+
1276+
Expr {
1277+
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
1278+
ty: upvar_ty,
1279+
span: closure_expr.span,
1280+
kind: ExprKind::ByUse { expr: expr_id, span },
1281+
}
1282+
}
12721283
ty::UpvarCapture::ByRef(upvar_borrow) => {
12731284
let borrow_kind = match upvar_borrow {
12741285
ty::BorrowKind::Immutable => BorrowKind::Shared,

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
170170
// this when building the field projection in the MIR body later on.
171171
let mut parent_capture_ty = parent_capture.place.ty();
172172
parent_capture_ty = match parent_capture.info.capture_kind {
173-
ty::UpvarCapture::ByValue => parent_capture_ty,
173+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => parent_capture_ty,
174174
ty::UpvarCapture::ByRef(kind) => Ty::new_ref(
175175
tcx,
176176
tcx.lifetimes.re_erased,

compiler/rustc_passes/src/liveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
706706
);
707707
self.acc(self.exit_ln, var, ACC_READ | ACC_USE);
708708
}
709-
ty::UpvarCapture::ByValue => {}
709+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {}
710710
}
711711
}
712712
}
@@ -1499,7 +1499,7 @@ impl<'tcx> Liveness<'_, 'tcx> {
14991499
for (&var_hir_id, min_capture_list) in closure_min_captures {
15001500
for captured_place in min_capture_list {
15011501
match captured_place.info.capture_kind {
1502-
ty::UpvarCapture::ByValue => {}
1502+
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {}
15031503
ty::UpvarCapture::ByRef(..) => continue,
15041504
};
15051505
let span = captured_place.get_capture_kind_span(self.ir.tcx);

tests/ui/ergonomic-clones/closure.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33

44
#![feature(ergonomic_clones)]
55

6-
fn ergonomic_clone_closure() -> i32 {
6+
fn ergonomic_clone_closure_no_captures() -> i32 {
77
let cl = use || {
88
1
99
};
1010
cl()
1111
}
1212

13+
fn ergonomic_clone_closure_with_captures() -> String {
14+
let s = String::from("hi");
15+
16+
let cl = use || {
17+
s
18+
};
19+
cl()
20+
}
21+
1322
fn ergonomic_clone_async_closures() -> String {
1423
let s = String::from("hi");
1524

1625
async use {
17-
22
26+
s
1827
};
1928

2029
s

tests/ui/feature-gates/feature-gate-ergonomic-clones.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ fn ergonomic_closure_clone() {
1313

1414
let s3 = use || {
1515
//~^ ERROR `.use` calls are experimental [E0658]
16-
//~| ERROR use of moved value: `s1` [E0382]
1716
s1
1817
};
1918
}

tests/ui/feature-gates/feature-gate-ergonomic-clones.stderr

+2-26
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,6 @@ LL | let s3 = use || {
2828
= help: add `#![feature(ergonomic_clones)]` to the crate attributes to enable
2929
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3030

31-
error[E0382]: use of moved value: `s1`
32-
--> $DIR/feature-gate-ergonomic-clones.rs:14:14
33-
|
34-
LL | let s1 = String::from("hi!");
35-
| -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
36-
LL |
37-
LL | let s2 = use || {
38-
| ------ value moved into closure here
39-
LL |
40-
LL | s1
41-
| -- variable moved due to use in closure
42-
...
43-
LL | let s3 = use || {
44-
| ^^^^^^ value used here after move
45-
...
46-
LL | s1
47-
| -- use occurs due to use in closure
48-
|
49-
help: consider cloning the value if the performance cost is acceptable
50-
|
51-
LL | s1.clone()
52-
| ++++++++
53-
54-
error: aborting due to 4 previous errors
31+
error: aborting due to 3 previous errors
5532

56-
Some errors have detailed explanations: E0382, E0658.
57-
For more information about an error, try `rustc --explain E0382`.
33+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)