From c5c7b3a054e5f41d13739f4c87e74e7b9e51e6eb Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 27 Dec 2022 10:14:58 -0300 Subject: [PATCH] checker: fix comptime var param passing with comptime selector (#16777) --- vlib/v/checker/check_types.v | 2 +- vlib/v/tests/comptime_var_param_test.v | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/comptime_var_param_test.v diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 85f75c82bd..8b542ce15d 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -871,7 +871,7 @@ fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr) { func_.name = '' idx := c.table.find_or_register_fn_type(func_, true, false) typ = ast.new_type(idx).derive(arg.typ) - } else if c.inside_comptime_for_field && sym.info is ast.Struct + } else if c.inside_comptime_for_field && sym.kind in [.struct_, .any] && arg.expr is ast.ComptimeSelector { compselector := arg.expr as ast.ComptimeSelector if compselector.field_expr is ast.SelectorExpr { diff --git a/vlib/v/tests/comptime_var_param_test.v b/vlib/v/tests/comptime_var_param_test.v new file mode 100644 index 0000000000..fba0c375bf --- /dev/null +++ b/vlib/v/tests/comptime_var_param_test.v @@ -0,0 +1,26 @@ +struct Aa { + sub AliasType +} + +type AliasType = Bb + +struct Bb { + a int +} + +fn encode_struct[U](val U) string { + $for field in U.fields { + encode_struct(val.$(field.name)) + } + return val.str() +} + +fn test_main() { + aa := Aa{} + + assert encode_struct(aa) == 'Aa{ + sub: AliasType(Bb{ + a: 0 +}) +}' +}