Skip to content

Commit

Permalink
cgen: fix VUNREACHABLE inside ternary (#10672)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Neubert authored Jul 5, 2021
1 parent d307ab2 commit 9e61321
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,16 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
}
}
if node.is_noreturn {
g.writeln(';')
g.write('VUNREACHABLE()')
if g.inside_ternary == 0 {
g.writeln(';')
g.write('VUNREACHABLE()')
} else {
$if msvc {
// MSVC has no support for the statement expressions used below
} $else {
g.write(', ({VUNREACHABLE();})')
}
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions vlib/v/tests/unreachable_code_paths_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn test_inside_ternary() {
x := if false {
'foo'
} else if true {
'bar'
} else {
panic('err')
'empty'
}
assert x == 'bar'
}

0 comments on commit 9e61321

Please sign in to comment.