1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix generics struct init in generic fn (fix #15080) (#15088)

This commit is contained in:
yuyi 2022-07-15 22:30:56 +08:00 committed by GitHub
parent 560afac5d5
commit ac7e809464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -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) c.error('unknown struct: $type_sym.name', node.pos)
return ast.void_type 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 // string & array are also structs but .kind of string/array
.struct_, .string, .array, .alias { .struct_, .string, .array, .alias {
mut info := ast.Struct{} mut info := ast.Struct{}

View File

@ -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) T {
max := T{
id: 2
}
return max
}