Skip to content

Commit 5ce4f95

Browse files
authored
cgen: improve the readability of switch() { statements, generated by match() { ones (#24618)
1 parent 21da729 commit 5ce4f95

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

vlib/v/gen/c/match.v

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,8 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str
281281

282282
fn (mut g Gen) match_expr_switch(node ast.MatchExpr, is_expr bool, cond_var string, tmp_var string, cond_fsym ast.TypeSymbol) {
283283
node_cond_type_unsigned := node.cond_type in [ast.u16_type, ast.u32_type, ast.u64_type]
284-
cname := '${cond_fsym.cname}__'
285284

286-
mut covered_enum_cap := 0
287-
if cond_fsym.info is ast.Enum {
288-
covered_enum_cap = cond_fsym.info.vals.len
289-
}
285+
covered_enum_cap := if cond_fsym.info is ast.Enum { cond_fsym.info.vals.len } else { 0 }
290286
mut covered_enum := []string{cap: covered_enum_cap} // collects missing enum variant branches to avoid cstrict errors
291287

292288
// A branch that has a RangeExpr condition, cannot be emitted as a switch case branch;
@@ -300,16 +296,17 @@ fn (mut g Gen) match_expr_switch(node ast.MatchExpr, is_expr bool, cond_var stri
300296
for branch in node.branches {
301297
if branch.is_else {
302298
if cond_fsym.info is ast.Enum {
299+
cname := '${cond_fsym.cname}__'
303300
for val in cond_fsym.info.vals {
304301
if val !in covered_enum {
305302
g.writeln('case ${cname}${val}:')
306303
}
307304
}
308305
}
309-
g.write('default: ')
306+
g.writeln('default: {')
310307
default_generated = true
308+
g.indent++
311309
if range_branches.len > 0 {
312-
g.indent++
313310
for range_branch in range_branches {
314311
g.write('if (')
315312
for i, expr in range_branch.exprs {
@@ -339,7 +336,6 @@ fn (mut g Gen) match_expr_switch(node ast.MatchExpr, is_expr bool, cond_var stri
339336
}
340337
g.writeln('}')
341338
}
342-
g.indent--
343339
}
344340
} else {
345341
if branch.exprs.any(it is ast.RangeExpr) {
@@ -352,14 +348,9 @@ fn (mut g Gen) match_expr_switch(node ast.MatchExpr, is_expr bool, cond_var stri
352348
}
353349
g.write('case ')
354350
g.expr(expr)
355-
if branch.stmts.len > 0 {
356-
g.write(': ')
357-
} else {
358-
g.writeln(': ')
359-
}
351+
g.write(': ')
360352
}
361353
}
362-
g.indent++
363354
g.writeln('{')
364355
if is_expr && tmp_var.len > 0 && g.table.sym(node.return_type).kind == .sum_type {
365356
g.expected_cast_type = node.return_type
@@ -369,12 +360,12 @@ fn (mut g Gen) match_expr_switch(node ast.MatchExpr, is_expr bool, cond_var stri
369360
if !ends_with_return {
370361
g.writeln('\tbreak;')
371362
}
372-
g.indent--
373363
g.writeln('}')
374364
}
375365
if range_branches.len > 0 && !default_generated {
376-
g.write('default: ')
366+
g.writeln('default: {')
377367
g.indent++
368+
default_generated = true
378369
for range_branch in range_branches {
379370
g.write('if (')
380371
for i, expr in range_branch.exprs {
@@ -404,7 +395,10 @@ fn (mut g Gen) match_expr_switch(node ast.MatchExpr, is_expr bool, cond_var stri
404395
}
405396
g.writeln('}')
406397
}
398+
}
399+
if default_generated {
407400
g.indent--
401+
g.writeln('}')
408402
}
409403
g.indent--
410404
g.writeln('}')

0 commit comments

Comments
 (0)