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

checker: check fn call using 'none' as argument (#17070)

This commit is contained in:
yuyi 2023-01-22 20:58:34 +08:00 committed by GitHub
parent ddf3909fed
commit b2dac566b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -1019,6 +1019,9 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
}
}
arg_typ_sym := c.table.sym(arg_typ)
if arg_typ_sym.kind == .none_ {
c.error('cannot use `none` as function argument', call_arg.pos)
}
param_typ_sym := c.table.sym(param.typ)
if func.is_variadic && arg_typ.has_flag(.variadic) && node.args.len - 1 > i {
c.error('when forwarding a variadic variable, it must be the final argument',
@ -1748,6 +1751,9 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
}
continue
}
if final_arg_sym.kind == .none_ {
c.error('cannot use `none` as method argument', arg.pos)
}
if param.typ.is_ptr() && !arg.typ.is_real_pointer() && arg.expr.is_literal()
&& !c.pref.translated {
c.error('literal argument cannot be passed as reference parameter `${c.table.type_to_str(param.typ)}`',

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/fn_call_using_none_arg_err.vv:6:4: error: cannot use `none` as function argument
4 |
5 | fn main() {
6 | f(none)
| ~~~~
7 | }

View File

@ -0,0 +1,7 @@
fn f[T](v T) {
println(v)
}
fn main() {
f(none)
}