Skip to content

Commit e6bf3e2

Browse files
committed
Issue rust-lang#50636: Improve error diagnostic with missing commas after struct fields.
1 parent c95e1cc commit e6bf3e2

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/libsyntax/parse/parser.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -5797,9 +5797,18 @@ impl<'a> Parser<'a> {
57975797
return Err(err);
57985798
}
57995799
}
5800-
_ => return Err(self.span_fatal_help(self.span,
5801-
&format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
5802-
"struct fields should be separated by commas")),
5800+
_ => {
5801+
let sp = self.sess.codemap().next_point(self.prev_span);
5802+
let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found `{}`",
5803+
self.this_token_to_string()));
5804+
if self.token.is_ident() {
5805+
// This is likely another field; emit the diagnostic and keep going
5806+
err.span_suggestion(sp, "try adding a comma", ",".into());
5807+
err.emit();
5808+
} else {
5809+
return Err(err)
5810+
}
5811+
}
58035812
}
58045813
Ok(a_var)
58055814
}

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

+23
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+
}
+8
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)