Skip to content

Commit

Permalink
Fix indexing a map rvalue
Browse files Browse the repository at this point in the history
Also add a cgen macro ADDR.
  • Loading branch information
ntrel committed Dec 18, 2020
1 parent 0f513a3 commit 42f805b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions vlib/v/gen/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1719,9 +1719,12 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
g.write('$styp _var_$left.pos.pos = *($styp*)map_get_1(')
}
if !left.left_type.is_ptr() {
g.write('&')
g.write('ADDR(map, ')
g.expr(left.left)
g.write(')')
} else {
g.expr(left.left)
}
g.expr(left.left)
g.write(', &(string[]){')
g.expr(left.index)
g.write('}')
Expand Down Expand Up @@ -3896,9 +3899,12 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write('(*($elem_type_str*)map_get_1(')
}
if !left_is_ptr || node.left_type.has_flag(.shared_f) {
g.write('&')
g.write('ADDR(map, ')
g.expr(node.left)
g.write(')')
} else {
g.expr(node.left)
}
g.expr(node.left)
if node.left_type.has_flag(.shared_f) {
if left_is_ptr {
g.write('->val')
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/gen/cheaders.v
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ static void* g_live_info = NULL;
//#define tos4(s, slen) ((string){.str=(s), .len=(slen)})
// `"" s` is used to enforce a string literal argument
#define _SLIT(s) ((string){.str=(byteptr)("" s), .len=(sizeof(s)-1), .is_lit=1})
// take the address of an rvalue
#define ADDR(type, expr) (&((type[]){expr}[0]))
#define _PUSH_MANY(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array_push_many(arr, tmp.data, tmp.len);}
#define _IN(typ, val, arr) array_##typ##_contains(arr, val)
#define _IN_MAP(val, m) map_exists(m, val)
Expand Down

0 comments on commit 42f805b

Please sign in to comment.