Skip to content

Commit b2538e8

Browse files
authored
checker: fix generic fn using generic type in if expr (#13027)
1 parent b94c5c2 commit b2538e8

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

vlib/v/checker/comptime.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Position) bool {
547547
return false
548548
}
549549
// `$if some_var {}`, or `[if user_defined_tag] fn abc(){}`
550-
typ := c.expr(cond)
550+
typ := c.unwrap_generic(c.expr(cond))
551551
if cond.obj !is ast.Var && cond.obj !is ast.ConstField
552552
&& cond.obj !is ast.GlobalField {
553553
if !c.inside_ct_attr {

vlib/v/checker/if.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
3939
} else {
4040
// check condition type is boolean
4141
c.expected_type = ast.bool_type
42-
cond_typ := c.expr(branch.cond)
42+
cond_typ := c.unwrap_generic(c.expr(branch.cond))
4343
if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.optional))
4444
&& !c.pref.translated {
4545
c.error('non-bool type `${c.table.type_to_str(cond_typ)}` used as if condition',
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn test_generic_fn_using_generic_type_in_if() {
2+
ret := generic_bool(true)
3+
assert ret == 'true'
4+
}
5+
6+
fn generic_bool<T>(val T) string {
7+
$if T is bool {
8+
if val {
9+
println('is true')
10+
return 'true'
11+
} else {
12+
println('is false')
13+
return 'false'
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)