about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/expr.rs
diff options
context:
space:
mode:
authorTheodore Luo Wang <wangtheo662@gmail.com>2021-08-31 23:07:58 -0400
committerTheodore Luo Wang <wangtheo662@gmail.com>2021-08-31 23:07:58 -0400
commit6cfe98f1962c442504a0b56f260620f1bccd7601 (patch)
tree1a29a7bedd67df30c563cafe273e75280edf562f /compiler/rustc_parse/src/parser/expr.rs
parent5eacec9ec7e112a0de1011519a57c45586d58414 (diff)
downloadrust-6cfe98f1962c442504a0b56f260620f1bccd7601.tar.gz
rust-6cfe98f1962c442504a0b56f260620f1bccd7601.zip
Improve error checking on unary plus
Diffstat (limited to 'compiler/rustc_parse/src/parser/expr.rs')
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs
index 326c8f81ffb..88dae48a901 100644
--- a/compiler/rustc_parse/src/parser/expr.rs
+++ b/compiler/rustc_parse/src/parser/expr.rs
@@ -23,6 +23,8 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
 use rustc_span::{BytePos, Pos};
 use std::mem;
 
+use tracing::debug;
+
 /// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression
 /// dropped into the token stream, which happens while parsing the result of
 /// macro expansion). Placement of these is not as complex as I feared it would
@@ -355,6 +357,7 @@ impl<'a> Parser<'a> {
     /// but the next token implies this should be parsed as an expression.
     /// For example: `if let Some(x) = x { x } else { 0 } / 2`.
     fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
+        debug!("error_found_expr_would_be_stmt(lhs: {:?})", lhs);
         let mut err = self.struct_span_err(
             self.token.span,
             &format!("expected expression, found `{}`", pprust::token_to_string(&self.token),),
@@ -516,6 +519,15 @@ impl<'a> Parser<'a> {
             token::BinOp(token::And) | token::AndAnd => {
                 make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo))
             }
+            token::BinOp(token::Plus) => {
+                this.struct_span_err(lo, "leading `+` is not supported")
+                    .span_label(lo, "unexpected `+`")
+                    .span_suggestion_short(lo, "remove the `+`", "".to_string(), Applicability::MachineApplicable)
+                    .emit();
+                this.bump();
+
+                this.parse_prefix_expr(None)
+            } // `+expr`
             token::Ident(..) if this.token.is_keyword(kw::Box) => {
                 make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
             }