Skip to content

Commit 2a3dc5c

Browse files
authored
parser: disallow invalid expr in comptime $for (fix #23953) (#23959)
1 parent 1e5c812 commit 2a3dc5c

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎vlib/v/parser/comptime.v‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ fn (mut p Parser) comptime_for() ast.ComptimeFor {
361361
mut typ_pos := p.tok.pos()
362362
lang := p.parse_language()
363363
mut typ := ast.void_type
364+
365+
if p.tok.lit.len == 0 {
366+
p.error('invalid expr, use `${p.peek_tok.lit}` instead')
367+
return ast.ComptimeFor{}
368+
}
364369
if p.tok.lit[0].is_capital() || p.tok.lit in p.imports {
365370
typ = p.parse_any_type(lang, false, true, false)
366371
} else {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/parser/tests/comptime_for_invalid_expr_err.vv:19:12: error: invalid expr, use `MyStruct` instead
2+
17 | }
3+
18 |
4+
19 | $for f in $MyStruct.fields {
5+
| ^
6+
20 | assert a.${f.field_name} == b.${f.field_name}
7+
21 | }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module main
2+
3+
struct MyStruct {
4+
f_u8 u8
5+
f_u16 u16
6+
}
7+
8+
fn main() {
9+
a := MyStruct {
10+
f_u8 : u8(12)
11+
f_u16 : u16(32)
12+
}
13+
14+
b := MyStruct {
15+
f_u8 : u8(12)
16+
f_u16 : u16(32)
17+
}
18+
19+
$for f in $MyStruct.fields {
20+
assert a.${f.field_name} == b.${f.field_name}
21+
}
22+
}

0 commit comments

Comments
 (0)