Pretty print parens around casts on the LHS of '<'

When pretty printing a cast expression occuring on the LHS of a '<'
or '<<' expression, we should add parens around the cast. Otherwise,
the '<'/'<<' gets interpreted as the beginning of the generics for
the type on the RHS of the cast.
This commit is contained in:
Alec Theriault
2017-11-05 09:28:00 -08:00
parent 59d484575a
commit 45a0aa4b4d
2 changed files with 18 additions and 0 deletions

View File

@@ -1986,6 +1986,15 @@ impl<'a> State<'a> {
Fixity::None => (prec + 1, prec + 1),
};
let left_prec = match (&lhs.node, op.node) {
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
(&ast::ExprKind::Cast { .. }, ast::BinOpKind::Lt) |
(&ast::ExprKind::Cast { .. }, ast::BinOpKind::Shl) => parser::PREC_FORCE_PAREN,
_ => left_prec,
};
self.print_expr_maybe_paren(lhs, left_prec)?;
self.s.space()?;
self.word_space(op.node.to_string())?;