mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: implement direct cast for sum type
This commit is contained in:

committed by
Alexander Medvednikov

parent
520ec0f53a
commit
43895269ff
@@ -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
|
||||
|
Reference in New Issue
Block a user