Skip to content

Commit 78ecd94

Browse files
committed
Add ParseContext for diagnostic in Parser and supress false comments suggestion
Signed-off-by: xizheyin <[email protected]>
1 parent 781de1e commit 78ecd94

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use crate::errors::{
4545
UnexpectedConstParamDeclaration, UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets,
4646
UseEqInstead, WrapType,
4747
};
48+
use crate::parser::ParserContext;
4849
use crate::parser::attr::InnerAttrPolicy;
4950
use crate::{exp, fluent_generated as fluent};
5051

@@ -685,7 +686,9 @@ impl<'a> Parser<'a> {
685686
);
686687
}
687688

688-
if let token::DocComment(kind, style, _) = self.token.kind {
689+
if let token::DocComment(kind, style, _) = self.token.kind
690+
&& self.context != ParserContext::ExpectItemListComma
691+
{
689692
// We have something like `expr //!val` where the user likely meant `expr // !val`
690693
let pos = self.token.span.lo() + BytePos(2);
691694
let span = self.token.span.with_lo(pos).with_hi(pos);

compiler/rustc_parse/src/parser/item.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use super::{
2424
Recovered, Trailing, UsePreAttrPos,
2525
};
2626
use crate::errors::{self, MacroExpandsToAdtField};
27+
use crate::parser::ParserContext;
2728
use crate::{exp, fluent_generated as fluent};
2829

2930
impl<'a> Parser<'a> {
@@ -1547,8 +1548,10 @@ impl<'a> Parser<'a> {
15471548
self.bump();
15481549
(thin_vec![], Trailing::No)
15491550
} else {
1550-
self.parse_delim_comma_seq(exp!(OpenBrace), exp!(CloseBrace), |p| {
1551-
p.parse_enum_variant(ident.span)
1551+
self.with_context(ParserContext::ExpectItemListComma, |p| {
1552+
p.parse_delim_comma_seq(exp!(OpenBrace), exp!(CloseBrace), |p| {
1553+
p.parse_enum_variant(ident.span)
1554+
})
15521555
})
15531556
.map_err(|mut err| {
15541557
err.span_label(ident.span, "while parsing this enum");

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ pub enum Recovery {
166166
Forbidden,
167167
}
168168

169+
/// Parser context for diagnostics and etc.
170+
/// It is used to determine whether to add suggestions.
171+
/// For example, in an item list, we don't want to add suggestions for doc comments.
172+
/// FIXME(xizheyin): This is a basic structure, we can add more fields
173+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
174+
pub enum ParserContext {
175+
/// empty context
176+
Empty,
177+
/// enum/struct/union/fields/variants item list
178+
ExpectItemListComma,
179+
}
180+
169181
#[derive(Clone)]
170182
pub struct Parser<'a> {
171183
pub psess: &'a ParseSess,
@@ -218,6 +230,7 @@ pub struct Parser<'a> {
218230
/// Whether the parser is allowed to do recovery.
219231
/// This is disabled when parsing macro arguments, see #103534
220232
recovery: Recovery,
233+
pub context: ParserContext,
221234
}
222235

223236
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with
@@ -367,6 +380,7 @@ impl<'a> Parser<'a> {
367380
},
368381
current_closure: None,
369382
recovery: Recovery::Allowed,
383+
context: ParserContext::Empty,
370384
};
371385

372386
// Make parser point to the first token.
@@ -1638,6 +1652,15 @@ impl<'a> Parser<'a> {
16381652
_ => self.prev_token.span,
16391653
}
16401654
}
1655+
1656+
/// Temporarily set the parser context for a closure, restoring it afterwards.
1657+
pub fn with_context<T>(&mut self, ctx: ParserContext, f: impl FnOnce(&mut Self) -> T) -> T {
1658+
let old = self.context;
1659+
self.context = ctx;
1660+
let res = f(self);
1661+
self.context = old;
1662+
res
1663+
}
16411664
}
16421665

16431666
// Metavar captures of various kinds.

tests/ui/enum/enum-lack-comma-suggestion-issue-142311.stderr

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `/// Like w
22
--> $DIR/enum-lack-comma-suggestion-issue-142311.rs:4:5
33
|
44
LL | Bar
5-
| - expected one of `(`, `,`, `=`, `{`, or `}`
5+
| -
6+
| |
7+
| expected one of `(`, `,`, `=`, `{`, or `}`
8+
| help: missing `,`
69
LL | /// Like where people drink
710
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
8-
|
9-
help: add a space before the last `/` to write a regular comment
10-
|
11-
LL | // / Like where people drink
12-
| +
13-
help: missing `,`
14-
|
15-
LL | Bar,
16-
| +
1711

1812
error: aborting due to 1 previous error
1913

0 commit comments

Comments
 (0)