From efeb3e04dac461942b6fe27d23c7f0173ff272a6 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 28 Feb 2022 16:38:20 +0800 Subject: [PATCH] ast: add table.is_sumtype_or_in_variant() (#13615) --- vlib/v/ast/table.v | 11 +++++++++++ vlib/v/checker/checker.v | 6 ++---- vlib/v/checker/containers.v | 3 +-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index a2440c4668..8e8107e9d0 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -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} diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2de288ce04..45e3580ab8 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) } diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index 05da835206..0f643b5638 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -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() {