mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix struct init with update of mutable receiver (#15758)
This commit is contained in:
parent
ea4152ee14
commit
5719344653
@ -548,7 +548,7 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
|||||||
if node.has_update_expr {
|
if node.has_update_expr {
|
||||||
update_type := c.expr(node.update_expr)
|
update_type := c.expr(node.update_expr)
|
||||||
node.update_expr_type = update_type
|
node.update_expr_type = update_type
|
||||||
if c.table.type_kind(update_type) != .struct_ {
|
if c.table.sym(update_type).kind != .struct_ {
|
||||||
s := c.table.type_to_str(update_type)
|
s := c.table.type_to_str(update_type)
|
||||||
c.error('expected struct, found `$s`', node.update_expr.pos())
|
c.error('expected struct, found `$s`', node.update_expr.pos())
|
||||||
} else if update_type != node.typ {
|
} else if update_type != node.typ {
|
||||||
|
21
vlib/v/tests/struct_init_update_with_mutable_receiver_test.v
Normal file
21
vlib/v/tests/struct_init_update_with_mutable_receiver_test.v
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
struct Person {
|
||||||
|
name string
|
||||||
|
age int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut p Person) change(name string) {
|
||||||
|
p = Person{
|
||||||
|
...p
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_struct_init_update_with_mutable_receiver() {
|
||||||
|
mut p := Person{'bob', 22}
|
||||||
|
p.change('tom')
|
||||||
|
println(p)
|
||||||
|
assert p.name == 'tom'
|
||||||
|
assert p.age == 22
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user