1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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
}
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
pub fn (t &Table) known_type_names() []string {
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
} else if left_value_sym.kind == .sum_type {
if right_final.kind != .array {
if left_value_type.idx() != right_type.idx()
&& !c.table.sumtype_has_variant(left_value_type, right_type, false) {
if !c.table.is_sumtype_or_in_variant(left_value_type, right_type) {
c.error('cannot append `$right_sym.name` to `$left_sym.name`',
right_pos)
}
} else {
right_value_type := c.table.value_type(right_type)
if left_value_type.idx() != right_value_type.idx()
&& !c.table.sumtype_has_variant(left_value_type, right_value_type, false) {
if !c.table.is_sumtype_or_in_variant(left_value_type, right_value_type) {
c.error('cannot append `$right_sym.name` to `$left_sym.name`',
right_pos)
}

View File

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