Skip to content

Commit

Permalink
Add checks for a block before a unary plus. Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theo-lw committed Sep 1, 2021
1 parent e7fb98e commit 5a863d5
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 53 deletions.
30 changes: 22 additions & 8 deletions compiler/rustc_parse/src/parser/expr.rs
Expand Up @@ -165,18 +165,24 @@ 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) {
self.last_type_ascription = None;
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 {
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_parse/src/parser/stmt.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions 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

6 changes: 3 additions & 3 deletions src/test/ui/parser/expr-as-stmt.fixed
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/parser/expr-as-stmt.rs
Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions 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
|
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -77,7 +77,7 @@ LL | let _ = +[+3, 4+6];
| ^
| |
| unexpected `+`
| help: remove the `+`
| help: try removing the `+`

error: aborting due to 9 previous errors

0 comments on commit 5a863d5

Please sign in to comment.