From 52ddefbdc5b5febe5d75618a37a327d99a5f9149 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 4 Jul 2023 10:28:35 -0300 Subject: [PATCH] checker: fix comptime evaluation is/!is operator with typenode (#18773) --- vlib/v/checker/comptime.v | 1 - vlib/v/checker/if.v | 7 +++++-- vlib/v/tests/comptime_is_check_test.v | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/comptime_is_check_test.v diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index cef1c2370f..92606b79a7 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -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 { diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index def64bc894..50ec92492d 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -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] { @@ -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 { diff --git a/vlib/v/tests/comptime_is_check_test.v b/vlib/v/tests/comptime_is_check_test.v new file mode 100644 index 0000000000..6b8c820ed4 --- /dev/null +++ b/vlib/v/tests/comptime_is_check_test.v @@ -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' +}