From 98757f14d0242e6dcae258df8aeb7e17580702ef Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Fri, 14 Feb 2020 22:28:13 -0500 Subject: Suggest a comma if a struct initializer field fails to parse Currently, we emit a "try adding a comma" suggestion if a comma is missing in a struct definition. However, we emit no such suggestion if a comma is missing in a struct initializer. This commit adds a "try adding a comma" suggestion when we don't find a comma during the parsing of a struct initializer field. The change to `src/test/ui/parser/removed-syntax-with-1.stderr` isn't great, but I don't see a good way of avoiding it. --- src/librustc_parse/parser/expr.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/librustc_parse/parser') diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 5a4225ece65..20b9df0a2d9 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1832,10 +1832,16 @@ impl<'a> Parser<'a> { } } Err(mut e) => { + e.span_label(struct_sp, "while parsing this struct"); if let Some(f) = recovery_field { fields.push(f); + e.span_suggestion( + self.prev_span.shrink_to_hi(), + "try adding a comma", + ",".into(), + Applicability::MachineApplicable, + ); } - e.span_label(struct_sp, "while parsing this struct"); e.emit(); self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore); self.eat(&token::Comma); -- cgit 1.4.1-3-g733a5