checker/table: rearrange sumtype check fns

pull/5681/head
joe-conigliaro 2020-07-06 00:53:56 +10:00
parent 1505f3e22c
commit 4e66728477
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
4 changed files with 15 additions and 14 deletions

View File

@ -102,7 +102,7 @@ pub fn (c &Checker) check_basic(got, expected table.Type) bool {
return true
}
// sum type
if c.table.check_sumtype_compatibility(got, expected) {
if c.check_sumtype_compatibility(got, expected) {
return true
}
// fn type
@ -346,3 +346,13 @@ pub fn (c &Checker) string_inter_lit(mut node ast.StringInterLiteral) table.Type
}
return table.string_type
}
pub fn (c &Checker) check_sumtype_compatibility(a table.Type, b table.Type) bool {
if c.table.sumtype_has_variant(a, b) {
return true
}
if c.table.sumtype_has_variant(b, a) {
return true
}
return false
}

View File

@ -2462,7 +2462,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) table.Type {
typ_sym := c.table.get_type_symbol(typ)
if node.is_sum_type || node.is_interface {
ok := if cond_type_sym.kind == .sum_type {
c.table.check_sumtype_has_variant(cond_type, typ)
c.table.sumtype_has_variant(cond_type, typ)
} else {
// interface match
c.type_implements(typ, cond_type, node.pos)

View File

@ -949,7 +949,7 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type, expected_type table.Type) {
// cast to sum type
if expected_type != table.void_type {
if g.table.check_sumtype_has_variant(expected_type, got_type) {
if g.table.sumtype_has_variant(expected_type, got_type) {
got_sym := g.table.get_type_symbol(got_type)
got_styp := g.typ(got_type)
exp_styp := g.typ(expected_type)

View File

@ -495,7 +495,7 @@ pub fn (table &Table) register_fn_gen_type(fn_name string, typ Type) {
// TODO: there is a bug when casting sumtype the other way if its pointer
// so until fixed at least show v (not C) error `x(variant) = y(SumType*)`
pub fn (table &Table) check_sumtype_has_variant(parent Type, variant Type) bool {
pub fn (table &Table) sumtype_has_variant(parent Type, variant Type) bool {
parent_sym := table.get_type_symbol(parent)
if parent_sym.kind ==.sum_type {
parent_info := parent_sym.info as SumType
@ -503,7 +503,7 @@ pub fn (table &Table) check_sumtype_has_variant(parent Type, variant Type) bool
if v.idx() == variant.idx() {
return true
}
if table.check_sumtype_has_variant(v, variant) {
if table.sumtype_has_variant(v, variant) {
return true
}
}
@ -511,12 +511,3 @@ pub fn (table &Table) check_sumtype_has_variant(parent Type, variant Type) bool
return false
}
pub fn (table &Table) check_sumtype_compatibility(a Type, b Type) bool {
if table.check_sumtype_has_variant(a, b) {
return true
}
if table.check_sumtype_has_variant(b, a) {
return true
}
return false
}