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

parser: disallow the array init: attr, when the len: attr is not provided (#16735)

This commit is contained in:
Swastik Baranwal 2022-12-22 00:54:16 +05:30 committed by GitHub
parent 87f8069728
commit 3c5cfa22d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 11 deletions

View File

@ -16,7 +16,7 @@ fn main() {
// Breadth-First Search (BFS) allows you to find the shortest distance between two nodes in the graph. // 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 { fn breadth_first_search_path(graph map[string][]string, vertex string, target string) []string {
mut path := []string{} mut path := []string{}
mut visited := []string{init: vertex} mut visited := []string{len: 6, init: vertex}
mut queue := [][][]string{} mut queue := [][][]string{}
queue << [[vertex], path] queue << [[vertex], path]
for queue.len > 0 { for queue.len > 0 {

View File

@ -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() { 1 | fn main() {
2 | mut a := []int{init: a} 2 | mut a := []int{len: 1, init: a}
| ^ | ^
3 | println(a) 3 | println(a)
4 | } 4 | }

View File

@ -1,4 +1,4 @@
fn main() { fn main() {
mut a := []int{init: a} mut a := []int{len: 1, init: a}
println(a) println(a)
} }

View File

@ -80,18 +80,18 @@ vlib/v/checker/tests/optional_fn_err.vv:56:8: error: bar() returns an option, so
55 | // init 55 | // init
56 | _ := [bar(0)] 56 | _ := [bar(0)]
| ~~~~~~ | ~~~~~~
57 | _ := []int{init: bar(0)} 57 | _ := []int{len: 1, init: bar(0)}
58 | _ := [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 55 | // init
56 | _ := [bar(0)] 56 | _ := [bar(0)]
57 | _ := []int{init: bar(0)} 57 | _ := []int{len: 1, init: bar(0)}
| ~~~~~~ | ~~~~~~
58 | _ := [bar(0)]! 58 | _ := [bar(0)]!
59 | _ := [1]int{init: 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 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)] 56 | _ := [bar(0)]
57 | _ := []int{init: bar(0)} 57 | _ := []int{len: 1, init: bar(0)}
58 | _ := [bar(0)]! 58 | _ := [bar(0)]!
| ~~~~~~ | ~~~~~~
59 | _ := [1]int{init: bar(0)} 59 | _ := [1]int{init: bar(0)}

View File

@ -54,7 +54,7 @@ fn main() {
arr << bar(0) arr << bar(0)
// init // init
_ := [bar(0)] _ := [bar(0)]
_ := []int{init: bar(0)} _ := []int{len: 1, init: bar(0)}
_ := [bar(0)]! _ := [bar(0)]!
_ := [1]int{init: bar(0)} _ := [1]int{init: bar(0)}
// index // index

View File

@ -4,6 +4,7 @@
module parser module parser
import v.ast import v.ast
import v.token
fn (mut p Parser) array_init() ast.ArrayInit { fn (mut p Parser) array_init() ast.ArrayInit {
first_pos := p.tok.pos() first_pos := p.tok.pos()
@ -132,10 +133,12 @@ fn (mut p Parser) array_init() ast.ArrayInit {
mut has_cap := false mut has_cap := false
mut len_expr := ast.empty_expr mut len_expr := ast.empty_expr
mut cap_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 { if p.tok.kind == .lcbr && exprs.len == 0 && array_type != ast.void_type {
// `[]int{ len: 10, cap: 100}` syntax // `[]int{ len: 10, cap: 100}` syntax
p.next() p.next()
for p.tok.kind != .rcbr { for p.tok.kind != .rcbr {
attr_pos = p.tok.pos()
key := p.check_name() key := p.check_name()
p.check(.colon) p.check(.colon)
match key { match key {
@ -172,6 +175,10 @@ fn (mut p Parser) array_init() ast.ArrayInit {
} }
} }
p.check(.rcbr) 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) pos := first_pos.extend_with_last_line(last_pos, p.prev_tok.line_nr)
return ast.ArrayInit{ return ast.ArrayInit{

View File

@ -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 {

View File

@ -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)
}