Skip to content

Commit d553071

Browse files
authored
parser, checker: correct error message for a fixed array size using a non constant (fix #13219) (#13228)
1 parent 5143837 commit d553071

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

vlib/v/checker/containers.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
163163
}
164164
}
165165
else {
166-
c.error('expecting `int` for fixed size', node.pos)
166+
c.error('fixed array size cannot use non-constant value', init_expr.position())
167167
}
168168
}
169169
if fixed_size <= 0 {

vlib/v/parser/parse_type.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ pub fn (mut p Parser) parse_array_type(expecting token.Kind) ast.Type {
5959
}
6060
}
6161
else {
62-
p.error('expecting `int` for fixed size')
62+
p.error_with_pos('fixed array size cannot use non-constant value',
63+
size_expr.position())
6364
}
6465
}
6566
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/fixed_array_size_using_non_constant_err.vv:9:25: error: fixed array size cannot use non-constant value
2+
7 | println(typeof(t.len).name)
3+
8 |
4+
9 | mut score := [t.len][t.len]int{}
5+
| ~~~
6+
10 | }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module main
2+
3+
fn main() {
4+
mut t := [0, 0]
5+
6+
println(t.len)
7+
println(typeof(t.len).name)
8+
9+
mut score := [t.len][t.len]int{}
10+
}

0 commit comments

Comments
 (0)