From 65089eee412e35b9ce60ef25115508eea5ecf9e1 Mon Sep 17 00:00:00 2001 From: shove Date: Fri, 24 Nov 2023 14:05:01 +0800 Subject: [PATCH] checker: add a qualification to array elements ref initialization checks (#19980) --- vlib/v/checker/containers.v | 5 ++++- ...y_map_elements_ref_fields_uninitialized_err.out | 14 +++++++------- ...ay_map_elements_ref_fields_uninitialized_err.vv | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index 3ec123bb3..1a88fed60 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -102,7 +102,10 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { c.warn('arrays of references need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`)', node.pos) } - c.check_elements_ref_fields_initialized(node.elem_type, node.pos) + // `&Struct{} check + if node.has_len { + c.check_elements_ref_fields_initialized(node.elem_type, node.pos) + } return node.typ } diff --git a/vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.out b/vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.out index 35886eaac..4e436c801 100644 --- a/vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.out +++ b/vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.out @@ -1,34 +1,34 @@ vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv:8:6: notice: reference field `Foo.n` must be initialized (part of struct `Foo`) - 6 | + 6 | 7 | fn main() { - 8 | _ = []Foo{} + 8 | _ = []Foo{len: 1} | ~~~~~~ 9 | _ = [1]Foo{} 10 | _ = map[string]Foo{} vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv:9:6: notice: reference field `Foo.n` must be initialized (part of struct `Foo`) 7 | fn main() { - 8 | _ = []Foo{} + 8 | _ = []Foo{len: 1} 9 | _ = [1]Foo{} | ~~~~~~~~ 10 | _ = map[string]Foo{} 11 | _ = map[string][]Foo{} vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv:10:6: notice: reference field `Foo.n` must be initialized (part of struct `Foo`) - 8 | _ = []Foo{} + 8 | _ = []Foo{len: 1} 9 | _ = [1]Foo{} 10 | _ = map[string]Foo{} | ~~~~~~~~~~~~~~~~ 11 | _ = map[string][]Foo{} - 12 | _ = []AliasFoo{} + 12 | _ = []AliasFoo{len: 1} vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv:11:6: notice: reference field `Foo.n` must be initialized (part of struct `Foo`) 9 | _ = [1]Foo{} 10 | _ = map[string]Foo{} 11 | _ = map[string][]Foo{} | ~~~~~~~~~~~~~~~~~~ - 12 | _ = []AliasFoo{} + 12 | _ = []AliasFoo{len: 1} 13 | } vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv:12:6: notice: reference field `Foo.n` must be initialized (part of struct `Foo`) 10 | _ = map[string]Foo{} 11 | _ = map[string][]Foo{} - 12 | _ = []AliasFoo{} + 12 | _ = []AliasFoo{len: 1} | ~~~~~~~~~~~ 13 | } diff --git a/vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv b/vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv index 7b8c214bf..c78f7daea 100644 --- a/vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv +++ b/vlib/v/checker/tests/array_map_elements_ref_fields_uninitialized_err.vv @@ -5,9 +5,9 @@ struct Foo { type AliasFoo = Foo fn main() { - _ = []Foo{} + _ = []Foo{len: 1} _ = [1]Foo{} _ = map[string]Foo{} _ = map[string][]Foo{} - _ = []AliasFoo{} + _ = []AliasFoo{len: 1} } -- 2.39.5