parser, checker: correct error message for a fixed array size using a non constant (fix #13219) (#13228)

pull/13231/head
yuyi 2022-01-20 19:03:19 +08:00 committed by GitHub
parent 5143837d66
commit d553071e65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 2 deletions

View File

@ -163,7 +163,7 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
}
}
else {
c.error('expecting `int` for fixed size', node.pos)
c.error('fixed array size cannot use non-constant value', init_expr.position())
}
}
if fixed_size <= 0 {

View File

@ -59,7 +59,8 @@ pub fn (mut p Parser) parse_array_type(expecting token.Kind) ast.Type {
}
}
else {
p.error('expecting `int` for fixed size')
p.error_with_pos('fixed array size cannot use non-constant value',
size_expr.position())
}
}
}

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/fixed_array_size_using_non_constant_err.vv:9:25: error: fixed array size cannot use non-constant value
7 | println(typeof(t.len).name)
8 |
9 | mut score := [t.len][t.len]int{}
| ~~~
10 | }

View File

@ -0,0 +1,10 @@
module main
fn main() {
mut t := [0, 0]
println(t.len)
println(typeof(t.len).name)
mut score := [t.len][t.len]int{}
}