diff --git a/vlib/v/gen/comptime.v b/vlib/v/gen/comptime.v index a583ec1137..f81f8fb019 100644 --- a/vlib/v/gen/comptime.v +++ b/vlib/v/gen/comptime.v @@ -213,6 +213,11 @@ fn (mut g Gen) comp_if(node ast.IfExpr) { if should_create_scope { g.writeln('}') } + if !comp_if_stmts_skip && branch.cond is ast.InfixExpr { + if (branch.cond as ast.InfixExpr).op == .key_is { + break + } + } } g.defer_ifdef = '' } diff --git a/vlib/v/tests/comptime_if_expr_test.v b/vlib/v/tests/comptime_if_expr_test.v index 86c458f6f2..c2b0e74339 100644 --- a/vlib/v/tests/comptime_if_expr_test.v +++ b/vlib/v/tests/comptime_if_expr_test.v @@ -80,3 +80,24 @@ fn test_generic_t_is3() { } assert res == GenericTIsTest{} } + +fn test_generic_t_is_with_else() { + res := generic_t_is_with_else('') or { + assert false + GenericTIsTest{} + } + assert res == GenericTIsTest{} + str := generic_t_is_with_else('test') or { + assert false + '' + } + assert str == 'test' +} + +fn generic_t_is_with_else(raw_data string) ?T { + $if T is string { + return raw_data + } $else { + return T{} + } +} \ No newline at end of file