Skip to content

Introduce move expressions (move($expr)) #155023

Open
TaKO8Ki wants to merge 17 commits intorust-lang:mainfrom
TaKO8Ki:move-expr-1
Open

Introduce move expressions (move($expr)) #155023
TaKO8Ki wants to merge 17 commits intorust-lang:mainfrom
TaKO8Ki:move-expr-1

Conversation

@TaKO8Ki
Copy link
Copy Markdown
Member

@TaKO8Ki TaKO8Ki commented Apr 9, 2026

View all comments

This is an experimental first version of move expressions.

This first version implements it just in plain closures. A support for coroutine closures will be added in follow up pull requests.

RFC: will be added later
Tracking issue: #155050
Project goal:

r? @nikomatsakis

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 9, 2026
@rustbot rustbot added T-clippy Relevant to the Clippy team. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Apr 9, 2026
@rust-lang rust-lang deleted a comment from rust-log-analyzer Apr 9, 2026
@rust-lang rust-lang deleted a comment from rust-log-analyzer Apr 9, 2026
@rust-lang rust-lang deleted a comment from rust-log-analyzer Apr 9, 2026
@TaKO8Ki TaKO8Ki changed the title Move expressions (move($expr)) Introduce move expressions (move($expr)) Apr 9, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@TaKO8Ki TaKO8Ki marked this pull request as ready for review April 16, 2026 12:26
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 16, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 16, 2026

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 16, 2026
Comment thread compiler/rustc_ast_passes/src/feature_gate.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 16, 2026
Comment thread tests/ui/feature-gates/feature-gate-move_expr.stderr
Comment thread compiler/rustc_ast_lowering/src/expr.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/expr.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/expr.rs
Comment thread compiler/rustc_ast_lowering/src/expr.rs
Comment thread compiler/rustc_hir/src/hir.rs Outdated
Comment thread tests/ui/move-expr/plain-closure.rs
Comment thread compiler/rustc_hir_typeck/src/upvar.rs
Comment thread compiler/rustc_hir_typeck/src/upvar.rs
Comment thread compiler/rustc_hir_typeck/src/upvar.rs Outdated
@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@TaKO8Ki TaKO8Ki requested a review from nikomatsakis April 30, 2026 12:26
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 30, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 30, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_ast_lowering/src/expr.rs
Comment thread compiler/rustc_ast_lowering/src/expr.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/expr.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/lib.rs Outdated
allow_async_fn_traits: Arc<[Symbol]>,

delayed_lints: Vec<DelayedLint>,
move_expr_bindings: Vec<NodeMap<(Ident, HirId)>>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

under my proposed change, this would be Vec<Option<NodeMap<_>>>

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 30, 2026
@rust-bors

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 7, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@TaKO8Ki TaKO8Ki requested a review from nikomatsakis May 7, 2026 13:46
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 7, 2026
@rust-log-analyzer

This comment has been minimized.

Copy link
Copy Markdown
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Left a few nits.

View changes since this review

fn_decl_span: Span,
fn_arg_span: Span,
whole_span: Span,
) -> hir::Expr<'hir> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would still like a comment :)

self.expr(whole_span, hir::ExprKind::Block(block, None))
}

fn lower_expr_closure(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these methods with very similar names (lower_expr_closure_expr, lower_expr_closure, etc) could use with some comments showing how they relate to one another

allow_async_fn_traits: Arc<[Symbol]>,

delayed_lints: Vec<DelayedLint>,
/// Stack of `move(...)` collection states. A plain closure body pushes
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Stack of `move(...)` collection states. A plain closure body pushes
/// Stack of `move(...)` collection states. A plain closure body pushes

assert_eq!(x, "hello");
};

outer();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
outer();
// `outer` captures `x` by *reference* (and `inner` takes ownership of a clone)
println!("{x");
outer();

I expect that this will wind up with outer capturing x by ref, right? let's add something to demonstrate this by having println!("{x}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also want a compile-fail test like

let mut x = String::from("hello");

let outer = || {
    let inner = || move(x.clone());
    let y = inner();
    assert_eq!(y, "hello");
    assert_eq!(x, "hello");
};

x.push("more test"); //~ ERROR

outer();

#![feature(move_expr)]

fn main() {
let x = String::from("hello");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let x = String::from("hello");
let x = Arc::new(String::from("hello"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could try printing the ref count here and there to check?

c();

let v = "Hello, Ferris".to_string();
let r = || {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be its own file


fn main() {
let s = vec![1, 2, 3];
let c = || {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add coroutine tests (which will error for now), unless they already exist.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants