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

cgen: fix nested or expr call (fix #18803) (#18807)

This commit is contained in:
yuyi 2023-07-08 03:06:10 +08:00 committed by GitHub
parent 7fe794a974
commit 97a726b188
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -6115,11 +6115,8 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty
}
if or_block.kind == .block {
g.or_expr_return_type = return_type.clear_flags(.option, .result)
if g.inside_or_block {
g.writeln('\terr = ${cvar_name}.err;')
} else {
g.writeln('\tIError err = ${cvar_name}.err;')
}
g.inside_or_block = true
defer {
g.inside_or_block = false

View File

@ -0,0 +1,18 @@
fn get_name() !string {
return error('failed')
}
fn test_nested_or_expr_call() {
uid_map := map[int]string{}
uid := 2
username := if uid <= 0 {
'unknown'
} else {
uid_map[uid] or {
name := get_name() or { 'unknown' }
name
}
}
assert username == 'unknown'
println('${uid} is ${username}')
}