diff --git a/examples/graphs/bfs.v b/examples/graphs/bfs.v index 1a2c286016..4fce1d1e1b 100644 --- a/examples/graphs/bfs.v +++ b/examples/graphs/bfs.v @@ -16,7 +16,7 @@ fn main() { // Breadth-First Search (BFS) allows you to find the shortest distance between two nodes in the graph. fn breadth_first_search_path(graph map[string][]string, vertex string, target string) []string { mut path := []string{} - mut visited := []string{init: vertex} + mut visited := []string{len: 6, init: vertex} mut queue := [][][]string{} queue << [[vertex], path] for queue.len > 0 { diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_i.out b/vlib/v/checker/tests/assign_expr_undefined_err_i.out index 74705af122..b5fe345ffa 100644 --- a/vlib/v/checker/tests/assign_expr_undefined_err_i.out +++ b/vlib/v/checker/tests/assign_expr_undefined_err_i.out @@ -1,6 +1,6 @@ -vlib/v/checker/tests/assign_expr_undefined_err_i.vv:2:23: error: undefined variable: `a` +vlib/v/checker/tests/assign_expr_undefined_err_i.vv:2:31: error: undefined variable: `a` 1 | fn main() { - 2 | mut a := []int{init: a} - | ^ + 2 | mut a := []int{len: 1, init: a} + | ^ 3 | println(a) 4 | } diff --git a/vlib/v/checker/tests/assign_expr_undefined_err_i.vv b/vlib/v/checker/tests/assign_expr_undefined_err_i.vv index 26d689e6a8..e3f131ad0b 100644 --- a/vlib/v/checker/tests/assign_expr_undefined_err_i.vv +++ b/vlib/v/checker/tests/assign_expr_undefined_err_i.vv @@ -1,4 +1,4 @@ fn main() { - mut a := []int{init: a} + mut a := []int{len: 1, init: a} println(a) } diff --git a/vlib/v/checker/tests/optional_fn_err.out b/vlib/v/checker/tests/optional_fn_err.out index 865753c2b3..105498e3e5 100644 --- a/vlib/v/checker/tests/optional_fn_err.out +++ b/vlib/v/checker/tests/optional_fn_err.out @@ -80,18 +80,18 @@ vlib/v/checker/tests/optional_fn_err.vv:56:8: error: bar() returns an option, so 55 | // init 56 | _ := [bar(0)] | ~~~~~~ - 57 | _ := []int{init: bar(0)} + 57 | _ := []int{len: 1, init: bar(0)} 58 | _ := [bar(0)]! -vlib/v/checker/tests/optional_fn_err.vv:57:19: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end +vlib/v/checker/tests/optional_fn_err.vv:57:27: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end 55 | // init 56 | _ := [bar(0)] - 57 | _ := []int{init: bar(0)} - | ~~~~~~ + 57 | _ := []int{len: 1, init: bar(0)} + | ~~~~~~ 58 | _ := [bar(0)]! 59 | _ := [1]int{init: bar(0)} vlib/v/checker/tests/optional_fn_err.vv:58:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end 56 | _ := [bar(0)] - 57 | _ := []int{init: bar(0)} + 57 | _ := []int{len: 1, init: bar(0)} 58 | _ := [bar(0)]! | ~~~~~~ 59 | _ := [1]int{init: bar(0)} diff --git a/vlib/v/checker/tests/optional_fn_err.vv b/vlib/v/checker/tests/optional_fn_err.vv index 76446d6638..1727144c06 100644 --- a/vlib/v/checker/tests/optional_fn_err.vv +++ b/vlib/v/checker/tests/optional_fn_err.vv @@ -54,7 +54,7 @@ fn main() { arr << bar(0) // init _ := [bar(0)] - _ := []int{init: bar(0)} + _ := []int{len: 1, init: bar(0)} _ := [bar(0)]! _ := [1]int{init: bar(0)} // index diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 19672ac99a..32bc44e486 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -4,6 +4,7 @@ module parser import v.ast +import v.token fn (mut p Parser) array_init() ast.ArrayInit { first_pos := p.tok.pos() @@ -132,10 +133,12 @@ fn (mut p Parser) array_init() ast.ArrayInit { mut has_cap := false mut len_expr := ast.empty_expr mut cap_expr := ast.empty_expr + mut attr_pos := token.Pos{} if p.tok.kind == .lcbr && exprs.len == 0 && array_type != ast.void_type { // `[]int{ len: 10, cap: 100}` syntax p.next() for p.tok.kind != .rcbr { + attr_pos = p.tok.pos() key := p.check_name() p.check(.colon) match key { @@ -172,6 +175,10 @@ fn (mut p Parser) array_init() ast.ArrayInit { } } p.check(.rcbr) + if has_default && !has_len { + p.error_with_pos('cannot use `init` attribute unless `len` attribute is also provided', + attr_pos) + } } pos := first_pos.extend_with_last_line(last_pos, p.prev_tok.line_nr) return ast.ArrayInit{ diff --git a/vlib/v/parser/tests/array_init_given_len_not_given_err.out b/vlib/v/parser/tests/array_init_given_len_not_given_err.out new file mode 100644 index 0000000000..ef28373ecb --- /dev/null +++ b/vlib/v/parser/tests/array_init_given_len_not_given_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/array_init_given_len_not_given_err.vv:8:27: error: cannot use `init` attribute unless `len` attribute is also provided + 6 | + 7 | fn main() { + 8 | mut list_elem := [][]f64{init: [0.0, 0.0]} + | ~~~~ + 9 | mut elem := 0.0 + 10 | for i in 0 .. lenn { diff --git a/vlib/v/parser/tests/array_init_given_len_not_given_err.vv b/vlib/v/parser/tests/array_init_given_len_not_given_err.vv new file mode 100644 index 0000000000..62d83bd219 --- /dev/null +++ b/vlib/v/parser/tests/array_init_given_len_not_given_err.vv @@ -0,0 +1,15 @@ +import rand + +const ( + lenn = 100 +) + +fn main() { + mut list_elem := [][]f64{init: [0.0, 0.0]} + mut elem := 0.0 + for i in 0 .. lenn { + elem = rand.int_in_range(-100000000, 100000001)! + list_elem[i] << [elem, elem] + } + println(list_elem) +}