Skip to content

Commit

Permalink
Merge pull request #407 from s-hadinger/walrus_index_member
Browse files Browse the repository at this point in the history
Fix walrus with member or index
  • Loading branch information
skiars authored Mar 24, 2024
2 parents 708755d + 98b534a commit b39f463
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/be_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,14 @@ int be_code_setvar(bfuncinfo *finfo, bexpdesc *e1, bexpdesc *e2, bbool keep_reg)
setsupvar(finfo, OP_SETUPV, e1, src);
break;
case ETMEMBER: /* store to member R(A).RK(B) <- RK(C) */
setsfxvar(finfo, OP_SETMBR, e1, src);
break;
case ETINDEX: /* store to member R(A)[RK(B)] <- RK(C) */
setsfxvar(finfo, OP_SETIDX, e1, src);
setsfxvar(finfo, (e1->type == ETMEMBER) ? OP_SETMBR : OP_SETIDX, e1, src);
if (keep_reg && e2->type == ETREG && e1->v.ss.obj >= be_list_count(finfo->local)) {
/* special case of walrus assignemnt when we need to recreate an ETREG */
code_move(finfo, e1->v.ss.obj, src); /* move from ETREG to MEMBER instance*/
free_expreg(finfo, e2); /* free source (checks only ETREG) */
e2->v.idx = e1->v.ss.obj; /* update to new register */
}
break;
default:
return 1;
Expand Down
37 changes: 37 additions & 0 deletions tests/walrus.be
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,40 @@ assert_attribute_error("var a,b def f() a end")

# while the following does have side effect
def f() a := b end

# bug when using walrus with member
def id(x) return x end
var a = 1
import global
def f() return id(global.a := 42) end
assert(f() == 42)
# bug: returns <module: global>

def concat(x, y, z) return str(x)+str(y)+str(z) end
var a = 1
import global
def f() return concat(global.a := 1, global.a := 42, global.a := 0) end
assert(f() == "1420")
# bug: returns '1<module: global>42'

# same bug when using index
def id(x) return x end
l = [10,11]
import global
def f() return id(global.l[0] := 42) end
assert(f() == 42)
# bug: returns [42, 11]

# bug when using member for self
class confused_walrus
var b
def f()
var c = 1
if self.b := true
c = 2
end
return self
end
end
var ins = confused_walrus()
assert(ins.f() == ins)

0 comments on commit b39f463

Please sign in to comment.