mirror of
https://github.com/vlang/v.git
synced 2025-09-13 01:16:02 -04:00
ast, parser: support $d()
in fixed size array struct
fields (#21731)
This commit is contained in:
parent
98a1ee2284
commit
a36c0d8315
@ -1967,6 +1967,19 @@ pub fn (mut cc ComptimeCall) resolve_compile_value(compile_values map[string]str
|
||||
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:
|
||||
pos token.Pos
|
||||
|
@ -662,7 +662,7 @@ pub fn (x Expr) str() string {
|
||||
return 'ast.ChanInit'
|
||||
}
|
||||
ComptimeCall {
|
||||
return 'ast.ComptimeCall'
|
||||
return x.expr_str()
|
||||
}
|
||||
EmptyExpr {
|
||||
return 'ast.EmptyExpr'
|
||||
|
@ -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 | }
|
@ -0,0 +1,3 @@
|
||||
struct St {
|
||||
fsa [$env('size')]int
|
||||
}
|
@ -2,3 +2,4 @@
|
||||
false
|
||||
done
|
||||
[0, 0, 0, 0]
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
@ -3,6 +3,10 @@
|
||||
|
||||
const my_f64 = $d('my_f64', 42.0)
|
||||
|
||||
struct StructFSAWithDSize {
|
||||
fsa [$d('field_fsa_size', 10)]int
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println(my_f64)
|
||||
cv_bool := $d('my_bool', false)
|
||||
@ -10,4 +14,6 @@ fn main() {
|
||||
println('done')
|
||||
fsa := [$d('fixed_size', 4)]int{}
|
||||
println(fsa)
|
||||
s := StructFSAWithDSize{}
|
||||
println(s.fsa)
|
||||
}
|
||||
|
@ -1,11 +1,15 @@
|
||||
// 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_i64 = $d('my_i64', 2)
|
||||
const my_string = $d('my_string', 'three')
|
||||
const my_bool = $d('my_bool', false)
|
||||
const my_char = $d('my_char', `f`)
|
||||
|
||||
struct StructFSAWithDSize {
|
||||
fsa [$d('field_fsa_size', 10)]int
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert my_f64 == 2.0
|
||||
assert my_i64 == 3
|
||||
@ -14,6 +18,8 @@ fn main() {
|
||||
assert my_char == `g`
|
||||
my_fixed_size_array := [$d('my_size', 4)]int{}
|
||||
assert my_fixed_size_array.len == 8
|
||||
s := StructFSAWithDSize{}
|
||||
assert s.fsa.len == 16
|
||||
println(my_f64)
|
||||
println(my_i64)
|
||||
println(my_string)
|
||||
|
@ -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 {
|
||||
p.check(expecting)
|
||||
// fixed array
|
||||
if p.tok.kind in [.number, .name] {
|
||||
if p.tok.kind in [.number, .name, .dollar] {
|
||||
mut fixed_size := 0
|
||||
mut size_expr := p.expr(0)
|
||||
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()
|
||||
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user