1
Fork 0

Reword ambigous parse error to fit with the current error

This commit is contained in:
Esteban Küber 2019-04-30 20:37:42 -07:00
parent bff0be3784
commit 617ce2b7ee
4 changed files with 14 additions and 13 deletions

View file

@ -3649,13 +3649,14 @@ impl<'a> Parser<'a> {
return Ok(lhs);
}
(true, Some(_)) => {
// #54186, #54482, #59975
// We've found an expression that would be parsed as a statement, but the next
// token implies this should be parsed as an expression.
let mut err = self.sess.span_diagnostic.struct_span_err(
self.span,
"ambiguous parse",
);
// For example: `if let Some(x) = x { x } else { 0 } / 2`
let mut err = self.sess.span_diagnostic.struct_span_err(self.span, &format!(
"expected expression, found `{}`",
pprust::token_to_string(&self.token),
));
err.span_label(self.span, "expected expression");
let snippet = self.sess.source_map().span_to_snippet(lhs.span)
.unwrap_or_else(|_| pprust::expr_to_string(&lhs));
err.span_suggestion(

View file

@ -27,14 +27,14 @@ fn baz() -> i32 {
fn qux(a: Option<u32>, b: Option<u32>) -> bool {
(if let Some(x) = a { true } else { false })
&& //~ ERROR ambiguous parse
&& //~ ERROR expected expression
if let Some(y) = a { true } else { false }
}
fn moo(x: u32) -> bool {
(match x {
_ => 1,
}) > 0 //~ ERROR ambiguous parse
}) > 0 //~ ERROR expected expression
}
fn main() {}

View file

@ -27,14 +27,14 @@ fn baz() -> i32 {
fn qux(a: Option<u32>, b: Option<u32>) -> bool {
if let Some(x) = a { true } else { false }
&& //~ ERROR ambiguous parse
&& //~ ERROR expected expression
if let Some(y) = a { true } else { false }
}
fn moo(x: u32) -> bool {
match x {
_ => 1,
} > 0 //~ ERROR ambiguous parse
} > 0 //~ ERROR expected expression
}
fn main() {}

View file

@ -22,19 +22,19 @@ LL | { 42 } + foo;
| |
| help: parenthesis are required to parse this as an expression: `({ 42 })`
error: ambiguous parse
error: expected expression, found `&&`
--> $DIR/expr-as-stmt.rs:30:5
|
LL | if let Some(x) = a { true } else { false }
| ------------------------------------------ help: parenthesis are required to parse this as an expression: `(if let Some(x) = a { true } else { false })`
LL | &&
| ^^
| ^^ expected expression
error: ambiguous parse
error: expected expression, found `>`
--> $DIR/expr-as-stmt.rs:37:7
|
LL | } > 0
| ^
| ^ expected expression
help: parenthesis are required to parse this as an expression
|
LL | (match x {