From 74686d0ec49f991f006a957b12dc23e81023fd3a Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 19 May 2020 20:04:51 +0800 Subject: [PATCH] test: fix alias type errors and type_test.v --- cmd/tools/vtest-fixed.v | 1 - vlib/v/checker/checker.v | 15 ++++++++-- vlib/v/gen/cgen.v | 2 +- vlib/v/parser/parser.v | 2 +- vlib/v/table/atypes.v | 4 +-- .../{type_test.v => sumtype_calls_test.v} | 30 ------------------- .../type_alias_str_method_override_test.v | 29 ++++++++++++++++++ 7 files changed, 46 insertions(+), 37 deletions(-) rename vlib/v/tests/{type_test.v => sumtype_calls_test.v} (70%) create mode 100644 vlib/v/tests/type_alias_str_method_override_test.v diff --git a/cmd/tools/vtest-fixed.v b/cmd/tools/vtest-fixed.v index f974bde42c..9723327220 100644 --- a/cmd/tools/vtest-fixed.v +++ b/cmd/tools/vtest-fixed.v @@ -9,7 +9,6 @@ const ( 'vlib/v/tests/enum_bitfield_test.v', 'vlib/v/tests/num_lit_call_method_test.v', 'vlib/v/tests/pointers_test.v', - 'vlib/v/tests/type_test.v', 'vlib/v/tests/pointers_str_test.v', 'vlib/arrays/arrays_test.v', 'vlib/net/http/http_httpbin_test.v', diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2772414cd9..68d2b1d7fd 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -308,8 +308,19 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { c.error('unknown struct: $type_sym.name', struct_init.pos) } // string & array are also structs but .kind of string/array - .struct_, .string, .array { - info := type_sym.info as table.Struct + .struct_, .string, .array, .alias { + mut info := table.Struct{} + if type_sym.kind == .alias { + info_t := type_sym.info as table.Alias + sym := c.table.get_type_symbol(info_t.parent_typ) + if sym.kind != .struct_ { + c.error('alias type name: $sym.name is not struct type', struct_init.pos) + } + info = sym.info as table.Struct + } else { + info = type_sym.info as table.Struct + } + if struct_init.is_short && struct_init.fields.len > info.fields.len { c.error('too many fields', struct_init.pos) } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 535045d89f..db13f1733e 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -318,7 +318,7 @@ fn (g &Gen) base_type(t table.Type) string { nr_muls := t.nr_muls() if nr_muls > 0 { styp += strings.repeat(`*`, nr_muls) - } + } return styp } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 8fc9155ff5..a1c1afadad 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1412,7 +1412,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl { parent_idx: pid mod: p.mod info: table.Alias{ - foo: '' + parent_typ: parent_type is_c: parent_name.len > 2 && parent_name[0] == `C` && parent_name[1] == `.` } is_public: is_pub diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index fbbfe05bf9..4a57d063d2 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -574,8 +574,8 @@ pub: pub struct Alias { pub: - foo string - is_c bool + parent_typ Type + is_c bool } // NB: FExpr here is a actually an ast.Expr . diff --git a/vlib/v/tests/type_test.v b/vlib/v/tests/sumtype_calls_test.v similarity index 70% rename from vlib/v/tests/type_test.v rename to vlib/v/tests/sumtype_calls_test.v index 3e8f609024..8a68736511 100644 --- a/vlib/v/tests/type_test.v +++ b/vlib/v/tests/sumtype_calls_test.v @@ -1,33 +1,3 @@ -struct Human { - name string -} - -fn (h Human) str() string { - return 'Human: $h.name' -} - -type Person Human - -fn test_type_print() { - p := Person{ - name: 'Bilbo' - } - println(p) - assert p.str() == 'Human: Bilbo' -} - -fn (h Person) str() string { - return 'Person: $h.name' -} - -fn test_person_str() { - p := Person{ - name: 'Bilbo' - } - println(p) - assert p.str() == 'Person: Bilbo' -} - struct Foo { } diff --git a/vlib/v/tests/type_alias_str_method_override_test.v b/vlib/v/tests/type_alias_str_method_override_test.v new file mode 100644 index 0000000000..da248801d9 --- /dev/null +++ b/vlib/v/tests/type_alias_str_method_override_test.v @@ -0,0 +1,29 @@ +struct Human { + name string +} + +fn (h Human) str() string { + return 'Human: $h.name' +} + +type Person Human + +fn (h Person) str() string { + return 'Person: $h.name' +} + +fn test_type_print() { + p := Human{ + name: 'Bilbo' + } + println(p) + assert p.str() == 'Human: Bilbo' +} + +fn test_person_str() { + p := Person{ + name: 'Bilbo' + } + println(p) + assert p.str() == 'Person: Bilbo' +}