mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix assigning a new value to the mut sumtype receiver (#13204)
This commit is contained in:
@@ -435,7 +435,12 @@ fn (mut g Gen) gen_assign_stmt(node ast.AssignStmt) {
|
|||||||
if op_overloaded {
|
if op_overloaded {
|
||||||
g.op_arg(val, op_expected_right, val_type)
|
g.op_arg(val, op_expected_right, val_type)
|
||||||
} else {
|
} 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
vlib/v/tests/mut_receiver_of_sumtype_test.v
Normal file
17
vlib/v/tests/mut_receiver_of_sumtype_test.v
Normal 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{})'
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user