From 4212835d993537158aa39406a489351a4efdda71 Mon Sep 17 00:00:00 2001 From: Noah Lev <camelidcamel@gmail.com> Date: Thu, 17 Feb 2022 16:30:48 -0800 Subject: [PATCH] Add heuristic to avoid treating `x + +2` as increment --- compiler/rustc_parse/src/parser/expr.rs | 1 + src/test/ui/associated-types/issue-36499.stderr | 14 +++++--------- .../ui/parser/issues/issue-88276-unary-plus.stderr | 14 +++++--------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 8ac29c2b5f6..8c4ec8dc6e3 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -269,6 +269,7 @@ impl<'a> Parser<'a> { if self.prev_token == token::BinOp(token::Plus) && self.token == token::BinOp(token::Plus) + && self.prev_token.span.between(self.token.span).is_empty() { let op_span = self.prev_token.span.to(self.token.span); // Eat the second `+` diff --git a/src/test/ui/associated-types/issue-36499.stderr b/src/test/ui/associated-types/issue-36499.stderr index b49a34be98f..610798d880f 100644 --- a/src/test/ui/associated-types/issue-36499.stderr +++ b/src/test/ui/associated-types/issue-36499.stderr @@ -1,17 +1,13 @@ -error: Rust has no postfix increment operator - --> $DIR/issue-36499.rs:4:7 +error: leading `+` is not supported + --> $DIR/issue-36499.rs:4:9 | LL | 2 + +2; - | ^^^ not a valid postfix operator + | ^ unexpected `+` | -help: use `+= 1` instead - | -LL | { let tmp = 2 ; 2 += 1; tmp }2; - | +++++++++++ ~~~~~~~~~~~~~~~ -help: or, if you don't need to use it as an expression, change it to this +help: try removing the `+` | LL - 2 + +2; -LL + 2 += 12; +LL + 2 + 2; | error: aborting due to previous error diff --git a/src/test/ui/parser/issues/issue-88276-unary-plus.stderr b/src/test/ui/parser/issues/issue-88276-unary-plus.stderr index e65bac2c5cc..b26761729a8 100644 --- a/src/test/ui/parser/issues/issue-88276-unary-plus.stderr +++ b/src/test/ui/parser/issues/issue-88276-unary-plus.stderr @@ -10,20 +10,16 @@ LL - let _ = +1; LL + let _ = 1; | -error: Rust has no postfix increment operator - --> $DIR/issue-88276-unary-plus.rs:5:18 +error: leading `+` is not supported + --> $DIR/issue-88276-unary-plus.rs:5:20 | LL | let _ = (1.0 + +2.0) * +3.0; - | ^^^ not a valid postfix operator + | ^ unexpected `+` | -help: use `+= 1` instead - | -LL | let _ = ({ let tmp = 1.0 ; 1.0 += 1; tmp }2.0) * +3.0; - | +++++++++++ ~~~~~~~~~~~~~~~~~ -help: or, if you don't need to use it as an expression, change it to this +help: try removing the `+` | LL - let _ = (1.0 + +2.0) * +3.0; -LL + let _ = (1.0 += 12.0) * +3.0; +LL + let _ = (1.0 + 2.0) * +3.0; | error: leading `+` is not supported