checker: check generic struct using in non-generic fn (#12136)

pull/11131/merge
yuyi 2021-10-11 07:46:44 +08:00 committed by GitHub
parent e69df54a36
commit 1831eccd5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 0 deletions

View File

@ -4592,6 +4592,9 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
c.ensure_sumtype_array_has_default_value(node)
}
c.ensure_type_exists(node.elem_type, node.elem_type_pos) or {}
if node.typ.has_flag(.generic) && c.table.cur_fn.generic_names.len == 0 {
c.error('generic struct cannot use in non-generic function', node.pos)
}
return node.typ
}
if node.is_fixed {

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/generics_struct_in_non_generic_fn_err.vv:5:7: error: generic struct cannot use in non-generic function
3 |
4 | fn main() {
5 | _ := []Example<T>{}
| ~~~~~~~~~~~~~
6 | }

View File

@ -0,0 +1,6 @@
struct Example<T> {
}
fn main() {
_ := []Example<T>{}
}