From c057b45bb1e1159bbbd914990c3f4bfa4690427a Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Mon, 15 Feb 2021 16:56:26 +0000 Subject: [PATCH] checker: check argument count for C fn with attribute (#8728) --- vlib/v/checker/checker.v | 7 ++++--- vlib/v/checker/tests/c_fn_surplus_args.out | 10 +++++++++- vlib/v/checker/tests/c_fn_surplus_args.vv | 5 +++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a384ca369a..577761e5e7 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) } diff --git a/vlib/v/checker/tests/c_fn_surplus_args.out b/vlib/v/checker/tests/c_fn_surplus_args.out index e3aa25a967..9f470d920d 100644 --- a/vlib/v/checker/tests/c_fn_surplus_args.out +++ b/vlib/v/checker/tests/c_fn_surplus_args.out @@ -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 | diff --git a/vlib/v/checker/tests/c_fn_surplus_args.vv b/vlib/v/checker/tests/c_fn_surplus_args.vv index 7e6aa38e25..e7c6466ded 100644 --- a/vlib/v/checker/tests/c_fn_surplus_args.vv +++ b/vlib/v/checker/tests/c_fn_surplus_args.vv @@ -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