v2: fix type aliases & add check

pull/3915/head
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) p.check(.key_type)
name := p.check_name() name := p.check_name()
mut is_sum := false
mut sum_variants := []table.Type mut sum_variants := []table.Type
// type SumType = A | B | c // type SumType = A | B | c
if p.tok.kind == .assign { if p.tok.kind == .assign {
@ -1860,13 +1859,7 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
break break
} }
p.check(.pipe) p.check(.pipe)
is_sum = true
} }
}
else {
p.parse_type()
}
if is_sum {
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol(table.TypeSymbol{
kind: .sum_type kind: .sum_type
name: p.prepend_mod(name) name: p.prepend_mod(name)
@ -1874,10 +1867,14 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
variants: sum_variants variants: sum_variants
} }
}) })
} else { }
// type MyType int
else {
parent_type := p.parse_type()
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol(table.TypeSymbol{
kind: .alias kind: .alias
name: p.prepend_mod(name) name: p.prepend_mod(name)
parent_idx: table.type_idx(parent_type)
info: table.Alias{ info: table.Alias{
foo: '' 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 { if got_type_sym.kind == .array && got_type_sym.name == 'array_void' && exp_type_sym.kind == .array {
return true 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 // sum type
if got_type_sym.kind == .sum_type { if got_type_sym.kind == .sum_type {
sum_info := got_type_sym.info as SumType sum_info := got_type_sym.info as SumType