checker, cgen: fix error for if expr with generic sumtype (#14056)
ci/woodpecker/push/woodpecker Pipeline failed Details

yuyi 2022-04-17 13:19:44 +08:00 committed by Jef Roosens
parent 9f9d24d616
commit 5e98ea12c8
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 25 additions and 1 deletions

View File

@ -204,6 +204,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
}
}
if node.is_expr && c.table.sym(former_expected_type).kind == .sum_type {
node.typ = former_expected_type
continue
}
if is_noreturn_callexpr(last_expr.expr) {

View File

@ -74,7 +74,12 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
g.expr(branch.cond)
g.write(' ? ')
}
prev_expected_cast_type := g.expected_cast_type
if node.is_expr && g.table.sym(node.typ).kind == .sum_type {
g.expected_cast_type = node.typ
}
g.stmts(branch.stmts)
g.expected_cast_type = prev_expected_cast_type
}
if node.branches.len == 1 {
g.write(': 0')
@ -195,11 +200,12 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
}
}
if needs_tmp_var {
prev_expected_cast_type := g.expected_cast_type
if node.is_expr && g.table.sym(node.typ).kind == .sum_type {
g.expected_cast_type = node.typ
}
g.stmts_with_tmp_var(branch.stmts, tmp)
g.expected_cast_type = 0
g.expected_cast_type = prev_expected_cast_type
} else {
// restore if_expr stmt header pos
stmt_pos := g.nth_stmt_pos(0)

View File

@ -0,0 +1,17 @@
type Opt<T> = None<T> | Some<T>
struct None<T> {}
struct Some<T> {
mut:
value T
}
fn operation(r int) Opt<int> {
return if r > 0 { Some<int>{r} } else { None<int>{} }
}
fn test_if_expr_with_generic_sumtype() {
op := operation(1)
assert Opt<int>(Some<int>{1}) == op
}