Skip to content

Commit

Permalink
Add suggestion for removing invalid path separator :: in function d…
Browse files Browse the repository at this point in the history
…efinition.

for example: `fn invalid_path_separator::<T>() {}`

fixes: #130791
  • Loading branch information
surechen committed Sep 26, 2024
1 parent 1b5aa96 commit 55692e7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2767,9 +2767,16 @@ impl<'a> Parser<'a> {
// might be typo'd trait impl, handled elsewhere
&& !self.token.is_keyword(kw::For)
{
// recover from missing argument list, e.g. `fn main -> () {}`
self.dcx()
.emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
if self.token == token::PathSep && self.look_ahead(1, |t| *t == TokenKind::Lt) {
// invalid path separator `::` in function definition
// for example `fn invalid_path_separator::<T>() {}`
self.dcx().emit_err(errors::InvalidPathSepInFnDefinition { span: self.token.span });
} else {
// recover from missing argument list, e.g. `fn main -> () {}`
self.dcx().emit_err(errors::MissingFnParams {
span: self.prev_token.span.shrink_to_hi(),
});
}
return Ok(ThinVec::new());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ run-rustfix

#[allow(dead_code)]
fn invalid_path_separator<T>() {}
//~^ ERROR invalid path separator in function definition
//~| ERROR expected

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ run-rustfix

#[allow(dead_code)]
fn invalid_path_separator::<T>() {}
//~^ ERROR invalid path separator in function definition
//~| ERROR expected

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: invalid path separator in function definition
--> $DIR/invalid-path-sep-in-fn-definition-issue-130791.rs:4:26
|
LL | fn invalid_path_separator::<T>() {}
| ^^
|
help: remove invalid path separator
|
LL - fn invalid_path_separator::<T>() {}
LL + fn invalid_path_separator<T>() {}
|

error: expected one of `->`, `<`, `where`, or `{`, found `::`
--> $DIR/invalid-path-sep-in-fn-definition-issue-130791.rs:4:26
|
LL | fn invalid_path_separator::<T>() {}
| ^^ expected one of `->`, `<`, `where`, or `{`

error: aborting due to 2 previous errors

0 comments on commit 55692e7

Please sign in to comment.