Skip to content

Commit cc5f32f

Browse files
authored
cgen: fix mutable ptr sumtype (#24021)
1 parent e733c00 commit cc5f32f

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2757,7 +2757,7 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty
27572757
mut mutable_idx := 0
27582758

27592759
is_not_ptr_and_fn := !got_is_ptr && !got_is_fn
2760-
is_sumtype_cast := is_not_ptr_and_fn && fname.contains('_to_sumtype_')
2760+
is_sumtype_cast := !got_is_fn && fname.contains('_to_sumtype_')
27612761
is_comptime_variant := is_not_ptr_and_fn && expr is ast.Ident
27622762
&& g.comptime.is_comptime_variant_var(expr)
27632763

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module main
2+
3+
type MySumType = MyStructA | MyStructB
4+
5+
struct MyStructA {
6+
mut:
7+
test bool
8+
}
9+
10+
struct MyStructB {
11+
}
12+
13+
fn test_main() {
14+
mut my_struct := &MyStructA{
15+
test: false
16+
}
17+
if $d('mutable_sumtype', false) {
18+
assert my_struct.test == false
19+
my_struct.test = true
20+
assert my_struct.test == true
21+
but_why(mut my_struct)
22+
assert my_struct.test == false
23+
}
24+
}
25+
26+
fn but_why(mut passed MySumType) {
27+
if mut passed is MyStructA {
28+
passed.test = false
29+
}
30+
}

0 commit comments

Comments
 (0)