From 7cdd937bb821881f82ee367757cc45ad74e8698b Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 29 Jul 2022 07:01:58 +0000 Subject: dont call type ascription 'cast' --- compiler/rustc_parse/src/parser/expr.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_parse/src') diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 43b48613976..c0f661f7dbb 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -827,11 +827,12 @@ impl<'a> Parser<'a> { cast_expr: P, ) -> PResult<'a, P> { let span = cast_expr.span; - let maybe_ascription_span = if let ExprKind::Type(ascripted_expr, _) = &cast_expr.kind { - Some(ascripted_expr.span.shrink_to_hi().with_hi(span.hi())) - } else { - None - }; + let (cast_kind, maybe_ascription_span) = + if let ExprKind::Type(ascripted_expr, _) = &cast_expr.kind { + ("type ascription", Some(ascripted_expr.span.shrink_to_hi().with_hi(span.hi()))) + } else { + ("cast", None) + }; // Save the memory location of expr before parsing any following postfix operators. // This will be compared with the memory location of the output expression. @@ -844,7 +845,7 @@ impl<'a> Parser<'a> { // If the resulting expression is not a cast, or has a different memory location, it is an illegal postfix operator. if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _)) || changed { let msg = format!( - "casts cannot be followed by {}", + "{cast_kind} cannot be followed by {}", match with_postfix.kind { ExprKind::Index(_, _) => "indexing", ExprKind::Try(_) => "`?`", -- cgit 1.4.1-3-g733a5 From d0e881eefe871d807fd7527a1366154f1712ada1 Mon Sep 17 00:00:00 2001 From: Gimgim <93856041+gimbles@users.noreply.github.com> Date: Fri, 29 Jul 2022 19:21:30 +0530 Subject: Add diagnostic when using public instead of pub --- compiler/rustc_parse/src/parser/diagnostics.rs | 11 +++++++++++ src/test/ui/parser/public-instead-of-pub.fixed | 8 ++++++++ src/test/ui/parser/public-instead-of-pub.rs | 8 ++++++++ src/test/ui/parser/public-instead-of-pub.stderr | 13 +++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 src/test/ui/parser/public-instead-of-pub.fixed create mode 100644 src/test/ui/parser/public-instead-of-pub.rs create mode 100644 src/test/ui/parser/public-instead-of-pub.stderr (limited to 'compiler/rustc_parse/src') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 63055c56c5c..09329f18c67 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -601,6 +601,17 @@ impl<'a> Parser<'a> { self.last_unexpected_token_span = Some(self.token.span); 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, + ); + } + } + // 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 diff --git a/src/test/ui/parser/public-instead-of-pub.fixed b/src/test/ui/parser/public-instead-of-pub.fixed new file mode 100644 index 00000000000..01db609990e --- /dev/null +++ b/src/test/ui/parser/public-instead-of-pub.fixed @@ -0,0 +1,8 @@ +// Checks what happens when `public` is used instead of the correct, `pub` +// edition:2018 +// run-rustfix +pub struct X; +//~^ ERROR expected one of `!` or `::`, found keyword `struct` +//~^^ HELP write `pub` instead of `public` to make the item public + +fn main() {} diff --git a/src/test/ui/parser/public-instead-of-pub.rs b/src/test/ui/parser/public-instead-of-pub.rs new file mode 100644 index 00000000000..18e0fd3af1c --- /dev/null +++ b/src/test/ui/parser/public-instead-of-pub.rs @@ -0,0 +1,8 @@ +// Checks what happens when `public` is used instead of the correct, `pub` +// edition:2018 +// run-rustfix +public struct X; +//~^ ERROR expected one of `!` or `::`, found keyword `struct` +//~^^ HELP write `pub` instead of `public` to make the item public + +fn main() {} diff --git a/src/test/ui/parser/public-instead-of-pub.stderr b/src/test/ui/parser/public-instead-of-pub.stderr new file mode 100644 index 00000000000..af875491e85 --- /dev/null +++ b/src/test/ui/parser/public-instead-of-pub.stderr @@ -0,0 +1,13 @@ +error: expected one of `!` or `::`, found keyword `struct` + --> $DIR/public-instead-of-pub.rs:4:8 + | +LL | public struct X; + | ^^^^^^ expected one of `!` or `::` + | +help: write `pub` instead of `public` to make the item public + | +LL | pub struct X; + | ~~~ + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5