cgen: fix mutable ptr sumtype (#24021)

This commit is contained in:
Felipe Pena 2025-03-23 20:01:11 -03:00 committed by GitHub
parent e733c007bf
commit cc5f32f1ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View file

@ -2757,7 +2757,7 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty
mut mutable_idx := 0 mut mutable_idx := 0
is_not_ptr_and_fn := !got_is_ptr && !got_is_fn is_not_ptr_and_fn := !got_is_ptr && !got_is_fn
is_sumtype_cast := is_not_ptr_and_fn && fname.contains('_to_sumtype_') is_sumtype_cast := !got_is_fn && fname.contains('_to_sumtype_')
is_comptime_variant := is_not_ptr_and_fn && expr is ast.Ident is_comptime_variant := is_not_ptr_and_fn && expr is ast.Ident
&& g.comptime.is_comptime_variant_var(expr) && g.comptime.is_comptime_variant_var(expr)

View file

@ -0,0 +1,30 @@
module main
type MySumType = MyStructA | MyStructB
struct MyStructA {
mut:
test bool
}
struct MyStructB {
}
fn test_main() {
mut my_struct := &MyStructA{
test: false
}
if $d('mutable_sumtype', false) {
assert my_struct.test == false
my_struct.test = true
assert my_struct.test == true
but_why(mut my_struct)
assert my_struct.test == false
}
}
fn but_why(mut passed MySumType) {
if mut passed is MyStructA {
passed.test = false
}
}