Skip to content

Commit b2b9555

Browse files
committed
Recover from incorrect pub kw in "reasonable" places
1 parent 532dd44 commit b2b9555

9 files changed

+26
-40
lines changed

src/libsyntax/parse/parser.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ impl<'a> Parser<'a> {
15241524
at_end: &mut bool,
15251525
mut attrs: Vec<Attribute>) -> PResult<'a, TraitItem> {
15261526
let lo = self.span;
1527-
1527+
self.eat_bad_pub();
15281528
let (name, node, generics) = if self.eat_keyword(keywords::Type) {
15291529
self.parse_trait_item_assoc_ty()?
15301530
} else if self.is_const_item() {
@@ -7680,6 +7680,7 @@ impl<'a> Parser<'a> {
76807680

76817681
let struct_def;
76827682
let mut disr_expr = None;
7683+
self.eat_bad_pub();
76837684
let ident = self.parse_ident()?;
76847685
if self.check(&token::OpenDelim(token::Brace)) {
76857686
// Parse a struct variant.
@@ -8618,6 +8619,17 @@ impl<'a> Parser<'a> {
86188619
Applicability::MaybeIncorrect,
86198620
).emit();
86208621
}
8622+
8623+
/// Recover from `pub` keyword in places where it seems _reasonable_ but isn't valid.
8624+
fn eat_bad_pub(&mut self) {
8625+
if self.token.is_keyword(keywords::Pub) {
8626+
self.bump();
8627+
let mut err = self.diagnostic()
8628+
.struct_span_err(self.prev_span, "unnecessary visibility qualifier");
8629+
err.span_label(self.prev_span, "`pub` not permitted here");
8630+
err.emit();
8631+
}
8632+
}
86218633
}
86228634

86238635
pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, handler: &errors::Handler) {

src/test/ui/issues/issue-28433.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
enum Bird {
44
pub Duck,
5-
//~^ ERROR expected identifier, found keyword `pub`
6-
//~| ERROR missing comma
7-
//~| WARN variant `pub` should have an upper camel case name
5+
//~^ ERROR unnecessary visibility qualifier
86
Goose
97
}
108

src/test/ui/issues/issue-28433.stderr

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
1-
error: expected identifier, found keyword `pub`
1+
error: unnecessary visibility qualifier
22
--> $DIR/issue-28433.rs:4:5
33
|
44
LL | pub Duck,
5-
| ^^^ expected identifier, found keyword
6-
help: you can escape reserved keywords to use them as identifiers
7-
|
8-
LL | r#pub Duck,
9-
| ^^^^^
10-
11-
error: missing comma
12-
--> $DIR/issue-28433.rs:4:8
13-
|
14-
LL | pub Duck,
15-
| ^ help: missing comma
16-
17-
warning: variant `pub` should have an upper camel case name
18-
--> $DIR/issue-28433.rs:4:5
19-
|
20-
LL | pub Duck,
21-
| ^^^ help: convert the identifier to upper camel case: `Pub`
22-
|
23-
= note: #[warn(non_camel_case_types)] on by default
5+
| ^^^ `pub` not permitted here
246

25-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
268

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
trait Foo {
22
pub const Foo: u32;
3-
//~^ ERROR expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found
3+
//~^ ERROR unnecessary visibility qualifier
44
}
55

66
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error: expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found `pub`
1+
error: unnecessary visibility qualifier
22
--> $DIR/trait-pub-assoc-const.rs:2:5
33
|
4-
LL | trait Foo {
5-
| - expected one of 7 possible tokens here
64
LL | pub const Foo: u32;
7-
| ^^^ unexpected token
5+
| ^^^ `pub` not permitted here
86

97
error: aborting due to previous error
108

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
trait Foo {
22
pub type Foo;
3-
//~^ ERROR expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found
3+
//~^ ERROR unnecessary visibility qualifier
44
}
55

66
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error: expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found `pub`
1+
error: unnecessary visibility qualifier
22
--> $DIR/trait-pub-assoc-ty.rs:2:5
33
|
4-
LL | trait Foo {
5-
| - expected one of 7 possible tokens here
64
LL | pub type Foo;
7-
| ^^^ unexpected token
5+
| ^^^ `pub` not permitted here
86

97
error: aborting due to previous error
108

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
trait Foo {
22
pub fn foo();
3-
//~^ ERROR expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found
3+
//~^ ERROR unnecessary visibility qualifier
44
}
55

66
fn main() {}
+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error: expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found `pub`
1+
error: unnecessary visibility qualifier
22
--> $DIR/trait-pub-method.rs:2:5
33
|
4-
LL | trait Foo {
5-
| - expected one of 7 possible tokens here
64
LL | pub fn foo();
7-
| ^^^ unexpected token
5+
| ^^^ `pub` not permitted here
86

97
error: aborting due to previous error
108

0 commit comments

Comments
 (0)