mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v.parser: add checks for interoperability (C. or JS.) function declarations (#11140)
This commit is contained in:
parent
89a8854e57
commit
6dbc6f233b
@ -212,9 +212,17 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||
mut language := ast.Language.v
|
||||
if p.tok.kind == .name && p.tok.lit == 'C' {
|
||||
is_unsafe = !is_trusted
|
||||
language = ast.Language.c
|
||||
language = .c
|
||||
} else if p.tok.kind == .name && p.tok.lit == 'JS' {
|
||||
language = ast.Language.js
|
||||
language = .js
|
||||
}
|
||||
if language != .v {
|
||||
for fna in p.attrs {
|
||||
if fna.name == 'export' {
|
||||
p.error_with_pos('interop function cannot be exported', fna.pos)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_keep_alive && language != .c {
|
||||
p.error_with_pos('attribute [keep_args_alive] is only supported for C functions',
|
||||
@ -435,6 +443,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||
mut stmts := []ast.Stmt{}
|
||||
body_start_pos := p.peek_tok.position()
|
||||
if p.tok.kind == .lcbr {
|
||||
if language != .v {
|
||||
p.error_with_pos('interop functions cannot have a body', p.tok.position())
|
||||
}
|
||||
p.inside_fn = true
|
||||
p.inside_unsafe_fn = is_unsafe
|
||||
stmts = p.parse_block_no_scope(true)
|
||||
|
4
vlib/v/parser/tests/export_interop_func_err.out
Normal file
4
vlib/v/parser/tests/export_interop_func_err.out
Normal file
@ -0,0 +1,4 @@
|
||||
vlib/v/parser/tests/export_interop_func_err.vv:1:1: error: interop function cannot be exported
|
||||
1 | [export: 'test']
|
||||
| ~~~~~~~~~~~~~~~~
|
||||
2 | fn C.printf(s string)
|
2
vlib/v/parser/tests/export_interop_func_err.vv
Normal file
2
vlib/v/parser/tests/export_interop_func_err.vv
Normal file
@ -0,0 +1,2 @@
|
||||
[export: 'test']
|
||||
fn C.printf(s string)
|
3
vlib/v/parser/tests/interop_func_body_err.out
Normal file
3
vlib/v/parser/tests/interop_func_body_err.out
Normal file
@ -0,0 +1,3 @@
|
||||
vlib/v/parser/tests/interop_func_body_err.vv:1:23: error: interop functions cannot have a body
|
||||
1 | fn C.printf(s string) {}
|
||||
| ^
|
1
vlib/v/parser/tests/interop_func_body_err.vv
Normal file
1
vlib/v/parser/tests/interop_func_body_err.vv
Normal file
@ -0,0 +1 @@
|
||||
fn C.printf(s string) {}
|
Loading…
Reference in New Issue
Block a user