diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index a9547347e4..b87b5e1d88 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -53,6 +53,14 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { right_len = node.right_types.len } else if right_type == ast.void_type { right_len = 0 + if mut right is ast.IfExpr { + last_branch := right.branches.last() + last_stmts := last_branch.stmts.filter(it is ast.ExprStmt) + if last_stmts.any((it as ast.ExprStmt).typ.has_flag(.generic)) { + right_len = last_branch.stmts.len + node.right_types = last_stmts.map((it as ast.ExprStmt).typ) + } + } } } if mut right is ast.InfixExpr { diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index ebeee0315f..430eca291b 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -473,6 +473,10 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`', node.pos) } else { + if node.is_expr == false && c.comptime.is_generic_param_var(stmt.expr) { + // generic variable no yet type bounded + node.is_expr = true + } if c.inside_assign && node.is_expr && !node.typ.has_flag(.shared_f) && stmt.typ != ast.voidptr_type { if stmt.typ.is_ptr() != node.typ.is_ptr() { diff --git a/vlib/v/tests/generic_if_ret_test.v b/vlib/v/tests/generic_if_ret_test.v new file mode 100644 index 0000000000..407746889c --- /dev/null +++ b/vlib/v/tests/generic_if_ret_test.v @@ -0,0 +1,8 @@ +fn clamp[T](a T, x T, b T) T { + min := if x < b { x } else { b } + return if min < a { a } else { min } +} + +fn test_main() { + assert dump(clamp(1, 2, 3)) == 2 +}