From 895a1711cb3144b29408345b8c7474cf794ce030 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Tue, 3 Mar 2020 09:19:04 +1100 Subject: [PATCH] v2: fix type aliases & add check --- vlib/v/parser/parser.v | 13 +++++-------- vlib/v/table/table.v | 5 +++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index f3b19b95dd..2705d5849b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1848,7 +1848,6 @@ fn (p mut Parser) type_decl() ast.TypeDecl { } p.check(.key_type) name := p.check_name() - mut is_sum := false mut sum_variants := []table.Type // type SumType = A | B | c if p.tok.kind == .assign { @@ -1860,13 +1859,7 @@ fn (p mut Parser) type_decl() ast.TypeDecl { break } p.check(.pipe) - is_sum = true } - } - else { - p.parse_type() - } - if is_sum { p.table.register_type_symbol(table.TypeSymbol{ kind: .sum_type name: p.prepend_mod(name) @@ -1874,10 +1867,14 @@ fn (p mut Parser) type_decl() ast.TypeDecl { variants: sum_variants } }) - } else { + } + // type MyType int + else { + parent_type := p.parse_type() p.table.register_type_symbol(table.TypeSymbol{ kind: .alias name: p.prepend_mod(name) + parent_idx: table.type_idx(parent_type) info: table.Alias{ foo: '' } diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 7f0545e05b..9500c1e5bf 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -421,6 +421,11 @@ pub fn (t &Table) check(got, expected Type) bool { if got_type_sym.kind == .array && got_type_sym.name == 'array_void' && exp_type_sym.kind == .array { return true } + // type alias + if (got_type_sym.kind == .alias && got_type_sym.parent_idx == exp_idx) || + (exp_type_sym.kind == .alias && exp_type_sym.parent_idx == got_idx) { + return true + } // sum type if got_type_sym.kind == .sum_type { sum_info := got_type_sym.info as SumType