Skip to content

Commit 351acc8

Browse files
committed
parser will not give wrong help message for 'public'
1 parent 38528d4 commit 351acc8

7 files changed

+65
-10
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_errors::{
2222
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed};
2323
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
2424
use rustc_span::source_map::Spanned;
25-
use rustc_span::symbol::{kw, Ident};
25+
use rustc_span::symbol::{kw, sym, Ident};
2626
use rustc_span::{Span, SpanSnippetError, DUMMY_SP};
2727
use std::ops::{Deref, DerefMut};
2828

@@ -977,15 +977,6 @@ impl<'a> Parser<'a> {
977977
let mut err = self.struct_span_err(self.token.span, &msg_exp);
978978

979979
if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
980-
if symbol.as_str() == "public" {
981-
err.span_suggestion_short(
982-
self.prev_token.span,
983-
"write `pub` instead of `public` to make the item public",
984-
"pub",
985-
appl,
986-
);
987-
}
988-
989980
if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
990981
err.span_suggestion_short(
991982
self.prev_token.span,
@@ -996,6 +987,19 @@ impl<'a> Parser<'a> {
996987
}
997988
}
998989

990+
// `pub` may be used for an item or `pub(crate)`
991+
if self.prev_token.is_ident_named(sym::public)
992+
&& (self.token.can_begin_item()
993+
|| self.token.kind == TokenKind::OpenDelim(Delimiter::Parenthesis))
994+
{
995+
err.span_suggestion_short(
996+
self.prev_token.span,
997+
"write `pub` instead of `public` to make the item public",
998+
"pub",
999+
appl,
1000+
);
1001+
}
1002+
9991003
// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
10001004
// there are unclosed angle brackets
10011005
if self.unmatched_angle_bracket_count > 0

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ symbols! {
11181118
ptr_offset_from_unsigned,
11191119
pub_macro_rules,
11201120
pub_restricted,
1121+
public,
11211122
pure,
11221123
pushpop_unsafe,
11231124
qreg,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Checks what happens when `public` is used instead of the correct, `pub`
2+
// run-rustfix
3+
4+
pub enum Test {
5+
//~^ ERROR expected one of `!` or `::`, found keyword `enum`
6+
//~^^ HELP write `pub` instead of `public` to make the item public
7+
A,
8+
B,
9+
}
10+
11+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Checks what happens when `public` is used instead of the correct, `pub`
2+
// run-rustfix
3+
4+
public enum Test {
5+
//~^ ERROR expected one of `!` or `::`, found keyword `enum`
6+
//~^^ HELP write `pub` instead of `public` to make the item public
7+
A,
8+
B,
9+
}
10+
11+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected one of `!` or `::`, found keyword `enum`
2+
--> $DIR/public-instead-of-pub-1.rs:4:8
3+
|
4+
LL | public enum Test {
5+
| ^^^^ expected one of `!` or `::`
6+
|
7+
help: write `pub` instead of `public` to make the item public
8+
|
9+
LL | pub enum Test {
10+
| ~~~
11+
12+
error: aborting due to previous error
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Checks what happens when `public` is used instead of the correct, `pub`
2+
// Won't give help message for this case
3+
4+
public let x = 1;
5+
//~^ ERROR expected one of `!` or `::`, found keyword `let`
6+
7+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected one of `!` or `::`, found keyword `let`
2+
--> $DIR/public-instead-of-pub-2.rs:4:8
3+
|
4+
LL | public let x = 1;
5+
| ^^^ expected one of `!` or `::`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)