1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: make use of mut_rec in method consistent (#9308)

This commit is contained in:
yuyi 2021-03-15 18:22:52 +08:00 committed by GitHub
parent e235022e10
commit 2d2e4610e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -4498,9 +4498,8 @@ fn (mut g Gen) return_statement(node ast.Return) {
}
if expr0.is_auto_deref_var() {
if g.fn_decl.return_type.is_ptr() {
g.write('&(*')
g.expr(expr0)
g.write(')')
var_str := g.expr_string(expr0)
g.write(var_str.trim('&'))
} else {
g.write('*')
g.expr(expr0)

View File

@ -0,0 +1,36 @@
struct Player {
mut:
name string
x int
y int
}
fn (mut p Player) set_name(name string) &Player {
p.name = name
return p // because of automatic (de)reference of return values
}
// NB: `p` is declared as a `mut` parameter,
// which now only affects its mutability.
fn (mut p Player) set_position(x int, y int) &Player {
p.x = x
p.y = y
// TODO: from the point of view of the V programmer,
// `p` has still type &Player.
// assert typeof(p).name == 'Player'
return &p
}
fn test_mut_receiver() {
mut p := &Player{}
f := p.set_name('frodo')
assert u64(p) == u64(f)
z := p.set_position(111, 222)
assert u64(p) == u64(z)
//
assert p.name == 'frodo'
assert p.x == 111
assert p.y == 222
assert u64(p.set_name('bilbo')) == u64(p)
assert p.name == 'bilbo'
}