From 3ee3c8b3eda93bf8c014de78154928ffe24f4fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 30 Dec 2020 20:18:36 +0100 Subject: [PATCH] parser: proper error on fn decl in script mode (#7680) --- vlib/v/parser/fn.v | 5 +++++ vlib/v/parser/tests/invalid_fn_decl_script_err.out | 7 +++++++ vlib/v/parser/tests/invalid_fn_decl_script_err.vv | 5 +++++ 3 files changed, 17 insertions(+) create mode 100644 vlib/v/parser/tests/invalid_fn_decl_script_err.out create mode 100644 vlib/v/parser/tests/invalid_fn_decl_script_err.vv diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index c18ec96fb6..162e895493 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -424,6 +424,11 @@ fn (mut p Parser) fn_decl() ast.FnDecl { fn (mut p Parser) anon_fn() ast.AnonFn { pos := p.tok.position() p.check(.key_fn) + if p.pref.is_script && p.tok.kind == .name { + p.error_with_pos('function declarations in script mode should be before all script statements', + p.tok.position()) + return ast.AnonFn{} + } p.open_scope() // TODO generics args, _, is_variadic := p.fn_args() diff --git a/vlib/v/parser/tests/invalid_fn_decl_script_err.out b/vlib/v/parser/tests/invalid_fn_decl_script_err.out new file mode 100644 index 0000000000..aead2d093f --- /dev/null +++ b/vlib/v/parser/tests/invalid_fn_decl_script_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/invalid_fn_decl_script_err.vv:3:4: error: function declarations in script mode should be before all script statements + 1 | mynum := 10 + 2 | + 3 | fn main() { + | ~~~~ + 4 | println(mynum) + 5 | } \ No newline at end of file diff --git a/vlib/v/parser/tests/invalid_fn_decl_script_err.vv b/vlib/v/parser/tests/invalid_fn_decl_script_err.vv new file mode 100644 index 0000000000..196895a1cf --- /dev/null +++ b/vlib/v/parser/tests/invalid_fn_decl_script_err.vv @@ -0,0 +1,5 @@ +mynum := 10 + +fn main() { + println(mynum) +}