diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 47b0c6a865..6e95f10ae4 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 5ec1b4c70e..f92443f777 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -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' diff --git a/vlib/v/checker/tests/comptime_value_d_only_d_in_fixed_size_array.out b/vlib/v/checker/tests/comptime_value_d_only_d_in_fixed_size_array.out new file mode 100644 index 0000000000..7cf6db5a9d --- /dev/null +++ b/vlib/v/checker/tests/comptime_value_d_only_d_in_fixed_size_array.out @@ -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 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/comptime_value_d_only_d_in_fixed_size_array.vv b/vlib/v/checker/tests/comptime_value_d_only_d_in_fixed_size_array.vv new file mode 100644 index 0000000000..5d2ed005b3 --- /dev/null +++ b/vlib/v/checker/tests/comptime_value_d_only_d_in_fixed_size_array.vv @@ -0,0 +1,3 @@ +struct St { + fsa [$env('size')]int +} diff --git a/vlib/v/checker/tests/run/using_comptime_d_value.run.out b/vlib/v/checker/tests/run/using_comptime_d_value.run.out index a73aabbfb7..634bdcc67a 100644 --- a/vlib/v/checker/tests/run/using_comptime_d_value.run.out +++ b/vlib/v/checker/tests/run/using_comptime_d_value.run.out @@ -2,3 +2,4 @@ false done [0, 0, 0, 0] +[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \ No newline at end of file diff --git a/vlib/v/checker/tests/run/using_comptime_d_value.vv b/vlib/v/checker/tests/run/using_comptime_d_value.vv index c0b8b47e7f..74e1c32981 100644 --- a/vlib/v/checker/tests/run/using_comptime_d_value.vv +++ b/vlib/v/checker/tests/run/using_comptime_d_value.vv @@ -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) } diff --git a/vlib/v/gen/c/testdata/use_flag_comptime_values.vv b/vlib/v/gen/c/testdata/use_flag_comptime_values.vv index 9adfcd321e..322596df9e 100644 --- a/vlib/v/gen/c/testdata/use_flag_comptime_values.vv +++ b/vlib/v/gen/c/testdata/use_flag_comptime_values.vv @@ -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) diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index a635601607..dfe432f089 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -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 {