From d0165956fe070d12090896aa5546e509045471eb Mon Sep 17 00:00:00 2001 From: surechen Date: Thu, 26 Sep 2024 12:35:44 +0800 Subject: [PATCH] Add suggestion for removing invalid path separator `::` in function definition. for example: `fn invalid_path_separator::() {}` fixes: #130791 --- compiler/rustc_parse/messages.ftl | 3 +++ compiler/rustc_parse/src/errors.rs | 8 ++++++++ compiler/rustc_parse/src/parser/generics.rs | 7 +++++++ ...id-path-sep-in-fn-definition-issue-130791.fixed | 7 +++++++ ...valid-path-sep-in-fn-definition-issue-130791.rs | 7 +++++++ ...d-path-sep-in-fn-definition-issue-130791.stderr | 14 ++++++++++++++ 6 files changed, 46 insertions(+) create mode 100644 tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.fixed create mode 100644 tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.rs create mode 100644 tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.stderr diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index 6cb851eb8df6b..e8bff931f835e 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -415,6 +415,9 @@ parse_invalid_meta_item = expected unsuffixed literal, found `{$token}` parse_invalid_offset_of = offset_of expects dot-separated field and variant names +parse_invalid_path_sep_in_fn_definition = invalid path separator in function definition + .suggestion = remove invalid path separator + parse_invalid_unicode_escape = invalid unicode character escape .label = invalid escape .help = unicode escape must {$surrogate -> diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 20bcefd4fe1e7..86ed38a96c53b 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1755,6 +1755,14 @@ pub(crate) struct MissingFnParams { pub span: Span, } +#[derive(Diagnostic)] +#[diag(parse_invalid_path_sep_in_fn_definition)] +pub(crate) struct InvalidPathSepInFnDefinition { + #[primary_span] + #[suggestion(code = "", applicability = "machine-applicable", style = "verbose")] + pub span: Span, +} + #[derive(Diagnostic)] #[diag(parse_missing_trait_in_trait_impl)] pub(crate) struct MissingTraitInTraitImpl { diff --git a/compiler/rustc_parse/src/parser/generics.rs b/compiler/rustc_parse/src/parser/generics.rs index b9256daa72525..5aebe716b0a10 100644 --- a/compiler/rustc_parse/src/parser/generics.rs +++ b/compiler/rustc_parse/src/parser/generics.rs @@ -269,6 +269,13 @@ impl<'a> Parser<'a> { /// | ( < lifetimes , typaramseq ( , )? > ) /// where typaramseq = ( typaram ) | ( typaram , typaramseq ) pub(super) fn parse_generics(&mut self) -> PResult<'a, ast::Generics> { + // invalid path separator `::` in function definition + // for example `fn invalid_path_separator::() {}` + if self.eat_noexpect(&token::PathSep) { + self.dcx() + .emit_err(errors::InvalidPathSepInFnDefinition { span: self.prev_token.span }); + } + let span_lo = self.token.span; let (params, span) = if self.eat_lt() { let params = self.parse_generic_params()?; diff --git a/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.fixed b/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.fixed new file mode 100644 index 0000000000000..2c6fddccb7315 --- /dev/null +++ b/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.fixed @@ -0,0 +1,7 @@ +//@ run-rustfix + +#[allow(dead_code)] +fn invalid_path_separator() {} +//~^ ERROR invalid path separator in function definition + +fn main() {} diff --git a/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.rs b/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.rs new file mode 100644 index 0000000000000..5f69061504385 --- /dev/null +++ b/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.rs @@ -0,0 +1,7 @@ +//@ run-rustfix + +#[allow(dead_code)] +fn invalid_path_separator::() {} +//~^ ERROR invalid path separator in function definition + +fn main() {} diff --git a/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.stderr b/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.stderr new file mode 100644 index 0000000000000..3ad05050da0db --- /dev/null +++ b/tests/ui/parser/issues/invalid-path-sep-in-fn-definition-issue-130791.stderr @@ -0,0 +1,14 @@ +error: invalid path separator in function definition + --> $DIR/invalid-path-sep-in-fn-definition-issue-130791.rs:4:26 + | +LL | fn invalid_path_separator::() {} + | ^^ + | +help: remove invalid path separator + | +LL - fn invalid_path_separator::() {} +LL + fn invalid_path_separator() {} + | + +error: aborting due to 1 previous error +