Skip to content

Improve expression & attribute parsing #69760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
error_block_no_opening_brace: handle closures better
  • Loading branch information
Centril committed Mar 10, 2020
commit 09997e72272157a88c40cd2b1b1b7a14f808c0e0
16 changes: 7 additions & 9 deletions src/librustc_parse/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,14 @@ impl<'a> Parser<'a> {
//
// which is valid in other languages, but not Rust.
match self.parse_stmt_without_recovery() {
Ok(Some(stmt)) => {
// If the next token is an open brace (e.g., `if a b {`), the place-
// inside-a-block suggestion would be more likely wrong than right.
Ok(Some(_))
if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace))
|| do_not_suggest_help
{
// If the next token is an open brace (e.g., `if a b {`), the place-
// inside-a-block suggestion would be more likely wrong than right.
e.span_label(sp, "expected `{`");
return Err(e);
}
let stmt_span = if self.eat(&token::Semi) {
|| do_not_suggest_help => {}
Ok(Some(stmt)) => {
let stmt_own_line = self.sess.source_map().is_line_before_span_empty(sp);
let stmt_span = if stmt_own_line && self.eat(&token::Semi) {
// Expand the span to include the semicolon.
stmt.span.with_hi(self.prev_token.span.hi())
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_span/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,13 @@ impl SourceMap {
Ok((lo, hi))
}

pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
match self.span_to_prev_source(sp) {
Ok(s) => s.split('\n').last().map(|l| l.trim_start().is_empty()).unwrap_or(false),
Err(_) => false,
}
}

pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
debug!("span_to_lines(sp={:?})", sp);
let (lo, hi) = self.is_valid_span(sp)?;
Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/parser/attr-stmt-expr-attr-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ error: expected `{`, found `#`
--> $DIR/attr-stmt-expr-attr-bad.rs:41:37
|
LL | #[cfg(FALSE)] fn e() { let _ = if 0 #[attr] {}; }
| -- ^ --- help: try placing this code inside a block: `{ {}; }`
| -- ^ -- help: try placing this code inside a block: `{ {} }`
| | |
| | expected `{`
| this `if` expression has a condition, but no block
Expand All @@ -163,7 +163,7 @@ error: expected `{`, found `#`
--> $DIR/attr-stmt-expr-attr-bad.rs:47:45
|
LL | #[cfg(FALSE)] fn e() { let _ = if 0 {} else #[attr] {}; }
| ^ --- help: try placing this code inside a block: `{ {}; }`
| ^ -- help: try placing this code inside a block: `{ {} }`
| |
| expected `{`

Expand All @@ -179,15 +179,15 @@ error: expected `{`, found `#`
--> $DIR/attr-stmt-expr-attr-bad.rs:51:45
|
LL | #[cfg(FALSE)] fn e() { let _ = if 0 {} else #[attr] if 0 {}; }
| ^ -------- help: try placing this code inside a block: `{ if 0 {}; }`
| ^ ------- help: try placing this code inside a block: `{ if 0 {} }`
| |
| expected `{`

error: expected `{`, found `#`
--> $DIR/attr-stmt-expr-attr-bad.rs:53:50
|
LL | #[cfg(FALSE)] fn e() { let _ = if 0 {} else if 0 #[attr] {}; }
| -- ^ --- help: try placing this code inside a block: `{ {}; }`
| -- ^ -- help: try placing this code inside a block: `{ {} }`
| | |
| | expected `{`
| this `if` expression has a condition, but no block
Expand All @@ -204,7 +204,7 @@ error: expected `{`, found `#`
--> $DIR/attr-stmt-expr-attr-bad.rs:57:45
|
LL | #[cfg(FALSE)] fn e() { let _ = if let _ = 0 #[attr] {}; }
| -- ^ --- help: try placing this code inside a block: `{ {}; }`
| -- ^ -- help: try placing this code inside a block: `{ {} }`
| | |
| | expected `{`
| this `if` expression has a condition, but no block
Expand All @@ -227,7 +227,7 @@ error: expected `{`, found `#`
--> $DIR/attr-stmt-expr-attr-bad.rs:63:53
|
LL | #[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else #[attr] {}; }
| ^ --- help: try placing this code inside a block: `{ {}; }`
| ^ -- help: try placing this code inside a block: `{ {} }`
| |
| expected `{`

Expand All @@ -243,15 +243,15 @@ error: expected `{`, found `#`
--> $DIR/attr-stmt-expr-attr-bad.rs:67:53
|
LL | #[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else #[attr] if let _ = 0 {}; }
| ^ ---------------- help: try placing this code inside a block: `{ if let _ = 0 {}; }`
| ^ --------------- help: try placing this code inside a block: `{ if let _ = 0 {} }`
| |
| expected `{`

error: expected `{`, found `#`
--> $DIR/attr-stmt-expr-attr-bad.rs:69:66
|
LL | #[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else if let _ = 0 #[attr] {}; }
| -- ^ --- help: try placing this code inside a block: `{ {}; }`
| -- ^ -- help: try placing this code inside a block: `{ {} }`
| | |
| | expected `{`
| this `if` expression has a condition, but no block
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/closure-return-syntax.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ error: expected `{`, found `22`
--> $DIR/closure-return-syntax.rs:5:23
|
LL | let x = || -> i32 22;
| ^^-
| ^^
| |
| expected `{`
| help: try placing this code inside a block: `{ 22; }`
| help: try placing this code inside a block: `{ 22 }`

error: aborting due to previous error