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(box 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(box 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::Use(expr, _) => {
634                self.print_expr_cond_paren(
635                    expr,
636                    expr.precedence() < ExprPrecedence::Unambiguous,
637                    fixup,
638                );
639                self.word(".use");
640            }
641            ast::ExprKind::Assign(lhs, rhs, _) => {
642                self.print_expr_cond_paren(
643                    lhs,
644                    // Ranges are allowed on the right-hand side of assignment,
645                    // but not the left. `(a..b) = c` needs parentheses.
646                    lhs.precedence() <= ExprPrecedence::Range,
647                    fixup.leftmost_subexpression(),
648                );
649                self.space();
650                self.word_space("=");
651                self.print_expr_cond_paren(
652                    rhs,
653                    fixup.precedence(rhs) < ExprPrecedence::Assign,
654                    fixup.rightmost_subexpression(),
655                );
656            }
657            ast::ExprKind::AssignOp(op, lhs, rhs) => {
658                self.print_expr_cond_paren(
659                    lhs,
660                    lhs.precedence() <= ExprPrecedence::Range,
661                    fixup.leftmost_subexpression(),
662                );
663                self.space();
664                self.word_space(op.node.as_str());
665                self.print_expr_cond_paren(
666                    rhs,
667                    fixup.precedence(rhs) < ExprPrecedence::Assign,
668                    fixup.rightmost_subexpression(),
669                );
670            }
671            ast::ExprKind::Field(expr, ident) => {
672                let needs_paren = expr.precedence() < ExprPrecedence::Unambiguous;
673                self.print_expr_cond_paren(
674                    expr,
675                    needs_paren,
676                    fixup.leftmost_subexpression_with_dot(),
677                );
678                if !needs_paren && expr_ends_with_dot(expr) {
679                    self.word(" ");
680                }
681                self.word(".");
682                self.print_ident(*ident);
683            }
684            ast::ExprKind::Index(expr, index, _) => {
685                let expr_fixup = fixup.leftmost_subexpression_with_operator(true);
686                self.print_expr_cond_paren(
687                    expr,
688                    expr_fixup.precedence(expr) < ExprPrecedence::Unambiguous
689                        || classify::expr_is_complete(expr),
690                    expr_fixup,
691                );
692                self.word("[");
693                self.print_expr(index, FixupContext::default());
694                self.word("]");
695            }
696            ast::ExprKind::Range(start, end, limits) => {
697                // Special case for `Range`. `AssocOp` claims that `Range` has higher precedence
698                // than `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`.
699                // Here we use a fake precedence value so that any child with lower precedence than
700                // a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
701                let fake_prec = ExprPrecedence::LOr;
702                if let Some(e) = start {
703                    let start_fixup = fixup.leftmost_subexpression_with_operator(true);
704                    let needs_paren = start_fixup.precedence(e) < fake_prec;
705                    self.print_expr_cond_paren(e, needs_paren, start_fixup);
706                    // If the start expression is a float literal ending with
707                    // `.`, we need a space before `..` or `..=` so that the
708                    // dots don't merge. E.g. `0. ..45.` must not become
709                    // `0...45.`.
710                    if !needs_paren && expr_ends_with_dot(e) {
711                        self.word(" ");
712                    }
713                }
714                match limits {
715                    ast::RangeLimits::HalfOpen => self.word(".."),
716                    ast::RangeLimits::Closed => self.word("..="),
717                }
718                if let Some(e) = end {
719                    self.print_expr_cond_paren(
720                        e,
721                        fixup.precedence(e) < fake_prec,
722                        fixup.rightmost_subexpression(),
723                    );
724                }
725            }
726            ast::ExprKind::Underscore => self.word("_"),
727            ast::ExprKind::Path(None, path) => self.print_path(path, true, 0),
728            ast::ExprKind::Path(Some(qself), path) => self.print_qpath(path, qself, true),
729            ast::ExprKind::Break(opt_label, opt_expr) => {
730                self.word("break");
731                if let Some(label) = opt_label {
732                    self.space();
733                    self.print_ident(label.ident);
734                }
735                if let Some(expr) = opt_expr {
736                    self.space();
737                    self.print_expr_cond_paren(
738                        expr,
739                        // Parenthesize `break 'inner: loop { break 'inner 1 } + 1`
740                        //                     ^---------------------------------^
741                        opt_label.is_none() && classify::leading_labeled_expr(expr),
742                        fixup.rightmost_subexpression(),
743                    );
744                }
745            }
746            ast::ExprKind::Continue(opt_label) => {
747                self.word("continue");
748                if let Some(label) = opt_label {
749                    self.space();
750                    self.print_ident(label.ident);
751                }
752            }
753            ast::ExprKind::Ret(result) => {
754                self.word("return");
755                if let Some(expr) = result {
756                    self.word(" ");
757                    self.print_expr(expr, fixup.rightmost_subexpression());
758                }
759            }
760            ast::ExprKind::Yeet(result) => {
761                self.word("do");
762                self.word(" ");
763                self.word("yeet");
764                if let Some(expr) = result {
765                    self.word(" ");
766                    self.print_expr(expr, fixup.rightmost_subexpression());
767                }
768            }
769            ast::ExprKind::Become(result) => {
770                self.word("become");
771                self.word(" ");
772                self.print_expr(result, fixup.rightmost_subexpression());
773            }
774            ast::ExprKind::InlineAsm(a) => {
775                // FIXME: Print `builtin # asm` once macro `asm` uses `builtin_syntax`.
776                self.word("asm!");
777                self.print_inline_asm(a);
778            }
779            ast::ExprKind::FormatArgs(fmt) => {
780                // FIXME: Print `builtin # format_args` once macro `format_args` uses `builtin_syntax`.
781                self.word("format_args!");
782                self.popen();
783                let ib = self.ibox(0);
784                self.word(reconstruct_format_args_template_string(&fmt.template));
785                for arg in fmt.arguments.all_args() {
786                    self.word_space(",");
787                    self.print_expr(&arg.expr, FixupContext::default());
788                }
789                self.end(ib);
790                self.pclose();
791            }
792            ast::ExprKind::OffsetOf(container, fields) => {
793                self.word("builtin # offset_of");
794                self.popen();
795                let ib = self.ibox(0);
796                self.print_type(container);
797                self.word(",");
798                self.space();
799
800                if let Some((&first, rest)) = fields.split_first() {
801                    self.print_ident(first);
802
803                    for &field in rest {
804                        self.word(".");
805                        self.print_ident(field);
806                    }
807                }
808                self.end(ib);
809                self.pclose();
810            }
811            ast::ExprKind::MacCall(m) => self.print_mac(m),
812            ast::ExprKind::Paren(e) => {
813                self.popen();
814                self.print_expr(e, FixupContext::default());
815                self.pclose();
816            }
817            ast::ExprKind::Yield(YieldKind::Prefix(e)) => {
818                self.word("yield");
819
820                if let Some(expr) = e {
821                    self.space();
822                    self.print_expr(expr, fixup.rightmost_subexpression());
823                }
824            }
825            ast::ExprKind::Yield(YieldKind::Postfix(e)) => {
826                self.print_expr_cond_paren(
827                    e,
828                    e.precedence() < ExprPrecedence::Unambiguous,
829                    fixup.leftmost_subexpression_with_dot(),
830                );
831                self.word(".yield");
832            }
833            ast::ExprKind::Try(e) => {
834                self.print_expr_cond_paren(
835                    e,
836                    e.precedence() < ExprPrecedence::Unambiguous,
837                    fixup.leftmost_subexpression_with_dot(),
838                );
839                self.word("?")
840            }
841            ast::ExprKind::TryBlock(blk, opt_ty) => {
842                let cb = self.cbox(0);
843                let ib = self.ibox(0);
844                self.word_nbsp("try");
845                if let Some(ty) = opt_ty {
846                    self.word_nbsp("bikeshed");
847                    self.print_type(ty);
848                    self.space();
849                }
850                self.print_block_with_attrs(blk, attrs, cb, ib)
851            }
852            ast::ExprKind::UnsafeBinderCast(kind, expr, ty) => {
853                self.word("builtin # ");
854                match kind {
855                    ast::UnsafeBinderCastKind::Wrap => self.word("wrap_binder"),
856                    ast::UnsafeBinderCastKind::Unwrap => self.word("unwrap_binder"),
857                }
858                self.popen();
859                let ib = self.ibox(0);
860                self.print_expr(expr, FixupContext::default());
861
862                if let Some(ty) = ty {
863                    self.word(",");
864                    self.space();
865                    self.print_type(ty);
866                }
867
868                self.end(ib);
869                self.pclose();
870            }
871            ast::ExprKind::Err(_) => {
872                self.popen();
873                self.word("/*ERROR*/");
874                self.pclose()
875            }
876            ast::ExprKind::Dummy => {
877                self.popen();
878                self.word("/*DUMMY*/");
879                self.pclose();
880            }
881        }
882
883        self.ann.post(self, AnnNode::Expr(expr));
884
885        if needs_par {
886            self.pclose();
887        }
888
889        self.end(ib);
890    }
891
892    fn print_arm(&mut self, arm: &ast::Arm) {
893        // Note, I have no idea why this check is necessary, but here it is.
894        if arm.attrs.is_empty() {
895            self.space();
896        }
897        let cb = self.cbox(INDENT_UNIT);
898        let ib = self.ibox(0);
899        self.maybe_print_comment(arm.pat.span.lo());
900        self.print_outer_attributes(&arm.attrs);
901        self.print_pat(&arm.pat);
902        self.space();
903        if let Some(guard) = &arm.guard {
904            self.word_space("if");
905            self.print_expr(&guard.cond, FixupContext::default());
906            self.space();
907        }
908
909        if let Some(body) = &arm.body {
910            self.word_space("=>");
911
912            match &body.kind {
913                ast::ExprKind::Block(blk, opt_label) => {
914                    if let Some(label) = opt_label {
915                        self.print_ident(label.ident);
916                        self.word_space(":");
917                    }
918
919                    self.print_block_unclosed_indent(blk, ib);
920
921                    // If it is a user-provided unsafe block, print a comma after it.
922                    if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules {
923                        self.word(",");
924                    }
925                }
926                _ => {
927                    self.end(ib);
928                    self.print_expr(body, FixupContext::new_match_arm());
929                    self.word(",");
930                }
931            }
932        } else {
933            self.end(ib);
934            self.word(",");
935        }
936        self.end(cb);
937    }
938
939    fn print_closure_binder(&mut self, binder: &ast::ClosureBinder) {
940        match binder {
941            ast::ClosureBinder::NotPresent => {}
942            ast::ClosureBinder::For { generic_params, .. } => {
943                self.print_formal_generic_params(generic_params)
944            }
945        }
946    }
947
948    fn print_movability(&mut self, movability: ast::Movability) {
949        match movability {
950            ast::Movability::Static => self.word_space("static"),
951            ast::Movability::Movable => {}
952        }
953    }
954
955    fn print_capture_clause(&mut self, capture_clause: ast::CaptureBy) {
956        match capture_clause {
957            ast::CaptureBy::Value { .. } => self.word_space("move"),
958            ast::CaptureBy::Use { .. } => self.word_space("use"),
959            ast::CaptureBy::Ref => {}
960        }
961    }
962}
963
964fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> String {
965    let mut template = "\"".to_string();
966    for piece in pieces {
967        match piece {
968            FormatArgsPiece::Literal(s) => {
969                for c in s.as_str().chars() {
970                    template.extend(c.escape_debug());
971                    if let '{' | '}' = c {
972                        template.push(c);
973                    }
974                }
975            }
976            FormatArgsPiece::Placeholder(p) => {
977                template.push('{');
978                let (Ok(n) | Err(n)) = p.argument.index;
979                template.write_fmt(format_args!("{0}", n))write!(template, "{n}").unwrap();
980                if p.format_options != Default::default() || p.format_trait != FormatTrait::Display
981                {
982                    template.push(':');
983                }
984                if let Some(fill) = p.format_options.fill {
985                    template.push(fill);
986                }
987                match p.format_options.alignment {
988                    Some(FormatAlignment::Left) => template.push('<'),
989                    Some(FormatAlignment::Right) => template.push('>'),
990                    Some(FormatAlignment::Center) => template.push('^'),
991                    None => {}
992                }
993                match p.format_options.sign {
994                    Some(FormatSign::Plus) => template.push('+'),
995                    Some(FormatSign::Minus) => template.push('-'),
996                    None => {}
997                }
998                if p.format_options.alternate {
999                    template.push('#');
1000                }
1001                if p.format_options.zero_pad {
1002                    template.push('0');
1003                }
1004                if let Some(width) = &p.format_options.width {
1005                    match width {
1006                        FormatCount::Literal(n) => template.write_fmt(format_args!("{0}", n))write!(template, "{n}").unwrap(),
1007                        FormatCount::Argument(FormatArgPosition {
1008                            index: Ok(n) | Err(n), ..
1009                        }) => {
1010                            template.write_fmt(format_args!("{0}$", n))write!(template, "{n}$").unwrap();
1011                        }
1012                    }
1013                }
1014                if let Some(precision) = &p.format_options.precision {
1015                    template.push('.');
1016                    match precision {
1017                        FormatCount::Literal(n) => template.write_fmt(format_args!("{0}", n))write!(template, "{n}").unwrap(),
1018                        FormatCount::Argument(FormatArgPosition {
1019                            index: Ok(n) | Err(n), ..
1020                        }) => {
1021                            template.write_fmt(format_args!("{0}$", n))write!(template, "{n}$").unwrap();
1022                        }
1023                    }
1024                }
1025                match p.format_options.debug_hex {
1026                    Some(FormatDebugHex::Lower) => template.push('x'),
1027                    Some(FormatDebugHex::Upper) => template.push('X'),
1028                    None => {}
1029                }
1030                template.push_str(match p.format_trait {
1031                    FormatTrait::Display => "",
1032                    FormatTrait::Debug => "?",
1033                    FormatTrait::LowerExp => "e",
1034                    FormatTrait::UpperExp => "E",
1035                    FormatTrait::Octal => "o",
1036                    FormatTrait::Pointer => "p",
1037                    FormatTrait::Binary => "b",
1038                    FormatTrait::LowerHex => "x",
1039                    FormatTrait::UpperHex => "X",
1040                });
1041                template.push('}');
1042            }
1043        }
1044    }
1045    template.push('"');
1046    template
1047}
1048
1049/// Returns `true` if the printed representation of this expression ends with
1050/// a `.` character — specifically, an unsuffixed float literal like `0.` or
1051/// `45.`. This is used to insert whitespace before range operators (`..`,
1052/// `..=`) so that the dots don't merge (e.g. `0. ..45.` instead of `0...45.`).
1053fn expr_ends_with_dot(expr: &ast::Expr) -> bool {
1054    match &expr.kind {
1055        ast::ExprKind::Lit(token_lit) => {
1056            token_lit.kind == token::Float
1057                && token_lit.suffix.is_none()
1058                && token_lit.symbol.as_str().ends_with('.')
1059        }
1060        _ => false,
1061    }
1062}