Skip to content

Commit

Permalink
Fix some double indents on exprs containing blocks
Browse files Browse the repository at this point in the history
The `print_expr` method already places an `ibox(INDENT_UNIT)` around
every expr that gets printed. Some exprs were then using `self.head`
inside of that, which does its own `cbox(INDENT_UNIT)`, resulting in two
levels of indentation:

    while true {
            stuff;
        }

This commit fixes those cases to produce the expected single level of
indentation within every expression containing a block.

    while true {
        stuff;
    }
  • Loading branch information
dtolnay committed Jan 31, 2022
1 parent cb93e9c commit 8ac05b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 16 additions & 8 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Expand Up @@ -320,7 +320,9 @@ impl<'a> State<'a> {
self.print_ident(label.ident);
self.word_space(":");
}
self.head("while");
self.cbox(0);
self.ibox(0);
self.word_nbsp("while");
self.print_expr_as_cond(test);
self.space();
self.print_block_with_attrs(blk, attrs);
Expand All @@ -330,7 +332,9 @@ impl<'a> State<'a> {
self.print_ident(label.ident);
self.word_space(":");
}
self.head("for");
self.cbox(0);
self.ibox(0);
self.word_nbsp("for");
self.print_pat(pat);
self.space();
self.word_space("in");
Expand All @@ -343,12 +347,14 @@ impl<'a> State<'a> {
self.print_ident(label.ident);
self.word_space(":");
}
self.head("loop");
self.cbox(0);
self.ibox(0);
self.word_nbsp("loop");
self.print_block_with_attrs(blk, attrs);
}
ast::ExprKind::Match(ref expr, ref arms) => {
self.cbox(INDENT_UNIT);
self.ibox(INDENT_UNIT);
self.cbox(0);
self.ibox(0);
self.word_nbsp("match");
self.print_expr_as_cond(expr);
self.space();
Expand Down Expand Up @@ -388,7 +394,7 @@ impl<'a> State<'a> {
self.word_space(":");
}
// containing cbox, will be closed by print-block at }
self.cbox(INDENT_UNIT);
self.cbox(0);
// head-box, will be closed by print-block after {
self.ibox(0);
self.print_block_with_attrs(blk, attrs);
Expand All @@ -397,7 +403,7 @@ impl<'a> State<'a> {
self.word_nbsp("async");
self.print_capture_clause(capture_clause);
// cbox/ibox in analogy to the `ExprKind::Block` arm above
self.cbox(INDENT_UNIT);
self.cbox(0);
self.ibox(0);
self.print_block_with_attrs(blk, attrs);
}
Expand Down Expand Up @@ -500,7 +506,9 @@ impl<'a> State<'a> {
self.word("?")
}
ast::ExprKind::TryBlock(ref blk) => {
self.head("try");
self.cbox(0);
self.ibox(0);
self.word_nbsp("try");
self.print_block_with_attrs(blk, attrs)
}
ast::ExprKind::Err => {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
@@ -1,5 +1,5 @@
use crate::pp::Breaks::Inconsistent;
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
use crate::pprust::state::{AnnNode, PrintState, State};

use rustc_ast as ast;
use rustc_ast::GenericBound;
Expand Down Expand Up @@ -377,7 +377,7 @@ impl<'a> State<'a> {
self.space_if_not_bol();
self.maybe_print_comment(v.span.lo());
self.print_outer_attributes(&v.attrs);
self.ibox(INDENT_UNIT);
self.ibox(0);
self.print_variant(v);
self.word(",");
self.end();
Expand Down

0 comments on commit 8ac05b9

Please sign in to comment.