Skip to content

Commit fbb575b

Browse files
authored
Rollup merge of rust-lang#100188 - chenyukang:fix-issue-100165, r=estebank
Parser will not suggest invalid expression when use public Fixes rust-lang#100165
2 parents ce43db3 + 863488d commit fbb575b

File tree

8 files changed

+79
-10
lines changed

8 files changed

+79
-10
lines changed

compiler/rustc_ast/src/token.rs

+20
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,26 @@ impl Token {
436436
|| self == &OpenDelim(Delimiter::Parenthesis)
437437
}
438438

439+
/// Returns `true` if the token can appear at the start of a item
440+
pub fn can_begin_item(&self) -> bool {
441+
self.is_keyword(kw::Use)
442+
|| self.is_keyword(kw::Fn)
443+
|| self.is_keyword(kw::Extern)
444+
|| self.is_keyword(kw::Crate)
445+
|| self.is_keyword(kw::Mod)
446+
|| self.is_keyword(kw::Const)
447+
|| self.is_keyword(kw::Static)
448+
|| self.is_keyword(kw::Trait)
449+
|| self.is_keyword(kw::Impl)
450+
|| self.is_keyword(kw::Mod)
451+
|| self.is_keyword(kw::Type)
452+
|| self.is_keyword(kw::Enum)
453+
|| self.is_keyword(kw::Struct)
454+
|| self.is_keyword(kw::Union)
455+
|| self.is_keyword(kw::Macro)
456+
|| self == &OpenDelim(Delimiter::Parenthesis)
457+
}
458+
439459
/// Returns `true` if the token is any literal.
440460
pub fn is_lit(&self) -> bool {
441461
matches!(self.kind, Literal(..))

compiler/rustc_parse/src/parser/diagnostics.rs

+8-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

@@ -602,15 +602,13 @@ impl<'a> Parser<'a> {
602602
self.last_unexpected_token_span = Some(self.token.span);
603603
let mut err = self.struct_span_err(self.token.span, &msg_exp);
604604

605-
if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
606-
if symbol.as_str() == "public" {
607-
err.span_suggestion_short(
608-
self.prev_token.span,
609-
"write `pub` instead of `public` to make the item public",
610-
"pub",
611-
appl,
612-
);
613-
}
605+
if self.prev_token.is_ident_named(sym::public) && self.token.can_begin_item() {
606+
err.span_suggestion_short(
607+
self.prev_token.span,
608+
"write `pub` instead of `public` to make the item public",
609+
"pub",
610+
appl,
611+
);
614612
}
615613

616614
// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens

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)