From 6cfe98f1962c442504a0b56f260620f1bccd7601 Mon Sep 17 00:00:00 2001 From: Theodore Luo Wang Date: Tue, 31 Aug 2021 23:07:58 -0400 Subject: [PATCH 1/7] Improve error checking on unary plus --- compiler/rustc_parse/src/parser/expr.rs | 12 +++ .../did_you_mean/issue-88276-unary-plus.fixed | 13 +++ .../ui/did_you_mean/issue-88276-unary-plus.rs | 13 +++ .../issue-88276-unary-plus.stderr | 83 +++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 src/test/ui/did_you_mean/issue-88276-unary-plus.fixed create mode 100644 src/test/ui/did_you_mean/issue-88276-unary-plus.rs create mode 100644 src/test/ui/did_you_mean/issue-88276-unary-plus.stderr diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 326c8f81ffbf9..88dae48a9013d 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 0000000000000..4bd248663b884 --- /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 0000000000000..e2cce677b4755 --- /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 0000000000000..9c1227d7c351a --- /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 + From e7fb98e7258557034f0d0caf81a53b0070a87297 Mon Sep 17 00:00:00 2001 From: Theodore Luo Wang Date: Tue, 31 Aug 2021 23:09:43 -0400 Subject: [PATCH 2/7] Apply formatting --- compiler/rustc_parse/src/parser/expr.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 88dae48a9013d..43de53e87231f 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -522,7 +522,12 @@ impl<'a> Parser<'a> { 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) + .span_suggestion_short( + lo, + "remove the `+`", + "".to_string(), + Applicability::MachineApplicable, + ) .emit(); this.bump(); From 5a863d594c725d09ab8f0df5caba374d72200976 Mon Sep 17 00:00:00 2001 From: Theodore Luo Wang Date: Wed, 1 Sep 2021 11:54:06 -0400 Subject: [PATCH 3/7] Add checks for a block before a unary plus. Fix failing tests --- compiler/rustc_parse/src/parser/expr.rs | 30 ++++++++--- compiler/rustc_parse/src/parser/stmt.rs | 3 ++ .../ui/associated-types/issue-36499.stderr | 7 ++- src/test/ui/parser/expr-as-stmt.fixed | 6 +-- src/test/ui/parser/expr-as-stmt.rs | 6 +-- src/test/ui/parser/expr-as-stmt.stderr | 12 ++--- .../issue-88276-unary-plus.fixed | 4 +- .../issue-88276-unary-plus.rs | 4 +- .../issue-88276-unary-plus.stderr | 54 +++++++++---------- 9 files changed, 73 insertions(+), 53 deletions(-) rename src/test/ui/{did_you_mean => parser}/issue-88276-unary-plus.fixed (78%) rename src/test/ui/{did_you_mean => parser}/issue-88276-unary-plus.rs (78%) rename src/test/ui/{did_you_mean => parser}/issue-88276-unary-plus.stderr (61%) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 43de53e87231f..c887f9fe0cf59 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -165,9 +165,12 @@ impl<'a> Parser<'a> { if [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind) { return self.parse_prefix_range_expr(attrs); } else { - self.parse_prefix_expr(attrs)? + let result = self.parse_prefix_expr(attrs); + debug!("parse_prefix_expr result: {:?}", &result); + result? } }; + debug!("parse_assoc_expr_with(lhs = {:?})", &lhs); let last_type_ascription_set = self.last_type_ascription.is_some(); if !self.should_continue_as_assoc_expr(&lhs) { @@ -175,8 +178,11 @@ impl<'a> Parser<'a> { return Ok(lhs); } + debug!("continue_as_assoc_expr"); + self.expected_tokens.push(TokenType::Operator); while let Some(op) = self.check_assoc_op() { + debug!("op: {:?}", op); // Adjust the span for interpolated LHS to point to the `$lhs` token // and not to what it refers to. let lhs_span = match self.prev_token.kind { @@ -520,17 +526,25 @@ impl<'a> Parser<'a> { 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( + debug!("leading + detected: {:?}", lo); + 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) { + this.sess.expr_parentheses_needed(&mut err, *sp); + } else { + err.span_suggestion( lo, - "remove the `+`", + "try removing the `+`", "".to_string(), Applicability::MachineApplicable, - ) - .emit(); - this.bump(); + ); + } + err.emit(); + this.bump(); this.parse_prefix_expr(None) } // `+expr` token::Ident(..) if this.token.is_keyword(kw::Box) => { diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 85515bd2a6314..329b60f9a8711 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -21,6 +21,8 @@ use rustc_span::symbol::{kw, sym}; use std::mem; +use tracing::debug; + impl<'a> Parser<'a> { /// Parses a statement. This stops just before trailing semicolons on everything but items. /// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed. @@ -418,6 +420,7 @@ impl<'a> Parser<'a> { if self.token == token::Eof { break; } + debug!("parsing statements, stmts: {:?}", &stmts); let stmt = match self.parse_full_stmt(recover) { Err(mut err) if recover.yes() => { self.maybe_annotate_with_ascription(&mut err, false); diff --git a/src/test/ui/associated-types/issue-36499.stderr b/src/test/ui/associated-types/issue-36499.stderr index ff450f60acc68..990ef09e3f742 100644 --- a/src/test/ui/associated-types/issue-36499.stderr +++ b/src/test/ui/associated-types/issue-36499.stderr @@ -1,8 +1,11 @@ -error: expected expression, found `+` +error: leading `+` is not supported --> $DIR/issue-36499.rs:4:9 | LL | 2 + +2; - | ^ expected expression + | ^ + | | + | unexpected `+` + | help: try removing the `+` 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 c217ab9774fd2..79d73090a0420 100644 --- a/src/test/ui/parser/expr-as-stmt.fixed +++ b/src/test/ui/parser/expr-as-stmt.fixed @@ -5,18 +5,18 @@ #![allow(unused_must_use)] fn foo() -> i32 { - ({2}) + {2} //~ ERROR expected expression, found `+` + ({2}) + {2} //~ ERROR leading `+` is not supported //~^ ERROR mismatched types } fn bar() -> i32 { - ({2}) + 2 //~ ERROR expected expression, found `+` + ({2}) + 2 //~ ERROR leading `+` is not supported //~^ ERROR mismatched types } fn zul() -> u32 { let foo = 3; - ({ 42 }) + foo; //~ ERROR expected expression, found `+` + ({ 42 }) + foo; //~ ERROR leading `+` is not supported //~^ 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 b04025faaec63..8698f99b81a41 100644 --- a/src/test/ui/parser/expr-as-stmt.rs +++ b/src/test/ui/parser/expr-as-stmt.rs @@ -5,18 +5,18 @@ #![allow(unused_must_use)] fn foo() -> i32 { - {2} + {2} //~ ERROR expected expression, found `+` + {2} + {2} //~ ERROR leading `+` is not supported //~^ ERROR mismatched types } fn bar() -> i32 { - {2} + 2 //~ ERROR expected expression, found `+` + {2} + 2 //~ ERROR leading `+` is not supported //~^ ERROR mismatched types } fn zul() -> u32 { let foo = 3; - { 42 } + foo; //~ ERROR expected expression, found `+` + { 42 } + foo; //~ ERROR leading `+` is not supported //~^ 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 ba5cd01abfcc7..91f97c4662a97 100644 --- a/src/test/ui/parser/expr-as-stmt.stderr +++ b/src/test/ui/parser/expr-as-stmt.stderr @@ -1,30 +1,30 @@ -error: expected expression, found `+` +error: leading `+` is not supported --> $DIR/expr-as-stmt.rs:8:9 | LL | {2} + {2} - | ^ expected expression + | ^ unexpected `+` | help: parentheses are required to parse this as an expression | LL | ({2}) + {2} | + + -error: expected expression, found `+` +error: leading `+` is not supported --> $DIR/expr-as-stmt.rs:13:9 | LL | {2} + 2 - | ^ expected expression + | ^ unexpected `+` | help: parentheses are required to parse this as an expression | LL | ({2}) + 2 | + + -error: expected expression, found `+` +error: leading `+` is not supported --> $DIR/expr-as-stmt.rs:19:12 | LL | { 42 } + foo; - | ^ expected expression + | ^ unexpected `+` | help: parentheses are required to parse this as an expression | diff --git a/src/test/ui/did_you_mean/issue-88276-unary-plus.fixed b/src/test/ui/parser/issue-88276-unary-plus.fixed similarity index 78% rename from src/test/ui/did_you_mean/issue-88276-unary-plus.fixed rename to src/test/ui/parser/issue-88276-unary-plus.fixed index 4bd248663b884..279cdb5060a42 100644 --- a/src/test/ui/did_you_mean/issue-88276-unary-plus.fixed +++ b/src/test/ui/parser/issue-88276-unary-plus.fixed @@ -3,10 +3,10 @@ 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 _ = (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/parser/issue-88276-unary-plus.rs similarity index 78% rename from src/test/ui/did_you_mean/issue-88276-unary-plus.rs rename to src/test/ui/parser/issue-88276-unary-plus.rs index e2cce677b4755..a72dad4dc7198 100644 --- a/src/test/ui/did_you_mean/issue-88276-unary-plus.rs +++ b/src/test/ui/parser/issue-88276-unary-plus.rs @@ -3,10 +3,10 @@ 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 _ = (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/parser/issue-88276-unary-plus.stderr similarity index 61% rename from src/test/ui/did_you_mean/issue-88276-unary-plus.stderr rename to src/test/ui/parser/issue-88276-unary-plus.stderr index 9c1227d7c351a..255839bc684b5 100644 --- a/src/test/ui/did_you_mean/issue-88276-unary-plus.stderr +++ b/src/test/ui/parser/issue-88276-unary-plus.stderr @@ -5,7 +5,7 @@ LL | let _ = +1; | ^ | | | unexpected `+` - | help: remove the `+` + | help: try removing the `+` error: leading `+` is not supported --> $DIR/issue-88276-unary-plus.rs:5:14 @@ -14,43 +14,43 @@ LL | let _ = -+(1+2)*3; | ^ | | | unexpected `+` - | help: remove the `+` + | help: try removing 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 + --> $DIR/issue-88276-unary-plus.rs:6:14 | LL | let _ = -+-+(1+2)*3; | ^ | | | unexpected `+` - | help: remove the `+` + | help: try removing the `+` error: leading `+` is not supported - --> $DIR/issue-88276-unary-plus.rs:8:16 + --> $DIR/issue-88276-unary-plus.rs:6:16 | LL | let _ = -+-+(1+2)*3; | ^ | | | unexpected `+` - | help: remove the `+` + | help: try removing the `+` + +error: leading `+` is not supported + --> $DIR/issue-88276-unary-plus.rs:8:18 + | +LL | let _ = (1 + +2) * +3; + | ^ + | | + | unexpected `+` + | help: try removing the `+` + +error: leading `+` is not supported + --> $DIR/issue-88276-unary-plus.rs:8:24 + | +LL | let _ = (1 + +2) * +3; + | ^ + | | + | unexpected `+` + | help: try removing the `+` error: leading `+` is not supported --> $DIR/issue-88276-unary-plus.rs:10:14 @@ -59,7 +59,7 @@ LL | let _ = (+&"hello"); | ^ | | | unexpected `+` - | help: remove the `+` + | help: try removing the `+` error: leading `+` is not supported --> $DIR/issue-88276-unary-plus.rs:11:13 @@ -68,7 +68,7 @@ LL | let _ = +[+3, 4+6]; | ^ | | | unexpected `+` - | help: remove the `+` + | help: try removing the `+` error: leading `+` is not supported --> $DIR/issue-88276-unary-plus.rs:11:15 @@ -77,7 +77,7 @@ LL | let _ = +[+3, 4+6]; | ^ | | | unexpected `+` - | help: remove the `+` + | help: try removing the `+` error: aborting due to 9 previous errors From ce9e76528abea4d5bc8d360e3c6d40e61cffb889 Mon Sep 17 00:00:00 2001 From: Theodore Luo Wang Date: Wed, 1 Sep 2021 11:55:36 -0400 Subject: [PATCH 4/7] Update formatting --- compiler/rustc_parse/src/parser/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index c887f9fe0cf59..b0f29601417cb 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -535,7 +535,7 @@ impl<'a> Parser<'a> { if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&sp) { this.sess.expr_parentheses_needed(&mut err, *sp); } else { - err.span_suggestion( + err.span_suggestion( lo, "try removing the `+`", "".to_string(), From bc9877c5af5155174f1a517d41d997b446cc074e Mon Sep 17 00:00:00 2001 From: Theodore Luo Wang Date: Wed, 1 Sep 2021 12:02:11 -0400 Subject: [PATCH 5/7] Undo debug statements --- compiler/rustc_parse/src/parser/expr.rs | 12 +----------- compiler/rustc_parse/src/parser/stmt.rs | 3 --- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index b0f29601417cb..4463d2fc6c8da 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -23,8 +23,6 @@ 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 @@ -165,12 +163,9 @@ impl<'a> Parser<'a> { if [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind) { return self.parse_prefix_range_expr(attrs); } else { - let result = self.parse_prefix_expr(attrs); - debug!("parse_prefix_expr result: {:?}", &result); - result? + self.parse_prefix_expr(attrs)? } }; - debug!("parse_assoc_expr_with(lhs = {:?})", &lhs); let last_type_ascription_set = self.last_type_ascription.is_some(); if !self.should_continue_as_assoc_expr(&lhs) { @@ -178,11 +173,8 @@ impl<'a> Parser<'a> { return Ok(lhs); } - debug!("continue_as_assoc_expr"); - self.expected_tokens.push(TokenType::Operator); while let Some(op) = self.check_assoc_op() { - debug!("op: {:?}", op); // Adjust the span for interpolated LHS to point to the `$lhs` token // and not to what it refers to. let lhs_span = match self.prev_token.kind { @@ -363,7 +355,6 @@ 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),), @@ -526,7 +517,6 @@ impl<'a> Parser<'a> { make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo)) } token::BinOp(token::Plus) => { - debug!("leading + detected: {:?}", lo); let mut err = this.struct_span_err(lo, "leading `+` is not supported"); err.span_label(lo, "unexpected `+`"); diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 329b60f9a8711..85515bd2a6314 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -21,8 +21,6 @@ use rustc_span::symbol::{kw, sym}; use std::mem; -use tracing::debug; - impl<'a> Parser<'a> { /// Parses a statement. This stops just before trailing semicolons on everything but items. /// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed. @@ -420,7 +418,6 @@ impl<'a> Parser<'a> { if self.token == token::Eof { break; } - debug!("parsing statements, stmts: {:?}", &stmts); let stmt = match self.parse_full_stmt(recover) { Err(mut err) if recover.yes() => { self.maybe_annotate_with_ascription(&mut err, false); From 65eb7e516c798a9447dbfe1d02aeeb314d4fec54 Mon Sep 17 00:00:00 2001 From: Theodore Luo Wang Date: Sat, 4 Sep 2021 22:35:59 -0400 Subject: [PATCH 6/7] 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 710a592e258b4..e467bd963bf3c 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -586,6 +586,10 @@ impl Token { 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 4463d2fc6c8da..83e1c5ff753a8 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -516,16 +516,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) => { + 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 990ef09e3f742..610798d880f0a 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 79d73090a0420..5f5e25991e0cc 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 8698f99b81a41..5428e1c32fed3 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 91f97c4662a97..d99e9be0000c3 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 279cdb5060a42..25b7c340f600d 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 a72dad4dc7198..11b2e9d601653 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 255839bc684b5..b26761729a837 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 From 20eba43283d59849627042d67413d32e08ace0f1 Mon Sep 17 00:00:00 2001 From: Theodore Luo Wang Date: Sat, 4 Sep 2021 22:38:39 -0400 Subject: [PATCH 7/7] Fix formatting --- compiler/rustc_ast/src/token.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index e467bd963bf3c..3a65ffe41ae87 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -587,7 +587,10 @@ impl Token { } pub fn is_numeric_lit(&self) -> bool { - matches!(self.kind, Literal(Lit { kind: LitKind::Integer, ..}) | Literal(Lit { kind: LitKind::Float, ..})) + 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.