checker: check the sumtype mismatch returned by match expr (#13751)

pull/13752/head
yuyi 2022-03-16 15:31:38 +08:00 committed by GitHub
parent 57cba4d3f0
commit 33167960ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 1 deletions

View File

@ -85,7 +85,10 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
&& !c.check_types(expr_type, ret_type) {
ret_sym := c.table.sym(ret_type)
is_noreturn := is_noreturn_callexpr(stmt.expr)
if !(node.is_expr && ret_sym.kind == .sum_type) && !is_noreturn {
if !(node.is_expr && ret_sym.kind == .sum_type
&& (ret_type.has_flag(.generic)
|| c.table.is_sumtype_or_in_variant(ret_type, expr_type)))
&& !is_noreturn {
c.error('return type mismatch, it should be `$ret_sym.name`',
stmt.expr.pos())
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/match_return_sumtype_mismatch_err.vv:15:11: error: return type mismatch, it should be `Myt`
13 | return match b {
14 | true { St('TRUE') }
15 | false { `F` }
| ~~~
16 | }
17 | }

View File

@ -0,0 +1,19 @@
type St = string
type Ru = rune
type Myt = Ru | St
fn myt_t1(b bool) Myt {
match b {
true { return St('TRUE') }
false { return Ru(`F`) }
}
}
fn myt_t2(b bool) Myt {
return match b {
true { St('TRUE') }
false { `F` }
}
}
fn main() {}