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

cgen: fix map exists bug (#7647) (#7655)

This commit is contained in:
ka-weihe 2020-12-28 18:32:51 +01:00 committed by GitHub
parent 32091dd90e
commit 93262353d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View File

@ -367,6 +367,17 @@ fn test_map_in_mut() {
assert m['one'] == 2
}
fn test_map_in() {
m := {
'Foo': 'bar'
}
if 'foo'.capitalize() in m {
println('ok')
} else {
assert false
}
}
fn mut_map_with_relation_op_in_fn(mut m map[string]int) {
if m['one'] == 1 {
m['three'] = 3

View File

@ -3081,12 +3081,22 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
return
} else if right_sym.kind == .map {
g.write('_IN_MAP(')
g.expr(node.left)
g.write(', ')
if node.right_type.is_ptr() {
g.write('*')
if !node.left_type.is_ptr() {
left_type_str := g.table.type_to_str(node.left_type)
g.write('ADDR($left_type_str, ')
g.expr(node.left)
g.write(')')
} else {
g.expr(node.left)
}
g.write(', ')
if !node.right_type.is_ptr() {
g.write('ADDR(map, ')
g.expr(node.right)
g.write(')')
} else {
g.expr(node.right)
}
g.expr(node.right)
g.write(')')
} else if right_sym.kind == .string {
g.write('string_contains(')

View File

@ -272,7 +272,7 @@ static void* g_live_info = NULL;
// 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_MAP(val, m) map_exists_1(&m, &val)
#define _IN_MAP(val, m) map_exists_1(m, val)
// unsigned/signed comparisons
static inline bool _us32_gt(uint32_t a, int32_t b) { return a > INT32_MAX || (int32_t)a > b; }