Skip to content

Commit e377632

Browse files
committed
Teach rustfmt to handle postfix yield
1 parent d038fb8 commit e377632

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub(crate) fn format_expr(
221221
Ok(format!("break{id_str}"))
222222
}
223223
}
224-
ast::ExprKind::Yield(ref opt_expr) => {
224+
ast::ExprKind::Yield(ref opt_expr, ast::YieldKind::Prefix) => {
225225
if let Some(ref expr) = *opt_expr {
226226
rewrite_unary_prefix(context, "yield ", &**expr, shape)
227227
} else {
@@ -243,7 +243,8 @@ pub(crate) fn format_expr(
243243
ast::ExprKind::Try(..)
244244
| ast::ExprKind::Field(..)
245245
| ast::ExprKind::MethodCall(..)
246-
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape),
246+
| ast::ExprKind::Await(_, _)
247+
| ast::ExprKind::Yield(_, ast::YieldKind::Postfix) => rewrite_chain(expr, context, shape),
247248
ast::ExprKind::MacCall(ref mac) => {
248249
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|_| {
249250
wrap_str(

tests/source/postfix-yield.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This demonstrates a proposed alternate or additional option of having yield in postfix position.
2+
//@ edition: 2024
3+
4+
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr)]
5+
6+
use std::ops::{Coroutine, CoroutineState};
7+
use std::pin::pin;
8+
9+
fn main() {
10+
let mut coro =
11+
pin!(#[coroutine] |_: i32| { let x = 1.yield;
12+
13+
14+
(x + 2).yield; });
15+
}

tests/target/postfix-yield.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This demonstrates a proposed alternate or additional option of having yield in postfix position.
2+
//@ edition: 2024
3+
4+
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr)]
5+
6+
use std::ops::{Coroutine, CoroutineState};
7+
use std::pin::pin;
8+
9+
fn main() {
10+
let mut coro =
11+
pin!(#[coroutine] |_: i32| { let x = 1.yield; (x + 2).yield; });
12+
}

0 commit comments

Comments
 (0)