Skip to content

Commit 2c57826

Browse files
committed
Auto merge of #48946 - PramodBisht:issues/48636, r=estebank
Doc comments present after a particular syntax error cause an unhelpful error message to be output. fixed: #48636 r? @estebank
2 parents e028687 + 1bed654 commit 2c57826

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5759,18 +5759,33 @@ impl<'a> Parser<'a> {
57595759
vis: Visibility,
57605760
attrs: Vec<Attribute> )
57615761
-> PResult<'a, StructField> {
5762+
let mut seen_comma: bool = false;
57625763
let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
5764+
if self.token == token::Comma {
5765+
seen_comma = true;
5766+
}
57635767
match self.token {
57645768
token::Comma => {
57655769
self.bump();
57665770
}
57675771
token::CloseDelim(token::Brace) => {}
57685772
token::DocComment(_) => {
5773+
let previous_span = self.prev_span;
57695774
let mut err = self.span_fatal_err(self.span, Error::UselessDocComment);
57705775
self.bump(); // consume the doc comment
5771-
if self.eat(&token::Comma) || self.token == token::CloseDelim(token::Brace) {
5776+
let comma_after_doc_seen = self.eat(&token::Comma);
5777+
// `seen_comma` is always false, because we are inside doc block
5778+
// condition is here to make code more readable
5779+
if seen_comma == false && comma_after_doc_seen == true {
5780+
seen_comma = true;
5781+
}
5782+
if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
57725783
err.emit();
57735784
} else {
5785+
if seen_comma == false {
5786+
let sp = self.sess.codemap().next_point(previous_span);
5787+
err.span_suggestion(sp, "missing comma here", ",".into());
5788+
}
57745789
return Err(err);
57755790
}
57765791
}

src/test/ui/issue-48636.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
struct S {
12+
x: u8
13+
/// The id of the parent core
14+
y: u8,
15+
}
16+
//~^^^ ERROR found a documentation comment that doesn't document anything
17+
fn main() {}

src/test/ui/issue-48636.stderr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0585]: found a documentation comment that doesn't document anything
2+
--> $DIR/issue-48636.rs:13:5
3+
|
4+
LL | x: u8
5+
| - help: missing comma here: `,`
6+
LL | /// The id of the parent core
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
|
9+
= help: doc comments must come before what they document, maybe a comment was intended with `//`?
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0585`.

0 commit comments

Comments
 (0)