mirror of
https://github.com/vlang/v.git
synced 2025-09-16 07:52:32 +03:00
parser: proper error on fn decl in script mode (#7680)
This commit is contained in:
parent
e4edc5925a
commit
3ee3c8b3ed
3 changed files with 17 additions and 0 deletions
|
@ -424,6 +424,11 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
fn (mut p Parser) anon_fn() ast.AnonFn {
|
fn (mut p Parser) anon_fn() ast.AnonFn {
|
||||||
pos := p.tok.position()
|
pos := p.tok.position()
|
||||||
p.check(.key_fn)
|
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()
|
p.open_scope()
|
||||||
// TODO generics
|
// TODO generics
|
||||||
args, _, is_variadic := p.fn_args()
|
args, _, is_variadic := p.fn_args()
|
||||||
|
|
7
vlib/v/parser/tests/invalid_fn_decl_script_err.out
Normal file
7
vlib/v/parser/tests/invalid_fn_decl_script_err.out
Normal file
|
@ -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 | }
|
5
vlib/v/parser/tests/invalid_fn_decl_script_err.vv
Normal file
5
vlib/v/parser/tests/invalid_fn_decl_script_err.vv
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
mynum := 10
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println(mynum)
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue