Teach rustfmt to handle postfix yield

This commit is contained in:
Eric Holk 2025-03-13 14:36:02 -07:00
parent 1c0916a2b3
commit 635eae2d4f
No known key found for this signature in database
GPG key ID: F1A772BB658A63E1
4 changed files with 33 additions and 4 deletions

View file

@ -201,7 +201,8 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
(Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lt, rt),
(Block(lb, ll), Block(rb, rl)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lb, rb),
(TryBlock(l), TryBlock(r)) => eq_block(l, r),
(Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l.as_ref(), r.as_ref()),
(Yield(l, lk), Yield(r, rk)) => eq_expr_opt(l.as_ref(), r.as_ref()) && lk == rk,
(Ret(l), Ret(r)) => eq_expr_opt(l.as_ref(), r.as_ref()),
(Break(ll, le), Break(rl, re)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_expr_opt(le.as_ref(), re.as_ref()),
(Continue(ll), Continue(rl)) => eq_label(ll.as_ref(), rl.as_ref()),
(Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2, _), Index(r1, r2, _)) => {
@ -688,7 +689,7 @@ pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
use WherePredicateKind::*;
over(&l.attrs, &r.attrs, eq_attr)
over(&l.attrs, &r.attrs, eq_attr)
&& match (&l.kind, &r.kind) {
(BoundPredicate(l), BoundPredicate(r)) => {
over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {

View file

@ -221,7 +221,7 @@ pub(crate) fn format_expr(
Ok(format!("break{id_str}"))
}
}
ast::ExprKind::Yield(ref opt_expr) => {
ast::ExprKind::Yield(ref opt_expr, ast::YieldKind::Prefix) => {
if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context, "yield ", &**expr, shape)
} else {
@ -243,7 +243,8 @@ pub(crate) fn format_expr(
ast::ExprKind::Try(..)
| ast::ExprKind::Field(..)
| ast::ExprKind::MethodCall(..)
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape),
| ast::ExprKind::Await(_, _)
| ast::ExprKind::Yield(_, ast::YieldKind::Postfix) => rewrite_chain(expr, context, shape),
ast::ExprKind::MacCall(ref mac) => {
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|_| {
wrap_str(

View file

@ -0,0 +1,15 @@
// This demonstrates a proposed alternate or additional option of having yield in postfix position.
//@ edition: 2024
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::pin;
fn main() {
let mut coro =
pin!(#[coroutine] |_: i32| { let x = 1.yield;
(x + 2).yield; });
}

View file

@ -0,0 +1,12 @@
// This demonstrates a proposed alternate or additional option of having yield in postfix position.
//@ edition: 2024
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::pin;
fn main() {
let mut coro =
pin!(#[coroutine] |_: i32| { let x = 1.yield; (x + 2).yield; });
}