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:
parent
47b5d206a6
commit
a38310f929
|
@ -7,7 +7,8 @@ import v.token
|
||||||
|
|
||||||
pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
|
pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
|
||||||
mut elem_type := ast.void_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.typ != ast.void_type {
|
||||||
if node.elem_type != 0 {
|
if node.elem_type != 0 {
|
||||||
elem_sym := c.table.sym(node.elem_type)
|
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 {}
|
c.ensure_type_exists(node.elem_type, node.elem_type_pos) or {}
|
||||||
if node.typ.has_flag(.generic) && !isnil(c.table.cur_fn)
|
if node.typ.has_flag(.generic) && !isnil(c.table.cur_fn)
|
||||||
&& c.table.cur_fn.generic_names.len == 0 {
|
&& 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
|
// &int{} check
|
||||||
|
@ -67,6 +68,10 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
|
||||||
if node.is_fixed {
|
if node.is_fixed {
|
||||||
c.ensure_sumtype_array_has_default_value(node)
|
c.ensure_sumtype_array_has_default_value(node)
|
||||||
c.ensure_type_exists(node.elem_type, node.elem_type_pos) or {}
|
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 = []
|
// a = []
|
||||||
if node.exprs.len == 0 {
|
if node.exprs.len == 0 {
|
||||||
|
|
|
@ -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 |
|
3 |
|
||||||
4 | fn main() {
|
4 | fn main() {
|
||||||
5 | _ := []Example<T>{}
|
5 | _ := []Example<T>{}
|
||||||
|
|
|
@ -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() {
|
1 | fn main() {
|
||||||
2 | println(*[]&int{len: 1}[0])
|
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 | }
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
println(*[]&int{len: 1}[0])
|
println(*[]&int{len: 1}[0])
|
||||||
|
println([1]&int{})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user