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

checker: fix comptime evaluation is/!is operator with typenode (#18773)

This commit is contained in:
Felipe Pena 2023-07-04 10:28:35 -03:00 committed by GitHub
parent 884fbb0a98
commit 52ddefbdc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 3 deletions

View File

@ -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) sym := c.table.sym(cond.right.typ)
if sym.kind != .interface_ { if sym.kind != .interface_ {
c.expr(cond.left) c.expr(cond.left)
// c.error('`$sym.name` is not an interface', cond.right.pos())
} }
return .unknown return .unknown
} else if cond.left is ast.TypeNode && cond.right is ast.ComptimeType { } else if cond.left is ast.TypeNode && cond.right is ast.ComptimeType {

View File

@ -100,7 +100,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
is_comptime_type_is_expr = true is_comptime_type_is_expr = true
} }
} else if mut branch.cond is ast.InfixExpr { } 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 left := branch.cond.left
right := branch.cond.right right := branch.cond.right
if right !in [ast.TypeNode, ast.ComptimeType] { 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) 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 left := branch.cond.left
right := branch.cond.right right := branch.cond.right
if left is ast.SelectorExpr && right is ast.IntegerLiteral { if left is ast.SelectorExpr && right is ast.IntegerLiteral {

View File

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