1use std::fmt::Write;
2
3use ast::{ForLoopKind, MatchKind};
4use itertools::{Itertools, Position};
5use rustc_ast::ptr::P;
6use rustc_ast::util::classify;
7use rustc_ast::util::literal::escape_byte_str_symbol;
8use rustc_ast::util::parser::{self, ExprPrecedence, Fixity};
9use rustc_ast::{
10 self as ast, BlockCheckMode, FormatAlignment, FormatArgPosition, FormatArgsPiece, FormatCount,
11 FormatDebugHex, FormatSign, FormatTrait, YieldKind, token,
12};
13
14use crate::pp::Breaks::Inconsistent;
15use crate::pprust::state::fixup::FixupContext;
16use crate::pprust::state::{AnnNode, INDENT_UNIT, PrintState, State};
17
18impl<'a> State<'a> {
19 fn print_else(&mut self, els: Option<&ast::Expr>) {
20 if let Some(_else) = els {
21 match &_else.kind {
22 ast::ExprKind::If(i, then, e) => {
24 let cb = self.cbox(0);
25 let ib = self.ibox(0);
26 self.word(" else if ");
27 self.print_expr_as_cond(i);
28 self.space();
29 self.print_block(then, cb, ib);
30 self.print_else(e.as_deref())
31 }
32 ast::ExprKind::Block(b, None) => {
34 let cb = self.cbox(0);
35 let ib = self.ibox(0);
36 self.word(" else ");
37 self.print_block(b, cb, ib)
38 }
39 _ => {
41 panic!("print_if saw if with weird alternative");
42 }
43 }
44 }
45 }
46
47 fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) {
48 let cb = self.cbox(0);
49 let ib = self.ibox(0);
50 self.word_nbsp("if");
51 self.print_expr_as_cond(test);
52 self.space();
53 self.print_block(blk, cb, ib);
54 self.print_else(elseopt)
55 }
56
57 fn print_call_post(&mut self, args: &[P<ast::Expr>]) {
58 self.popen();
59 self.commasep_exprs(Inconsistent, args);
60 self.pclose()
61 }
62
63 fn print_expr_as_cond(&mut self, expr: &ast::Expr) {
66 self.print_expr_cond_paren(expr, Self::cond_needs_par(expr), FixupContext::new_cond())
67 }
68
69 fn cond_needs_par(expr: &ast::Expr) -> bool {
74 match expr.kind {
75 ast::ExprKind::Break(..)
76 | ast::ExprKind::Closure(..)
77 | ast::ExprKind::Ret(..)
78 | ast::ExprKind::Yeet(..) => true,
79 _ => parser::contains_exterior_struct_lit(expr),
80 }
81 }
82
83 pub(super) fn print_expr_cond_paren(
85 &mut self,
86 expr: &ast::Expr,
87 needs_par: bool,
88 mut fixup: FixupContext,
89 ) {
90 if needs_par {
91 self.popen();
92
93 fixup = FixupContext::default();
105 }
106
107 self.print_expr(expr, fixup);
108
109 if needs_par {
110 self.pclose();
111 }
112 }
113
114 fn print_expr_vec(&mut self, exprs: &[P<ast::Expr>]) {
115 let ib = self.ibox(INDENT_UNIT);
116 self.word("[");
117 self.commasep_exprs(Inconsistent, exprs);
118 self.word("]");
119 self.end(ib);
120 }
121
122 pub(super) fn print_expr_anon_const(
123 &mut self,
124 expr: &ast::AnonConst,
125 attrs: &[ast::Attribute],
126 ) {
127 let ib = self.ibox(INDENT_UNIT);
128 self.word("const");
129 self.nbsp();
130 if let ast::ExprKind::Block(block, None) = &expr.value.kind {
131 let cb = self.cbox(0);
132 let ib = self.ibox(0);
133 self.print_block_with_attrs(block, attrs, cb, ib);
134 } else {
135 self.print_expr(&expr.value, FixupContext::default());
136 }
137 self.end(ib);
138 }
139
140 fn print_expr_repeat(&mut self, element: &ast::Expr, count: &ast::AnonConst) {
141 let ib = self.ibox(INDENT_UNIT);
142 self.word("[");
143 self.print_expr(element, FixupContext::default());
144 self.word_space(";");
145 self.print_expr(&count.value, FixupContext::default());
146 self.word("]");
147 self.end(ib);
148 }
149
150 fn print_expr_struct(
151 &mut self,
152 qself: &Option<P<ast::QSelf>>,
153 path: &ast::Path,
154 fields: &[ast::ExprField],
155 rest: &ast::StructRest,
156 ) {
157 if let Some(qself) = qself {
158 self.print_qpath(path, qself, true);
159 } else {
160 self.print_path(path, true, 0);
161 }
162 self.nbsp();
163 self.word("{");
164 let has_rest = match rest {
165 ast::StructRest::Base(_) | ast::StructRest::Rest(_) => true,
166 ast::StructRest::None => false,
167 };
168 if fields.is_empty() && !has_rest {
169 self.word("}");
170 return;
171 }
172 let cb = self.cbox(0);
173 for (pos, field) in fields.iter().with_position() {
174 let is_first = matches!(pos, Position::First | Position::Only);
175 let is_last = matches!(pos, Position::Last | Position::Only);
176 self.maybe_print_comment(field.span.hi());
177 self.print_outer_attributes(&field.attrs);
178 if is_first {
179 self.space_if_not_bol();
180 }
181 if !field.is_shorthand {
182 self.print_ident(field.ident);
183 self.word_nbsp(":");
184 }
185 self.print_expr(&field.expr, FixupContext::default());
186 if !is_last || has_rest {
187 self.word_space(",");
188 } else {
189 self.trailing_comma_or_space();
190 }
191 }
192 if has_rest {
193 if fields.is_empty() {
194 self.space();
195 }
196 self.word("..");
197 if let ast::StructRest::Base(expr) = rest {
198 self.print_expr(expr, FixupContext::default());
199 }
200 self.space();
201 }
202 self.offset(-INDENT_UNIT);
203 self.end(cb);
204 self.word("}");
205 }
206
207 fn print_expr_tup(&mut self, exprs: &[P<ast::Expr>]) {
208 self.popen();
209 self.commasep_exprs(Inconsistent, exprs);
210 if exprs.len() == 1 {
211 self.word(",");
212 }
213 self.pclose()
214 }
215
216 fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>], fixup: FixupContext) {
217 let needs_paren = match func.kind {
218 ast::ExprKind::Field(_, name) => !name.is_numeric(),
221 _ => func.precedence() < ExprPrecedence::Unambiguous,
222 };
223
224 self.print_expr_cond_paren(func, needs_paren, fixup.leftmost_subexpression());
241
242 self.print_call_post(args)
243 }
244
245 fn print_expr_method_call(
246 &mut self,
247 segment: &ast::PathSegment,
248 receiver: &ast::Expr,
249 base_args: &[P<ast::Expr>],
250 fixup: FixupContext,
251 ) {
252 self.print_expr_cond_paren(
264 receiver,
265 receiver.precedence() < ExprPrecedence::Unambiguous,
266 fixup.leftmost_subexpression_with_dot(),
267 );
268
269 self.word(".");
270 self.print_ident(segment.ident);
271 if let Some(args) = &segment.args {
272 self.print_generic_args(args, true);
273 }
274 self.print_call_post(base_args)
275 }
276
277 fn print_expr_binary(
278 &mut self,
279 op: ast::BinOpKind,
280 lhs: &ast::Expr,
281 rhs: &ast::Expr,
282 fixup: FixupContext,
283 ) {
284 let binop_prec = op.precedence();
285 let left_prec = lhs.precedence();
286 let right_prec = rhs.precedence();
287
288 let (mut left_needs_paren, right_needs_paren) = match op.fixity() {
289 Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
290 Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
291 Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),
292 };
293
294 match (&lhs.kind, op) {
295 (&ast::ExprKind::Cast { .. }, ast::BinOpKind::Lt | ast::BinOpKind::Shl) => {
299 left_needs_paren = true;
300 }
301 (&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(binop_prec) => {
310 left_needs_paren = true;
311 }
312 _ => {}
313 }
314
315 self.print_expr_cond_paren(lhs, left_needs_paren, fixup.leftmost_subexpression());
316 self.space();
317 self.word_space(op.as_str());
318 self.print_expr_cond_paren(rhs, right_needs_paren, fixup.subsequent_subexpression());
319 }
320
321 fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr, fixup: FixupContext) {
322 self.word(op.as_str());
323 self.print_expr_cond_paren(
324 expr,
325 expr.precedence() < ExprPrecedence::Prefix,
326 fixup.subsequent_subexpression(),
327 );
328 }
329
330 fn print_expr_addr_of(
331 &mut self,
332 kind: ast::BorrowKind,
333 mutability: ast::Mutability,
334 expr: &ast::Expr,
335 fixup: FixupContext,
336 ) {
337 self.word("&");
338 match kind {
339 ast::BorrowKind::Ref => self.print_mutability(mutability, false),
340 ast::BorrowKind::Raw => {
341 self.word_nbsp("raw");
342 self.print_mutability(mutability, true);
343 }
344 }
345 self.print_expr_cond_paren(
346 expr,
347 expr.precedence() < ExprPrecedence::Prefix,
348 fixup.subsequent_subexpression(),
349 );
350 }
351
352 pub(super) fn print_expr(&mut self, expr: &ast::Expr, fixup: FixupContext) {
353 self.print_expr_outer_attr_style(expr, true, fixup)
354 }
355
356 pub(super) fn print_expr_outer_attr_style(
357 &mut self,
358 expr: &ast::Expr,
359 is_inline: bool,
360 mut fixup: FixupContext,
361 ) {
362 self.maybe_print_comment(expr.span.lo());
363
364 let attrs = &expr.attrs;
365 if is_inline {
366 self.print_outer_attributes_inline(attrs);
367 } else {
368 self.print_outer_attributes(attrs);
369 }
370
371 let ib = self.ibox(INDENT_UNIT);
372
373 let needs_par = fixup.would_cause_statement_boundary(expr);
385 if needs_par {
386 self.popen();
387 fixup = FixupContext::default();
388 }
389
390 self.ann.pre(self, AnnNode::Expr(expr));
391
392 match &expr.kind {
393 ast::ExprKind::Array(exprs) => {
394 self.print_expr_vec(exprs);
395 }
396 ast::ExprKind::ConstBlock(anon_const) => {
397 self.print_expr_anon_const(anon_const, attrs);
398 }
399 ast::ExprKind::Repeat(element, count) => {
400 self.print_expr_repeat(element, count);
401 }
402 ast::ExprKind::Struct(se) => {
403 self.print_expr_struct(&se.qself, &se.path, &se.fields, &se.rest);
404 }
405 ast::ExprKind::Tup(exprs) => {
406 self.print_expr_tup(exprs);
407 }
408 ast::ExprKind::Call(func, args) => {
409 self.print_expr_call(func, args, fixup);
410 }
411 ast::ExprKind::MethodCall(box ast::MethodCall { seg, receiver, args, .. }) => {
412 self.print_expr_method_call(seg, receiver, args, fixup);
413 }
414 ast::ExprKind::Binary(op, lhs, rhs) => {
415 self.print_expr_binary(op.node, lhs, rhs, fixup);
416 }
417 ast::ExprKind::Unary(op, expr) => {
418 self.print_expr_unary(*op, expr, fixup);
419 }
420 ast::ExprKind::AddrOf(k, m, expr) => {
421 self.print_expr_addr_of(*k, *m, expr, fixup);
422 }
423 ast::ExprKind::Lit(token_lit) => {
424 self.print_token_literal(*token_lit, expr.span);
425 }
426 ast::ExprKind::IncludedBytes(bytes) => {
427 let lit = token::Lit::new(token::ByteStr, escape_byte_str_symbol(bytes), None);
428 self.print_token_literal(lit, expr.span)
429 }
430 ast::ExprKind::Cast(expr, ty) => {
431 self.print_expr_cond_paren(
432 expr,
433 expr.precedence() < ExprPrecedence::Cast,
434 fixup.leftmost_subexpression(),
435 );
436 self.space();
437 self.word_space("as");
438 self.print_type(ty);
439 }
440 ast::ExprKind::Type(expr, ty) => {
441 self.word("builtin # type_ascribe");
442 self.popen();
443 let ib = self.ibox(0);
444 self.print_expr(expr, FixupContext::default());
445
446 self.word(",");
447 self.space_if_not_bol();
448 self.print_type(ty);
449
450 self.end(ib);
451 self.pclose();
452 }
453 ast::ExprKind::Let(pat, scrutinee, _, _) => {
454 self.print_let(pat, scrutinee, fixup);
455 }
456 ast::ExprKind::If(test, blk, elseopt) => self.print_if(test, blk, elseopt.as_deref()),
457 ast::ExprKind::While(test, blk, opt_label) => {
458 if let Some(label) = opt_label {
459 self.print_ident(label.ident);
460 self.word_space(":");
461 }
462 let cb = self.cbox(0);
463 let ib = self.ibox(0);
464 self.word_nbsp("while");
465 self.print_expr_as_cond(test);
466 self.space();
467 self.print_block_with_attrs(blk, attrs, cb, ib);
468 }
469 ast::ExprKind::ForLoop { pat, iter, body, label, kind } => {
470 if let Some(label) = label {
471 self.print_ident(label.ident);
472 self.word_space(":");
473 }
474 let cb = self.cbox(0);
475 let ib = self.ibox(0);
476 self.word_nbsp("for");
477 if kind == &ForLoopKind::ForAwait {
478 self.word_nbsp("await");
479 }
480 self.print_pat(pat);
481 self.space();
482 self.word_space("in");
483 self.print_expr_as_cond(iter);
484 self.space();
485 self.print_block_with_attrs(body, attrs, cb, ib);
486 }
487 ast::ExprKind::Loop(blk, opt_label, _) => {
488 let cb = self.cbox(0);
489 let ib = self.ibox(0);
490 if let Some(label) = opt_label {
491 self.print_ident(label.ident);
492 self.word_space(":");
493 }
494 self.word_nbsp("loop");
495 self.print_block_with_attrs(blk, attrs, cb, ib);
496 }
497 ast::ExprKind::Match(expr, arms, match_kind) => {
498 let cb = self.cbox(0);
499 let ib = self.ibox(0);
500
501 match match_kind {
502 MatchKind::Prefix => {
503 self.word_nbsp("match");
504 self.print_expr_as_cond(expr);
505 self.space();
506 }
507 MatchKind::Postfix => {
508 self.print_expr_cond_paren(
509 expr,
510 expr.precedence() < ExprPrecedence::Unambiguous,
511 fixup.leftmost_subexpression_with_dot(),
512 );
513 self.word_nbsp(".match");
514 }
515 }
516
517 self.bopen(ib);
518 self.print_inner_attributes_no_trailing_hardbreak(attrs);
519 for arm in arms {
520 self.print_arm(arm);
521 }
522 let empty = attrs.is_empty() && arms.is_empty();
523 self.bclose(expr.span, empty, cb);
524 }
525 ast::ExprKind::Closure(box ast::Closure {
526 binder,
527 capture_clause,
528 constness,
529 coroutine_kind,
530 movability,
531 fn_decl,
532 body,
533 fn_decl_span: _,
534 fn_arg_span: _,
535 }) => {
536 self.print_closure_binder(binder);
537 self.print_constness(*constness);
538 self.print_movability(*movability);
539 coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
540 self.print_capture_clause(*capture_clause);
541
542 self.print_fn_params_and_ret(fn_decl, true);
543 self.space();
544 self.print_expr(body, FixupContext::default());
545 }
546 ast::ExprKind::Block(blk, opt_label) => {
547 if let Some(label) = opt_label {
548 self.print_ident(label.ident);
549 self.word_space(":");
550 }
551 let cb = self.cbox(0);
553 let ib = self.ibox(0);
555 self.print_block_with_attrs(blk, attrs, cb, ib);
556 }
557 ast::ExprKind::Gen(capture_clause, blk, kind, _decl_span) => {
558 self.word_nbsp(kind.modifier());
559 self.print_capture_clause(*capture_clause);
560 let cb = self.cbox(0);
562 let ib = self.ibox(0);
563 self.print_block_with_attrs(blk, attrs, cb, ib);
564 }
565 ast::ExprKind::Await(expr, _) => {
566 self.print_expr_cond_paren(
567 expr,
568 expr.precedence() < ExprPrecedence::Unambiguous,
569 fixup.leftmost_subexpression_with_dot(),
570 );
571 self.word(".await");
572 }
573 ast::ExprKind::Use(expr, _) => {
574 self.print_expr_cond_paren(
575 expr,
576 expr.precedence() < ExprPrecedence::Unambiguous,
577 fixup,
578 );
579 self.word(".use");
580 }
581 ast::ExprKind::Assign(lhs, rhs, _) => {
582 self.print_expr_cond_paren(
583 lhs,
584 lhs.precedence() <= ExprPrecedence::Range,
587 fixup.leftmost_subexpression(),
588 );
589 self.space();
590 self.word_space("=");
591 self.print_expr_cond_paren(
592 rhs,
593 rhs.precedence() < ExprPrecedence::Assign,
594 fixup.subsequent_subexpression(),
595 );
596 }
597 ast::ExprKind::AssignOp(op, lhs, rhs) => {
598 self.print_expr_cond_paren(
599 lhs,
600 lhs.precedence() <= ExprPrecedence::Range,
601 fixup.leftmost_subexpression(),
602 );
603 self.space();
604 self.word_space(op.node.as_str());
605 self.print_expr_cond_paren(
606 rhs,
607 rhs.precedence() < ExprPrecedence::Assign,
608 fixup.subsequent_subexpression(),
609 );
610 }
611 ast::ExprKind::Field(expr, ident) => {
612 self.print_expr_cond_paren(
613 expr,
614 expr.precedence() < ExprPrecedence::Unambiguous,
615 fixup.leftmost_subexpression_with_dot(),
616 );
617 self.word(".");
618 self.print_ident(*ident);
619 }
620 ast::ExprKind::Index(expr, index, _) => {
621 self.print_expr_cond_paren(
622 expr,
623 expr.precedence() < ExprPrecedence::Unambiguous,
624 fixup.leftmost_subexpression(),
625 );
626 self.word("[");
627 self.print_expr(index, FixupContext::default());
628 self.word("]");
629 }
630 ast::ExprKind::Range(start, end, limits) => {
631 let fake_prec = ExprPrecedence::LOr;
636 if let Some(e) = start {
637 self.print_expr_cond_paren(
638 e,
639 e.precedence() < fake_prec,
640 fixup.leftmost_subexpression(),
641 );
642 }
643 match limits {
644 ast::RangeLimits::HalfOpen => self.word(".."),
645 ast::RangeLimits::Closed => self.word("..="),
646 }
647 if let Some(e) = end {
648 self.print_expr_cond_paren(
649 e,
650 e.precedence() < fake_prec,
651 fixup.subsequent_subexpression(),
652 );
653 }
654 }
655 ast::ExprKind::Underscore => self.word("_"),
656 ast::ExprKind::Path(None, path) => self.print_path(path, true, 0),
657 ast::ExprKind::Path(Some(qself), path) => self.print_qpath(path, qself, true),
658 ast::ExprKind::Break(opt_label, opt_expr) => {
659 self.word("break");
660 if let Some(label) = opt_label {
661 self.space();
662 self.print_ident(label.ident);
663 }
664 if let Some(expr) = opt_expr {
665 self.space();
666 self.print_expr_cond_paren(
667 expr,
668 expr.precedence() < ExprPrecedence::Jump
671 || (opt_label.is_none() && classify::leading_labeled_expr(expr)),
672 fixup.subsequent_subexpression(),
673 );
674 }
675 }
676 ast::ExprKind::Continue(opt_label) => {
677 self.word("continue");
678 if let Some(label) = opt_label {
679 self.space();
680 self.print_ident(label.ident);
681 }
682 }
683 ast::ExprKind::Ret(result) => {
684 self.word("return");
685 if let Some(expr) = result {
686 self.word(" ");
687 self.print_expr_cond_paren(
688 expr,
689 expr.precedence() < ExprPrecedence::Jump,
690 fixup.subsequent_subexpression(),
691 );
692 }
693 }
694 ast::ExprKind::Yeet(result) => {
695 self.word("do");
696 self.word(" ");
697 self.word("yeet");
698 if let Some(expr) = result {
699 self.word(" ");
700 self.print_expr_cond_paren(
701 expr,
702 expr.precedence() < ExprPrecedence::Jump,
703 fixup.subsequent_subexpression(),
704 );
705 }
706 }
707 ast::ExprKind::Become(result) => {
708 self.word("become");
709 self.word(" ");
710 self.print_expr_cond_paren(
711 result,
712 result.precedence() < ExprPrecedence::Jump,
713 fixup.subsequent_subexpression(),
714 );
715 }
716 ast::ExprKind::InlineAsm(a) => {
717 self.word("asm!");
719 self.print_inline_asm(a);
720 }
721 ast::ExprKind::FormatArgs(fmt) => {
722 self.word("format_args!");
724 self.popen();
725 let ib = self.ibox(0);
726 self.word(reconstruct_format_args_template_string(&fmt.template));
727 for arg in fmt.arguments.all_args() {
728 self.word_space(",");
729 self.print_expr(&arg.expr, FixupContext::default());
730 }
731 self.end(ib);
732 self.pclose();
733 }
734 ast::ExprKind::OffsetOf(container, fields) => {
735 self.word("builtin # offset_of");
736 self.popen();
737 let ib = self.ibox(0);
738 self.print_type(container);
739 self.word(",");
740 self.space();
741
742 if let Some((&first, rest)) = fields.split_first() {
743 self.print_ident(first);
744
745 for &field in rest {
746 self.word(".");
747 self.print_ident(field);
748 }
749 }
750 self.end(ib);
751 self.pclose();
752 }
753 ast::ExprKind::MacCall(m) => self.print_mac(m),
754 ast::ExprKind::Paren(e) => {
755 self.popen();
756 self.print_expr(e, FixupContext::default());
757 self.pclose();
758 }
759 ast::ExprKind::Yield(YieldKind::Prefix(e)) => {
760 self.word("yield");
761
762 if let Some(expr) = e {
763 self.space();
764 self.print_expr_cond_paren(
765 expr,
766 expr.precedence() < ExprPrecedence::Jump,
767 fixup.subsequent_subexpression(),
768 );
769 }
770 }
771 ast::ExprKind::Yield(YieldKind::Postfix(e)) => {
772 self.print_expr_cond_paren(
773 e,
774 e.precedence() < ExprPrecedence::Unambiguous,
775 fixup.leftmost_subexpression_with_dot(),
776 );
777 self.word(".yield");
778 }
779 ast::ExprKind::Try(e) => {
780 self.print_expr_cond_paren(
781 e,
782 e.precedence() < ExprPrecedence::Unambiguous,
783 fixup.leftmost_subexpression_with_dot(),
784 );
785 self.word("?")
786 }
787 ast::ExprKind::TryBlock(blk) => {
788 let cb = self.cbox(0);
789 let ib = self.ibox(0);
790 self.word_nbsp("try");
791 self.print_block_with_attrs(blk, attrs, cb, ib)
792 }
793 ast::ExprKind::UnsafeBinderCast(kind, expr, ty) => {
794 self.word("builtin # ");
795 match kind {
796 ast::UnsafeBinderCastKind::Wrap => self.word("wrap_binder"),
797 ast::UnsafeBinderCastKind::Unwrap => self.word("unwrap_binder"),
798 }
799 self.popen();
800 let ib = self.ibox(0);
801 self.print_expr(expr, FixupContext::default());
802
803 if let Some(ty) = ty {
804 self.word(",");
805 self.space();
806 self.print_type(ty);
807 }
808
809 self.end(ib);
810 self.pclose();
811 }
812 ast::ExprKind::Err(_) => {
813 self.popen();
814 self.word("/*ERROR*/");
815 self.pclose()
816 }
817 ast::ExprKind::Dummy => {
818 self.popen();
819 self.word("/*DUMMY*/");
820 self.pclose();
821 }
822 }
823
824 self.ann.post(self, AnnNode::Expr(expr));
825
826 if needs_par {
827 self.pclose();
828 }
829
830 self.end(ib);
831 }
832
833 fn print_arm(&mut self, arm: &ast::Arm) {
834 if arm.attrs.is_empty() {
836 self.space();
837 }
838 let cb = self.cbox(INDENT_UNIT);
839 let ib = self.ibox(0);
840 self.maybe_print_comment(arm.pat.span.lo());
841 self.print_outer_attributes(&arm.attrs);
842 self.print_pat(&arm.pat);
843 self.space();
844 if let Some(e) = &arm.guard {
845 self.word_space("if");
846 self.print_expr(e, FixupContext::default());
847 self.space();
848 }
849
850 if let Some(body) = &arm.body {
851 self.word_space("=>");
852
853 match &body.kind {
854 ast::ExprKind::Block(blk, opt_label) => {
855 if let Some(label) = opt_label {
856 self.print_ident(label.ident);
857 self.word_space(":");
858 }
859
860 self.print_block_unclosed_indent(blk, ib);
861
862 if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules {
864 self.word(",");
865 }
866 }
867 _ => {
868 self.end(ib);
869 self.print_expr(body, FixupContext::new_match_arm());
870 self.word(",");
871 }
872 }
873 } else {
874 self.end(ib);
875 self.word(",");
876 }
877 self.end(cb);
878 }
879
880 fn print_closure_binder(&mut self, binder: &ast::ClosureBinder) {
881 match binder {
882 ast::ClosureBinder::NotPresent => {}
883 ast::ClosureBinder::For { generic_params, .. } => {
884 self.print_formal_generic_params(generic_params)
885 }
886 }
887 }
888
889 fn print_movability(&mut self, movability: ast::Movability) {
890 match movability {
891 ast::Movability::Static => self.word_space("static"),
892 ast::Movability::Movable => {}
893 }
894 }
895
896 fn print_capture_clause(&mut self, capture_clause: ast::CaptureBy) {
897 match capture_clause {
898 ast::CaptureBy::Value { .. } => self.word_space("move"),
899 ast::CaptureBy::Use { .. } => self.word_space("use"),
900 ast::CaptureBy::Ref => {}
901 }
902 }
903}
904
905fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> String {
906 let mut template = "\"".to_string();
907 for piece in pieces {
908 match piece {
909 FormatArgsPiece::Literal(s) => {
910 for c in s.as_str().chars() {
911 template.extend(c.escape_debug());
912 if let '{' | '}' = c {
913 template.push(c);
914 }
915 }
916 }
917 FormatArgsPiece::Placeholder(p) => {
918 template.push('{');
919 let (Ok(n) | Err(n)) = p.argument.index;
920 write!(template, "{n}").unwrap();
921 if p.format_options != Default::default() || p.format_trait != FormatTrait::Display
922 {
923 template.push(':');
924 }
925 if let Some(fill) = p.format_options.fill {
926 template.push(fill);
927 }
928 match p.format_options.alignment {
929 Some(FormatAlignment::Left) => template.push('<'),
930 Some(FormatAlignment::Right) => template.push('>'),
931 Some(FormatAlignment::Center) => template.push('^'),
932 None => {}
933 }
934 match p.format_options.sign {
935 Some(FormatSign::Plus) => template.push('+'),
936 Some(FormatSign::Minus) => template.push('-'),
937 None => {}
938 }
939 if p.format_options.alternate {
940 template.push('#');
941 }
942 if p.format_options.zero_pad {
943 template.push('0');
944 }
945 if let Some(width) = &p.format_options.width {
946 match width {
947 FormatCount::Literal(n) => write!(template, "{n}").unwrap(),
948 FormatCount::Argument(FormatArgPosition {
949 index: Ok(n) | Err(n), ..
950 }) => {
951 write!(template, "{n}$").unwrap();
952 }
953 }
954 }
955 if let Some(precision) = &p.format_options.precision {
956 template.push('.');
957 match precision {
958 FormatCount::Literal(n) => write!(template, "{n}").unwrap(),
959 FormatCount::Argument(FormatArgPosition {
960 index: Ok(n) | Err(n), ..
961 }) => {
962 write!(template, "{n}$").unwrap();
963 }
964 }
965 }
966 match p.format_options.debug_hex {
967 Some(FormatDebugHex::Lower) => template.push('x'),
968 Some(FormatDebugHex::Upper) => template.push('X'),
969 None => {}
970 }
971 template.push_str(match p.format_trait {
972 FormatTrait::Display => "",
973 FormatTrait::Debug => "?",
974 FormatTrait::LowerExp => "e",
975 FormatTrait::UpperExp => "E",
976 FormatTrait::Octal => "o",
977 FormatTrait::Pointer => "p",
978 FormatTrait::Binary => "b",
979 FormatTrait::LowerHex => "x",
980 FormatTrait::UpperHex => "X",
981 });
982 template.push('}');
983 }
984 }
985 }
986 template.push('"');
987 template
988}