about summary refs log tree commit diff
path: root/src/libsyntax/print/pprust.rs
diff options
context:
space:
mode:
authorAlec Theriault <alec.theriault@gmail.com>2017-11-05 09:28:00 -0800
committerAlec Theriault <alec.theriault@gmail.com>2017-11-05 09:45:06 -0800
commit45a0aa4b4d4fb124f8db4e8b037465b457e66147 (patch)
tree8b55f099cedf2a099f0d129102e4cabb0da4377a /src/libsyntax/print/pprust.rs
parent59d484575a714291481563d13ad058b9a3d31fa8 (diff)
downloadrust-45a0aa4b4d4fb124f8db4e8b037465b457e66147.tar.gz
rust-45a0aa4b4d4fb124f8db4e8b037465b457e66147.zip
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.
Diffstat (limited to 'src/libsyntax/print/pprust.rs')
-rw-r--r--src/libsyntax/print/pprust.rs9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 8a970fd4098..e6ffbb2cce9 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -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())?;