diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 0b7700f6e8c89b..0809504f94c511 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1282,6 +1282,14 @@ fn (mut g Gen) resolve_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concre if param_typ.nr_muls() > 0 && comptime_args[k].nr_muls() > 0 { comptime_args[k] = comptime_args[k].set_nr_muls(0) } + } else if mut call_arg.expr.right is ast.Ident { + mut ctyp := g.comptime.get_comptime_var_type(call_arg.expr.right) + if ctyp != ast.void_type { + comptime_args[k] = ctyp + if param_typ.nr_muls() > 0 && comptime_args[k].nr_muls() > 0 { + comptime_args[k] = comptime_args[k].set_nr_muls(0) + } + } } } else if mut call_arg.expr is ast.ComptimeSelector { comptime_args[k] = g.comptime.comptime_for_field_type diff --git a/vlib/v/tests/comptime_ref_arg_test.v b/vlib/v/tests/comptime_ref_arg_test.v new file mode 100644 index 00000000000000..d6602c4732cb34 --- /dev/null +++ b/vlib/v/tests/comptime_ref_arg_test.v @@ -0,0 +1,101 @@ +// Declare structures covering a subset of a HAR file + +struct HarContent { + size i64 + mime_type string @[json: 'mimeType'] +} + +struct HarResponse { + content HarContent +} + +struct HarEntry { + response HarResponse +} + +pub struct HarLog { + entries []HarEntry +} + +struct Har { + log HarLog +} + +// Declare function printing object contents using generics + +fn show[T](val T) { + $if T is string { + show_string(val) + } $else $if T is $array { + show_array(val) + } $else $if T is $struct { + show_struct(&val) + } $else { + print('primitive: ${val.str()}') + } +} + +fn show_array[T](array []T) { + println('array []${T.name}') + for i, item in array { + println('item ${i}') + show(item) + println('') + } +} + +fn show_struct[T](object &T) { + println('struct ${T.name}') + $for field in T.fields { + mut json_name := field.name + for attr in field.attrs { + if attr.starts_with('json: ') { + json_name = attr[6..] + } + } + + print('key: ') + show_string(json_name) + println('') + + println('value ${T.name}.${field.name} (json: ${json_name}), field.typ: ${field.typ}') + $if field.typ is string { + print('string: ') + show_string(object.$(field.name)) + } $else $if field.is_array { + show_array(object.$(field.name)) + } $else $if field.is_struct { + item := object.$(field.name) + show_struct(&item) + } $else { + print('primitive: ') + print(object.$(field.name).str()) + } + println('') + } +} + +fn show_string(s string) { + print(`"`) + print(s) + print(`"`) +} + +fn test_main() { + har := Har{ + log: HarLog{ + entries: [ + HarEntry{ + response: HarResponse{ + content: HarContent{ + size: 48752 + mime_type: 'text/html' + } + } + }, + ] + } + } + show(har) + assert true +}