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

parser, checker: fix generic struct init with field struct init (#18052)

This commit is contained in:
yuyi 2023-04-27 03:22:15 +08:00 committed by GitHub
parent 13b4cd9d58
commit f4b7f83121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -324,7 +324,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
continue
}
gtyp_name := c.table.sym(gtyp).name
if gtyp_name !in c.table.cur_fn.generic_names {
if gtyp_name.len == 1 && gtyp_name !in c.table.cur_fn.generic_names {
cur_generic_names := '(' + c.table.cur_fn.generic_names.join(',') + ')'
c.error('generic struct init type parameter `${gtyp_name}` must be within the parameters `${cur_generic_names}` of the current generic function',
node.pos)

View File

@ -398,6 +398,7 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
first_pos := (if kind == .short_syntax && p.prev_tok.kind == .lcbr { p.prev_tok } else { p.tok }).pos()
p.struct_init_generic_types = []ast.Type{}
mut typ := if kind == .short_syntax { ast.void_type } else { p.parse_type() }
struct_init_generic_types := p.struct_init_generic_types.clone()
if is_option {
typ = typ.set_flag(.option)
}
@ -492,7 +493,7 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
is_short_syntax: kind == .short_syntax
is_anon: kind == .anon
pre_comments: pre_comments
generic_types: p.struct_init_generic_types
generic_types: struct_init_generic_types
}
}

View File

@ -0,0 +1,18 @@
struct Child {
}
struct Test[T] {
child Child
}
fn new[T]() Test[T] {
return Test[T]{
child: Child{}
}
}
fn test_generic_struct_init_with_field_struct_init() {
t := new[int]()
println(t)
assert t.child == Child{}
}