about summary refs log tree commit diff
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
parent5eacec9ec7e112a0de1011519a57c45586d58414 (diff)
downloadrust-6cfe98f1962c442504a0b56f260620f1bccd7601.tar.gz
rust-6cfe98f1962c442504a0b56f260620f1bccd7601.zip
Improve error checking on unary plus
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs12
-rw-r--r--src/test/ui/did_you_mean/issue-88276-unary-plus.fixed13
-rw-r--r--src/test/ui/did_you_mean/issue-88276-unary-plus.rs13
-rw-r--r--src/test/ui/did_you_mean/issue-88276-unary-plus.stderr83
4 files changed, 121 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))
             }
diff --git a/src/test/ui/did_you_mean/issue-88276-unary-plus.fixed b/src/test/ui/did_you_mean/issue-88276-unary-plus.fixed
new file mode 100644
index 00000000000..4bd248663b8
--- /dev/null
+++ b/src/test/ui/did_you_mean/issue-88276-unary-plus.fixed
@@ -0,0 +1,13 @@
+// run-rustfix
+#[allow(unused_parens)]
+fn main() {
+    let _ = 1; //~ ERROR leading `+` is not supported
+    let _ = -(1+2)*3; //~ ERROR leading `+` is not supported
+    let _ = -(1+2)*3; //~ ERROR leading `+` is not supported
+                        //~| ERROR leading `+` is not supported
+    let _ = --(1+2)*3; //~ ERROR leading `+` is not supported
+                         //~| ERROR leading `+` is not supported
+    let _ = (&"hello"); //~ ERROR leading `+` is not supported
+    let _ = [3, 4+6]; //~ ERROR leading `+` is not supported
+                        //~| ERROR leading `+` is not supported
+}
diff --git a/src/test/ui/did_you_mean/issue-88276-unary-plus.rs b/src/test/ui/did_you_mean/issue-88276-unary-plus.rs
new file mode 100644
index 00000000000..e2cce677b47
--- /dev/null
+++ b/src/test/ui/did_you_mean/issue-88276-unary-plus.rs
@@ -0,0 +1,13 @@
+// run-rustfix
+#[allow(unused_parens)]
+fn main() {
+    let _ = +1; //~ ERROR leading `+` is not supported
+    let _ = -+(1+2)*3; //~ ERROR leading `+` is not supported
+    let _ = +-+(1+2)*3; //~ ERROR leading `+` is not supported
+                        //~| ERROR leading `+` is not supported
+    let _ = -+-+(1+2)*3; //~ ERROR leading `+` is not supported
+                         //~| ERROR leading `+` is not supported
+    let _ = (+&"hello"); //~ ERROR leading `+` is not supported
+    let _ = +[+3, 4+6]; //~ ERROR leading `+` is not supported
+                        //~| ERROR leading `+` is not supported
+}
diff --git a/src/test/ui/did_you_mean/issue-88276-unary-plus.stderr b/src/test/ui/did_you_mean/issue-88276-unary-plus.stderr
new file mode 100644
index 00000000000..9c1227d7c35
--- /dev/null
+++ b/src/test/ui/did_you_mean/issue-88276-unary-plus.stderr
@@ -0,0 +1,83 @@
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:4:13
+   |
+LL |     let _ = +1;
+   |             ^
+   |             |
+   |             unexpected `+`
+   |             help: remove the `+`
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:5:14
+   |
+LL |     let _ = -+(1+2)*3;
+   |              ^
+   |              |
+   |              unexpected `+`
+   |              help: remove the `+`
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:6:13
+   |
+LL |     let _ = +-+(1+2)*3;
+   |             ^
+   |             |
+   |             unexpected `+`
+   |             help: remove the `+`
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:6:15
+   |
+LL |     let _ = +-+(1+2)*3;
+   |               ^
+   |               |
+   |               unexpected `+`
+   |               help: remove the `+`
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:8:14
+   |
+LL |     let _ = -+-+(1+2)*3;
+   |              ^
+   |              |
+   |              unexpected `+`
+   |              help: remove the `+`
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:8:16
+   |
+LL |     let _ = -+-+(1+2)*3;
+   |                ^
+   |                |
+   |                unexpected `+`
+   |                help: remove the `+`
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:10:14
+   |
+LL |     let _ = (+&"hello");
+   |              ^
+   |              |
+   |              unexpected `+`
+   |              help: remove the `+`
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:11:13
+   |
+LL |     let _ = +[+3, 4+6];
+   |             ^
+   |             |
+   |             unexpected `+`
+   |             help: remove the `+`
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:11:15
+   |
+LL |     let _ = +[+3, 4+6];
+   |               ^
+   |               |
+   |               unexpected `+`
+   |               help: remove the `+`
+
+error: aborting due to 9 previous errors
+