checker: check generic fn called no arg without generic names (#9825)

pull/9833/head
yuyi 2021-04-21 02:36:21 +08:00 committed by GitHub
parent bd539b6427
commit 7abf59710b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -2104,6 +2104,11 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type {
for generic_type in call_expr.generic_types { for generic_type in call_expr.generic_types {
c.ensure_type_exists(generic_type, call_expr.generic_list_pos) or {} c.ensure_type_exists(generic_type, call_expr.generic_list_pos) or {}
} }
if func.generic_names.len > 0 && call_expr.args.len == 0 && call_expr.generic_types.len == 0 {
c.error('no argument generic function must add concrete types, e.g. foo<int>()',
call_expr.pos)
return func.return_type
}
if func.generic_names.len > 0 && func.return_type.has_flag(.generic) { if func.generic_names.len > 0 && func.return_type.has_flag(.generic) {
c.check_return_generics_struct(func.return_type, mut call_expr, generic_types) c.check_return_generics_struct(func.return_type, mut call_expr, generic_types)
} else { } else {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/generics_fn_called_no_arg_err.vv:13:10: error: no argument generic function must add concrete types, e.g. foo<int>()
11 |
12 | fn main() {
13 | q := new_queue()
| ~~~~~~~~~~~
14 | println(q)
15 | }

View File

@ -0,0 +1,15 @@
struct Queue<T>{
buffer []T
}
fn new_queue<T>() Queue<T> {
q := Queue<T>{
buffer: []T{cap: 1024}
}
return q
}
fn main() {
q := new_queue()
println(q)
}