1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix match expr with last aggregate branch (#16101)

This commit is contained in:
yuyi 2022-10-19 16:49:39 +08:00 committed by GitHub
parent 117c829a97
commit 026fccd373
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -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
for {
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)
if branch.is_else || (use_ternary && is_last) {
if use_ternary {

View 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
}