Skip to content

Commit 617ce2b

Browse files
committed
Reword ambigous parse error to fit with the current error
1 parent bff0be3 commit 617ce2b

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

src/libsyntax/parse/parser.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -3649,13 +3649,14 @@ impl<'a> Parser<'a> {
36493649
return Ok(lhs);
36503650
}
36513651
(true, Some(_)) => {
3652-
// #54186, #54482, #59975
36533652
// We've found an expression that would be parsed as a statement, but the next
36543653
// token implies this should be parsed as an expression.
3655-
let mut err = self.sess.span_diagnostic.struct_span_err(
3656-
self.span,
3657-
"ambiguous parse",
3658-
);
3654+
// For example: `if let Some(x) = x { x } else { 0 } / 2`
3655+
let mut err = self.sess.span_diagnostic.struct_span_err(self.span, &format!(
3656+
"expected expression, found `{}`",
3657+
pprust::token_to_string(&self.token),
3658+
));
3659+
err.span_label(self.span, "expected expression");
36593660
let snippet = self.sess.source_map().span_to_snippet(lhs.span)
36603661
.unwrap_or_else(|_| pprust::expr_to_string(&lhs));
36613662
err.span_suggestion(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ fn baz() -> i32 {
2727

2828
fn qux(a: Option<u32>, b: Option<u32>) -> bool {
2929
(if let Some(x) = a { true } else { false })
30-
&& //~ ERROR ambiguous parse
30+
&& //~ ERROR expected expression
3131
if let Some(y) = a { true } else { false }
3232
}
3333

3434
fn moo(x: u32) -> bool {
3535
(match x {
3636
_ => 1,
37-
}) > 0 //~ ERROR ambiguous parse
37+
}) > 0 //~ ERROR expected expression
3838
}
3939

4040
fn main() {}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ fn baz() -> i32 {
2727

2828
fn qux(a: Option<u32>, b: Option<u32>) -> bool {
2929
if let Some(x) = a { true } else { false }
30-
&& //~ ERROR ambiguous parse
30+
&& //~ ERROR expected expression
3131
if let Some(y) = a { true } else { false }
3232
}
3333

3434
fn moo(x: u32) -> bool {
3535
match x {
3636
_ => 1,
37-
} > 0 //~ ERROR ambiguous parse
37+
} > 0 //~ ERROR expected expression
3838
}
3939

4040
fn main() {}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ LL | { 42 } + foo;
2222
| |
2323
| help: parenthesis are required to parse this as an expression: `({ 42 })`
2424

25-
error: ambiguous parse
25+
error: expected expression, found `&&`
2626
--> $DIR/expr-as-stmt.rs:30:5
2727
|
2828
LL | if let Some(x) = a { true } else { false }
2929
| ------------------------------------------ help: parenthesis are required to parse this as an expression: `(if let Some(x) = a { true } else { false })`
3030
LL | &&
31-
| ^^
31+
| ^^ expected expression
3232

33-
error: ambiguous parse
33+
error: expected expression, found `>`
3434
--> $DIR/expr-as-stmt.rs:37:7
3535
|
3636
LL | } > 0
37-
| ^
37+
| ^ expected expression
3838
help: parenthesis are required to parse this as an expression
3939
|
4040
LL | (match x {

0 commit comments

Comments
 (0)