Skip to content

Commit c057b45

Browse files
authored
checker: check argument count for C fn with attribute (#8728)
1 parent 2911f03 commit c057b45

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

vlib/v/checker/checker.v

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,10 +1909,11 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
19091909
if f.language != .v || call_expr.language != .v {
19101910
// ignore C function of type `fn()`, assume untyped
19111911
// For now don't check C functions that are variadic, underscored, capitalized
1912-
// or have no params and return int
1912+
// or have no params or attributes and return int
19131913
if f.language == .c && f.params.len != call_expr.args.len && !f.is_variadic
1914-
&& f.name[2] != `_` && !f.name[2].is_capital()
1915-
&& (f.params.len != 0 || f.return_type !in [table.void_type, table.int_type]) {
1914+
&& f.name[2] != `_` && !f.name[2].is_capital() && (f.params.len != 0
1915+
|| f.return_type !in [table.void_type, table.int_type]
1916+
|| f.attrs.len > 0) {
19161917
// change to error later
19171918
c.warn('expected $f.params.len arguments, but got $call_expr.args.len', call_expr.pos)
19181919
}

vlib/v/checker/tests/c_fn_surplus_args.out

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ vlib/v/checker/tests/c_fn_surplus_args.vv:13:2: error: the `main` function canno
2424
12 | // avoid cgen whilst warning, later above should error
2525
13 | main()
2626
| ~~~~~~
27-
14 | }
27+
14 | C.af() // ok
28+
15 | C.af(3)
29+
vlib/v/checker/tests/c_fn_surplus_args.vv:15:4: error: expected 0 arguments, but got 1
30+
13 | main()
31+
14 | C.af() // ok
32+
15 | C.af(3)
33+
| ~~~~~
34+
16 | }
35+
17 |

vlib/v/checker/tests/c_fn_surplus_args.vv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ fn main() {
1111
C.ret(1)
1212
// avoid cgen whilst warning, later above should error
1313
main()
14+
C.af() // ok
15+
C.af(3)
1416
}
17+
18+
[trusted]
19+
fn C.af()int

0 commit comments

Comments
 (0)