1
0
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:
yuyi 2022-09-15 00:46:05 +08:00 committed by GitHub
parent ea4152ee14
commit 5719344653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -548,7 +548,7 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
if node.has_update_expr {
update_type := c.expr(node.update_expr)
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)
c.error('expected struct, found `$s`', node.update_expr.pos())
} else if update_type != node.typ {

View 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
}