diff --git a/cmd/tools/vtest-fmt.v b/cmd/tools/vtest-fmt.v index 79cf68b3c0..55e3ec39ba 100644 --- a/cmd/tools/vtest-fmt.v +++ b/cmd/tools/vtest-fmt.v @@ -7,7 +7,6 @@ import v.util // os.v - // embeded comments, mib := [1/* CTL_KERN */, 14/* KERN_PROC */, 12/* KERN_PROC_PATHNAME */, -1] => comment the rest of the line const ( known_failing_exceptions = [ - 'vlib/v/tests/generics_test.v', // struct Repo { => struct Repo { 'vlib/crypto/aes/aes.v', // pub fn (c &AesCipher) encrypt(mut dst, mut src []byte) { 'vlib/crypto/aes/block_generic.v', // fn expand_key_generic(key []byte, mut enc, mut dec []u32) { 'vlib/crypto/aes/const.v', // multiple narrow columns of []string turned to 1 long single column, otherwise works diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 1ef726efff..e2fe9bac47 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -170,6 +170,7 @@ pub struct StructDecl { pub: pos token.Position name string + gen_types []table.Type is_pub bool mut_pos int // mut: pub_pos int // pub: diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 90789972ec..d03d88e5bb 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -599,7 +599,14 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { } f.write_language_prefix(node.language) name := node.name.after('.') - f.writeln('$name {') + f.write(name) + if node.gen_types.len > 0 { + f.write(' <') + gtypes := node.gen_types.map(f.table.type_to_str(it)).join(', ') + f.write(gtypes) + f.write('>') + } + f.writeln(' {') mut max := 0 mut max_type := 0 mut field_types := []string{cap: node.fields.len} diff --git a/vlib/v/fmt/tests/generic_structs_keep.vv b/vlib/v/fmt/tests/generic_structs_keep.vv new file mode 100644 index 0000000000..acb5a1a591 --- /dev/null +++ b/vlib/v/fmt/tests/generic_structs_keep.vv @@ -0,0 +1,28 @@ +struct Foo { +pub: + data T +} + +fn (f Foo) value() string { + return f.data.str() +} + +type DB = string + +struct Repo { + db DB +pub mut: + model T + permission U +} + +fn main() { + foo_int := Foo{2} + assert foo_int.value() == '2' + println(foo_int) + // + x := Repo{'abc', 3, 1.5} + println(x.db) + println(x.model) + println(x.permission) +} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 580f7507b6..29e343452e 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -287,6 +287,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { is_union: is_union attrs: attrs end_comments: end_comments + gen_types: generic_types } }