Skip to content

Commit 905898c

Browse files
committed
Add suggestion for removing invalid path separator :: in function definition.
for example: `fn invalid_path_separator::<T>() {}` fixes: #130791
1 parent 1b5aa96 commit 905898c

6 files changed

+53
-1
lines changed

compiler/rustc_parse/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
415415
416416
parse_invalid_offset_of = offset_of expects dot-separated field and variant names
417417
418+
parse_invalid_path_sep_in_fn_definition = invalid path separator in function definition
419+
.suggestion = remove invalid path separator
420+
418421
parse_invalid_unicode_escape = invalid unicode character escape
419422
.label = invalid escape
420423
.help = unicode escape must {$surrogate ->

compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,14 @@ pub(crate) struct MissingFnParams {
17551755
pub span: Span,
17561756
}
17571757

1758+
#[derive(Diagnostic)]
1759+
#[diag(parse_invalid_path_sep_in_fn_definition)]
1760+
pub(crate) struct InvalidPathSepInFnDefinition {
1761+
#[primary_span]
1762+
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
1763+
pub span: Span,
1764+
}
1765+
17581766
#[derive(Diagnostic)]
17591767
#[diag(parse_missing_trait_in_trait_impl)]
17601768
pub(crate) struct MissingTraitInTraitImpl {

compiler/rustc_parse/src/parser/generics.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ast::token::Delimiter;
1+
use ast::token::{Delimiter, TokenKind};
22
use rustc_ast::{
33
self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, TyKind, WhereClause, token,
44
};
@@ -275,6 +275,11 @@ impl<'a> Parser<'a> {
275275
self.expect_gt_or_maybe_suggest_closing_generics(&params)?;
276276
(params, span_lo.to(self.prev_token.span))
277277
} else {
278+
if self.token == token::PathSep && self.look_ahead(1, |t| *t == TokenKind::Lt) {
279+
// invalid path separator `::` in function definition
280+
// for example `fn invalid_path_separator::<T>() {}`
281+
self.dcx().emit_err(errors::InvalidPathSepInFnDefinition { span: self.token.span });
282+
}
278283
(ThinVec::new(), self.prev_token.span.shrink_to_hi())
279284
};
280285
Ok(ast::Generics {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ run-rustfix
2+
3+
#[allow(dead_code)]
4+
fn invalid_path_separator<T>() {}
5+
//~^ ERROR invalid path separator in function definition
6+
//~| ERROR expected
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ run-rustfix
2+
3+
#[allow(dead_code)]
4+
fn invalid_path_separator::<T>() {}
5+
//~^ ERROR invalid path separator in function definition
6+
//~| ERROR expected
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: invalid path separator in function definition
2+
--> $DIR/invalid-path-sep-in-fn-definition-issue-130791.rs:4:26
3+
|
4+
LL | fn invalid_path_separator::<T>() {}
5+
| ^^
6+
|
7+
help: remove invalid path separator
8+
|
9+
LL - fn invalid_path_separator::<T>() {}
10+
LL + fn invalid_path_separator<T>() {}
11+
|
12+
13+
error: expected one of `->`, `<`, `where`, or `{`, found `::`
14+
--> $DIR/invalid-path-sep-in-fn-definition-issue-130791.rs:4:26
15+
|
16+
LL | fn invalid_path_separator::<T>() {}
17+
| ^^ expected one of `->`, `<`, `where`, or `{`
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)