From 2fb561ba7f0910c15b5685198e08929b63bcc344 Mon Sep 17 00:00:00 2001 From: phoebe Date: Tue, 4 Jul 2023 05:45:30 +0200 Subject: [PATCH] checker, cgen: allow comptime ident is array of types (#18765) --- vlib/v/checker/comptime.v | 6 +++++- .../run/comptime_ident_in_typearray.run.out | 4 ++++ .../tests/run/comptime_ident_in_typearray.vv | 16 ++++++++++++++++ vlib/v/gen/c/comptime.v | 11 ++++------- 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 vlib/v/checker/tests/run/comptime_ident_in_typearray.run.out create mode 100644 vlib/v/checker/tests/run/comptime_ident_in_typearray.vv diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index e0e1fcc39..cef1c2370 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -743,7 +743,11 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran } } .key_in, .not_in { - if cond.left in [ast.SelectorExpr, ast.TypeNode] && cond.right is ast.ArrayInit { + if cond.left is ast.Ident { + c.expr(cond.left) + } + if cond.left in [ast.TypeNode, ast.SelectorExpr, ast.Ident] + && cond.right is ast.ArrayInit { for expr in cond.right.exprs { if expr !in [ast.ComptimeType, ast.TypeNode] { c.error('invalid `\$if` condition, only types are allowed', diff --git a/vlib/v/checker/tests/run/comptime_ident_in_typearray.run.out b/vlib/v/checker/tests/run/comptime_ident_in_typearray.run.out new file mode 100644 index 000000000..94d7df4a5 --- /dev/null +++ b/vlib/v/checker/tests/run/comptime_ident_in_typearray.run.out @@ -0,0 +1,4 @@ +str +this is an else block +32-bit number +32-bit number \ No newline at end of file diff --git a/vlib/v/checker/tests/run/comptime_ident_in_typearray.vv b/vlib/v/checker/tests/run/comptime_ident_in_typearray.vv new file mode 100644 index 000000000..e4b687742 --- /dev/null +++ b/vlib/v/checker/tests/run/comptime_ident_in_typearray.vv @@ -0,0 +1,16 @@ +fn test[T](val T) string { + $if val is string { + return val + } $else $if val in [i32, u32] { + return '32-bit number' + } $else { + return 'this is an else block' + } +} + +fn main() { + println(test('str')) + println(test(7)) + println(test(u32(7))) + println(test(i32(7))) +} diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 15631793d..87d7aed18 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -617,7 +617,8 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) { } } .key_in, .not_in { - if cond.left in [ast.TypeNode, ast.SelectorExpr] && cond.right is ast.ArrayInit { + if cond.left in [ast.TypeNode, ast.SelectorExpr, ast.Ident] + && cond.right is ast.ArrayInit { checked_type := g.get_expr_type(cond.left) for expr in cond.right.exprs { @@ -632,9 +633,8 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) { } } else if expr is ast.TypeNode { got_type := g.unwrap_generic(expr.typ) - is_true := checked_type.idx() == got_type.idx() - && checked_type.has_flag(.option) == got_type.has_flag(.option) - if is_true { + if checked_type.idx() == got_type.idx() + && checked_type.has_flag(.option) == got_type.has_flag(.option) { if cond.op == .key_in { g.write('1') } else { @@ -650,9 +650,6 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) { g.write('0') } return cond.op == .not_in, true - } else { - g.write('1') - return true, true } } .gt, .lt, .ge, .le { -- 2.39.5