From 9e61321d4cb03d260260043f88ddae459048f1b2 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 5 Jul 2021 17:26:01 +0200 Subject: [PATCH] cgen: fix VUNREACHABLE inside ternary (#10672) --- vlib/v/gen/c/fn.v | 12 ++++++++++-- vlib/v/tests/unreachable_code_paths_test.v | 11 +++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/unreachable_code_paths_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 926739bfe0..f0097c9829 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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();})') + } + } } } diff --git a/vlib/v/tests/unreachable_code_paths_test.v b/vlib/v/tests/unreachable_code_paths_test.v new file mode 100644 index 0000000000..95fcd10cb6 --- /dev/null +++ b/vlib/v/tests/unreachable_code_paths_test.v @@ -0,0 +1,11 @@ +fn test_inside_ternary() { + x := if false { + 'foo' + } else if true { + 'bar' + } else { + panic('err') + 'empty' + } + assert x == 'bar' +}