Skip to content

Commit ffdc1ab

Browse files
authored
cgen: fix codegen for match with sumtype ptrptr (fix #23776) (#23785)
1 parent 3f44780 commit ffdc1ab

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,6 +3904,9 @@ fn (mut g Gen) typeof_expr(node ast.TypeOf) {
39043904
// When encountering a .sum_type, typeof() should be done at runtime,
39053905
// because the subtype of the expression may change:
39063906
g.write('charptr_vstring_literal(v_typeof_sumtype_${sym.cname}( (')
3907+
if typ.nr_muls() > 0 {
3908+
g.write('*'.repeat(typ.nr_muls()))
3909+
}
39073910
g.expr(node.expr)
39083911
g.write(')._typ ))')
39093912
} else if sym.kind == .array_fixed {

vlib/v/gen/c/match.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,14 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str
208208
g.write_v_source_line_info(branch)
209209
g.write('if (')
210210
}
211+
need_deref := node.cond_type.nr_muls() > 1
212+
if need_deref {
213+
g.write2('(', '*'.repeat(node.cond_type.nr_muls() - 1))
214+
}
211215
g.write(cond_var)
216+
if need_deref {
217+
g.write(')')
218+
}
212219
cur_expr := unsafe { &branch.exprs[sumtype_index] }
213220
if cond_sym.kind == .sum_type {
214221
g.write('${dot_or_ptr}_typ == ')
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub type Command = Assign | Call
2+
3+
pub struct Assign {}
4+
5+
pub struct Call {
6+
mut:
7+
text string
8+
}
9+
10+
fn test_main() {
11+
mut command_arr_ptr := [&Command(Call{})]
12+
mut command_arr_el_ptr := &command_arr_ptr[0] // type is && ?
13+
14+
match mut command_arr_el_ptr {
15+
Call {
16+
command_arr_el_ptr.text = 'foo'
17+
}
18+
Assign {}
19+
}
20+
if mut command_arr_el_ptr is Call {
21+
assert command_arr_el_ptr.text == 'foo'
22+
} else {
23+
assert false
24+
}
25+
26+
assert typeof(command_arr_el_ptr) == 'Call'
27+
assert typeof(command_arr_ptr) == '[]&Command'
28+
}

0 commit comments

Comments
 (0)