1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check argument count for C fn with attribute (#8728)

This commit is contained in:
Nick Treleaven 2021-02-15 16:56:26 +00:00 committed by GitHub
parent 2911f03627
commit c057b45bb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View File

@ -1909,10 +1909,11 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
if f.language != .v || call_expr.language != .v {
// ignore C function of type `fn()`, assume untyped
// For now don't check C functions that are variadic, underscored, capitalized
// or have no params and return int
// or have no params or attributes and return int
if f.language == .c && f.params.len != call_expr.args.len && !f.is_variadic
&& f.name[2] != `_` && !f.name[2].is_capital()
&& (f.params.len != 0 || f.return_type !in [table.void_type, table.int_type]) {
&& f.name[2] != `_` && !f.name[2].is_capital() && (f.params.len != 0
|| f.return_type !in [table.void_type, table.int_type]
|| f.attrs.len > 0) {
// change to error later
c.warn('expected $f.params.len arguments, but got $call_expr.args.len', call_expr.pos)
}

View File

@ -24,4 +24,12 @@ vlib/v/checker/tests/c_fn_surplus_args.vv:13:2: error: the `main` function canno
12 | // avoid cgen whilst warning, later above should error
13 | main()
| ~~~~~~
14 | }
14 | C.af() // ok
15 | C.af(3)
vlib/v/checker/tests/c_fn_surplus_args.vv:15:4: error: expected 0 arguments, but got 1
13 | main()
14 | C.af() // ok
15 | C.af(3)
| ~~~~~
16 | }
17 |

View File

@ -11,4 +11,9 @@ fn main() {
C.ret(1)
// avoid cgen whilst warning, later above should error
main()
C.af() // ok
C.af(3)
}
[trusted]
fn C.af()int