about summary refs log tree commit diff
path: root/src/libsyntax/util/parser.rs
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-06-18 01:28:20 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-06-23 01:29:29 +0200
commit851066f57e19b645e2dd33392ee7f822cbb2e374 (patch)
treef87ee3c3ecf6ec822e06cae2a4cf8cf11b815425 /src/libsyntax/util/parser.rs
parent7465eb44f0288da9b48c86881b9e88e73d8f27dd (diff)
downloadrust-851066f57e19b645e2dd33392ee7f822cbb2e374.tar.gz
rust-851066f57e19b645e2dd33392ee7f822cbb2e374.zip
let_chains: Fix bugs in pretty printing.
Diffstat (limited to 'src/libsyntax/util/parser.rs')
-rw-r--r--src/libsyntax/util/parser.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/libsyntax/util/parser.rs b/src/libsyntax/util/parser.rs
index 81b5b085937..1e52186a106 100644
--- a/src/libsyntax/util/parser.rs
+++ b/src/libsyntax/util/parser.rs
@@ -318,6 +318,9 @@ impl ExprPrecedence {
             ExprPrecedence::Box |
             ExprPrecedence::AddrOf |
             // Here `let pats = expr` has `let pats =` as a "unary" prefix of `expr`.
+            // However, this is not exactly right. When `let _ = a` is the LHS of a binop we
+            // need parens sometimes. E.g. we can print `(let _ = a) && b` as `let _ = a && b`
+            // but we need to print `(let _ = a) < b` as-is with parens.
             ExprPrecedence::Let |
             ExprPrecedence::Unary => PREC_PREFIX,
 
@@ -352,6 +355,19 @@ impl ExprPrecedence {
     }
 }
 
+/// In `let p = e`, operators with precedence `<=` this one requires parenthesis in `e`.
+crate fn prec_let_scrutinee_needs_par() -> usize {
+    AssocOp::LAnd.precedence()
+}
+
+/// Suppose we have `let _ = e` and the `order` of `e`.
+/// Is the `order` such that `e` in `let _ = e` needs parenthesis when it is on the RHS?
+///
+/// Conversely, suppose that we have `(let _ = a) OP b` and `order` is that of `OP`.
+/// Can we print this as `let _ = a OP b`?
+crate fn needs_par_as_let_scrutinee(order: i8) -> bool {
+    order <= prec_let_scrutinee_needs_par() as i8
+}
 
 /// Expressions that syntactically contain an "exterior" struct literal i.e., not surrounded by any
 /// parens or other delimiters, e.g., `X { y: 1 }`, `X { y: 1 }.method()`, `foo == X { y: 1 }` and