cgen: fix assigning a new value to the mut sumtype receiver (#13204)

pull/13215/head
yuyi 2022-01-18 22:16:15 +08:00 committed by GitHub
parent 91bfab79a5
commit ef562c92a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -435,7 +435,12 @@ fn (mut g Gen) gen_assign_stmt(node ast.AssignStmt) {
if op_overloaded {
g.op_arg(val, op_expected_right, val_type)
} else {
g.expr_with_cast(val, val_type, var_type)
exp_type := if left.is_auto_deref_var() {
var_type.deref()
} else {
var_type
}
g.expr_with_cast(val, val_type, exp_type)
}
}
}

View File

@ -0,0 +1,17 @@
pub type Foo = Bar | Baz
fn (mut f Foo) set_baz() {
f = Baz{}
}
pub struct Bar {}
pub struct Baz {}
fn test_mut_receiver_of_sumtype() {
mut x := Foo(Bar{})
x.set_baz()
println(x)
assert '$x' == 'Foo(Baz{})'
}