1 use crate::pp
::Breaks
::Inconsistent
;
2 use crate::pprust
::state
::{AnnNode, IterDelimited, PrintState, State, INDENT_UNIT}
;
5 use rustc_ast
::util
::parser
::{self, AssocOp, Fixity}
;
6 use rustc_ast
::{self as ast, BlockCheckMode}
;
9 fn print_else(&mut self, els
: Option
<&ast
::Expr
>) {
10 if let Some(_else
) = els
{
12 // Another `else if` block.
13 ast
::ExprKind
::If(ref i
, ref then
, ref e
) => {
14 self.cbox(INDENT_UNIT
- 1);
16 self.word(" else if ");
17 self.print_expr_as_cond(i
);
19 self.print_block(then
);
20 self.print_else(e
.as_deref())
22 // Final `else` block.
23 ast
::ExprKind
::Block(ref b
, _
) => {
24 self.cbox(INDENT_UNIT
- 1);
29 // Constraints would be great here!
31 panic
!("print_if saw if with weird alternative");
37 fn print_if(&mut self, test
: &ast
::Expr
, blk
: &ast
::Block
, elseopt
: Option
<&ast
::Expr
>) {
39 self.print_expr_as_cond(test
);
41 self.print_block(blk
);
42 self.print_else(elseopt
)
45 fn print_call_post(&mut self, args
: &[P
<ast
::Expr
>]) {
47 self.commasep_exprs(Inconsistent
, args
);
51 fn print_expr_maybe_paren(&mut self, expr
: &ast
::Expr
, prec
: i8) {
52 self.print_expr_cond_paren(expr
, expr
.precedence().order() < prec
)
55 /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
56 /// `if cond { ... }`.
57 fn print_expr_as_cond(&mut self, expr
: &ast
::Expr
) {
58 self.print_expr_cond_paren(expr
, Self::cond_needs_par(expr
))
61 // Does `expr` need parentheses when printed in a condition position?
63 // These cases need parens due to the parse error observed in #26461: `if return {}`
64 // parses as the erroneous construct `if (return {})`, not `if (return) {}`.
65 pub(super) fn cond_needs_par(expr
: &ast
::Expr
) -> bool
{
67 ast
::ExprKind
::Break(..) | ast
::ExprKind
::Closure(..) | ast
::ExprKind
::Ret(..) => true,
68 _
=> parser
::contains_exterior_struct_lit(expr
),
72 /// Prints `expr` or `(expr)` when `needs_par` holds.
73 pub(super) fn print_expr_cond_paren(&mut self, expr
: &ast
::Expr
, needs_par
: bool
) {
77 self.print_expr(expr
);
83 fn print_expr_vec(&mut self, exprs
: &[P
<ast
::Expr
>]) {
84 self.ibox(INDENT_UNIT
);
86 self.commasep_exprs(Inconsistent
, exprs
);
91 pub(super) fn print_expr_anon_const(&mut self, expr
: &ast
::AnonConst
) {
92 self.ibox(INDENT_UNIT
);
94 self.print_expr(&expr
.value
);
98 fn print_expr_repeat(&mut self, element
: &ast
::Expr
, count
: &ast
::AnonConst
) {
99 self.ibox(INDENT_UNIT
);
101 self.print_expr(element
);
102 self.word_space(";");
103 self.print_expr(&count
.value
);
108 fn print_expr_struct(
110 qself
: &Option
<ast
::QSelf
>,
112 fields
: &[ast
::ExprField
],
113 rest
: &ast
::StructRest
,
115 if let Some(qself
) = qself
{
116 self.print_qpath(path
, qself
, true);
118 self.print_path(path
, true, 0);
122 let has_rest
= match rest
{
123 ast
::StructRest
::Base(_
) | ast
::StructRest
::Rest(_
) => true,
124 ast
::StructRest
::None
=> false,
126 if fields
.is_empty() && !has_rest
{
131 for field
in fields
.iter().delimited() {
132 self.maybe_print_comment(field
.span
.hi());
133 self.print_outer_attributes(&field
.attrs
);
135 self.space_if_not_bol();
137 if !field
.is_shorthand
{
138 self.print_ident(field
.ident
);
141 self.print_expr(&field
.expr
);
142 if !field
.is_last
|| has_rest
{
143 self.word_space(",");
145 self.trailing_comma_or_space();
149 if fields
.is_empty() {
153 if let ast
::StructRest
::Base(expr
) = rest
{
154 self.print_expr(expr
);
158 self.offset(-INDENT_UNIT
);
163 fn print_expr_tup(&mut self, exprs
: &[P
<ast
::Expr
>]) {
165 self.commasep_exprs(Inconsistent
, exprs
);
166 if exprs
.len() == 1 {
172 fn print_expr_call(&mut self, func
: &ast
::Expr
, args
: &[P
<ast
::Expr
>]) {
173 let prec
= match func
.kind
{
174 ast
::ExprKind
::Field(..) => parser
::PREC_FORCE_PAREN
,
175 _
=> parser
::PREC_POSTFIX
,
178 self.print_expr_maybe_paren(func
, prec
);
179 self.print_call_post(args
)
182 fn print_expr_method_call(&mut self, segment
: &ast
::PathSegment
, args
: &[P
<ast
::Expr
>]) {
183 let base_args
= &args
[1..];
184 self.print_expr_maybe_paren(&args
[0], parser
::PREC_POSTFIX
);
186 self.print_ident(segment
.ident
);
187 if let Some(ref args
) = segment
.args
{
188 self.print_generic_args(args
, true);
190 self.print_call_post(base_args
)
193 fn print_expr_binary(&mut self, op
: ast
::BinOp
, lhs
: &ast
::Expr
, rhs
: &ast
::Expr
) {
194 let assoc_op
= AssocOp
::from_ast_binop(op
.node
);
195 let prec
= assoc_op
.precedence() as i8;
196 let fixity
= assoc_op
.fixity();
198 let (left_prec
, right_prec
) = match fixity
{
199 Fixity
::Left
=> (prec
, prec
+ 1),
200 Fixity
::Right
=> (prec
+ 1, prec
),
201 Fixity
::None
=> (prec
+ 1, prec
+ 1),
204 let left_prec
= match (&lhs
.kind
, op
.node
) {
205 // These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
206 // the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
207 // of `(x as i32) < ...`. We need to convince it _not_ to do that.
208 (&ast
::ExprKind
::Cast { .. }
, ast
::BinOpKind
::Lt
| ast
::BinOpKind
::Shl
) => {
209 parser
::PREC_FORCE_PAREN
211 // We are given `(let _ = a) OP b`.
213 // - When `OP <= LAnd` we should print `let _ = a OP b` to avoid redundant parens
214 // as the parser will interpret this as `(let _ = a) OP b`.
216 // - Otherwise, e.g. when we have `(let a = b) < c` in AST,
217 // parens are required since the parser would interpret `let a = b < c` as
218 // `let a = (b < c)`. To achieve this, we force parens.
219 (&ast
::ExprKind
::Let { .. }
, _
) if !parser
::needs_par_as_let_scrutinee(prec
) => {
220 parser
::PREC_FORCE_PAREN
225 self.print_expr_maybe_paren(lhs
, left_prec
);
227 self.word_space(op
.node
.to_string());
228 self.print_expr_maybe_paren(rhs
, right_prec
)
231 fn print_expr_unary(&mut self, op
: ast
::UnOp
, expr
: &ast
::Expr
) {
232 self.word(ast
::UnOp
::to_string(op
));
233 self.print_expr_maybe_paren(expr
, parser
::PREC_PREFIX
)
236 fn print_expr_addr_of(
238 kind
: ast
::BorrowKind
,
239 mutability
: ast
::Mutability
,
244 ast
::BorrowKind
::Ref
=> self.print_mutability(mutability
, false),
245 ast
::BorrowKind
::Raw
=> {
246 self.word_nbsp("raw");
247 self.print_mutability(mutability
, true);
250 self.print_expr_maybe_paren(expr
, parser
::PREC_PREFIX
)
253 pub fn print_expr(&mut self, expr
: &ast
::Expr
) {
254 self.print_expr_outer_attr_style(expr
, true)
257 pub(super) fn print_expr_outer_attr_style(&mut self, expr
: &ast
::Expr
, is_inline
: bool
) {
258 self.maybe_print_comment(expr
.span
.lo());
260 let attrs
= &expr
.attrs
;
262 self.print_outer_attributes_inline(attrs
);
264 self.print_outer_attributes(attrs
);
267 self.ibox(INDENT_UNIT
);
268 self.ann
.pre(self, AnnNode
::Expr(expr
));
270 ast
::ExprKind
::Box(ref expr
) => {
271 self.word_space("box");
272 self.print_expr_maybe_paren(expr
, parser
::PREC_PREFIX
);
274 ast
::ExprKind
::Array(ref exprs
) => {
275 self.print_expr_vec(exprs
);
277 ast
::ExprKind
::ConstBlock(ref anon_const
) => {
278 self.print_expr_anon_const(anon_const
);
280 ast
::ExprKind
::Repeat(ref element
, ref count
) => {
281 self.print_expr_repeat(element
, count
);
283 ast
::ExprKind
::Struct(ref se
) => {
284 self.print_expr_struct(&se
.qself
, &se
.path
, &se
.fields
, &se
.rest
);
286 ast
::ExprKind
::Tup(ref exprs
) => {
287 self.print_expr_tup(exprs
);
289 ast
::ExprKind
::Call(ref func
, ref args
) => {
290 self.print_expr_call(func
, &args
);
292 ast
::ExprKind
::MethodCall(ref segment
, ref args
, _
) => {
293 self.print_expr_method_call(segment
, &args
);
295 ast
::ExprKind
::Binary(op
, ref lhs
, ref rhs
) => {
296 self.print_expr_binary(op
, lhs
, rhs
);
298 ast
::ExprKind
::Unary(op
, ref expr
) => {
299 self.print_expr_unary(op
, expr
);
301 ast
::ExprKind
::AddrOf(k
, m
, ref expr
) => {
302 self.print_expr_addr_of(k
, m
, expr
);
304 ast
::ExprKind
::Lit(ref lit
) => {
305 self.print_literal(lit
);
307 ast
::ExprKind
::Cast(ref expr
, ref ty
) => {
308 let prec
= AssocOp
::As
.precedence() as i8;
309 self.print_expr_maybe_paren(expr
, prec
);
311 self.word_space("as");
314 ast
::ExprKind
::Type(ref expr
, ref ty
) => {
315 let prec
= AssocOp
::Colon
.precedence() as i8;
316 self.print_expr_maybe_paren(expr
, prec
);
317 self.word_space(":");
320 ast
::ExprKind
::Let(ref pat
, ref scrutinee
, _
) => {
321 self.print_let(pat
, scrutinee
);
323 ast
::ExprKind
::If(ref test
, ref blk
, ref elseopt
) => {
324 self.print_if(test
, blk
, elseopt
.as_deref())
326 ast
::ExprKind
::While(ref test
, ref blk
, opt_label
) => {
327 if let Some(label
) = opt_label
{
328 self.print_ident(label
.ident
);
329 self.word_space(":");
333 self.word_nbsp("while");
334 self.print_expr_as_cond(test
);
336 self.print_block_with_attrs(blk
, attrs
);
338 ast
::ExprKind
::ForLoop(ref pat
, ref iter
, ref blk
, opt_label
) => {
339 if let Some(label
) = opt_label
{
340 self.print_ident(label
.ident
);
341 self.word_space(":");
345 self.word_nbsp("for");
348 self.word_space("in");
349 self.print_expr_as_cond(iter
);
351 self.print_block_with_attrs(blk
, attrs
);
353 ast
::ExprKind
::Loop(ref blk
, opt_label
) => {
354 if let Some(label
) = opt_label
{
355 self.print_ident(label
.ident
);
356 self.word_space(":");
360 self.word_nbsp("loop");
361 self.print_block_with_attrs(blk
, attrs
);
363 ast
::ExprKind
::Match(ref expr
, ref arms
) => {
366 self.word_nbsp("match");
367 self.print_expr_as_cond(expr
);
370 self.print_inner_attributes_no_trailing_hardbreak(attrs
);
374 let empty
= attrs
.is_empty() && arms
.is_empty();
375 self.bclose(expr
.span
, empty
);
377 ast
::ExprKind
::Closure(
385 self.print_movability(movability
);
386 self.print_asyncness(asyncness
);
387 self.print_capture_clause(capture_clause
);
389 self.print_fn_params_and_ret(decl
, true);
391 self.print_expr(body
);
392 self.end(); // need to close a box
394 // a box will be closed by print_expr, but we didn't want an overall
395 // wrapper so we closed the corresponding opening. so create an
396 // empty box to satisfy the close.
399 ast
::ExprKind
::Block(ref blk
, opt_label
) => {
400 if let Some(label
) = opt_label
{
401 self.print_ident(label
.ident
);
402 self.word_space(":");
404 // containing cbox, will be closed by print-block at }
406 // head-box, will be closed by print-block after {
408 self.print_block_with_attrs(blk
, attrs
);
410 ast
::ExprKind
::Async(capture_clause
, _
, ref blk
) => {
411 self.word_nbsp("async");
412 self.print_capture_clause(capture_clause
);
413 // cbox/ibox in analogy to the `ExprKind::Block` arm above
416 self.print_block_with_attrs(blk
, attrs
);
418 ast
::ExprKind
::Await(ref expr
) => {
419 self.print_expr_maybe_paren(expr
, parser
::PREC_POSTFIX
);
422 ast
::ExprKind
::Assign(ref lhs
, ref rhs
, _
) => {
423 let prec
= AssocOp
::Assign
.precedence() as i8;
424 self.print_expr_maybe_paren(lhs
, prec
+ 1);
426 self.word_space("=");
427 self.print_expr_maybe_paren(rhs
, prec
);
429 ast
::ExprKind
::AssignOp(op
, ref lhs
, ref rhs
) => {
430 let prec
= AssocOp
::Assign
.precedence() as i8;
431 self.print_expr_maybe_paren(lhs
, prec
+ 1);
433 self.word(op
.node
.to_string());
434 self.word_space("=");
435 self.print_expr_maybe_paren(rhs
, prec
);
437 ast
::ExprKind
::Field(ref expr
, ident
) => {
438 self.print_expr_maybe_paren(expr
, parser
::PREC_POSTFIX
);
440 self.print_ident(ident
);
442 ast
::ExprKind
::Index(ref expr
, ref index
) => {
443 self.print_expr_maybe_paren(expr
, parser
::PREC_POSTFIX
);
445 self.print_expr(index
);
448 ast
::ExprKind
::Range(ref start
, ref end
, limits
) => {
449 // Special case for `Range`. `AssocOp` claims that `Range` has higher precedence
450 // than `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`.
451 // Here we use a fake precedence value so that any child with lower precedence than
452 // a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
453 let fake_prec
= AssocOp
::LOr
.precedence() as i8;
454 if let Some(ref e
) = *start
{
455 self.print_expr_maybe_paren(e
, fake_prec
);
457 if limits
== ast
::RangeLimits
::HalfOpen
{
462 if let Some(ref e
) = *end
{
463 self.print_expr_maybe_paren(e
, fake_prec
);
466 ast
::ExprKind
::Underscore
=> self.word("_"),
467 ast
::ExprKind
::Path(None
, ref path
) => self.print_path(path
, true, 0),
468 ast
::ExprKind
::Path(Some(ref qself
), ref path
) => self.print_qpath(path
, qself
, true),
469 ast
::ExprKind
::Break(opt_label
, ref opt_expr
) => {
471 if let Some(label
) = opt_label
{
473 self.print_ident(label
.ident
);
475 if let Some(ref expr
) = *opt_expr
{
477 self.print_expr_maybe_paren(expr
, parser
::PREC_JUMP
);
480 ast
::ExprKind
::Continue(opt_label
) => {
481 self.word("continue");
482 if let Some(label
) = opt_label
{
484 self.print_ident(label
.ident
);
487 ast
::ExprKind
::Ret(ref result
) => {
489 if let Some(ref expr
) = *result
{
491 self.print_expr_maybe_paren(expr
, parser
::PREC_JUMP
);
494 ast
::ExprKind
::InlineAsm(ref a
) => {
496 self.print_inline_asm(a
);
498 ast
::ExprKind
::MacCall(ref m
) => self.print_mac(m
),
499 ast
::ExprKind
::Paren(ref e
) => {
504 ast
::ExprKind
::Yield(ref e
) => {
507 if let Some(ref expr
) = *e
{
509 self.print_expr_maybe_paren(expr
, parser
::PREC_JUMP
);
512 ast
::ExprKind
::Try(ref e
) => {
513 self.print_expr_maybe_paren(e
, parser
::PREC_POSTFIX
);
516 ast
::ExprKind
::TryBlock(ref blk
) => {
519 self.word_nbsp("try");
520 self.print_block_with_attrs(blk
, attrs
)
522 ast
::ExprKind
::Err
=> {
524 self.word("/*ERROR*/");
528 self.ann
.post(self, AnnNode
::Expr(expr
));
532 fn print_arm(&mut self, arm
: &ast
::Arm
) {
533 // Note, I have no idea why this check is necessary, but here it is.
534 if arm
.attrs
.is_empty() {
537 self.cbox(INDENT_UNIT
);
539 self.maybe_print_comment(arm
.pat
.span
.lo());
540 self.print_outer_attributes(&arm
.attrs
);
541 self.print_pat(&arm
.pat
);
543 if let Some(ref e
) = arm
.guard
{
544 self.word_space("if");
548 self.word_space("=>");
550 match arm
.body
.kind
{
551 ast
::ExprKind
::Block(ref blk
, opt_label
) => {
552 if let Some(label
) = opt_label
{
553 self.print_ident(label
.ident
);
554 self.word_space(":");
557 // The block will close the pattern's ibox.
558 self.print_block_unclosed_indent(blk
);
560 // If it is a user-provided unsafe block, print a comma after it.
561 if let BlockCheckMode
::Unsafe(ast
::UserProvided
) = blk
.rules
{
566 self.end(); // Close the ibox for the pattern.
567 self.print_expr(&arm
.body
);
571 self.end(); // Close enclosing cbox.
574 fn print_movability(&mut self, movability
: ast
::Movability
) {
576 ast
::Movability
::Static
=> self.word_space("static"),
577 ast
::Movability
::Movable
=> {}
581 fn print_capture_clause(&mut self, capture_clause
: ast
::CaptureBy
) {
582 match capture_clause
{
583 ast
::CaptureBy
::Value
=> self.word_space("move"),
584 ast
::CaptureBy
::Ref
=> {}