Skip to content

Commit

Permalink
Rollup merge of rust-lang#100188 - chenyukang:fix-issue-100165, r=est…
Browse files Browse the repository at this point in the history
…ebank

Parser will not suggest invalid expression when use public

Fixes rust-lang#100165
  • Loading branch information
JohnTitor committed Aug 24, 2022
2 parents 3d5e1d7 + 351acc8 commit e21c1e4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 10 deletions.
24 changes: 14 additions & 10 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_errors::{
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed};
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, Ident};
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{Span, SpanSnippetError, DUMMY_SP};
use std::ops::{Deref, DerefMut};

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

if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
if symbol.as_str() == "public" {
err.span_suggestion_short(
self.prev_token.span,
"write `pub` instead of `public` to make the item public",
"pub",
appl,
);
}

if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
err.span_suggestion_short(
self.prev_token.span,
Expand All @@ -996,6 +987,19 @@ impl<'a> Parser<'a> {
}
}

// `pub` may be used for an item or `pub(crate)`
if self.prev_token.is_ident_named(sym::public)
&& (self.token.can_begin_item()
|| self.token.kind == TokenKind::OpenDelim(Delimiter::Parenthesis))
{
err.span_suggestion_short(
self.prev_token.span,
"write `pub` instead of `public` to make the item public",
"pub",
appl,
);
}

// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
// there are unclosed angle brackets
if self.unmatched_angle_bracket_count > 0
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@ symbols! {
ptr_offset_from_unsigned,
pub_macro_rules,
pub_restricted,
public,
pure,
pushpop_unsafe,
qreg,
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/parser/public-instead-of-pub-1.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Checks what happens when `public` is used instead of the correct, `pub`
// run-rustfix

pub enum Test {
//~^ ERROR expected one of `!` or `::`, found keyword `enum`
//~^^ HELP write `pub` instead of `public` to make the item public
A,
B,
}

fn main() { }
11 changes: 11 additions & 0 deletions src/test/ui/parser/public-instead-of-pub-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Checks what happens when `public` is used instead of the correct, `pub`
// run-rustfix

public enum Test {
//~^ ERROR expected one of `!` or `::`, found keyword `enum`
//~^^ HELP write `pub` instead of `public` to make the item public
A,
B,
}

fn main() { }
13 changes: 13 additions & 0 deletions src/test/ui/parser/public-instead-of-pub-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: expected one of `!` or `::`, found keyword `enum`
--> $DIR/public-instead-of-pub-1.rs:4:8
|
LL | public enum Test {
| ^^^^ expected one of `!` or `::`
|
help: write `pub` instead of `public` to make the item public
|
LL | pub enum Test {
| ~~~

error: aborting due to previous error

7 changes: 7 additions & 0 deletions src/test/ui/parser/public-instead-of-pub-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Checks what happens when `public` is used instead of the correct, `pub`
// Won't give help message for this case

public let x = 1;
//~^ ERROR expected one of `!` or `::`, found keyword `let`

fn main() { }
8 changes: 8 additions & 0 deletions src/test/ui/parser/public-instead-of-pub-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected one of `!` or `::`, found keyword `let`
--> $DIR/public-instead-of-pub-2.rs:4:8
|
LL | public let x = 1;
| ^^^ expected one of `!` or `::`

error: aborting due to previous error

0 comments on commit e21c1e4

Please sign in to comment.