Skip to content

improved diagnostic for function defined with def, fun, func, or function instead of fn #100750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,15 @@ impl<'a> Parser<'a> {
appl,
);
}

if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
err.span_suggestion_short(
self.prev_token.span,
&format!("write `fn` instead of `{symbol}` to declare a function"),
"fn",
appl,
);
}
}

// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/parser/fn-defined-using-def.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Check what happens when `def` is used to define a function, instead of `fn`
// edition:2021

#![allow(dead_code)]

def foo() {}
//~^ ERROR expected one of `!` or `::`, found `foo`
//~^^ HELP write `fn` instead of `def` to declare a function

fn main() {}
10 changes: 10 additions & 0 deletions src/test/ui/parser/fn-defined-using-def.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected one of `!` or `::`, found `foo`
--> $DIR/fn-defined-using-def.rs:6:5
|
LL | def foo() {}
| --- ^^^ expected one of `!` or `::`
| |
| help: write `fn` instead of `def` to declare a function

error: aborting due to previous error

10 changes: 10 additions & 0 deletions src/test/ui/parser/fn-defined-using-fun.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Check what happens when `fun` is used to define a function, instead of `fn`
// edition:2021

#![allow(dead_code)]

fun foo() {}
//~^ ERROR expected one of `!` or `::`, found `foo`
//~^^ HELP write `fn` instead of `fun` to declare a function

fn main() {}
10 changes: 10 additions & 0 deletions src/test/ui/parser/fn-defined-using-fun.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected one of `!` or `::`, found `foo`
--> $DIR/fn-defined-using-fun.rs:6:5
|
LL | fun foo() {}
| --- ^^^ expected one of `!` or `::`
| |
| help: write `fn` instead of `fun` to declare a function

error: aborting due to previous error

10 changes: 10 additions & 0 deletions src/test/ui/parser/fn-defined-using-func.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Check what happens when `func` is used to define a function, instead of `fn`
// edition:2021

#![allow(dead_code)]

func foo() {}
//~^ ERROR expected one of `!` or `::`, found `foo`
//~^^ HELP write `fn` instead of `func` to declare a function

fn main() {}
10 changes: 10 additions & 0 deletions src/test/ui/parser/fn-defined-using-func.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected one of `!` or `::`, found `foo`
--> $DIR/fn-defined-using-func.rs:6:6
|
LL | func foo() {}
| ---- ^^^ expected one of `!` or `::`
| |
| help: write `fn` instead of `func` to declare a function

error: aborting due to previous error

10 changes: 10 additions & 0 deletions src/test/ui/parser/fn-defined-using-function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Check what happens when `function` is used to define a function, instead of `fn`
// edition:2021

#![allow(dead_code)]

function foo() {}
//~^ ERROR expected one of `!` or `::`, found `foo`
//~^^ HELP write `fn` instead of `function` to declare a function

fn main() {}
10 changes: 10 additions & 0 deletions src/test/ui/parser/fn-defined-using-function.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected one of `!` or `::`, found `foo`
--> $DIR/fn-defined-using-function.rs:6:10
|
LL | function foo() {}
| -------- ^^^ expected one of `!` or `::`
| |
| help: write `fn` instead of `function` to declare a function

error: aborting due to previous error