diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index b5d85e0e03..4ef9b675f5 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -303,6 +303,13 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type { c.error('unknown struct: $type_sym.name', node.pos) return ast.void_type } + .any { + // `T{ foo: 22 }` + for mut field in node.fields { + field.typ = c.expr(field.expr) + field.expected_type = field.typ + } + } // string & array are also structs but .kind of string/array .struct_, .string, .array, .alias { mut info := ast.Struct{} diff --git a/vlib/v/tests/generics_struct_init_in_generic_fn_test.v b/vlib/v/tests/generics_struct_init_in_generic_fn_test.v new file mode 100644 index 0000000000..8e719d2a56 --- /dev/null +++ b/vlib/v/tests/generics_struct_init_in_generic_fn_test.v @@ -0,0 +1,25 @@ +module main + +pub struct Person { +pub mut: + id int +} + +fn test_generics_struct_init_in_generic_fn() { + leo := Person{ + id: 1 + } + println(leo) + assert leo.id == 1 + + ret := do(leo) + println(ret) + assert ret.id == 2 +} + +pub fn do(t T) T { + max := T{ + id: 2 + } + return max +}