Skip to content

Commit

Permalink
checker: fix comptime evaluation is/!is operator with typenode (#18773)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jul 4, 2023
1 parent 884fbb0 commit 52ddefb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 0 additions & 1 deletion vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
sym := c.table.sym(cond.right.typ)
if sym.kind != .interface_ {
c.expr(cond.left)
// c.error('`$sym.name` is not an interface', cond.right.pos())
}
return .unknown
} else if cond.left is ast.TypeNode && cond.right is ast.ComptimeType {
Expand Down
7 changes: 5 additions & 2 deletions vlib/v/checker/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
is_comptime_type_is_expr = true
}
} else if mut branch.cond is ast.InfixExpr {
if branch.cond.op == .key_is {
if branch.cond.op in [.not_is, .key_is] {
left := branch.cond.left
right := branch.cond.right
if right !in [ast.TypeNode, ast.ComptimeType] {
Expand Down Expand Up @@ -170,7 +170,10 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
skip_state = c.check_compatible_types(checked_type, right as ast.TypeNode)
}
}
} else if branch.cond.op in [.eq, .ne] {
if branch.cond.op == .not_is && skip_state != .unknown {
skip_state = if skip_state == .eval { .skip } else { .eval }
}
} else if branch.cond.op in [.eq, .ne, .gt, .lt, .ge, .le] {
left := branch.cond.left
right := branch.cond.right
if left is ast.SelectorExpr && right is ast.IntegerLiteral {
Expand Down
15 changes: 15 additions & 0 deletions vlib/v/tests/comptime_is_check_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn test[T](val T) string {
$if T is u32 {
$compile_error('u32')
return ''
} $else $if T !is string {
$compile_error('not string')
return ''
} $else {
return val
}
}

fn test_main() {
assert test('str') == 'str'
}

0 comments on commit 52ddefb

Please sign in to comment.