Skip to content

Commit 09997e7

Browse files
committed
error_block_no_opening_brace: handle closures better
1 parent c01b3e6 commit 09997e7

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

src/librustc_parse/parser/stmt.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,14 @@ impl<'a> Parser<'a> {
259259
//
260260
// which is valid in other languages, but not Rust.
261261
match self.parse_stmt_without_recovery() {
262-
Ok(Some(stmt)) => {
262+
// If the next token is an open brace (e.g., `if a b {`), the place-
263+
// inside-a-block suggestion would be more likely wrong than right.
264+
Ok(Some(_))
263265
if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace))
264-
|| do_not_suggest_help
265-
{
266-
// If the next token is an open brace (e.g., `if a b {`), the place-
267-
// inside-a-block suggestion would be more likely wrong than right.
268-
e.span_label(sp, "expected `{`");
269-
return Err(e);
270-
}
271-
let stmt_span = if self.eat(&token::Semi) {
266+
|| do_not_suggest_help => {}
267+
Ok(Some(stmt)) => {
268+
let stmt_own_line = self.sess.source_map().is_line_before_span_empty(sp);
269+
let stmt_span = if stmt_own_line && self.eat(&token::Semi) {
272270
// Expand the span to include the semicolon.
273271
stmt.span.with_hi(self.prev_token.span.hi())
274272
} else {

src/librustc_span/source_map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,13 @@ impl SourceMap {
517517
Ok((lo, hi))
518518
}
519519

520+
pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
521+
match self.span_to_prev_source(sp) {
522+
Ok(s) => s.split('\n').last().map(|l| l.trim_start().is_empty()).unwrap_or(false),
523+
Err(_) => false,
524+
}
525+
}
526+
520527
pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
521528
debug!("span_to_lines(sp={:?})", sp);
522529
let (lo, hi) = self.is_valid_span(sp)?;

src/test/ui/parser/attr-stmt-expr-attr-bad.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ error: expected `{`, found `#`
140140
--> $DIR/attr-stmt-expr-attr-bad.rs:41:37
141141
|
142142
LL | #[cfg(FALSE)] fn e() { let _ = if 0 #[attr] {}; }
143-
| -- ^ --- help: try placing this code inside a block: `{ {}; }`
143+
| -- ^ -- help: try placing this code inside a block: `{ {} }`
144144
| | |
145145
| | expected `{`
146146
| this `if` expression has a condition, but no block
@@ -163,7 +163,7 @@ error: expected `{`, found `#`
163163
--> $DIR/attr-stmt-expr-attr-bad.rs:47:45
164164
|
165165
LL | #[cfg(FALSE)] fn e() { let _ = if 0 {} else #[attr] {}; }
166-
| ^ --- help: try placing this code inside a block: `{ {}; }`
166+
| ^ -- help: try placing this code inside a block: `{ {} }`
167167
| |
168168
| expected `{`
169169

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

186186
error: expected `{`, found `#`
187187
--> $DIR/attr-stmt-expr-attr-bad.rs:53:50
188188
|
189189
LL | #[cfg(FALSE)] fn e() { let _ = if 0 {} else if 0 #[attr] {}; }
190-
| -- ^ --- help: try placing this code inside a block: `{ {}; }`
190+
| -- ^ -- help: try placing this code inside a block: `{ {} }`
191191
| | |
192192
| | expected `{`
193193
| this `if` expression has a condition, but no block
@@ -204,7 +204,7 @@ error: expected `{`, found `#`
204204
--> $DIR/attr-stmt-expr-attr-bad.rs:57:45
205205
|
206206
LL | #[cfg(FALSE)] fn e() { let _ = if let _ = 0 #[attr] {}; }
207-
| -- ^ --- help: try placing this code inside a block: `{ {}; }`
207+
| -- ^ -- help: try placing this code inside a block: `{ {} }`
208208
| | |
209209
| | expected `{`
210210
| this `if` expression has a condition, but no block
@@ -227,7 +227,7 @@ error: expected `{`, found `#`
227227
--> $DIR/attr-stmt-expr-attr-bad.rs:63:53
228228
|
229229
LL | #[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else #[attr] {}; }
230-
| ^ --- help: try placing this code inside a block: `{ {}; }`
230+
| ^ -- help: try placing this code inside a block: `{ {} }`
231231
| |
232232
| expected `{`
233233

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

250250
error: expected `{`, found `#`
251251
--> $DIR/attr-stmt-expr-attr-bad.rs:69:66
252252
|
253253
LL | #[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else if let _ = 0 #[attr] {}; }
254-
| -- ^ --- help: try placing this code inside a block: `{ {}; }`
254+
| -- ^ -- help: try placing this code inside a block: `{ {} }`
255255
| | |
256256
| | expected `{`
257257
| this `if` expression has a condition, but no block

src/test/ui/parser/closure-return-syntax.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ error: expected `{`, found `22`
22
--> $DIR/closure-return-syntax.rs:5:23
33
|
44
LL | let x = || -> i32 22;
5-
| ^^-
5+
| ^^
66
| |
77
| expected `{`
8-
| help: try placing this code inside a block: `{ 22; }`
8+
| help: try placing this code inside a block: `{ 22 }`
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)