parser: disallow invalid expr in comptime $for (fix #23953) (#23959)

This commit is contained in:
Swastik Baranwal 2025-03-17 20:14:26 +05:30 committed by GitHub
parent 1e5c812540
commit 2a3dc5c068
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 0 deletions

View file

@ -361,6 +361,11 @@ fn (mut p Parser) comptime_for() ast.ComptimeFor {
mut typ_pos := p.tok.pos()
lang := p.parse_language()
mut typ := ast.void_type
if p.tok.lit.len == 0 {
p.error('invalid expr, use `${p.peek_tok.lit}` instead')
return ast.ComptimeFor{}
}
if p.tok.lit[0].is_capital() || p.tok.lit in p.imports {
typ = p.parse_any_type(lang, false, true, false)
} else {

View file

@ -0,0 +1,7 @@
vlib/v/parser/tests/comptime_for_invalid_expr_err.vv:19:12: error: invalid expr, use `MyStruct` instead
17 | }
18 |
19 | $for f in $MyStruct.fields {
| ^
20 | assert a.${f.field_name} == b.${f.field_name}
21 | }

View file

@ -0,0 +1,22 @@
module main
struct MyStruct {
f_u8 u8
f_u16 u16
}
fn main() {
a := MyStruct {
f_u8 : u8(12)
f_u16 : u16(32)
}
b := MyStruct {
f_u8 : u8(12)
f_u16 : u16(32)
}
$for f in $MyStruct.fields {
assert a.${f.field_name} == b.${f.field_name}
}
}