Visitar URL original
Rollup merge of #157738 - TaKO8Ki:move-expr-coroutine-closures, r=nik… · JonathanBrouwer/rust@cf24767 · GitHub
Skip to content

Commit cf24767

Browse files
Rollup merge of rust-lang#157738 - TaKO8Ki:move-expr-coroutine-closures, r=nikomatsakis
Support move expressions in coroutine closures This adds `move(expr)` support for coroutine closures. - [x] Support for move expressions in coroutine closures - [x] Support for move expressions in async blocks RFC: rust-lang/rfcs#3968 Tracking issue: rust-lang#155050 Project goal: - rust-lang/goals#107 - https://rust-lang.github.io/rust-project-goals/2026/ergonomic-rc.html I used AI to write the tests and reviewed them myself. r? @nikomatsakis
2 parents 0b1760a + 2ed8d0d commit cf24767

21 files changed

Lines changed: 736 additions & 209 deletions

‎compiler/rustc_ast_lowering/src/diagnostics.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,17 @@ pub(crate) struct ClosureCannotBeStatic {
148148
}
149149

150150
#[derive(Diagnostic)]
151-
#[diag("`move(expr)` is only supported in plain closures")]
152-
pub(crate) struct MoveExprOnlyInPlainClosures {
151+
#[diag("`move(expr)` is only supported in closures, `async`, `gen`, and `async gen` blocks")]
152+
pub(crate) struct MoveExprOnlyInSupportedContexts {
153+
#[primary_span]
154+
pub span: Span,
155+
}
156+
157+
#[derive(Diagnostic)]
158+
#[diag(
159+
"nested `move(expr)` requires another enclosing closure, `async`, `gen`, or `async gen` block"
160+
)]
161+
pub(crate) struct NestedMoveExprWithoutEnclosingContext {
153162
#[primary_span]
154163
pub span: Span,
155164
}

‎compiler/rustc_ast_lowering/src/expr.rs‎

Lines changed: 141 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ mod closure;
2121
use crate::diagnostics::{
2222
AsyncCoroutinesNotSupported, AwaitOnlyInAsyncFnAndBlocks,
2323
FunctionalRecordUpdateDestructuringAssignment, InclusiveRangeWithNoEnd,
24-
InvalidLegacyConstGenericArg, MatchArmWithNoBody, MoveExprOnlyInPlainClosures,
25-
NeverPatternWithBody, NeverPatternWithGuard, UnderscoreExprLhsAssign, UseConstGenericArg,
26-
YieldInClosure,
24+
InvalidLegacyConstGenericArg, MatchArmWithNoBody, MoveExprOnlyInSupportedContexts,
25+
NestedMoveExprWithoutEnclosingContext, NeverPatternWithBody, NeverPatternWithGuard,
26+
UnderscoreExprLhsAssign, UseConstGenericArg, YieldInClosure,
2727
};
2828
use crate::{
2929
AllowReturnTypeNotation, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext,
@@ -36,30 +36,20 @@ pub(super) struct WillCreateDefIdsVisitor;
3636
struct MoveExprInitializer<'a> {
3737
/// The `NodeId` of the outer `move(...)` expression.
3838
id: NodeId,
39-
/// Span of the `move` token, used for the generated binding name.
40-
move_kw_span: Span,
4139
/// The expression inside `move(...)`; e.g. `foo.bar` in `move(foo.bar)`.
4240
expr: &'a Expr,
4341
}
4442

45-
/// State for `move(...)` expressions found while lowering one plain closure body.
43+
/// State for `move(...)` expressions found while lowering one closure-like body.
44+
#[derive(Default)]
4645
pub(super) struct MoveExprState<'hir> {
47-
pub(super) bindings: NodeMap<(Ident, HirId)>,
4846
pub(super) occurrences: Vec<MoveExprOccurrence<'hir>>,
4947
}
5048

51-
impl<'hir> Default for MoveExprState<'hir> {
52-
fn default() -> Self {
53-
Self { bindings: NodeMap::default(), occurrences: Vec::new() }
54-
}
55-
}
56-
5749
pub(super) struct MoveExprOccurrence<'hir> {
5850
id: NodeId,
59-
ident: Ident,
6051
pat: &'hir hir::Pat<'hir>,
6152
binding: HirId,
62-
explicit_capture: bool,
6353
}
6454

6555
/// Looks up the initializer expression for each `move(...)` occurrence.
@@ -73,20 +63,22 @@ impl<'a> MoveExprInitializerFinder<'a> {
7363
this.visit_expr(expr);
7464
this.initializers
7565
}
66+
67+
fn collect_block(block: &'a Block) -> Vec<MoveExprInitializer<'a>> {
68+
let mut this = Self { initializers: Vec::new() };
69+
this.visit_block(block);
70+
this.initializers
71+
}
7672
}
7773

7874
impl<'a> Visitor<'a> for MoveExprInitializerFinder<'a> {
7975
fn visit_expr(&mut self, expr: &'a Expr) {
8076
match &expr.kind {
81-
ExprKind::Move(inner, move_kw_span) => {
77+
ExprKind::Move(inner, _) => {
8278
self.visit_expr(inner);
83-
self.initializers.push(MoveExprInitializer {
84-
id: expr.id,
85-
move_kw_span: *move_kw_span,
86-
expr: inner,
87-
});
79+
self.initializers.push(MoveExprInitializer { id: expr.id, expr: inner });
8880
}
89-
ExprKind::Closure(..) | ExprKind::Gen(..) | ExprKind::ConstBlock(..) => {}
81+
ExprKind::ConstBlock(..) => {}
9082
_ => walk_expr(self, expr),
9183
}
9284
}
@@ -129,13 +121,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
129121
(result, state)
130122
}
131123

132-
fn record_move_expr(
133-
&mut self,
134-
id: NodeId,
135-
inner: &Expr,
136-
move_kw_span: Span,
137-
explicit_capture: bool,
138-
) -> (Ident, HirId) {
124+
fn with_move_expr_initializer<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
125+
let old = self.lowering_move_expr_initializer;
126+
self.lowering_move_expr_initializer = true;
127+
let result = f(self);
128+
self.lowering_move_expr_initializer = old;
129+
result
130+
}
131+
132+
fn record_move_expr(&mut self, id: NodeId, inner: &Expr, move_kw_span: Span) -> (Ident, HirId) {
139133
let index = self
140134
.move_expr_bindings
141135
.last()
@@ -145,13 +139,74 @@ impl<'hir> LoweringContext<'_, 'hir> {
145139
let (pat, binding) = self.pat_ident(inner.span, ident);
146140
let Some(state) = self.move_expr_bindings.last_mut().and_then(|state| state.as_mut())
147141
else {
148-
span_bug!(move_kw_span, "`move(...)` lowered without a plain closure body state");
142+
span_bug!(move_kw_span, "`move(...)` lowered without a closure-like body state");
149143
};
150-
state.bindings.insert(id, (ident, binding));
151-
state.occurrences.push(MoveExprOccurrence { id, ident, pat, binding, explicit_capture });
144+
state.occurrences.push(MoveExprOccurrence { id, pat, binding });
152145
(ident, binding)
153146
}
154147

148+
fn lower_expr_with_move_exprs(
149+
&mut self,
150+
expr: hir::Expr<'hir>,
151+
move_expr_state: MoveExprState<'hir>,
152+
body: &Expr,
153+
whole_span: Span,
154+
) -> hir::Expr<'hir> {
155+
let initializers = MoveExprInitializerFinder::collect(body);
156+
self.lower_expr_with_move_expr_initializers(expr, move_expr_state, initializers, whole_span)
157+
}
158+
159+
fn lower_expr_with_move_exprs_in_block(
160+
&mut self,
161+
expr: hir::Expr<'hir>,
162+
move_expr_state: MoveExprState<'hir>,
163+
body: &Block,
164+
whole_span: Span,
165+
) -> hir::Expr<'hir> {
166+
let initializers = MoveExprInitializerFinder::collect_block(body);
167+
self.lower_expr_with_move_expr_initializers(expr, move_expr_state, initializers, whole_span)
168+
}
169+
170+
fn lower_expr_with_move_expr_initializers(
171+
&mut self,
172+
expr: hir::Expr<'hir>,
173+
move_expr_state: MoveExprState<'hir>,
174+
initializers: Vec<MoveExprInitializer<'_>>,
175+
whole_span: Span,
176+
) -> hir::Expr<'hir> {
177+
if move_expr_state.occurrences.is_empty() {
178+
return expr;
179+
}
180+
181+
let initializers = initializers
182+
.into_iter()
183+
.map(|initializer| (initializer.id, initializer.expr))
184+
.collect::<NodeMap<_>>();
185+
let mut stmts = Vec::with_capacity(move_expr_state.occurrences.len());
186+
for occurrence in &move_expr_state.occurrences {
187+
// Evaluate the expression inside `move(...)` before creating the
188+
// closure/coroutine and store it in a synthetic local:
189+
// `|| move(foo).bar` becomes roughly
190+
// `let __move_expr_0 = foo; || __move_expr_0.bar`.
191+
let expr = initializers[&occurrence.id];
192+
// This state has already been popped, so a nested `move(...)` in
193+
// the initializer is recorded by the immediately enclosing
194+
// closure-like body instead of this one.
195+
let init = self.with_move_expr_initializer(|this| this.lower_expr(expr));
196+
stmts.push(self.stmt_let_pat(
197+
None,
198+
expr.span,
199+
Some(init),
200+
occurrence.pat,
201+
hir::LocalSource::Normal,
202+
));
203+
}
204+
205+
let stmts = self.arena.alloc_from_iter(stmts);
206+
let block = self.block_all(whole_span, stmts, Some(self.arena.alloc(expr)));
207+
self.expr(whole_span, hir::ExprKind::Block(block, None))
208+
}
209+
155210
fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
156211
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
157212
}
@@ -305,19 +360,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
305360
if !self.tcx.features().move_expr() {
306361
return self.expr_err(*move_kw_span, self.dcx().has_errors().unwrap());
307362
}
308-
if let Some(state) = self.move_expr_bindings.last().and_then(Option::as_ref) {
309-
let existing = state.bindings.get(&e.id).copied();
310-
let (ident, binding) = existing.unwrap_or_else(|| {
311-
for nested in MoveExprInitializerFinder::collect(inner) {
312-
self.record_move_expr(
313-
nested.id,
314-
nested.expr,
315-
nested.move_kw_span,
316-
false,
317-
);
318-
}
319-
self.record_move_expr(e.id, inner, *move_kw_span, true)
320-
});
363+
if self.move_expr_bindings.last().is_some_and(Option::is_some) {
364+
let (ident, binding) = self.record_move_expr(e.id, inner, *move_kw_span);
321365
hir::ExprKind::Path(hir::QPath::Resolved(
322366
None,
323367
self.arena.alloc(hir::Path {
@@ -333,9 +377,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
333377
],
334378
}),
335379
))
380+
} else if self.lowering_move_expr_initializer && self.move_expr_bindings.is_empty()
381+
{
382+
let guar = self
383+
.dcx()
384+
.emit_err(NestedMoveExprWithoutEnclosingContext { span: *move_kw_span });
385+
hir::ExprKind::Err(guar)
336386
} else {
337-
let guar =
338-
self.dcx().emit_err(MoveExprOnlyInPlainClosures { span: *move_kw_span });
387+
let guar = self
388+
.dcx()
389+
.emit_err(MoveExprOnlyInSupportedContexts { span: *move_kw_span });
339390
hir::ExprKind::Err(guar)
340391
}
341392
}
@@ -346,22 +397,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
346397
CoroutineKind::Gen => hir::CoroutineDesugaring::Gen,
347398
CoroutineKind::AsyncGen => hir::CoroutineDesugaring::AsyncGen,
348399
};
349-
self.make_desugared_coroutine_expr(
350-
*capture_clause,
351-
e.id,
352-
None,
353-
*decl_span,
400+
let (kind, move_expr_state) =
401+
self.with_move_expr_bindings(Some(MoveExprState::default()), |this| {
402+
this.make_desugared_coroutine_expr(
403+
*capture_clause,
404+
e.id,
405+
None,
406+
*decl_span,
407+
e.span,
408+
desugaring_kind,
409+
hir::CoroutineSource::Block,
410+
|this| {
411+
this.with_new_scopes(e.span, |this| this.lower_block_expr(block))
412+
},
413+
)
414+
});
415+
let Some(move_expr_state) = move_expr_state else {
416+
span_bug!(
417+
*decl_span,
418+
"coroutine block lowering did not return `move(...)` state"
419+
);
420+
};
421+
let expr = hir::Expr { hir_id: expr_hir_id, kind, span };
422+
return self.lower_expr_with_move_exprs_in_block(
423+
expr,
424+
move_expr_state,
425+
block,
354426
e.span,
355-
desugaring_kind,
356-
hir::CoroutineSource::Block,
357-
|this| {
358-
this.with_new_scopes(e.span, |this| {
359-
let (expr, _) = this
360-
.with_move_expr_bindings(None, |this| this.lower_block_expr(block));
361-
expr
362-
})
363-
},
364-
)
427+
);
365428
}
366429
ExprKind::Block(blk, opt_label) => {
367430
// Different from loops, label of block resolves to block id rather than
@@ -865,6 +928,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
865928
(params, res)
866929
});
867930

931+
let explicit_captures: &'hir [hir::ExplicitCapture] = match coroutine_source {
932+
hir::CoroutineSource::Block
933+
if let Some(move_expr_state) =
934+
self.move_expr_bindings.last().and_then(Option::as_ref) =>
935+
{
936+
self.arena.alloc_from_iter(
937+
move_expr_state
938+
.occurrences
939+
.iter()
940+
.map(|occurrence| hir::ExplicitCapture { var_hir_id: occurrence.binding }),
941+
)
942+
}
943+
_ => &[],
944+
};
945+
868946
// `static |<_task_context?>| -> <return_ty> { <body> }`:
869947
hir::ExprKind::Closure(self.arena.alloc(hir::Closure {
870948
def_id: closure_def_id,
@@ -877,7 +955,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
877955
fn_arg_span: None,
878956
kind: hir::ClosureKind::Coroutine(coroutine_kind),
879957
constness: hir::Constness::NotConst,
880-
explicit_captures: &[],
958+
explicit_captures,
881959
}))
882960
}
883961

0 commit comments

Comments
 (0)