From 7aca67fb600afeba7344dcbc71ec322d4b12f8c0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 28 Apr 2022 22:37:19 +0800 Subject: [PATCH] ast, parser: fix complex map init (#14206) --- vlib/v/ast/ast.v | 9 ++++++++- vlib/v/parser/expr.v | 2 +- vlib/v/tests/complex_map_op_test.v | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 78c1e97c51..b2244306c8 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -2074,9 +2074,16 @@ pub fn (mut lx IndexExpr) recursive_mapset_is_setter(val bool) { if lx.left.is_map { lx.left.recursive_mapset_is_setter(val) } + } +} + +pub fn (mut lx IndexExpr) recursive_arraymap_set_is_setter() { + lx.is_setter = true + if mut lx.left is IndexExpr { + lx.left.recursive_arraymap_set_is_setter() } else if mut lx.left is SelectorExpr { if mut lx.left.expr is IndexExpr { - lx.left.expr.recursive_mapset_is_setter(val) + lx.left.expr.recursive_arraymap_set_is_setter() } } } diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index c123ccf73a..41cac7f6d1 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -423,7 +423,7 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden right := p.expr(precedence - 1) pos.update_last_line(p.prev_tok.line_nr) if mut node is ast.IndexExpr { - node.recursive_mapset_is_setter(true) + node.recursive_arraymap_set_is_setter() } node = ast.InfixExpr{ left: node diff --git a/vlib/v/tests/complex_map_op_test.v b/vlib/v/tests/complex_map_op_test.v index 4e9947abf4..d317b5d966 100644 --- a/vlib/v/tests/complex_map_op_test.v +++ b/vlib/v/tests/complex_map_op_test.v @@ -1,10 +1,11 @@ -fn test_complex_map_op() { +fn test_complex_map_op1() { mut test_map := map[string]Point_map{} test_map['test1'].points['point3'] << Point{10, 20} test_map['test2'].points['point4'] << Point{50, 60} println(test_map) + assert test_map.len == 2 assert Point{10, 20} in test_map['test1'].points['point3'] assert Point{50, 60} in test_map['test2'].points['point4'] @@ -12,6 +13,21 @@ fn test_complex_map_op() { assert Point{1, 2} !in test_map['test1'].points['point3'] } +fn test_complex_map_op2() { + mut test_map := map[string]map[string][]Point{} + + test_map['test1']['point3'] << Point{10, 20} + test_map['test2']['point4'] << Point{50, 60} + + println(test_map) + + assert test_map.len == 2 + assert Point{10, 20} in test_map['test1']['point3'] + assert Point{50, 60} in test_map['test2']['point4'] + assert Point{10, 20} !in test_map['test2']['point4'] + assert Point{1, 2} !in test_map['test1']['point3'] +} + struct Point { mut: x int