ast, parser: support $d() in fixed size array struct fields (#21731)

This commit is contained in:
larpon 2024-06-26 00:13:05 +02:00 committed by GitHub
parent 98a1ee2284
commit a36c0d8315
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 40 additions and 3 deletions

View File

@ -1967,6 +1967,19 @@ pub fn (mut cc ComptimeCall) resolve_compile_value(compile_values map[string]str
cc.is_d_resolved = true cc.is_d_resolved = true
} }
// expr_str returns the string representation of `ComptimeCall` for use with
// `ast.Expr`'s `str()' method (used by e.g. vfmt).
pub fn (cc ComptimeCall) expr_str() string {
mut str := 'ast.ComptimeCall'
if cc.is_compile_value {
arg := cc.args[0] or { return str }
if arg.expr.is_pure_literal() {
str = "\$${cc.method_name}('${cc.args_var}', ${arg})"
}
}
return str
}
pub struct None { pub struct None {
pub: pub:
pos token.Pos pos token.Pos

View File

@ -662,7 +662,7 @@ pub fn (x Expr) str() string {
return 'ast.ChanInit' return 'ast.ChanInit'
} }
ComptimeCall { ComptimeCall {
return 'ast.ComptimeCall' return x.expr_str()
} }
EmptyExpr { EmptyExpr {
return 'ast.EmptyExpr' return 'ast.EmptyExpr'

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/comptime_value_d_only_d_in_fixed_size_array.vv:2:7: error: only $d() is supported as fixed array size quantifier at compile time
1 | struct St {
2 | fsa [$env('size')]int
| ~~~~~~~~~~~~
3 | }

View File

@ -0,0 +1,3 @@
struct St {
fsa [$env('size')]int
}

View File

@ -2,3 +2,4 @@
false false
done done
[0, 0, 0, 0] [0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

View File

@ -3,6 +3,10 @@
const my_f64 = $d('my_f64', 42.0) const my_f64 = $d('my_f64', 42.0)
struct StructFSAWithDSize {
fsa [$d('field_fsa_size', 10)]int
}
fn main() { fn main() {
println(my_f64) println(my_f64)
cv_bool := $d('my_bool', false) cv_bool := $d('my_bool', false)
@ -10,4 +14,6 @@ fn main() {
println('done') println('done')
fsa := [$d('fixed_size', 4)]int{} fsa := [$d('fixed_size', 4)]int{}
println(fsa) println(fsa)
s := StructFSAWithDSize{}
println(s.fsa)
} }

View File

@ -1,11 +1,15 @@
// This file should pass if compiled/run with: // This file should pass if compiled/run with:
// vtest vflags: -d my_f64=2.0 -d my_i64=3 -d my_string="a four" -d my_bool -d my_char=g -d my_size=8 // vtest vflags: -d my_f64=2.0 -d my_i64=3 -d my_string="a four" -d my_bool -d my_char=g -d my_size=8 -d field_fsa_size=16
const my_f64 = $d('my_f64', 1.0) const my_f64 = $d('my_f64', 1.0)
const my_i64 = $d('my_i64', 2) const my_i64 = $d('my_i64', 2)
const my_string = $d('my_string', 'three') const my_string = $d('my_string', 'three')
const my_bool = $d('my_bool', false) const my_bool = $d('my_bool', false)
const my_char = $d('my_char', `f`) const my_char = $d('my_char', `f`)
struct StructFSAWithDSize {
fsa [$d('field_fsa_size', 10)]int
}
fn main() { fn main() {
assert my_f64 == 2.0 assert my_f64 == 2.0
assert my_i64 == 3 assert my_i64 == 3
@ -14,6 +18,8 @@ fn main() {
assert my_char == `g` assert my_char == `g`
my_fixed_size_array := [$d('my_size', 4)]int{} my_fixed_size_array := [$d('my_size', 4)]int{}
assert my_fixed_size_array.len == 8 assert my_fixed_size_array.len == 8
s := StructFSAWithDSize{}
assert s.fsa.len == 16
println(my_f64) println(my_f64)
println(my_i64) println(my_i64)
println(my_string) println(my_string)

View File

@ -13,7 +13,7 @@ const maximum_inline_sum_type_variants = 3
fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Type { fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Type {
p.check(expecting) p.check(expecting)
// fixed array // fixed array
if p.tok.kind in [.number, .name] { if p.tok.kind in [.number, .name, .dollar] {
mut fixed_size := 0 mut fixed_size := 0
mut size_expr := p.expr(0) mut size_expr := p.expr(0)
mut size_unresolved := true mut size_unresolved := true
@ -36,6 +36,9 @@ fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Typ
} }
fixed_size = size_expr.compile_value.int() fixed_size = size_expr.compile_value.int()
size_unresolved = false size_unresolved = false
} else {
p.error_with_pos('only \$d() is supported as fixed array size quantifier at compile time',
size_expr.pos)
} }
} }
ast.Ident { ast.Ident {