@@ -281,12 +281,8 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str
281
281
282
282
fn (mut g Gen) match_expr_switch (node ast.MatchExpr, is_expr bool , cond_var string , tmp_var string , cond_fsym ast.TypeSymbol) {
283
283
node_cond_type_unsigned := node.cond_type in [ast.u16_ type, ast.u32_ type, ast.u64_ type]
284
- cname := '${cond_fsym.cname} __'
285
284
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 }
290
286
mut covered_enum := []string {cap: covered_enum_cap} // collects missing enum variant branches to avoid cstrict errors
291
287
292
288
// 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
300
296
for branch in node.branches {
301
297
if branch.is_else {
302
298
if cond_fsym.info is ast.Enum {
299
+ cname := '${cond_fsym.cname} __'
303
300
for val in cond_fsym.info.vals {
304
301
if val ! in covered_enum {
305
302
g.writeln ('case ${cname}${val} :' )
306
303
}
307
304
}
308
305
}
309
- g.write ('default: ' )
306
+ g.writeln ('default: { ' )
310
307
default_generated = true
308
+ g.indent++
311
309
if range_branches.len > 0 {
312
- g.indent++
313
310
for range_branch in range_branches {
314
311
g.write ('if (' )
315
312
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
339
336
}
340
337
g.writeln ('}' )
341
338
}
342
- g.indent--
343
339
}
344
340
} else {
345
341
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
352
348
}
353
349
g.write ('case ' )
354
350
g.expr (expr)
355
- if branch.stmts.len > 0 {
356
- g.write (': ' )
357
- } else {
358
- g.writeln (': ' )
359
- }
351
+ g.write (': ' )
360
352
}
361
353
}
362
- g.indent++
363
354
g.writeln ('{' )
364
355
if is_expr && tmp_var.len > 0 && g.table.sym (node.return_type).kind == .sum_type {
365
356
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
369
360
if ! ends_with_return {
370
361
g.writeln ('\t break;' )
371
362
}
372
- g.indent--
373
363
g.writeln ('}' )
374
364
}
375
365
if range_branches.len > 0 && ! default_generated {
376
- g.write ('default: ' )
366
+ g.writeln ('default: { ' )
377
367
g.indent++
368
+ default_generated = true
378
369
for range_branch in range_branches {
379
370
g.write ('if (' )
380
371
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
404
395
}
405
396
g.writeln ('}' )
406
397
}
398
+ }
399
+ if default_generated {
407
400
g.indent--
401
+ g.writeln ('}' )
408
402
}
409
403
g.indent--
410
404
g.writeln ('}' )
0 commit comments