ast: add table.is_sumtype_or_in_variant() (#13615)

pull/13617/head
yuyi 2022-02-28 16:38:20 +08:00 committed by GitHub
parent 81c787ef91
commit efeb3e04da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View File

@ -1284,6 +1284,17 @@ pub fn (t &Table) sumtype_has_variant(parent Type, variant Type, is_as bool) boo
return false return false
} }
pub fn (t &Table) is_sumtype_or_in_variant(parent Type, typ Type) bool {
if typ == 0 {
return false
}
if t.type_kind(typ) == .sum_type && parent.idx() == typ.idx()
&& parent.nr_muls() == typ.nr_muls() {
return true
}
return t.sumtype_has_variant(parent, typ, false)
}
// only used for debugging V compiler type bugs // only used for debugging V compiler type bugs
pub fn (t &Table) known_type_names() []string { pub fn (t &Table) known_type_names() []string {
mut res := []string{cap: t.type_idxs.len} mut res := []string{cap: t.type_idxs.len}

View File

@ -847,15 +847,13 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
return ast.void_type return ast.void_type
} else if left_value_sym.kind == .sum_type { } else if left_value_sym.kind == .sum_type {
if right_final.kind != .array { if right_final.kind != .array {
if left_value_type.idx() != right_type.idx() if !c.table.is_sumtype_or_in_variant(left_value_type, right_type) {
&& !c.table.sumtype_has_variant(left_value_type, right_type, false) {
c.error('cannot append `$right_sym.name` to `$left_sym.name`', c.error('cannot append `$right_sym.name` to `$left_sym.name`',
right_pos) right_pos)
} }
} else { } else {
right_value_type := c.table.value_type(right_type) right_value_type := c.table.value_type(right_type)
if left_value_type.idx() != right_value_type.idx() if !c.table.is_sumtype_or_in_variant(left_value_type, right_value_type) {
&& !c.table.sumtype_has_variant(left_value_type, right_value_type, false) {
c.error('cannot append `$right_sym.name` to `$left_sym.name`', c.error('cannot append `$right_sym.name` to `$left_sym.name`',
right_pos) right_pos)
} }

View File

@ -110,8 +110,7 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
continue continue
} else if expecting_sumtype_array { } else if expecting_sumtype_array {
if i == 0 { if i == 0 {
if expected_value_type.idx() == typ.idx() if c.table.is_sumtype_or_in_variant(expected_value_type, typ) {
|| c.table.sumtype_has_variant(expected_value_type, typ, false) {
elem_type = expected_value_type elem_type = expected_value_type
} else { } else {
if expr.is_auto_deref_var() { if expr.is_auto_deref_var() {