1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix comptime "ident is type" (#18747)

This commit is contained in:
phoebe 2023-07-03 07:01:34 +02:00 committed by GitHub
parent ad1d5e7adb
commit e01d973c27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -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] {

View File

@ -0,0 +1,6 @@
u64: true
u64 array: 0
other array: 1
other array: 0
other int: false
unknown type

View File

@ -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')
}