Skip to content

Commit af89769

Browse files
authored
Rollup merge of #100750 - akabinds:akabinds/improved-invalid-function-qual-error, r=davidtwco
improved diagnostic for function defined with `def`, `fun`, `func`, or `function` instead of `fn` Closes #99751
2 parents 1e47e8a + 1b54ad0 commit af89769

9 files changed

+89
-0
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+9
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,15 @@ impl<'a> Parser<'a> {
640640
appl,
641641
);
642642
}
643+
644+
if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
645+
err.span_suggestion_short(
646+
self.prev_token.span,
647+
&format!("write `fn` instead of `{symbol}` to declare a function"),
648+
"fn",
649+
appl,
650+
);
651+
}
643652
}
644653

645654
// 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)