Skip to content

Commit 1b54ad0

Browse files
author
akabinds
committed
added improved diagnostic for a function defined with an invalid qualifier
1 parent 801821d commit 1b54ad0

9 files changed

+89
-0
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+9
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,15 @@ impl<'a> Parser<'a> {
611611
appl,
612612
);
613613
}
614+
615+
if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
616+
err.span_suggestion_short(
617+
self.prev_token.span,
618+
&format!("write `fn` instead of `{symbol}` to declare a function"),
619+
"fn",
620+
appl,
621+
);
622+
}
614623
}
615624

616625
// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Check what happens when `def` is used to define a function, instead of `fn`
2+
// edition:2021
3+
4+
#![allow(dead_code)]
5+
6+
def foo() {}
7+
//~^ ERROR expected one of `!` or `::`, found `foo`
8+
//~^^ HELP write `fn` instead of `def` to declare a function
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected one of `!` or `::`, found `foo`
2+
--> $DIR/fn-defined-using-def.rs:6:5
3+
|
4+
LL | def foo() {}
5+
| --- ^^^ expected one of `!` or `::`
6+
| |
7+
| help: write `fn` instead of `def` to declare a function
8+
9+
error: aborting due to previous error
10+
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Check what happens when `fun` is used to define a function, instead of `fn`
2+
// edition:2021
3+
4+
#![allow(dead_code)]
5+
6+
fun foo() {}
7+
//~^ ERROR expected one of `!` or `::`, found `foo`
8+
//~^^ HELP write `fn` instead of `fun` to declare a function
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected one of `!` or `::`, found `foo`
2+
--> $DIR/fn-defined-using-fun.rs:6:5
3+
|
4+
LL | fun foo() {}
5+
| --- ^^^ expected one of `!` or `::`
6+
| |
7+
| help: write `fn` instead of `fun` to declare a function
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Check what happens when `func` is used to define a function, instead of `fn`
2+
// edition:2021
3+
4+
#![allow(dead_code)]
5+
6+
func foo() {}
7+
//~^ ERROR expected one of `!` or `::`, found `foo`
8+
//~^^ HELP write `fn` instead of `func` to declare a function
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected one of `!` or `::`, found `foo`
2+
--> $DIR/fn-defined-using-func.rs:6:6
3+
|
4+
LL | func foo() {}
5+
| ---- ^^^ expected one of `!` or `::`
6+
| |
7+
| help: write `fn` instead of `func` to declare a function
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Check what happens when `function` is used to define a function, instead of `fn`
2+
// edition:2021
3+
4+
#![allow(dead_code)]
5+
6+
function foo() {}
7+
//~^ ERROR expected one of `!` or `::`, found `foo`
8+
//~^^ HELP write `fn` instead of `function` to declare a function
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected one of `!` or `::`, found `foo`
2+
--> $DIR/fn-defined-using-function.rs:6:10
3+
|
4+
LL | function foo() {}
5+
| -------- ^^^ expected one of `!` or `::`
6+
| |
7+
| help: write `fn` instead of `function` to declare a function
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)