Skip to content

Commit 696b84c

Browse files
authored
Rollup merge of rust-lang#50914 - simartin:issue_50636, r=oli-obk
Issue rust-lang#50636: Improve error diagnostic with missing commas after struct fields. Fixes rust-lang#50636
2 parents e51b35e + e6bf3e2 commit 696b84c

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5807,9 +5807,18 @@ impl<'a> Parser<'a> {
58075807
return Err(err);
58085808
}
58095809
}
5810-
_ => return Err(self.span_fatal_help(self.span,
5811-
&format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
5812-
"struct fields should be separated by commas")),
5810+
_ => {
5811+
let sp = self.sess.codemap().next_point(self.prev_span);
5812+
let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found `{}`",
5813+
self.this_token_to_string()));
5814+
if self.token.is_ident() {
5815+
// This is likely another field; emit the diagnostic and keep going
5816+
err.span_suggestion(sp, "try adding a comma", ",".into());
5817+
err.emit();
5818+
} else {
5819+
return Err(err)
5820+
}
5821+
}
58135822
}
58145823
Ok(a_var)
58155824
}

src/test/ui/struct-missing-comma.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only
12+
13+
// Issue #50636
14+
15+
struct S {
16+
foo: u32 //~ expected `,`, or `}`, found `bar`
17+
// ~^ HELP try adding a comma: ','
18+
bar: u32
19+
}
20+
21+
fn main() {
22+
let s = S { foo: 5, bar: 6 };
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected `,`, or `}`, found `bar`
2+
--> $DIR/struct-missing-comma.rs:16:13
3+
|
4+
LL | foo: u32 //~ expected `,`, or `}`, found `bar`
5+
| ^ help: try adding a comma: `,`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)