Skip to content

Commit 32ad8de

Browse files
authored
checker: fix comptime else branch handling (fix #25586) (#25589)
1 parent 5ac98e3 commit 32ad8de

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

vlib/v/checker/if.v

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
308308
node.is_expr = true
309309
node.typ = c.unwrap_generic(c.expected_type)
310310
}
311-
continue
311+
unsafe {
312+
goto end_if
313+
}
312314
}
313315
if c.expected_expr_type != ast.void_type {
314316
c.expected_type = c.expected_expr_type
@@ -327,7 +329,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
327329
// (e.g. argument expression, variable declaration / assignment)
328330
c.error('the final expression in `if` or `match`, must have a value of a non-void type',
329331
stmt.pos)
330-
continue
332+
unsafe {
333+
goto end_if
334+
}
331335
}
332336
if !c.check_types(stmt.typ, node.typ) {
333337
if node.typ == ast.void_type {
@@ -339,37 +343,51 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
339343
node.typ = stmt.typ
340344
}
341345
c.expected_expr_type = node.typ
342-
continue
346+
unsafe {
347+
goto end_if
348+
}
343349
} else if node.typ in [ast.float_literal_type, ast.int_literal_type] {
344350
if node.typ == ast.int_literal_type {
345351
if stmt.typ.is_int() || stmt.typ.is_float() {
346352
node.typ = stmt.typ
347-
continue
353+
unsafe {
354+
goto end_if
355+
}
348356
}
349357
} else { // node.typ == float_literal
350358
if stmt.typ.is_float() {
351359
node.typ = stmt.typ
352-
continue
360+
unsafe {
361+
goto end_if
362+
}
353363
}
354364
}
355365
}
356366
if stmt.typ in [ast.float_literal_type, ast.int_literal_type] {
357367
if stmt.typ == ast.int_literal_type {
358368
if node.typ.is_int() || node.typ.is_float() {
359-
continue
369+
unsafe {
370+
goto end_if
371+
}
360372
}
361373
} else { // expr_type == float_literal
362374
if node.typ.is_float() {
363-
continue
375+
unsafe {
376+
goto end_if
377+
}
364378
}
365379
}
366380
}
367381
if node.is_expr && c.table.sym(former_expected_type).kind == .sum_type {
368382
node.typ = former_expected_type
369-
continue
383+
unsafe {
384+
goto end_if
385+
}
370386
}
371387
if is_noreturn_callexpr(stmt.expr) {
372-
continue
388+
unsafe {
389+
goto end_if
390+
}
373391
}
374392
if (node.typ.has_option_or_result())
375393
&& c.table.sym(stmt.typ).kind == .struct
@@ -382,7 +400,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
382400
pos: node.pos
383401
}
384402
stmt.typ = ast.error_type
385-
continue
403+
unsafe {
404+
goto end_if
405+
}
386406
}
387407
if (node.typ == ast.none_type && stmt.typ != ast.none_type)
388408
|| (stmt.typ == ast.none_type && node.typ != ast.none_type) {
@@ -391,7 +411,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
391411
} else {
392412
node.typ.set_flag(.option)
393413
}
394-
continue
414+
unsafe {
415+
goto end_if
416+
}
395417
}
396418
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
397419
node.pos)
@@ -440,7 +462,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
440462
nbranches_without_return++
441463
}
442464
}
443-
465+
end_if:
444466
if comptime_remove_curr_branch_stmts && !c.pref.output_cross_c {
445467
// remove the branch statements since they may contain OS-specific code.
446468
branch.stmts = []
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
string _t1;
2+
#if defined(CUSTOM_DEFINE_some_custom_define)
3+
#elif defined(CUSTOM_DEFINE_some_other_define)
4+
_t1 = _S("xyz");
5+
;
6+
#else
7+
#endif
8+
string platform = _t1;
9+
builtin__println(platform);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// vtest vflags: -d some_other_define
2+
platform := $if some_custom_define ? {
3+
'abc'
4+
} $else $if some_other_define ? {
5+
'xyz'
6+
} $else {
7+
eprintln('oh no')
8+
exit(1)
9+
}
10+
println(platform)

0 commit comments

Comments
 (0)