1
0
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:
Daniel Däschle 2020-04-14 15:12:26 +02:00 committed by GitHub
parent c04c973f84
commit 75603beeea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1332,15 +1332,23 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
if cond_type == 0 {
c.error('match 0 cond type', node.pos)
}
// check for exhaustion when match is expr
if node.is_sum_type && node.is_expr && !node.branches[node.branches.len - 1].is_else {
type_sym := c.table.get_type_symbol(cond_type)
info := type_sym.info as table.SumType
mut used_sum_types_count := 0
if !node.branches[node.branches.len - 1].is_else {
mut used_values_count := 0
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)
}
}