about summary refs log tree commit diff
path: root/src/libsyntax/util/parser.rs
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2019-04-22 19:37:23 -0700
committerEsteban Küber <esteban@kuber.com.ar>2019-04-29 14:07:02 -0700
commitf007e6f442adafae3e5f2f7f635dc12463bbe0bb (patch)
tree7b48de502fcceeefbb8b6767455aa1d7248e4c3e /src/libsyntax/util/parser.rs
parenta55c2eb325029960991508e64650a139b040d24f (diff)
downloadrust-f007e6f442adafae3e5f2f7f635dc12463bbe0bb.tar.gz
rust-f007e6f442adafae3e5f2f7f635dc12463bbe0bb.zip
Identify when a stmt could have been parsed as an expr
There are some expressions that can be parsed as a statement without
a trailing semicolon depending on the context, which can lead to
confusing errors due to the same looking code being accepted in some
places and not others. Identify these cases and suggest enclosing in
parenthesis making the parse non-ambiguous without changing the
accepted grammar.
Diffstat (limited to 'src/libsyntax/util/parser.rs')
-rw-r--r--src/libsyntax/util/parser.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libsyntax/util/parser.rs b/src/libsyntax/util/parser.rs
index 5f15ede7b0b..d76dede8155 100644
--- a/src/libsyntax/util/parser.rs
+++ b/src/libsyntax/util/parser.rs
@@ -207,6 +207,28 @@ impl AssocOp {
             ObsoleteInPlace | Assign | AssignOp(_) | As | DotDot | DotDotEq | Colon => None
         }
     }
+
+    pub fn can_continue_expr_unambiguously(&self) -> bool {
+        use AssocOp::*;
+        match self {
+            BitXor | // `{ 42 } ^ 3`
+            Assign | // `{ 42 } = { 42 }`
+            Divide | // `{ 42 } / 42`
+            Modulus | // `{ 42 } % 2`
+            ShiftRight | // `{ 42 } >> 2`
+            LessEqual | // `{ 42 } <= 3`
+            Greater | // `{ 42 } > 3`
+            GreaterEqual | // `{ 42 } >= 3`
+            AssignOp(_) | // `{ 42 } +=`
+            LAnd | // `{ 42 } &&foo`
+            As | // `{ 42 } as usize`
+            // Equal | // `{ 42 } == { 42 }`    Accepting these here would regress incorrect
+            // NotEqual | // `{ 42 } != { 42 }  struct literals parser recovery.
+            Colon => true, // `{ 42 }: usize`
+            _ => false,
+        }
+
+    }
 }
 
 pub const PREC_RESET: i8 = -100;