Skip to content

Commit 9b0e7f6

Browse files
committed
Teach rustfmt to handle postfix yield
This involved fixing the span when parsing .yield
1 parent c5093ac commit 9b0e7f6

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,9 +1315,8 @@ impl<'a> Parser<'a> {
13151315
if self.eat_keyword(exp!(Yield)) {
13161316
let yield_span = self.prev_token.span;
13171317
self.psess.gated_spans.gate(sym::yield_expr, yield_span);
1318-
return Ok(
1319-
self.mk_expr(yield_span, ExprKind::Yield(Some(self_arg), YieldKind::Postfix))
1320-
);
1318+
return Ok(self
1319+
.mk_expr(lo.to(yield_span), ExprKind::Yield(Some(self_arg), YieldKind::Postfix)));
13211320
}
13221321

13231322
let fn_span_lo = self.token.span;

src/tools/rustfmt/src/chains.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ enum ChainItemKind {
192192
StructField(symbol::Ident),
193193
TupleField(symbol::Ident, bool),
194194
Await,
195+
Yield,
195196
Comment(String, CommentPosition),
196197
}
197198

@@ -203,6 +204,7 @@ impl ChainItemKind {
203204
| ChainItemKind::StructField(..)
204205
| ChainItemKind::TupleField(..)
205206
| ChainItemKind::Await
207+
| ChainItemKind::Yield
206208
| ChainItemKind::Comment(..) => false,
207209
}
208210
}
@@ -257,6 +259,10 @@ impl ChainItemKind {
257259
let span = mk_sp(nested.span.hi(), expr.span.hi());
258260
(ChainItemKind::Await, span)
259261
}
262+
ast::ExprKind::Yield(Some(ref nested), ast::YieldKind::Postfix) => {
263+
let span = mk_sp(nested.span.hi(), expr.span.hi());
264+
(ChainItemKind::Yield, span)
265+
}
260266
_ => {
261267
return (
262268
ChainItemKind::Parent {
@@ -306,6 +312,7 @@ impl Rewrite for ChainItem {
306312
rewrite_ident(context, ident)
307313
),
308314
ChainItemKind::Await => ".await".to_owned(),
315+
ChainItemKind::Yield => ".yield".to_owned(),
309316
ChainItemKind::Comment(ref comment, _) => {
310317
rewrite_comment(comment, false, shape, context.config)?
311318
}
@@ -508,7 +515,8 @@ impl Chain {
508515
}),
509516
ast::ExprKind::Field(ref subexpr, _)
510517
| ast::ExprKind::Try(ref subexpr)
511-
| ast::ExprKind::Await(ref subexpr, _) => Some(SubExpr {
518+
| ast::ExprKind::Await(ref subexpr, _)
519+
| ast::ExprKind::Yield(Some(ref subexpr), ast::YieldKind::Postfix) => Some(SubExpr {
512520
expr: Self::convert_try(subexpr, context),
513521
is_method_call_receiver: false,
514522
}),

src/tools/rustfmt/src/utils.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::ast::{
44
self, Attribute, MetaItem, MetaItemInner, MetaItemKind, NodeId, Path, Visibility,
55
VisibilityKind,
66
};
7-
use rustc_ast::ptr;
7+
use rustc_ast::{YieldKind, ptr};
88
use rustc_ast_pretty::pprust;
99
use rustc_span::{BytePos, LocalExpnId, Span, Symbol, SyntaxContext, sym, symbol};
1010
use unicode_width::UnicodeWidthStr;
@@ -485,7 +485,9 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
485485
| ast::ExprKind::Index(_, ref expr, _)
486486
| ast::ExprKind::Unary(_, ref expr)
487487
| ast::ExprKind::Try(ref expr)
488-
| ast::ExprKind::Yield(Some(ref expr), _) => is_block_expr(context, expr, repr),
488+
| ast::ExprKind::Yield(Some(ref expr), YieldKind::Prefix) => {
489+
is_block_expr(context, expr, repr)
490+
}
489491
ast::ExprKind::Closure(ref closure) => is_block_expr(context, &closure.body, repr),
490492
// This can only be a string lit
491493
ast::ExprKind::Lit(_) => {
@@ -515,7 +517,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
515517
| ast::ExprKind::Tup(..)
516518
| ast::ExprKind::Use(..)
517519
| ast::ExprKind::Type(..)
518-
| ast::ExprKind::Yield(None, _)
520+
| ast::ExprKind::Yield(_, _)
519521
| ast::ExprKind::Underscore => false,
520522
}
521523
}

src/tools/rustfmt/tests/source/postfix-yield.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/tools/rustfmt/tests/target/postfix-yield.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ use std::ops::{Coroutine, CoroutineState};
77
use std::pin::pin;
88

99
fn main() {
10-
let mut coro =
11-
pin!(#[coroutine] |_: i32| { let x = 1.yield; (x + 2).yield; });
10+
let mut coro = pin!(
11+
#[coroutine]
12+
|_: i32| {
13+
let x = 1.yield;
14+
(x + 2).await;
15+
}
16+
);
1217
}

0 commit comments

Comments
 (0)