Skip to content

Commit

Permalink
checker: fix comptime "ident is type" (#18747)
Browse files Browse the repository at this point in the history
  • Loading branch information
phoreverpheebs committed Jul 3, 2023
1 parent ad1d5e7 commit e01d973
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vlib/v/checker/if.v
Expand Up @@ -161,6 +161,13 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
is_comptime_type_is_expr = true
left_type := c.unwrap_generic(left.typ)
skip_state = c.check_compatible_types(left_type, right as ast.TypeNode)
} else if left is ast.Ident {
is_comptime_type_is_expr = true
mut checked_type := ast.void_type
if var := left.scope.find_var(left.name) {
checked_type = c.unwrap_generic(var.typ)
}
skip_state = c.check_compatible_types(checked_type, right as ast.TypeNode)
}
}
} else if branch.cond.op in [.eq, .ne] {
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/run/comptime_ident_is_type.run.out
@@ -0,0 +1,6 @@
u64: true
u64 array: 0
other array: 1
other array: 0
other int: false
unknown type
22 changes: 22 additions & 0 deletions vlib/v/checker/tests/run/comptime_ident_is_type.vv
@@ -0,0 +1,22 @@
fn test[T](val T) {
$if val is u64 {
println('u64: ${val == u64(0)}')
} $else $if val is []u64 {
println('u64 array: ${val.len}')
} $else $if val is $int {
println('other int: ${val == 0}')
} $else $if val is $array {
println('other array: ${val.len}')
} $else {
println('unknown type')
}
}

fn main() {
test(u64(0))
test([]u64{})
test(['string'])
test([]u32{})
test(int(12))
test('string')
}

0 comments on commit e01d973

Please sign in to comment.