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

gen: fix generic comp time if (#8254)

This commit is contained in:
Louis Schmieder 2021-01-21 20:17:44 +01:00 committed by GitHub
parent d8c94cd1fd
commit 0d204603d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

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

View File

@ -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<GenericTIsTest>('') or {
assert false
GenericTIsTest{}
}
assert res == GenericTIsTest{}
str := generic_t_is_with_else<string>('test') or {
assert false
''
}
assert str == 'test'
}
fn generic_t_is_with_else<T>(raw_data string) ?T {
$if T is string {
return raw_data
} $else {
return T{}
}
}