Skip to main content

rustc_ast_pretty/pprust/state/
expr.rs

1use std::fmt::Write;
2
3use ast::{ForLoopKind, MatchKind};
4use itertools::{Itertools, Position};
5use rustc_ast::util::classify;
6use rustc_ast::util::literal::escape_byte_str_symbol;
7use rustc_ast::util::parser::{self, ExprPrecedence, Fixity};
8use rustc_ast::{
9    self as ast, BinOpKind, BlockCheckMode, FormatAlignment, FormatArgPosition, FormatArgsPiece,
10    FormatCount, FormatDebugHex, FormatSign, FormatTrait, YieldKind, token,
11};
12
13use crate::pp::Breaks::Inconsistent;
14use crate::pprust::state::fixup::FixupContext;
15use crate::pprust::state::{AnnNode, INDENT_UNIT, PrintState, State};
16
17impl<'a> State<'a> {
18    fn print_else(&mut self, els: Option<&ast::Expr>) {
19        if let Some(_else) = els {
20            match &_else.kind {
21                // Another `else if` block.
22                ast::ExprKind::If(i, then, e) => {
23                    let cb = self.cbox(0);
24                    let ib = self.ibox(0);
25                    self.word(" else if ");
26                    self.print_expr_as_cond(i);
27                    self.space();
28                    self.print_block(then, cb, ib);
29                    self.print_else(e.as_deref())
30                }
31                // Final `else` block.
32                ast::ExprKind::Block(b, None) => {
33                    let cb = self.cbox(0);
34                    let ib = self.ibox(0);
35                    self.word(" else ");
36                    self.print_block(b, cb, ib)
37                }
38                // Constraints would be great here!
39                _ => {
40                    {
    ::core::panicking::panic_fmt(format_args!("print_if saw if with weird alternative"));
};panic!("print_if saw if with weird alternative");
41                }
42            }
43        }
44    }
45
46    fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) {
47        let cb = self.cbox(0);
48        let ib = self.ibox(0);
49        self.word_nbsp("if");
50        self.print_expr_as_cond(test);
51        self.space();
52        self.print_block(blk, cb, ib);
53        self.print_else(elseopt)
54    }
55
56    fn print_call_post(&mut self, args: &[Box<ast::Expr>]) {
57        self.popen();
58        self.commasep_exprs(Inconsistent, args);
59        self.pclose()
60    }
61
62    /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
63    /// `if cond { ... }`.
64    fn print_expr_as_cond(&mut self, expr: &ast::Expr) {
65        self.print_expr_cond_paren(expr, Self::cond_needs_par(expr), FixupContext::new_cond())
66    }
67
68    /// Does `expr` need parentheses when printed in a condition position?
69    ///
70    /// These cases need parens due to the parse error observed in #26461: `if return {}`
71    /// parses as the erroneous construct `if (return {})`, not `if (return) {}`.
72    fn cond_needs_par(expr: &ast::Expr) -> bool {
73        match expr.kind {
74            ast::ExprKind::Break(..)
75            | ast::ExprKind::Closure(..)
76            | ast::ExprKind::Ret(..)
77            | ast::ExprKind::Yeet(..) => true,
78            _ => parser::contains_exterior_struct_lit(expr),
79        }
80    }
81
82    /// Prints `expr` or `(expr)` when `needs_par` holds.
83    pub(super) fn print_expr_cond_paren(
84        &mut self,
85        expr: &ast::Expr,
86        needs_par: bool,
87        mut fixup: FixupContext,
88    ) {
89        if needs_par {
90            self.popen();
91
92            // If we are surrounding the whole cond in parentheses, such as:
93            //
94            //     if (return Struct {}) {}
95            //
96            // then there is no need for parenthesizing the individual struct
97            // expressions within. On the other hand if the whole cond is not
98            // parenthesized, then print_expr must parenthesize exterior struct
99            // literals.
100            //
101            //     if x == (Struct {}) {}
102            //
103            fixup = FixupContext::default();
104        }
105
106        self.print_expr(expr, fixup);
107
108        if needs_par {
109            self.pclose();
110        }
111    }
112
113    fn print_expr_vec(&mut self, exprs: &[Box<ast::Expr>]) {
114        let ib = self.ibox(INDENT_UNIT);
115        self.word("[");
116        self.commasep_exprs(Inconsistent, exprs);
117        self.word("]");
118        self.end(ib);
119    }
120
121    pub(super) fn print_expr_anon_const(
122        &mut self,
123        expr: &ast::AnonConst,
124        attrs: &[ast::Attribute],
125    ) {
126        let ib = self.ibox(INDENT_UNIT);
127        self.word("const");
128        self.nbsp();
129        if let ast::ExprKind::Block(block, None) = &expr.value.kind {
130            let cb = self.cbox(0);
131            let ib = self.ibox(0);
132            self.print_block_with_attrs(block, attrs, cb, ib);
133        } else {
134            self.print_expr(&expr.value, FixupContext::default());
135        }
136        self.end(ib);
137    }
138
139    fn print_expr_repeat(&mut self, element: &ast::Expr, count: &ast::AnonConst) {
140        let ib = self.ibox(INDENT_UNIT);
141        self.word("[");
142        self.print_expr(element, FixupContext::default());
143        self.word_space(";");
144        self.print_expr(&count.value, FixupContext::default());
145        self.word("]");
146        self.end(ib);
147    }
148
149    fn print_expr_struct(
150        &mut self,
151        qself: &Option<Box<ast::QSelf>>,
152        path: &ast::Path,
153        fields: &[ast::ExprField],
154        rest: &ast::StructRest,
155    ) {
156        if let Some(qself) = qself {
157            self.print_qpath(path, qself, true);
158        } else {
159            self.print_path(path, true, 0);
160        }
161        self.nbsp();
162        self.word("{");
163        let has_rest = match rest {
164            ast::StructRest::Base(_) | ast::StructRest::Rest(_) => true,
165            ast::StructRest::None | ast::StructRest::NoneWithError(_) => false,
166        };
167        if fields.is_empty() && !has_rest {
168            self.word("}");
169            return;
170        }
171        let cb = self.cbox(0);
172        for (pos, field) in fields.iter().with_position() {
173            let is_first = #[allow(non_exhaustive_omitted_patterns)] match pos {
    Position::First | Position::Only => true,
    _ => false,
}matches!(pos, Position::First | Position::Only);
174            let is_last = #[allow(non_exhaustive_omitted_patterns)] match pos {
    Position::Last | Position::Only => true,
    _ => false,
}matches!(pos, Position::Last | Position::Only);
175            self.maybe_print_comment(field.span.hi());
176            self.print_outer_attributes(&field.attrs);
177            if is_first {
178                self.space_if_not_bol();
179            }
180            if !field.is_shorthand {
181                self.print_ident(field.ident);
182                self.word_nbsp(":");
183            }
184            self.print_expr(&field.expr, FixupContext::default());
185            if !is_last || has_rest {
186                self.word_space(",");
187            } else {
188                self.trailing_comma_or_space();
189            }
190        }
191        if has_rest {
192            if fields.is_empty() {
193                self.space();
194            }
195            self.word("..");
196            if let ast::StructRest::Base(expr) = rest {
197                self.print_expr(expr, FixupContext::default());
198            }
199            self.space();
200        }
201        self.offset(-INDENT_UNIT);
202        self.end(cb);
203        self.word("}");
204    }
205
206    fn print_expr_tup(&mut self, exprs: &[Box<ast::Expr>]) {
207        self.popen();
208        self.commasep_exprs(Inconsistent, exprs);
209        if exprs.len() == 1 {
210            self.word(",");
211        }
212        self.pclose()
213    }
214
215    fn print_expr_call(&mut self, func: &ast::Expr, args: &[Box<ast::Expr>], fixup: FixupContext) {
216        // Independent of parenthesization related to precedence, we must
217        // parenthesize `func` if this is a statement context in which without
218        // parentheses, a statement boundary would occur inside `func` or
219        // immediately after `func`.
220        //
221        // Suppose `func` represents `match () { _ => f }`. We must produce:
222        //
223        //     (match () { _ => f })();
224        //
225        // instead of:
226        //
227        //     match () { _ => f } ();
228        //
229        // because the latter is valid syntax but with the incorrect meaning.
230        // It's a match-expression followed by tuple-expression, not a function
231        // call.
232        let func_fixup = fixup.leftmost_subexpression_with_operator(true);
233
234        let needs_paren = match func.kind {
235            // In order to call a named field, needs parens: `(self.fun)()`
236            // But not for an unnamed field: `self.0()`
237            ast::ExprKind::Field(_, name) => !name.is_numeric(),
238            // Block-like expressions (block, match, if, loop, ...) never
239            // parse as the callee of a call, regardless of context: the
240            // closing brace ends the expression and `(args)` becomes a
241            // separate tuple. Parenthesize them so the call survives a
242            // pretty-print round trip.
243            _ => {
244                func_fixup.precedence(func) < ExprPrecedence::Unambiguous
245                    || classify::expr_is_complete(func)
246            }
247        };
248
249        self.print_expr_cond_paren(func, needs_paren, func_fixup);
250        self.print_call_post(args)
251    }
252
253    fn print_expr_method_call(
254        &mut self,
255        segment: &ast::PathSegment,
256        receiver: &ast::Expr,
257        base_args: &[Box<ast::Expr>],
258        fixup: FixupContext,
259    ) {
260        // The fixup here is different than in `print_expr_call` because
261        // statement boundaries never occur in front of a `.` (or `?`) token.
262        //
263        // Needs parens:
264        //
265        //     (loop { break x; })();
266        //
267        // Does not need parens:
268        //
269        //     loop { break x; }.method();
270        //
271        let needs_paren = receiver.precedence() < ExprPrecedence::Unambiguous;
272        self.print_expr_cond_paren(receiver, needs_paren, fixup.leftmost_subexpression_with_dot());
273
274        // If the receiver is an unsuffixed float literal like `0.`, insert
275        // a space so the `.` of the method call doesn't merge with the
276        // trailing dot: `0. .method()` instead of `0..method()`.
277        if !needs_paren && expr_ends_with_dot(receiver) {
278            self.word(" ");
279        }
280        self.word(".");
281        self.print_ident(segment.ident);
282        if let Some(args) = &segment.args {
283            self.print_generic_args(args, true);
284        }
285        self.print_call_post(base_args)
286    }
287
288    fn print_expr_binary(
289        &mut self,
290        op: ast::BinOpKind,
291        lhs: &ast::Expr,
292        rhs: &ast::Expr,
293        fixup: FixupContext,
294    ) {
295        let operator_can_begin_expr = match op {
296            | BinOpKind::Sub     // -x
297            | BinOpKind::Mul     // *x
298            | BinOpKind::And     // &&x
299            | BinOpKind::Or      // || x
300            | BinOpKind::BitAnd  // &x
301            | BinOpKind::BitOr   // |x| x
302            | BinOpKind::Shl     // <<T as Trait>::Type as Trait>::CONST
303            | BinOpKind::Lt      // <T as Trait>::CONST
304              => true,
305            _ => false,
306        };
307
308        let left_fixup = fixup.leftmost_subexpression_with_operator(operator_can_begin_expr);
309
310        let binop_prec = op.precedence();
311        let left_prec = left_fixup.precedence(lhs);
312        let right_prec = fixup.precedence(rhs);
313
314        let (mut left_needs_paren, right_needs_paren) = match op.fixity() {
315            Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
316            Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
317            Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),
318        };
319
320        match (&lhs.kind, op) {
321            // These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
322            // the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
323            // of `(x as i32) < ...`. We need to convince it _not_ to do that.
324            (&ast::ExprKind::Cast { .. }, ast::BinOpKind::Lt | ast::BinOpKind::Shl) => {
325                left_needs_paren = true;
326            }
327            // We are given `(let _ = a) OP b`.
328            //
329            // - When `OP <= LAnd` we should print `let _ = a OP b` to avoid redundant parens
330            //   as the parser will interpret this as `(let _ = a) OP b`.
331            //
332            // - Otherwise, e.g. when we have `(let a = b) < c` in AST,
333            //   parens are required since the parser would interpret `let a = b < c` as
334            //   `let a = (b < c)`. To achieve this, we force parens.
335            (&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(binop_prec) => {
336                left_needs_paren = true;
337            }
338            _ => {}
339        }
340
341        self.print_expr_cond_paren(lhs, left_needs_paren, left_fixup);
342        self.space();
343        self.word_space(op.as_str());
344        self.print_expr_cond_paren(rhs, right_needs_paren, fixup.rightmost_subexpression());
345    }
346
347    fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr, fixup: FixupContext) {
348        self.word(op.as_str());
349        self.print_expr_cond_paren(
350            expr,
351            fixup.precedence(expr) < ExprPrecedence::Prefix,
352            fixup.rightmost_subexpression(),
353        );
354    }
355
356    fn print_expr_addr_of(
357        &mut self,
358        kind: ast::BorrowKind,
359        mutability: ast::Mutability,
360        expr: &ast::Expr,
361        fixup: FixupContext,
362    ) {
363        self.word("&");
364        match kind {
365            ast::BorrowKind::Ref => self.print_mutability(mutability, false),
366            ast::BorrowKind::Raw => {
367                self.word_nbsp("raw");
368                self.print_mutability(mutability, true);
369            }
370            ast::BorrowKind::Pin => {
371                self.word_nbsp("pin");
372                self.print_mutability(mutability, true);
373            }
374        }
375        self.print_expr_cond_paren(
376            expr,
377            fixup.precedence(expr) < ExprPrecedence::Prefix,
378            fixup.rightmost_subexpression(),
379        );
380    }
381
382    pub(super) fn print_expr(&mut self, expr: &ast::Expr, fixup: FixupContext) {
383        self.print_expr_outer_attr_style(expr, true, fixup)
384    }
385
386    pub(super) fn print_expr_outer_attr_style(
387        &mut self,
388        expr: &ast::Expr,
389        is_inline: bool,
390        mut fixup: FixupContext,
391    ) {
392        self.maybe_print_comment(expr.span.lo());
393
394        let attrs = &expr.attrs;
395        if is_inline {
396            self.print_outer_attributes_inline(attrs);
397        } else {
398            self.print_outer_attributes(attrs);
399        }
400
401        let ib = self.ibox(INDENT_UNIT);
402
403        let needs_par = {
404            // The Match subexpression in `match x {} - 1` must be parenthesized
405            // if it is the leftmost subexpression in a statement:
406            //
407            //     (match x {}) - 1;
408            //
409            // But not otherwise:
410            //
411            //     let _ = match x {} - 1;
412            //
413            // Same applies to a small set of other expression kinds which
414            // eagerly terminate a statement which opens with them.
415            fixup.would_cause_statement_boundary(expr)
416        } || {
417            // If a binary operation ends up with an attribute, such as
418            // resulting from the following macro expansion, then parentheses
419            // are required so that the attribute encompasses the right
420            // subexpression and not just the left one.
421            //
422            //     #![feature(stmt_expr_attributes)]
423            //
424            //     macro_rules! add_attr {
425            //         ($e:expr) => { #[attr] $e };
426            //     }
427            //
428            //     let _ = add_attr!(1 + 1);
429            //
430            // We must pretty-print `#[attr] (1 + 1)` not `#[attr] 1 + 1`.
431            !attrs.is_empty()
432                && #[allow(non_exhaustive_omitted_patterns)] match expr.kind {
    ast::ExprKind::Binary(..) | ast::ExprKind::Cast(..) |
        ast::ExprKind::Assign(..) | ast::ExprKind::AssignOp(..) |
        ast::ExprKind::Range(..) => true,
    _ => false,
}matches!(
433                    expr.kind,
434                    ast::ExprKind::Binary(..)
435                        | ast::ExprKind::Cast(..)
436                        | ast::ExprKind::Assign(..)
437                        | ast::ExprKind::AssignOp(..)
438                        | ast::ExprKind::Range(..)
439                )
440        };
441        if needs_par {
442            self.popen();
443            fixup = FixupContext::default();
444        }
445
446        self.ann.pre(self, AnnNode::Expr(expr));
447
448        match &expr.kind {
449            ast::ExprKind::Array(exprs) => {
450                self.print_expr_vec(exprs);
451            }
452            ast::ExprKind::ConstBlock(anon_const) => {
453                self.print_expr_anon_const(anon_const, attrs);
454            }
455            ast::ExprKind::Repeat(element, count) => {
456                self.print_expr_repeat(element, count);
457            }
458            ast::ExprKind::Struct(se) => {
459                self.print_expr_struct(&se.qself, &se.path, &se.fields, &se.rest);
460            }
461            ast::ExprKind::Tup(exprs) => {
462                self.print_expr_tup(exprs);
463            }
464            ast::ExprKind::Call(func, args) => {
465                self.print_expr_call(func, args, fixup);
466            }
467            ast::ExprKind::MethodCall(ast::MethodCall { seg, receiver, args, .. }) => {
468                self.print_expr_method_call(seg, receiver, args, fixup);
469            }
470            ast::ExprKind::Binary(op, lhs, rhs) => {
471                self.print_expr_binary(op.node, lhs, rhs, fixup);
472            }
473            ast::ExprKind::Unary(op, expr) => {
474                self.print_expr_unary(*op, expr, fixup);
475            }
476            ast::ExprKind::AddrOf(k, m, expr) => {
477                self.print_expr_addr_of(*k, *m, expr, fixup);
478            }
479            ast::ExprKind::Lit(token_lit) => {
480                self.print_token_literal(*token_lit, expr.span);
481            }
482            ast::ExprKind::IncludedBytes(byte_sym) => {
483                let lit = token::Lit::new(
484                    token::ByteStr,
485                    escape_byte_str_symbol(byte_sym.as_byte_str()),
486                    None,
487                );
488                self.print_token_literal(lit, expr.span)
489            }
490            ast::ExprKind::Cast(expr, ty) => {
491                self.print_expr_cond_paren(
492                    expr,
493                    expr.precedence() < ExprPrecedence::Cast,
494                    fixup.leftmost_subexpression(),
495                );
496                self.space();
497                self.word_space("as");
498                self.print_type(ty);
499            }
500            ast::ExprKind::Type(expr, ty) => {
501                self.word("builtin # type_ascribe");
502                self.popen();
503                let ib = self.ibox(0);
504                self.print_expr(expr, FixupContext::default());
505
506                self.word(",");
507                self.space_if_not_bol();
508                self.print_type(ty);
509
510                self.end(ib);
511                self.pclose();
512            }
513            ast::ExprKind::Let(pat, scrutinee, _, _) => {
514                self.print_let(pat, scrutinee, fixup);
515            }
516            ast::ExprKind::If(test, blk, elseopt) => self.print_if(test, blk, elseopt.as_deref()),
517            ast::ExprKind::While(test, blk, opt_label) => {
518                if let Some(label) = opt_label {
519                    self.print_ident(label.ident);
520                    self.word_space(":");
521                }
522                let cb = self.cbox(0);
523                let ib = self.ibox(0);
524                self.word_nbsp("while");
525                self.print_expr_as_cond(test);
526                self.space();
527                self.print_block_with_attrs(blk, attrs, cb, ib);
528            }
529            ast::ExprKind::ForLoop { pat, iter, body, label, kind } => {
530                if let Some(label) = label {
531                    self.print_ident(label.ident);
532                    self.word_space(":");
533                }
534                let cb = self.cbox(0);
535                let ib = self.ibox(0);
536                self.word_nbsp("for");
537                if kind == &ForLoopKind::ForAwait {
538                    self.word_nbsp("await");
539                }
540                self.print_pat(pat);
541                self.space();
542                self.word_space("in");
543                self.print_expr_as_cond(iter);
544                self.space();
545                self.print_block_with_attrs(body, attrs, cb, ib);
546            }
547            ast::ExprKind::Loop(blk, opt_label, _) => {
548                let cb = self.cbox(0);
549                let ib = self.ibox(0);
550                if let Some(label) = opt_label {
551                    self.print_ident(label.ident);
552                    self.word_space(":");
553                }
554                self.word_nbsp("loop");
555                self.print_block_with_attrs(blk, attrs, cb, ib);
556            }
557            ast::ExprKind::Match(expr, arms, match_kind) => {
558                let cb = self.cbox(0);
559                let ib = self.ibox(0);
560
561                match match_kind {
562                    MatchKind::Prefix => {
563                        self.word_nbsp("match");
564                        self.print_expr_as_cond(expr);
565                        self.space();
566                    }
567                    MatchKind::Postfix => {
568                        self.print_expr_cond_paren(
569                            expr,
570                            expr.precedence() < ExprPrecedence::Unambiguous,
571                            fixup.leftmost_subexpression_with_dot(),
572                        );
573                        self.word_nbsp(".match");
574                    }
575                }
576
577                self.bopen(ib);
578                self.print_inner_attributes_no_trailing_hardbreak(attrs);
579                for arm in arms {
580                    self.print_arm(arm);
581                }
582                let empty = attrs.is_empty() && arms.is_empty();
583                self.bclose(expr.span, empty, cb);
584            }
585            ast::ExprKind::Closure(ast::Closure {
586                binder,
587                capture_clause,
588                constness,
589                coroutine_kind,
590                movability,
591                fn_decl,
592                body,
593                fn_decl_span: _,
594                fn_arg_span: _,
595            }) => {
596                self.print_closure_binder(binder);
597                self.print_constness(*constness);
598                self.print_movability(*movability);
599                coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
600                self.print_capture_clause(*capture_clause);
601
602                self.print_fn_params_and_ret(fn_decl, true);
603                self.space();
604                self.print_expr(body, FixupContext::default());
605            }
606            ast::ExprKind::Block(blk, opt_label) => {
607                if let Some(label) = opt_label {
608                    self.print_ident(label.ident);
609                    self.word_space(":");
610                }
611                // containing cbox, will be closed by print-block at }
612                let cb = self.cbox(0);
613                // head-box, will be closed by print-block after {
614                let ib = self.ibox(0);
615                self.print_block_with_attrs(blk, attrs, cb, ib);
616            }
617            ast::ExprKind::Gen(capture_clause, blk, kind, _decl_span) => {
618                self.word_nbsp(kind.modifier());
619                self.print_capture_clause(*capture_clause);
620                // cbox/ibox in analogy to the `ExprKind::Block` arm above
621                let cb = self.cbox(0);
622                let ib = self.ibox(0);
623                self.print_block_with_attrs(blk, attrs, cb, ib);
624            }
625            ast::ExprKind::Await(expr, _) => {
626                self.print_expr_cond_paren(
627                    expr,
628                    expr.precedence() < ExprPrecedence::Unambiguous,
629                    fixup.leftmost_subexpression_with_dot(),
630                );
631                self.word(".await");
632            }
633            ast::ExprKind::Move(expr, _) => {
634                self.word("move(");
635                self.print_expr(expr, FixupContext::default());
636                self.word(")");
637            }
638            ast::ExprKind::Use(expr, _) => {
639                self.print_expr_cond_paren(
640                    expr,
641                    expr.precedence() < ExprPrecedence::Unambiguous,
642                    fixup,
643                );
644                self.word(".use");
645            }
646            ast::ExprKind::Assign(lhs, rhs, _) => {
647                self.print_expr_cond_paren(
648                    lhs,
649                    // Ranges are allowed on the right-hand side of assignment,
650                    // but not the left. `(a..b) = c` needs parentheses.
651                    lhs.precedence() <= ExprPrecedence::Range,
652                    fixup.leftmost_subexpression(),
653                );
654                self.space();
655                self.word_space("=");
656                self.print_expr_cond_paren(
657                    rhs,
658                    fixup.precedence(rhs) < ExprPrecedence::Assign,
659                    fixup.rightmost_subexpression(),
660                );
661            }
662            ast::ExprKind::AssignOp(op, lhs, rhs) => {
663                self.print_expr_cond_paren(
664                    lhs,
665                    lhs.precedence() <= ExprPrecedence::Range,
666                    fixup.leftmost_subexpression(),
667                );
668                self.space();
669                self.word_space(op.node.as_str());
670                self.print_expr_cond_paren(
671                    rhs,
672                    fixup.precedence(rhs) < ExprPrecedence::Assign,
673                    fixup.rightmost_subexpression(),
674                );
675            }
676            ast::ExprKind::Field(expr, ident) => {
677                let needs_paren = expr.precedence() < ExprPrecedence::Unambiguous;
678                self.print_expr_cond_paren(
679                    expr,
680                    needs_paren,
681                    fixup.leftmost_subexpression_with_dot(),
682                );
683                if !needs_paren && expr_ends_with_dot(expr) {
684                    self.word(" ");
685                }
686                self.word(".");
687                self.print_ident(*ident);
688            }
689            ast::ExprKind::Index(expr, index, _) => {
690                let expr_fixup = fixup.leftmost_subexpression_with_operator(true);
691                self.print_expr_cond_paren(
692                    expr,
693                    expr_fixup.precedence(expr) < ExprPrecedence::Unambiguous
694                        || classify::expr_is_complete(expr),
695                    expr_fixup,
696                );
697                self.word("[");
698                self.print_expr(index, FixupContext::default());
699                self.word("]");
700            }
701            ast::ExprKind::Range(start, end, limits) => {
702                // Special case for `Range`. `AssocOp` claims that `Range` has higher precedence
703                // than `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`.
704                // Here we use a fake precedence value so that any child with lower precedence than
705                // a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
706                let fake_prec = ExprPrecedence::LOr;
707                if let Some(e) = start {
708                    let start_fixup = fixup.leftmost_subexpression_with_operator(true);
709                    let needs_paren = start_fixup.precedence(e) < fake_prec;
710                    self.print_expr_cond_paren(e, needs_paren, start_fixup);
711                    // If the start expression is a float literal ending with
712                    // `.`, we need a space before `..` or `..=` so that the
713                    // dots don't merge. E.g. `0. ..45.` must not become
714                    // `0...45.`.
715                    if !needs_paren && expr_ends_with_dot(e) {
716                        self.word(" ");
717                    }
718                }
719                match limits {
720                    ast::RangeLimits::HalfOpen => self.word(".."),
721                    ast::RangeLimits::Closed => self.word("..="),
722                }
723                if let Some(e) = end {
724                    self.print_expr_cond_paren(
725                        e,
726                        fixup.precedence(e) < fake_prec,
727                        fixup.rightmost_subexpression(),
728                    );
729                }
730            }
731            ast::ExprKind::Underscore => self.word("_"),
732            ast::ExprKind::Path(None, path) => self.print_path(path, true, 0),
733            ast::ExprKind::Path(Some(qself), path) => self.print_qpath(path, qself, true),
734            ast::ExprKind::Break(opt_label, opt_expr) => {
735                self.word("break");
736                if let Some(label) = opt_label {
737                    self.space();
738                    self.print_ident(label.ident);
739                }
740                if let Some(expr) = opt_expr {
741                    self.space();
742                    self.print_expr_cond_paren(
743                        expr,
744                        // Parenthesize `break 'inner: loop { break 'inner 1 } + 1`
745                        //                     ^---------------------------------^
746                        opt_label.is_none() && classify::leading_labeled_expr(expr),
747                        fixup.rightmost_subexpression(),
748                    );
749                }
750            }
751            ast::ExprKind::Continue(opt_label) => {
752                self.word("continue");
753                if let Some(label) = opt_label {
754                    self.space();
755                    self.print_ident(label.ident);
756                }
757            }
758            ast::ExprKind::Ret(result) => {
759                self.word("return");
760                if let Some(expr) = result {
761                    self.word(" ");
762                    self.print_expr(expr, fixup.rightmost_subexpression());
763                }
764            }
765            ast::ExprKind::Yeet(result) => {
766                self.word("do");
767                self.word(" ");
768                self.word("yeet");
769                if let Some(expr) = result {
770                    self.word(" ");
771                    self.print_expr(expr, fixup.rightmost_subexpression());
772                }
773            }
774            ast::ExprKind::Become(result) => {
775                self.word("become");
776                self.word(" ");
777                self.print_expr(result, fixup.rightmost_subexpression());
778            }
779            ast::ExprKind::InlineAsm(a) => {
780                // FIXME: Print `builtin # asm` once macro `asm` uses `builtin_syntax`.
781                self.word("asm!");
782                self.print_inline_asm(a);
783            }
784            ast::ExprKind::FormatArgs(fmt) => {
785                // FIXME: Print `builtin # format_args` once macro `format_args` uses `builtin_syntax`.
786                self.word("format_args!");
787                self.popen();
788                let ib = self.ibox(0);
789                self.word(reconstruct_format_args_template_string(&fmt.template));
790                for arg in fmt.arguments.all_args() {
791                    self.word_space(",");
792                    self.print_expr(&arg.expr, FixupContext::default());
793                }
794                self.end(ib);
795                self.pclose();
796            }
797            ast::ExprKind::OffsetOf(container, fields) => {
798                self.word("builtin # offset_of");
799                self.popen();
800                let ib = self.ibox(0);
801                self.print_type(container);
802                self.word(",");
803                self.space();
804
805                if let Some((&first, rest)) = fields.split_first() {
806                    self.print_ident(first);
807
808                    for &field in rest {
809                        self.word(".");
810                        self.print_ident(field);
811                    }
812                }
813                self.end(ib);
814                self.pclose();
815            }
816            ast::ExprKind::MacCall(m) => self.print_mac(m),
817            ast::ExprKind::Paren(e) => {
818                self.popen();
819                self.print_expr(e, FixupContext::default());
820                self.pclose();
821            }
822            ast::ExprKind::Yield(YieldKind::Prefix(e)) => {
823                self.word("yield");
824
825                if let Some(expr) = e {
826                    self.space();
827                    self.print_expr(expr, fixup.rightmost_subexpression());
828                }
829            }
830            ast::ExprKind::Yield(YieldKind::Postfix(e)) => {
831                self.print_expr_cond_paren(
832                    e,
833                    e.precedence() < ExprPrecedence::Unambiguous,
834                    fixup.leftmost_subexpression_with_dot(),
835                );
836                self.word(".yield");
837            }
838            ast::ExprKind::Try(e) => {
839                self.print_expr_cond_paren(
840                    e,
841                    e.precedence() < ExprPrecedence::Unambiguous,
842                    fixup.leftmost_subexpression_with_dot(),
843                );
844                self.word("?")
845            }
846            ast::ExprKind::TryBlock(blk, opt_ty) => {
847                let cb = self.cbox(0);
848                let ib = self.ibox(0);
849                self.word_nbsp("try");
850                if let Some(ty) = opt_ty {
851                    self.word_nbsp("bikeshed");
852                    self.print_type(ty);
853                    self.space();
854                }
855                self.print_block_with_attrs(blk, attrs, cb, ib)
856            }
857            ast::ExprKind::UnsafeBinderCast(kind, expr, ty) => {
858                self.word("builtin # ");
859                match kind {
860                    ast::UnsafeBinderCastKind::Wrap => self.word("wrap_binder"),
861                    ast::UnsafeBinderCastKind::Unwrap => self.word("unwrap_binder"),
862                }
863                self.popen();
864                let ib = self.ibox(0);
865                self.print_expr(expr, FixupContext::default());
866
867                if let Some(ty) = ty {
868                    self.word(",");
869                    self.space();
870                    self.print_type(ty);
871                }
872
873                self.end(ib);
874                self.pclose();
875            }
876            ast::ExprKind::Err(_) => {
877                self.popen();
878                self.word("/*ERROR*/");
879                self.pclose()
880            }
881            ast::ExprKind::Dummy => {
882                self.popen();
883                self.word("/*DUMMY*/");
884                self.pclose();
885            }
886        }
887
888        self.ann.post(self, AnnNode::Expr(expr));
889
890        if needs_par {
891            self.pclose();
892        }
893
894        self.end(ib);
895    }
896
897    fn print_arm(&mut self, arm: &ast::Arm) {
898        // Note, I have no idea why this check is necessary, but here it is.
899        if arm.attrs.is_empty() {
900            self.space();
901        }
902        let cb = self.cbox(INDENT_UNIT);
903        let ib = self.ibox(0);
904        self.maybe_print_comment(arm.pat.span.lo());
905        self.print_outer_attributes(&arm.attrs);
906        self.print_pat(&arm.pat);
907        self.space();
908        if let Some(guard) = &arm.guard {
909            self.word_space("if");
910            self.print_expr(&guard.cond, FixupContext::default());
911            self.space();
912        }
913
914        if let Some(body) = &arm.body {
915            self.word_space("=>");
916
917            match &body.kind {
918                ast::ExprKind::Block(blk, opt_label) => {
919                    if let Some(label) = opt_label {
920                        self.print_ident(label.ident);
921                        self.word_space(":");
922                    }
923
924                    self.print_block_unclosed_indent(blk, ib);
925
926                    // If it is a user-provided unsafe block, print a comma after it.
927                    if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules {
928                        self.word(",");
929                    }
930                }
931                _ => {
932                    self.end(ib);
933                    self.print_expr(body, FixupContext::new_match_arm());
934                    self.word(",");
935                }
936            }
937        } else {
938            self.end(ib);
939            self.word(",");
940        }
941        self.end(cb);
942    }
943
944    fn print_closure_binder(&mut self, binder: &ast::ClosureBinder) {
945        match binder {
946            ast::ClosureBinder::NotPresent => {}
947            ast::ClosureBinder::For { generic_params, .. } => {
948                self.print_formal_generic_params(generic_params)
949            }
950        }
951    }
952
953    fn print_movability(&mut self, movability: ast::Movability) {
954        match movability {
955            ast::Movability::Static => self.word_space("static"),
956            ast::Movability::Movable => {}
957        }
958    }
959
960    fn print_capture_clause(&mut self, capture_clause: ast::CaptureBy) {
961        match capture_clause {
962            ast::CaptureBy::Value { .. } => self.word_space("move"),
963            ast::CaptureBy::Use { .. } => self.word_space("use"),
964            ast::CaptureBy::Ref => {}
965        }
966    }
967}
968
969fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> String {
970    let mut template = "\"".to_string();
971    for piece in pieces {
972        match piece {
973            FormatArgsPiece::Literal(s) => {
974                for c in s.as_str().chars() {
975                    template.extend(c.escape_debug());
976                    if let '{' | '}' = c {
977                        template.push(c);
978                    }
979                }
980            }
981            FormatArgsPiece::Placeholder(p) => {
982                template.push('{');
983                let (Ok(n) | Err(n)) = p.argument.index;
984                template.write_fmt(format_args!("{0}", n))write!(template, "{n}").unwrap();
985                if p.format_options != Default::default() || p.format_trait != FormatTrait::Display
986                {
987                    template.push(':');
988                }
989                if let Some(fill) = p.format_options.fill {
990                    template.push(fill);
991                }
992                match p.format_options.alignment {
993                    Some(FormatAlignment::Left) => template.push('<'),
994                    Some(FormatAlignment::Right) => template.push('>'),
995                    Some(FormatAlignment::Center) => template.push('^'),
996                    None => {}
997                }
998                match p.format_options.sign {
999                    Some(FormatSign::Plus) => template.push('+'),
1000                    Some(FormatSign::Minus) => template.push('-'),
1001                    None => {}
1002                }
1003                if p.format_options.alternate {
1004                    template.push('#');
1005                }
1006                if p.format_options.zero_pad {
1007                    template.push('0');
1008                }
1009                if let Some(width) = &p.format_options.width {
1010                    match width {
1011                        FormatCount::Literal(n) => template.write_fmt(format_args!("{0}", n))write!(template, "{n}").unwrap(),
1012                        FormatCount::Argument(FormatArgPosition {
1013                            index: Ok(n) | Err(n), ..
1014                        }) => {
1015                            template.write_fmt(format_args!("{0}$", n))write!(template, "{n}$").unwrap();
1016                        }
1017                    }
1018                }
1019                if let Some(precision) = &p.format_options.precision {
1020                    template.push('.');
1021                    match precision {
1022                        FormatCount::Literal(n) => template.write_fmt(format_args!("{0}", n))write!(template, "{n}").unwrap(),
1023                        FormatCount::Argument(FormatArgPosition {
1024                            index: Ok(n) | Err(n), ..
1025                        }) => {
1026                            template.write_fmt(format_args!("{0}$", n))write!(template, "{n}$").unwrap();
1027                        }
1028                    }
1029                }
1030                match p.format_options.debug_hex {
1031                    Some(FormatDebugHex::Lower) => template.push('x'),
1032                    Some(FormatDebugHex::Upper) => template.push('X'),
1033                    None => {}
1034                }
1035                template.push_str(match p.format_trait {
1036                    FormatTrait::Display => "",
1037                    FormatTrait::Debug => "?",
1038                    FormatTrait::LowerExp => "e",
1039                    FormatTrait::UpperExp => "E",
1040                    FormatTrait::Octal => "o",
1041                    FormatTrait::Pointer => "p",
1042                    FormatTrait::Binary => "b",
1043                    FormatTrait::LowerHex => "x",
1044                    FormatTrait::UpperHex => "X",
1045                });
1046                template.push('}');
1047            }
1048        }
1049    }
1050    template.push('"');
1051    template
1052}
1053
1054/// Returns `true` if the printed representation of this expression ends with
1055/// a `.` character — specifically, an unsuffixed float literal like `0.` or
1056/// `45.`. This is used to insert whitespace before range operators (`..`,
1057/// `..=`) so that the dots don't merge (e.g. `0. ..45.` instead of `0...45.`).
1058fn expr_ends_with_dot(expr: &ast::Expr) -> bool {
1059    match &expr.kind {
1060        ast::ExprKind::Lit(token_lit) => {
1061            token_lit.kind == token::Float
1062                && token_lit.suffix.is_none()
1063                && token_lit.symbol.as_str().ends_with('.')
1064        }
1065        _ => false,
1066    }
1067}