mirror of
https://github.com/vlang/v.git
synced 2025-09-18 20:07:02 -04:00
cgen: fix match expr with last aggregate branch (#16101)
This commit is contained in:
parent
117c829a97
commit
026fccd373
@ -145,7 +145,7 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str
|
|||||||
// iterates through all types in sumtype branches
|
// iterates through all types in sumtype branches
|
||||||
for {
|
for {
|
||||||
g.aggregate_type_idx = sumtype_index
|
g.aggregate_type_idx = sumtype_index
|
||||||
is_last := j == node.branches.len - 1
|
is_last := j == node.branches.len - 1 && sumtype_index == branch.exprs.len - 1
|
||||||
sym := g.table.sym(node.cond_type)
|
sym := g.table.sym(node.cond_type)
|
||||||
if branch.is_else || (use_ternary && is_last) {
|
if branch.is_else || (use_ternary && is_last) {
|
||||||
if use_ternary {
|
if use_ternary {
|
||||||
|
24
vlib/v/tests/match_expr_with_last_branch_aggregate_test.v
Normal file
24
vlib/v/tests/match_expr_with_last_branch_aggregate_test.v
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
type MyT = f32 | f64 | i64 | int | rune | string | u32 | u64
|
||||||
|
type FnMyT = fn (MyT)
|
||||||
|
|
||||||
|
fn (any []MyT) each(fun FnMyT) {
|
||||||
|
for x in any {
|
||||||
|
fun(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (myt MyT) to_s() string {
|
||||||
|
return match myt {
|
||||||
|
string { myt }
|
||||||
|
rune { rune(myt).str() }
|
||||||
|
int, u32, i64, u64, f32, f64 { myt.str() } // FAIL compilation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_match_expr_with_last_branch_aggregate() {
|
||||||
|
mut arr := [MyT(1), `😝`, 'we are mamamoo', f32(u64(-1)), f64(u64(-1)), i64(-1), u64(-1)]
|
||||||
|
arr.each(fn (my MyT) {
|
||||||
|
println(my.to_s())
|
||||||
|
})
|
||||||
|
assert true
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user