mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix generic struct no_key init (#17059)
This commit is contained in:
parent
1c6195c1b6
commit
630fb2af37
@ -86,7 +86,14 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
|
|||||||
if !field.typ.has_flag(.shared_f) {
|
if !field.typ.has_flag(.shared_f) {
|
||||||
g.is_shared = false
|
g.is_shared = false
|
||||||
}
|
}
|
||||||
inited_fields[field.name] = i
|
mut field_name := field.name
|
||||||
|
if node.no_keys && sym.kind == .struct_ {
|
||||||
|
info := sym.info as ast.Struct
|
||||||
|
if info.fields.len == node.fields.len {
|
||||||
|
field_name = info.fields[i].name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inited_fields[field_name] = i
|
||||||
if sym.kind != .struct_ {
|
if sym.kind != .struct_ {
|
||||||
if field.typ == 0 {
|
if field.typ == 0 {
|
||||||
g.checker_bug('struct init, field.typ is 0', field.pos)
|
g.checker_bug('struct init, field.typ is 0', field.pos)
|
||||||
@ -205,6 +212,12 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
|
|||||||
sfield.expected_type = tt
|
sfield.expected_type = tt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if node.no_keys && sym.kind == .struct_ {
|
||||||
|
sym_info := sym.info as ast.Struct
|
||||||
|
if sym_info.fields.len == node.fields.len {
|
||||||
|
sfield.name = sym_info.fields[already_initalised_node_field_index].name
|
||||||
|
}
|
||||||
|
}
|
||||||
g.struct_init_field(sfield, sym.language)
|
g.struct_init_field(sfield, sym.language)
|
||||||
if is_multiline {
|
if is_multiline {
|
||||||
g.writeln(',')
|
g.writeln(',')
|
||||||
|
12
vlib/v/tests/generics_struct_no_key_init_test.v
Normal file
12
vlib/v/tests/generics_struct_no_key_init_test.v
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
struct Am {
|
||||||
|
inner int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert[T](num int) T {
|
||||||
|
return T{num}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_generic_struct_no_key_init() {
|
||||||
|
println(convert[Am](3).inner)
|
||||||
|
assert convert[Am](3).inner == 3
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user