Preserve yield position during pretty printing

This commit is contained in:
Eric Holk
2025-03-12 16:27:52 -07:00
parent edf65e735c
commit 1c0916a2b3
12 changed files with 55 additions and 18 deletions

View File

@@ -8,7 +8,7 @@ use rustc_ast::util::literal::escape_byte_str_symbol;
use rustc_ast::util::parser::{self, ExprPrecedence, Fixity};
use rustc_ast::{
self as ast, BlockCheckMode, FormatAlignment, FormatArgPosition, FormatArgsPiece, FormatCount,
FormatDebugHex, FormatSign, FormatTrait, token,
FormatDebugHex, FormatSign, FormatTrait, YieldKind, token,
};
use crate::pp::Breaks::Inconsistent;
@@ -761,7 +761,7 @@ impl<'a> State<'a> {
self.print_expr(e, FixupContext::default());
self.pclose();
}
ast::ExprKind::Yield(e) => {
ast::ExprKind::Yield(e, YieldKind::Prefix) => {
self.word("yield");
if let Some(expr) = e {
@@ -773,6 +773,16 @@ impl<'a> State<'a> {
);
}
}
ast::ExprKind::Yield(e, YieldKind::Postfix) => {
// it's not possible to have a postfix yield with no expression.
let e = e.as_ref().unwrap();
self.print_expr_cond_paren(
e,
e.precedence() < ExprPrecedence::Unambiguous,
fixup.leftmost_subexpression_with_dot(),
);
self.word(".yield");
}
ast::ExprKind::Try(e) => {
self.print_expr_cond_paren(
e,