Skip to content

Add #![feature(ergonomic_clones)] formatting #6532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ enum ChainItemKind {
StructField(symbol::Ident),
TupleField(symbol::Ident, bool),
Await,
Use,
Yield,
Comment(String, CommentPosition),
}
Expand All @@ -207,6 +208,7 @@ impl ChainItemKind {
| ChainItemKind::StructField(..)
| ChainItemKind::TupleField(..)
| ChainItemKind::Await
| ChainItemKind::Use
| ChainItemKind::Yield
| ChainItemKind::Comment(..) => false,
}
Expand Down Expand Up @@ -262,6 +264,10 @@ impl ChainItemKind {
let span = mk_sp(nested.span.hi(), expr.span.hi());
(ChainItemKind::Await, span)
}
ast::ExprKind::Use(ref nested, _) => {
let span = mk_sp(nested.span.hi(), expr.span.hi());
(ChainItemKind::Use, span)
}
ast::ExprKind::Yield(ast::YieldKind::Postfix(ref nested)) => {
let span = mk_sp(nested.span.hi(), expr.span.hi());
(ChainItemKind::Yield, span)
Expand Down Expand Up @@ -313,6 +319,7 @@ impl Rewrite for ChainItem {
rewrite_ident(context, ident)
),
ChainItemKind::Await => ".await".to_owned(),
ChainItemKind::Use => ".use".to_owned(),
ChainItemKind::Yield => ".yield".to_owned(),
ChainItemKind::Comment(ref comment, _) => {
rewrite_comment(comment, false, shape, context.config)?
Expand Down Expand Up @@ -517,6 +524,7 @@ impl Chain {
ast::ExprKind::Field(ref subexpr, _)
| ast::ExprKind::Try(ref subexpr)
| ast::ExprKind::Await(ref subexpr, _)
| ast::ExprKind::Use(ref subexpr, _)
| ast::ExprKind::Yield(ast::YieldKind::Postfix(ref subexpr)) => Some(SubExpr {
expr: Self::convert_try(subexpr, context),
is_method_call_receiver: false,
Expand Down
12 changes: 6 additions & 6 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ fn rewrite_closure_fn_decl(
Some(ast::CoroutineKind::AsyncGen { .. }) => "async gen ",
None => "",
};
let mover = if matches!(capture, ast::CaptureBy::Value { .. }) {
"move "
} else {
""
let capture_str = match capture {
ast::CaptureBy::Value { .. } => "move ",
ast::CaptureBy::Use { .. } => "use ",
ast::CaptureBy::Ref => "",
};
// 4 = "|| {".len(), which is overconservative when the closure consists of
// a single expression.
let offset = binder.len() + const_.len() + immovable.len() + coro.len() + mover.len();
let offset = binder.len() + const_.len() + immovable.len() + coro.len() + capture_str.len();
let nested_shape = shape.shrink_left(offset, span)?.sub_width(4, span)?;

// 1 = |
Expand Down Expand Up @@ -339,7 +339,7 @@ fn rewrite_closure_fn_decl(
.tactic(tactic)
.preserve_newline(true);
let list_str = write_list(&item_vec, &fmt)?;
let mut prefix = format!("{binder}{const_}{immovable}{coro}{mover}|{list_str}|");
let mut prefix = format!("{binder}{const_}{immovable}{coro}{capture_str}|{list_str}|");

if !ret_str.is_empty() {
if prefix.contains('\n') {
Expand Down
5 changes: 1 addition & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ pub(crate) fn format_expr(
ast::ExprKind::Tup(ref items) => {
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
}
ast::ExprKind::Use(_, _) => {
// FIXME: properly implement this
Ok(context.snippet(expr.span()).to_owned())
}
ast::ExprKind::Let(ref pat, ref expr, _span, _) => rewrite_let(context, shape, pat, expr),
ast::ExprKind::If(..)
| ast::ExprKind::ForLoop { .. }
Expand Down Expand Up @@ -276,6 +272,7 @@ pub(crate) fn format_expr(
| ast::ExprKind::Field(..)
| ast::ExprKind::MethodCall(..)
| ast::ExprKind::Await(_, _)
| ast::ExprKind::Use(_, _)
| ast::ExprKind::Yield(ast::YieldKind::Postfix(_)) => rewrite_chain(expr, context, shape),
ast::ExprKind::MacCall(ref mac) => {
rewrite_macro(mac, context, shape, MacroPosition::Expression).or_else(|_| {
Expand Down
73 changes: 73 additions & 0 deletions tests/source/ergonomic_clones.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// rustfmt-edition: 2018

#![feature(ergonomic_clones)]

mod closure_syntax {
fn ergonomic_clone_closure_no_captures() -> i32 {
let cl = use || {
1
};
cl()
}

fn ergonomic_clone_closure_move() -> String {
let s = String::from("hi");

let cl = use || {
s
};
cl()
}

fn ergonomic_clone_closure_use_cloned() -> Foo {
let f = Foo;

let f1 = use || {
f
};

let f2 = use || {
f
};

f
}

fn ergonomic_clone_closure_copy() -> i32 {
let i = 1;

let i1 = use || {
i
};

let i2 = use || {
i
};

i
}

fn nested() {
let a = Box::new(Foo);
foo(use || { foo(use || { work(a) }) });
let x = use || { use || { Foo } };
let _y = x();
}

fn with_binders() {
for<'a> use || -> () {};
}
}

mod dotuse {
fn basic_test(x: i32) -> i32 {
x.use
.use
.abs()
}

fn do_not_move_test(x: Foo) -> Foo {
let s = x.use;
x
}
}
59 changes: 59 additions & 0 deletions tests/target/ergonomic_clones.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// rustfmt-edition: 2018

#![feature(ergonomic_clones)]

mod closure_syntax {
fn ergonomic_clone_closure_no_captures() -> i32 {
let cl = use || 1;
cl()
}

fn ergonomic_clone_closure_move() -> String {
let s = String::from("hi");

let cl = use || s;
cl()
}

fn ergonomic_clone_closure_use_cloned() -> Foo {
let f = Foo;

let f1 = use || f;

let f2 = use || f;

f
}

fn ergonomic_clone_closure_copy() -> i32 {
let i = 1;

let i1 = use || i;

let i2 = use || i;

i
}

fn nested() {
let a = Box::new(Foo);
foo(use || foo(use || work(a)));
let x = use || use || Foo;
let _y = x();
}

fn with_binders() {
for<'a> use || -> () {};
}
}

mod dotuse {
fn basic_test(x: i32) -> i32 {
x.use.use.abs()
}

fn do_not_move_test(x: Foo) -> Foo {
let s = x.use;
x
}
}
Loading