From 65eb7e516c798a9447dbfe1d02aeeb314d4fec54 Mon Sep 17 00:00:00 2001 From: Theodore Luo Wang Date: Sat, 4 Sep 2021 22:35:59 -0400 Subject: [PATCH] Use verbose suggestions and only match if the + is seen before a numeric literal --- compiler/rustc_ast/src/token.rs | 4 + compiler/rustc_parse/src/parser/expr.rs | 7 +- .../ui/associated-types/issue-36499.stderr | 11 ++- src/test/ui/parser/expr-as-stmt.fixed | 4 +- src/test/ui/parser/expr-as-stmt.rs | 4 +- src/test/ui/parser/expr-as-stmt.stderr | 8 +- .../ui/parser/issue-88276-unary-plus.fixed | 7 +- src/test/ui/parser/issue-88276-unary-plus.rs | 9 +- .../ui/parser/issue-88276-unary-plus.stderr | 93 ++++++------------- 9 files changed, 55 insertions(+), 92 deletions(-) diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index 710a592e258..e467bd963bf 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -586,6 +586,10 @@ pub fn is_bool_lit(&self) -> bool { self.is_non_raw_ident_where(|id| id.name.is_bool_lit()) } + pub fn is_numeric_lit(&self) -> bool { + matches!(self.kind, Literal(Lit { kind: LitKind::Integer, ..}) | Literal(Lit { kind: LitKind::Float, ..})) + } + /// Returns `true` if the token is a non-raw identifier for which `pred` holds. pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool { match self.ident() { diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 4463d2fc6c8..83e1c5ff753 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -516,16 +516,15 @@ macro_rules! make_it { token::BinOp(token::And) | token::AndAnd => { make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo)) } - token::BinOp(token::Plus) => { + token::BinOp(token::Plus) if this.look_ahead(1, |tok| tok.is_numeric_lit()) => { let mut err = this.struct_span_err(lo, "leading `+` is not supported"); err.span_label(lo, "unexpected `+`"); // a block on the LHS might have been intended to be an expression instead - let sp = this.sess.source_map().start_point(lo); - if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&sp) { + if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&lo) { this.sess.expr_parentheses_needed(&mut err, *sp); } else { - err.span_suggestion( + err.span_suggestion_verbose( lo, "try removing the `+`", "".to_string(), diff --git a/src/test/ui/associated-types/issue-36499.stderr b/src/test/ui/associated-types/issue-36499.stderr index 990ef09e3f7..610798d880f 100644 --- a/src/test/ui/associated-types/issue-36499.stderr +++ b/src/test/ui/associated-types/issue-36499.stderr @@ -2,10 +2,13 @@ error: leading `+` is not supported --> $DIR/issue-36499.rs:4:9 | LL | 2 + +2; - | ^ - | | - | unexpected `+` - | help: try removing the `+` + | ^ unexpected `+` + | +help: try removing the `+` + | +LL - 2 + +2; +LL + 2 + 2; + | error: aborting due to previous error diff --git a/src/test/ui/parser/expr-as-stmt.fixed b/src/test/ui/parser/expr-as-stmt.fixed index 79d73090a04..5f5e25991e0 100644 --- a/src/test/ui/parser/expr-as-stmt.fixed +++ b/src/test/ui/parser/expr-as-stmt.fixed @@ -5,7 +5,7 @@ #![allow(unused_must_use)] fn foo() -> i32 { - ({2}) + {2} //~ ERROR leading `+` is not supported + ({2}) + {2} //~ ERROR expected expression, found `+` //~^ ERROR mismatched types } @@ -16,7 +16,7 @@ fn bar() -> i32 { fn zul() -> u32 { let foo = 3; - ({ 42 }) + foo; //~ ERROR leading `+` is not supported + ({ 42 }) + foo; //~ ERROR expected expression, found `+` //~^ ERROR mismatched types 32 } diff --git a/src/test/ui/parser/expr-as-stmt.rs b/src/test/ui/parser/expr-as-stmt.rs index 8698f99b81a..5428e1c32fe 100644 --- a/src/test/ui/parser/expr-as-stmt.rs +++ b/src/test/ui/parser/expr-as-stmt.rs @@ -5,7 +5,7 @@ #![allow(unused_must_use)] fn foo() -> i32 { - {2} + {2} //~ ERROR leading `+` is not supported + {2} + {2} //~ ERROR expected expression, found `+` //~^ ERROR mismatched types } @@ -16,7 +16,7 @@ fn bar() -> i32 { fn zul() -> u32 { let foo = 3; - { 42 } + foo; //~ ERROR leading `+` is not supported + { 42 } + foo; //~ ERROR expected expression, found `+` //~^ ERROR mismatched types 32 } diff --git a/src/test/ui/parser/expr-as-stmt.stderr b/src/test/ui/parser/expr-as-stmt.stderr index 91f97c4662a..d99e9be0000 100644 --- a/src/test/ui/parser/expr-as-stmt.stderr +++ b/src/test/ui/parser/expr-as-stmt.stderr @@ -1,8 +1,8 @@ -error: leading `+` is not supported +error: expected expression, found `+` --> $DIR/expr-as-stmt.rs:8:9 | LL | {2} + {2} - | ^ unexpected `+` + | ^ expected expression | help: parentheses are required to parse this as an expression | @@ -20,11 +20,11 @@ help: parentheses are required to parse this as an expression LL | ({2}) + 2 | + + -error: leading `+` is not supported +error: expected expression, found `+` --> $DIR/expr-as-stmt.rs:19:12 | LL | { 42 } + foo; - | ^ unexpected `+` + | ^ expected expression | help: parentheses are required to parse this as an expression | diff --git a/src/test/ui/parser/issue-88276-unary-plus.fixed b/src/test/ui/parser/issue-88276-unary-plus.fixed index 279cdb5060a..25b7c340f60 100644 --- a/src/test/ui/parser/issue-88276-unary-plus.fixed +++ b/src/test/ui/parser/issue-88276-unary-plus.fixed @@ -2,12 +2,7 @@ #[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 + let _ = (1.0 + 2.0) * 3.0; //~ 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/parser/issue-88276-unary-plus.rs b/src/test/ui/parser/issue-88276-unary-plus.rs index a72dad4dc71..11b2e9d6016 100644 --- a/src/test/ui/parser/issue-88276-unary-plus.rs +++ b/src/test/ui/parser/issue-88276-unary-plus.rs @@ -2,12 +2,7 @@ #[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 + let _ = (1.0 + +2.0) * +3.0; //~ 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 + let _ = [+3, 4+6]; //~ ERROR leading `+` is not supported } diff --git a/src/test/ui/parser/issue-88276-unary-plus.stderr b/src/test/ui/parser/issue-88276-unary-plus.stderr index 255839bc684..b26761729a8 100644 --- a/src/test/ui/parser/issue-88276-unary-plus.stderr +++ b/src/test/ui/parser/issue-88276-unary-plus.stderr @@ -2,82 +2,49 @@ error: leading `+` is not supported --> $DIR/issue-88276-unary-plus.rs:4:13 | LL | let _ = +1; - | ^ - | | - | unexpected `+` - | help: try removing the `+` - -error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:5:14 + | ^ unexpected `+` | -LL | let _ = -+(1+2)*3; - | ^ - | | - | unexpected `+` - | help: try removing the `+` - -error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:6:14 +help: try removing the `+` | -LL | let _ = -+-+(1+2)*3; - | ^ - | | - | unexpected `+` - | help: try removing the `+` +LL - let _ = +1; +LL + let _ = 1; + | error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:6:16 + --> $DIR/issue-88276-unary-plus.rs:5:20 | -LL | let _ = -+-+(1+2)*3; - | ^ - | | - | unexpected `+` - | help: try removing the `+` - -error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:8:18 +LL | let _ = (1.0 + +2.0) * +3.0; + | ^ unexpected `+` | -LL | let _ = (1 + +2) * +3; - | ^ - | | - | unexpected `+` - | help: try removing the `+` - -error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:8:24 +help: try removing the `+` | -LL | let _ = (1 + +2) * +3; - | ^ - | | - | unexpected `+` - | help: try removing the `+` +LL - let _ = (1.0 + +2.0) * +3.0; +LL + let _ = (1.0 + 2.0) * +3.0; + | error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:10:14 + --> $DIR/issue-88276-unary-plus.rs:5:28 | -LL | let _ = (+&"hello"); - | ^ - | | - | unexpected `+` - | help: try removing the `+` - -error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:11:13 +LL | let _ = (1.0 + +2.0) * +3.0; + | ^ unexpected `+` | -LL | let _ = +[+3, 4+6]; - | ^ - | | - | unexpected `+` - | help: try removing the `+` +help: try removing the `+` + | +LL - let _ = (1.0 + +2.0) * +3.0; +LL + let _ = (1.0 + +2.0) * 3.0; + | error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:11:15 + --> $DIR/issue-88276-unary-plus.rs:7:14 + | +LL | let _ = [+3, 4+6]; + | ^ unexpected `+` + | +help: try removing the `+` | -LL | let _ = +[+3, 4+6]; - | ^ - | | - | unexpected `+` - | help: try removing the `+` +LL - let _ = [+3, 4+6]; +LL + let _ = [3, 4+6]; + | -error: aborting due to 9 previous errors +error: aborting due to 4 previous errors -- 2.44.0