diff options
| author | akabinds <staticxxd@gmail.com> | 2022-08-18 16:14:04 -0500 |
|---|---|---|
| committer | akabinds <staticxxd@gmail.com> | 2022-08-18 16:14:04 -0500 |
| commit | 1b54ad0585ca25b779be93d1149930664f722dc3 (patch) | |
| tree | 3f56da8171e0297af4739417f7f71c56e9e974dc | |
| parent | 801821d1560f84e4716fcbd9244ec959320a13d5 (diff) | |
| download | rust-1b54ad0585ca25b779be93d1149930664f722dc3.tar.gz rust-1b54ad0585ca25b779be93d1149930664f722dc3.zip | |
added improved diagnostic for a function defined with an invalid qualifier
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 9 | ||||
| -rw-r--r-- | src/test/ui/parser/fn-defined-using-def.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/parser/fn-defined-using-def.stderr | 10 | ||||
| -rw-r--r-- | src/test/ui/parser/fn-defined-using-fun.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/parser/fn-defined-using-fun.stderr | 10 | ||||
| -rw-r--r-- | src/test/ui/parser/fn-defined-using-func.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/parser/fn-defined-using-func.stderr | 10 | ||||
| -rw-r--r-- | src/test/ui/parser/fn-defined-using-function.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/parser/fn-defined-using-function.stderr | 10 |
9 files changed, 89 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index f4c6b33a529..3bc00635013 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -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 diff --git a/src/test/ui/parser/fn-defined-using-def.rs b/src/test/ui/parser/fn-defined-using-def.rs new file mode 100644 index 00000000000..21da34c47c9 --- /dev/null +++ b/src/test/ui/parser/fn-defined-using-def.rs @@ -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() {} diff --git a/src/test/ui/parser/fn-defined-using-def.stderr b/src/test/ui/parser/fn-defined-using-def.stderr new file mode 100644 index 00000000000..f34329012a0 --- /dev/null +++ b/src/test/ui/parser/fn-defined-using-def.stderr @@ -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 + diff --git a/src/test/ui/parser/fn-defined-using-fun.rs b/src/test/ui/parser/fn-defined-using-fun.rs new file mode 100644 index 00000000000..4f74605043e --- /dev/null +++ b/src/test/ui/parser/fn-defined-using-fun.rs @@ -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() {} diff --git a/src/test/ui/parser/fn-defined-using-fun.stderr b/src/test/ui/parser/fn-defined-using-fun.stderr new file mode 100644 index 00000000000..2f6cfff350c --- /dev/null +++ b/src/test/ui/parser/fn-defined-using-fun.stderr @@ -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 + diff --git a/src/test/ui/parser/fn-defined-using-func.rs b/src/test/ui/parser/fn-defined-using-func.rs new file mode 100644 index 00000000000..2dce96fdce0 --- /dev/null +++ b/src/test/ui/parser/fn-defined-using-func.rs @@ -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() {} diff --git a/src/test/ui/parser/fn-defined-using-func.stderr b/src/test/ui/parser/fn-defined-using-func.stderr new file mode 100644 index 00000000000..355741e8949 --- /dev/null +++ b/src/test/ui/parser/fn-defined-using-func.stderr @@ -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 + diff --git a/src/test/ui/parser/fn-defined-using-function.rs b/src/test/ui/parser/fn-defined-using-function.rs new file mode 100644 index 00000000000..fd8782728e2 --- /dev/null +++ b/src/test/ui/parser/fn-defined-using-function.rs @@ -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() {} diff --git a/src/test/ui/parser/fn-defined-using-function.stderr b/src/test/ui/parser/fn-defined-using-function.stderr new file mode 100644 index 00000000000..43c33a2cdd7 --- /dev/null +++ b/src/test/ui/parser/fn-defined-using-function.stderr @@ -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 + |
