mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: match exhaustive as stmt and with enum
This commit is contained in:
parent
c04c973f84
commit
75603beeea
@ -1332,15 +1332,23 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
|
|||||||
if cond_type == 0 {
|
if cond_type == 0 {
|
||||||
c.error('match 0 cond type', node.pos)
|
c.error('match 0 cond type', node.pos)
|
||||||
}
|
}
|
||||||
// check for exhaustion when match is expr
|
if !node.branches[node.branches.len - 1].is_else {
|
||||||
if node.is_sum_type && node.is_expr && !node.branches[node.branches.len - 1].is_else {
|
mut used_values_count := 0
|
||||||
type_sym := c.table.get_type_symbol(cond_type)
|
|
||||||
info := type_sym.info as table.SumType
|
|
||||||
mut used_sum_types_count := 0
|
|
||||||
for branch in node.branches {
|
for branch in node.branches {
|
||||||
used_sum_types_count += branch.exprs.len
|
used_values_count += branch.exprs.len
|
||||||
}
|
}
|
||||||
if used_sum_types_count < info.variants.len {
|
type_sym := c.table.get_type_symbol(cond_type)
|
||||||
|
mut err := false
|
||||||
|
match type_sym.info {
|
||||||
|
table.SumType {
|
||||||
|
err = used_values_count < it.variants.len
|
||||||
|
}
|
||||||
|
table.Enum {
|
||||||
|
err = used_values_count < it.vals.len
|
||||||
|
}
|
||||||
|
else { err = false }
|
||||||
|
}
|
||||||
|
if err {
|
||||||
c.error('match must be exhaustive', node.pos)
|
c.error('match must be exhaustive', node.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user