Skip to content

Commit 5a863d5

Browse files
committed
Add checks for a block before a unary plus. Fix failing tests
1 parent e7fb98e commit 5a863d5

File tree

9 files changed

+73
-53
lines changed

9 files changed

+73
-53
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,24 @@ impl<'a> Parser<'a> {
165165
if [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind) {
166166
return self.parse_prefix_range_expr(attrs);
167167
} else {
168-
self.parse_prefix_expr(attrs)?
168+
let result = self.parse_prefix_expr(attrs);
169+
debug!("parse_prefix_expr result: {:?}", &result);
170+
result?
169171
}
170172
};
173+
debug!("parse_assoc_expr_with(lhs = {:?})", &lhs);
171174
let last_type_ascription_set = self.last_type_ascription.is_some();
172175

173176
if !self.should_continue_as_assoc_expr(&lhs) {
174177
self.last_type_ascription = None;
175178
return Ok(lhs);
176179
}
177180

181+
debug!("continue_as_assoc_expr");
182+
178183
self.expected_tokens.push(TokenType::Operator);
179184
while let Some(op) = self.check_assoc_op() {
185+
debug!("op: {:?}", op);
180186
// Adjust the span for interpolated LHS to point to the `$lhs` token
181187
// and not to what it refers to.
182188
let lhs_span = match self.prev_token.kind {
@@ -520,17 +526,25 @@ impl<'a> Parser<'a> {
520526
make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo))
521527
}
522528
token::BinOp(token::Plus) => {
523-
this.struct_span_err(lo, "leading `+` is not supported")
524-
.span_label(lo, "unexpected `+`")
525-
.span_suggestion_short(
529+
debug!("leading + detected: {:?}", lo);
530+
let mut err = this.struct_span_err(lo, "leading `+` is not supported");
531+
err.span_label(lo, "unexpected `+`");
532+
533+
// a block on the LHS might have been intended to be an expression instead
534+
let sp = this.sess.source_map().start_point(lo);
535+
if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&sp) {
536+
this.sess.expr_parentheses_needed(&mut err, *sp);
537+
} else {
538+
err.span_suggestion(
526539
lo,
527-
"remove the `+`",
540+
"try removing the `+`",
528541
"".to_string(),
529542
Applicability::MachineApplicable,
530-
)
531-
.emit();
532-
this.bump();
543+
);
544+
}
545+
err.emit();
533546

547+
this.bump();
534548
this.parse_prefix_expr(None)
535549
} // `+expr`
536550
token::Ident(..) if this.token.is_keyword(kw::Box) => {

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use rustc_span::symbol::{kw, sym};
2121

2222
use std::mem;
2323

24+
use tracing::debug;
25+
2426
impl<'a> Parser<'a> {
2527
/// Parses a statement. This stops just before trailing semicolons on everything but items.
2628
/// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
@@ -418,6 +420,7 @@ impl<'a> Parser<'a> {
418420
if self.token == token::Eof {
419421
break;
420422
}
423+
debug!("parsing statements, stmts: {:?}", &stmts);
421424
let stmt = match self.parse_full_stmt(recover) {
422425
Err(mut err) if recover.yes() => {
423426
self.maybe_annotate_with_ascription(&mut err, false);
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error: expected expression, found `+`
1+
error: leading `+` is not supported
22
--> $DIR/issue-36499.rs:4:9
33
|
44
LL | 2 + +2;
5-
| ^ expected expression
5+
| ^
6+
| |
7+
| unexpected `+`
8+
| help: try removing the `+`
69

710
error: aborting due to previous error
811

src/test/ui/parser/expr-as-stmt.fixed

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
#![allow(unused_must_use)]
66

77
fn foo() -> i32 {
8-
({2}) + {2} //~ ERROR expected expression, found `+`
8+
({2}) + {2} //~ ERROR leading `+` is not supported
99
//~^ ERROR mismatched types
1010
}
1111

1212
fn bar() -> i32 {
13-
({2}) + 2 //~ ERROR expected expression, found `+`
13+
({2}) + 2 //~ ERROR leading `+` is not supported
1414
//~^ ERROR mismatched types
1515
}
1616

1717
fn zul() -> u32 {
1818
let foo = 3;
19-
({ 42 }) + foo; //~ ERROR expected expression, found `+`
19+
({ 42 }) + foo; //~ ERROR leading `+` is not supported
2020
//~^ ERROR mismatched types
2121
32
2222
}

src/test/ui/parser/expr-as-stmt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
#![allow(unused_must_use)]
66

77
fn foo() -> i32 {
8-
{2} + {2} //~ ERROR expected expression, found `+`
8+
{2} + {2} //~ ERROR leading `+` is not supported
99
//~^ ERROR mismatched types
1010
}
1111

1212
fn bar() -> i32 {
13-
{2} + 2 //~ ERROR expected expression, found `+`
13+
{2} + 2 //~ ERROR leading `+` is not supported
1414
//~^ ERROR mismatched types
1515
}
1616

1717
fn zul() -> u32 {
1818
let foo = 3;
19-
{ 42 } + foo; //~ ERROR expected expression, found `+`
19+
{ 42 } + foo; //~ ERROR leading `+` is not supported
2020
//~^ ERROR mismatched types
2121
32
2222
}

src/test/ui/parser/expr-as-stmt.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
error: expected expression, found `+`
1+
error: leading `+` is not supported
22
--> $DIR/expr-as-stmt.rs:8:9
33
|
44
LL | {2} + {2}
5-
| ^ expected expression
5+
| ^ unexpected `+`
66
|
77
help: parentheses are required to parse this as an expression
88
|
99
LL | ({2}) + {2}
1010
| + +
1111

12-
error: expected expression, found `+`
12+
error: leading `+` is not supported
1313
--> $DIR/expr-as-stmt.rs:13:9
1414
|
1515
LL | {2} + 2
16-
| ^ expected expression
16+
| ^ unexpected `+`
1717
|
1818
help: parentheses are required to parse this as an expression
1919
|
2020
LL | ({2}) + 2
2121
| + +
2222

23-
error: expected expression, found `+`
23+
error: leading `+` is not supported
2424
--> $DIR/expr-as-stmt.rs:19:12
2525
|
2626
LL | { 42 } + foo;
27-
| ^ expected expression
27+
| ^ unexpected `+`
2828
|
2929
help: parentheses are required to parse this as an expression
3030
|

src/test/ui/did_you_mean/issue-88276-unary-plus.fixed renamed to src/test/ui/parser/issue-88276-unary-plus.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
fn main() {
44
let _ = 1; //~ ERROR leading `+` is not supported
55
let _ = -(1+2)*3; //~ ERROR leading `+` is not supported
6-
let _ = -(1+2)*3; //~ ERROR leading `+` is not supported
7-
//~| ERROR leading `+` is not supported
86
let _ = --(1+2)*3; //~ ERROR leading `+` is not supported
97
//~| ERROR leading `+` is not supported
8+
let _ = (1 + 2) * 3; //~ ERROR leading `+` is not supported
9+
//~| ERROR leading `+` is not supported
1010
let _ = (&"hello"); //~ ERROR leading `+` is not supported
1111
let _ = [3, 4+6]; //~ ERROR leading `+` is not supported
1212
//~| ERROR leading `+` is not supported

src/test/ui/did_you_mean/issue-88276-unary-plus.rs renamed to src/test/ui/parser/issue-88276-unary-plus.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
fn main() {
44
let _ = +1; //~ ERROR leading `+` is not supported
55
let _ = -+(1+2)*3; //~ ERROR leading `+` is not supported
6-
let _ = +-+(1+2)*3; //~ ERROR leading `+` is not supported
7-
//~| ERROR leading `+` is not supported
86
let _ = -+-+(1+2)*3; //~ ERROR leading `+` is not supported
97
//~| ERROR leading `+` is not supported
8+
let _ = (1 + +2) * +3; //~ ERROR leading `+` is not supported
9+
//~| ERROR leading `+` is not supported
1010
let _ = (+&"hello"); //~ ERROR leading `+` is not supported
1111
let _ = +[+3, 4+6]; //~ ERROR leading `+` is not supported
1212
//~| ERROR leading `+` is not supported

src/test/ui/did_you_mean/issue-88276-unary-plus.stderr renamed to src/test/ui/parser/issue-88276-unary-plus.stderr

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let _ = +1;
55
| ^
66
| |
77
| unexpected `+`
8-
| help: remove the `+`
8+
| help: try removing the `+`
99

1010
error: leading `+` is not supported
1111
--> $DIR/issue-88276-unary-plus.rs:5:14
@@ -14,43 +14,43 @@ LL | let _ = -+(1+2)*3;
1414
| ^
1515
| |
1616
| unexpected `+`
17-
| help: remove the `+`
17+
| help: try removing the `+`
1818

1919
error: leading `+` is not supported
20-
--> $DIR/issue-88276-unary-plus.rs:6:13
21-
|
22-
LL | let _ = +-+(1+2)*3;
23-
| ^
24-
| |
25-
| unexpected `+`
26-
| help: remove the `+`
27-
28-
error: leading `+` is not supported
29-
--> $DIR/issue-88276-unary-plus.rs:6:15
30-
|
31-
LL | let _ = +-+(1+2)*3;
32-
| ^
33-
| |
34-
| unexpected `+`
35-
| help: remove the `+`
36-
37-
error: leading `+` is not supported
38-
--> $DIR/issue-88276-unary-plus.rs:8:14
20+
--> $DIR/issue-88276-unary-plus.rs:6:14
3921
|
4022
LL | let _ = -+-+(1+2)*3;
4123
| ^
4224
| |
4325
| unexpected `+`
44-
| help: remove the `+`
26+
| help: try removing the `+`
4527

4628
error: leading `+` is not supported
47-
--> $DIR/issue-88276-unary-plus.rs:8:16
29+
--> $DIR/issue-88276-unary-plus.rs:6:16
4830
|
4931
LL | let _ = -+-+(1+2)*3;
5032
| ^
5133
| |
5234
| unexpected `+`
53-
| help: remove the `+`
35+
| help: try removing the `+`
36+
37+
error: leading `+` is not supported
38+
--> $DIR/issue-88276-unary-plus.rs:8:18
39+
|
40+
LL | let _ = (1 + +2) * +3;
41+
| ^
42+
| |
43+
| unexpected `+`
44+
| help: try removing the `+`
45+
46+
error: leading `+` is not supported
47+
--> $DIR/issue-88276-unary-plus.rs:8:24
48+
|
49+
LL | let _ = (1 + +2) * +3;
50+
| ^
51+
| |
52+
| unexpected `+`
53+
| help: try removing the `+`
5454

5555
error: leading `+` is not supported
5656
--> $DIR/issue-88276-unary-plus.rs:10:14
@@ -59,7 +59,7 @@ LL | let _ = (+&"hello");
5959
| ^
6060
| |
6161
| unexpected `+`
62-
| help: remove the `+`
62+
| help: try removing the `+`
6363

6464
error: leading `+` is not supported
6565
--> $DIR/issue-88276-unary-plus.rs:11:13
@@ -68,7 +68,7 @@ LL | let _ = +[+3, 4+6];
6868
| ^
6969
| |
7070
| unexpected `+`
71-
| help: remove the `+`
71+
| help: try removing the `+`
7272

7373
error: leading `+` is not supported
7474
--> $DIR/issue-88276-unary-plus.rs:11:15
@@ -77,7 +77,7 @@ LL | let _ = +[+3, 4+6];
7777
| ^
7878
| |
7979
| unexpected `+`
80-
| help: remove the `+`
80+
| help: try removing the `+`
8181

8282
error: aborting due to 9 previous errors
8383

0 commit comments

Comments
 (0)