From c9345be6deee5b2944152135d33d999e000fa93d Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 22 Mar 2023 04:50:58 -0300 Subject: [PATCH] ast: fix embed name with enum as generic struct type (fix #17721) (#17727) --- vlib/v/ast/types.v | 14 +++++++------- vlib/v/tests/embed_struct_name_test.v | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 vlib/v/tests/embed_struct_name_test.v diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 1fdcb967e7..644ddb55ab 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -1492,14 +1492,14 @@ pub fn (t &TypeSymbol) symbol_name_except_generic() string { } pub fn (t &TypeSymbol) embed_name() string { - // main.Abc[int] => Abc[int] - mut embed_name := t.name.split('.').last() - // remove generic part from name - // Abc[int] => Abc - if embed_name.contains('[') { - embed_name = embed_name.split('[')[0] + if t.name.contains('[') { + // Abc[int] => Abc + // main.Abc[main.Enum] => Abc + return t.name.split('[')[0].split('.').last() + } else { + // main.Abc => Abc + return t.name.split('.').last() } - return embed_name } pub fn (t &TypeSymbol) has_method(name string) bool { diff --git a/vlib/v/tests/embed_struct_name_test.v b/vlib/v/tests/embed_struct_name_test.v new file mode 100644 index 0000000000..4d3c3e5793 --- /dev/null +++ b/vlib/v/tests/embed_struct_name_test.v @@ -0,0 +1,27 @@ +struct MyTemplate[T] { + data T +} + +enum MyEnum as u8 { + client + server +} + +struct Embed { + a int +} + +struct MyStructure { + Embed + MyTemplate[MyEnum] +} + +fn test_embed_name_with_enum() { + t := MyStructure{ + a: 10 + data: .server + } + dump(t) + assert t.a == 10 + assert t.data == .server +}