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

v2: fix type aliases & add check

This commit is contained in:
Joe Conigliaro 2020-03-03 09:19:04 +11:00
parent 8ac0739858
commit 895a1711cb
2 changed files with 10 additions and 8 deletions

View File

@ -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: ''
}

View File

@ -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