From 7969b78222dc66e21c6a21470365f404992729b1 Mon Sep 17 00:00:00 2001 From: Lamb Date: Tue, 29 May 2018 13:19:58 +0200 Subject: Issue #50974: Suboptimal error in case of duplicate `,` in struct constructor --- src/libsyntax/parse/parser.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 3955ccb4c42..3d386708645 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -776,6 +776,9 @@ impl<'a> Parser<'a> { err.span_label(self.span, format!("expected identifier, found {}", token_descr)); } else { err.span_label(self.span, "expected identifier"); + if self.token == token::Comma { + err.span_suggestion(self.span, "try removing a comma", ",".into()); + } } err } @@ -2446,8 +2449,11 @@ impl<'a> Parser<'a> { Err(mut e) => { e.span_label(struct_sp, "while parsing this struct"); e.emit(); - self.recover_stmt(); - break; + + if self.token != token::Comma { + self.recover_stmt(); + break; + } } } -- cgit 1.4.1-3-g733a5 From 783815d219413ae8475d0d37cb40e4139ac79f24 Mon Sep 17 00:00:00 2001 From: Lamb Date: Tue, 29 May 2018 20:15:47 +0200 Subject: Issue #50974: Change text of suggestion to be more direct --- src/libsyntax/parse/parser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 3d386708645..1ed902e8861 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -777,7 +777,7 @@ impl<'a> Parser<'a> { } else { err.span_label(self.span, "expected identifier"); if self.token == token::Comma { - err.span_suggestion(self.span, "try removing a comma", ",".into()); + err.span_suggestion(self.span, "remove this comma", ",".into()); } } err -- cgit 1.4.1-3-g733a5 From fadb86f25d4053289c612cbba6b92da793976c16 Mon Sep 17 00:00:00 2001 From: Maerten <39694331+lambtowolf@users.noreply.github.com> Date: Wed, 30 May 2018 09:16:18 +0200 Subject: Fix when the help message is displayed Only display the "remove this comma" suggestion when followed by an identifier --- src/libsyntax/parse/parser.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 1ed902e8861..d6916c1c344 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -776,8 +776,8 @@ impl<'a> Parser<'a> { err.span_label(self.span, format!("expected identifier, found {}", token_descr)); } else { err.span_label(self.span, "expected identifier"); - if self.token == token::Comma { - err.span_suggestion(self.span, "remove this comma", ",".into()); + if self.token == token::Comma && self.look_ahead(1, |t| *t.is_ident()) { + err.span_suggestion(self.span, "remove this comma", "".into()); } } err -- cgit 1.4.1-3-g733a5 From 815765dadeb011180ad3ea0ce02c9cc59f8c93cc Mon Sep 17 00:00:00 2001 From: Lamb Date: Wed, 30 May 2018 13:06:05 +0200 Subject: Issue #50974: Fix compilation error and test --- src/libsyntax/parse/parser.rs | 2 +- src/test/ui/struct-duplicate-comma.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index d6916c1c344..456054ee251 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -776,7 +776,7 @@ impl<'a> Parser<'a> { err.span_label(self.span, format!("expected identifier, found {}", token_descr)); } else { err.span_label(self.span, "expected identifier"); - if self.token == token::Comma && self.look_ahead(1, |t| *t.is_ident()) { + if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) { err.span_suggestion(self.span, "remove this comma", "".into()); } } diff --git a/src/test/ui/struct-duplicate-comma.stderr b/src/test/ui/struct-duplicate-comma.stderr index 37b573c2681..06e3b95c248 100644 --- a/src/test/ui/struct-duplicate-comma.stderr +++ b/src/test/ui/struct-duplicate-comma.stderr @@ -7,7 +7,7 @@ LL | a: 0,, | ^ | | | expected identifier - | help: remove this comma: `,` + | help: remove this comma error: aborting due to previous error -- cgit 1.4.1-3-g733a5 From a99767f64f400f694aec02655af4b5cb5913609e Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 5 Jun 2018 13:04:15 -0400 Subject: add an explanatory comment for recovery behavior --- src/libsyntax/parse/parser.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 456054ee251..dfd896fbda4 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2450,6 +2450,9 @@ impl<'a> Parser<'a> { e.span_label(struct_sp, "while parsing this struct"); e.emit(); + // If the next token is a comma, then try to parse + // what comes next as additional fields, rather than + // bailing out until next `}`. if self.token != token::Comma { self.recover_stmt(); break; -- cgit 1.4.1-3-g733a5