compiler: implement direct cast for sum type

This commit is contained in:
joe-conigliaro 2020-01-04 00:42:48 +11:00 committed by Alexander Medvednikov
parent 520ec0f53a
commit 43895269ff
2 changed files with 19 additions and 4 deletions

View file

@ -70,8 +70,17 @@ fn (p mut Parser) bool_expression() string {
if typ == cast_typ {
p.warn('casting `$typ` to `$cast_typ` is not needed')
}
p.cgen.set_placeholder(start_ph, '($cast_typ)(')
p.gen(')')
if typ in p.table.sum_types {
T := p.table.find_type(cast_typ)
if T.parent != typ {
p.error('cannot cast `$typ` to `$cast_typ`. `$cast_typ` is not a variant of `$typ`')
}
p.cgen.set_placeholder(start_ph, '*($cast_typ*)')
p.gen('.obj')
} else {
p.cgen.set_placeholder(start_ph, '($cast_typ)(')
p.gen(')')
}
return cast_typ
}
return typ