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

checker: array of references check

This commit is contained in:
Alexander Medvednikov 2022-07-14 00:58:14 +03:00
parent 47b5d206a6
commit a38310f929
4 changed files with 19 additions and 6 deletions

View File

@ -7,7 +7,8 @@ import v.token
pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
mut elem_type := ast.void_type
// []string - was set in parser
// `x := []string{}` (the type was set in the parser)
// TODO type is not set for fixed arrays
if node.typ != ast.void_type {
if node.elem_type != 0 {
elem_sym := c.table.sym(node.elem_type)
@ -54,7 +55,7 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
c.ensure_type_exists(node.elem_type, node.elem_type_pos) or {}
if node.typ.has_flag(.generic) && !isnil(c.table.cur_fn)
&& c.table.cur_fn.generic_names.len == 0 {
c.error('generic struct cannot use in non-generic function', node.pos)
c.error('generic struct cannot be used in non-generic function', node.pos)
}
// &int{} check
@ -67,6 +68,10 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
if node.is_fixed {
c.ensure_sumtype_array_has_default_value(node)
c.ensure_type_exists(node.elem_type, node.elem_type_pos) or {}
if node.elem_type.is_any_kind_of_pointer() && !c.inside_unsafe && !c.is_builtin_mod {
c.warn('fixed arrays of references need to be initialized right away (unless inside `unsafe`)',
node.pos)
}
}
// a = []
if node.exprs.len == 0 {

View File

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

View File

@ -1,5 +1,12 @@
vlib/v/checker/tests/ptr_array_init.vv:2:14: warning: arrays of references need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`)
vlib/v/checker/tests/ptr_array_init.vv:2:11: warning: arrays of references need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`)
1 | fn main() {
2 | println(*[]&int{len: 1}[0])
| ~~~~~~~
3 | }
3 | println([1]&int{})
4 | }
vlib/v/checker/tests/ptr_array_init.vv:3:10: warning: fixed arrays of references need to be initialized right away (unless inside `unsafe`)
1 | fn main() {
2 | println(*[]&int{len: 1}[0])
3 | println([1]&int{})
| ~~~~~~~~~
4 | }

View File

@ -1,3 +1,4 @@
fn main() {
println(*[]&int{len: 1}[0])
println(*[]&int{len: 1}[0])
println([1]&int{})
}