1
0
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:
Daniel Däschle 2021-08-12 09:47:24 +02:00 committed by GitHub
parent 89a8854e57
commit 6dbc6f233b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 2 deletions

View File

@ -212,9 +212,17 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
mut language := ast.Language.v mut language := ast.Language.v
if p.tok.kind == .name && p.tok.lit == 'C' { if p.tok.kind == .name && p.tok.lit == 'C' {
is_unsafe = !is_trusted is_unsafe = !is_trusted
language = ast.Language.c language = .c
} else if p.tok.kind == .name && p.tok.lit == 'JS' { } 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 { if is_keep_alive && language != .c {
p.error_with_pos('attribute [keep_args_alive] is only supported for C functions', 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{} mut stmts := []ast.Stmt{}
body_start_pos := p.peek_tok.position() body_start_pos := p.peek_tok.position()
if p.tok.kind == .lcbr { 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_fn = true
p.inside_unsafe_fn = is_unsafe p.inside_unsafe_fn = is_unsafe
stmts = p.parse_block_no_scope(true) stmts = p.parse_block_no_scope(true)

View 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)

View File

@ -0,0 +1,2 @@
[export: 'test']
fn C.printf(s string)

View 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) {}
| ^

View File

@ -0,0 +1 @@
fn C.printf(s string) {}