Skip to content

Commit

Permalink
cgen: fix promoting an alias typed value, to a sumtype of the alias's…
Browse files Browse the repository at this point in the history
… base type (fix #19407) (#19423)
  • Loading branch information
lv37 committed Sep 26, 2023
1 parent a8a3d3a commit 245f8e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 7 additions & 3 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -2265,15 +2265,19 @@ struct SumtypeCastingFn {
fn (mut g Gen) get_sumtype_casting_fn(got_ ast.Type, exp_ ast.Type) string {
got, exp := got_.idx(), exp_.idx()
i := got | int(u32(exp) << 16)
got_cname, exp_cname := g.table.sym(got).cname, g.table.sym(exp).cname
fn_name := '${got_cname}_to_sumtype_${exp_cname}'
exp_sym := g.table.sym(exp)
mut got_sym := g.table.sym(got)
fn_name := '${got_sym.cname}_to_sumtype_${exp_sym.cname}'
if got == exp || g.sumtype_definitions[i] {
return fn_name
}
for got_sym.parent_idx != 0 && got_sym.idx !in (exp_sym.info as ast.SumType).variants {
got_sym = g.table.sym(got_sym.parent_idx)
}
g.sumtype_definitions[i] = true
g.sumtype_casting_fns << SumtypeCastingFn{
fn_name: fn_name
got: got
got: got_sym.idx
exp: exp
}
return fn_name
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/alias_to_sum_with_enum_test.v
@@ -0,0 +1,13 @@
enum Test {
abc
}

type SumTest = Test | u8

type TestAlias = Test

fn test_main() {
a := TestAlias.abc
b := SumTest(a)
assert b is Test
}

0 comments on commit 245f8e3

Please sign in to comment.