From 6038264a4c06381f7edcd5e2e343418c2ee12b8a Mon Sep 17 00:00:00 2001 From: Enzo Date: Sat, 10 Oct 2020 12:03:23 +0200 Subject: [PATCH] gen: fix slicing mutable arguments (#6596) --- vlib/v/checker/checker.v | 24 ++++++++++----------- vlib/v/gen/cgen.v | 1 - vlib/v/tests/array_mut_slicing_test.v | 8 ------- vlib/v/tests/array_slice_clone_test.v | 11 ---------- vlib/v/tests/array_slice_test.v | 31 +++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 32 deletions(-) delete mode 100644 vlib/v/tests/array_mut_slicing_test.v delete mode 100644 vlib/v/tests/array_slice_clone_test.v create mode 100644 vlib/v/tests/array_slice_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c037ddac88..f0c0dc8426 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3666,18 +3666,7 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type { c.warn('pointer indexing is only allowed in `unsafe` blocks', node.pos) } } - if node.index !is ast.RangeExpr { // [1] - index_type := c.expr(node.index) - c.check_index_type(typ_sym, index_type, node.pos) - if typ_sym.kind == .map && index_type.idx() != table.string_type_idx { - c.error('non-string map index (map type `$typ_sym.source_name`)', node.pos) - } - value_type := c.table.value_type(typ) - if value_type != table.void_type { - return value_type - } - } else { // [1..2] - range := node.index as ast.RangeExpr + if node.index is ast.RangeExpr as range { // [1..2] if range.has_low { index_type := c.expr(range.low) c.check_index_type(typ_sym, index_type, node.pos) @@ -3693,6 +3682,17 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type { idx := c.table.find_or_register_array(elem_type, 1, c.mod) return table.new_type(idx) } + return typ.set_nr_muls(0) + } else { // [1] + index_type := c.expr(node.index) + c.check_index_type(typ_sym, index_type, node.pos) + if typ_sym.kind == .map && index_type.idx() != table.string_type_idx { + c.error('non-string map index (map type `$typ_sym.source_name`)', node.pos) + } + value_type := c.table.value_type(typ) + if value_type != table.void_type { + return value_type + } } return typ } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e65e7e4293..69dee6c44b 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -2228,7 +2228,6 @@ fn (mut g Gen) expr(node ast.Expr) { } ast.SelectorExpr { if node.expr is ast.TypeOf as left { - // typeof(expr).name g.typeof_name(left) return } diff --git a/vlib/v/tests/array_mut_slicing_test.v b/vlib/v/tests/array_mut_slicing_test.v deleted file mode 100644 index 6019dcdd16..0000000000 --- a/vlib/v/tests/array_mut_slicing_test.v +++ /dev/null @@ -1,8 +0,0 @@ -fn array_mut_slice(mut a []int) { - assert a[1..3].map(it) == [3, 5] -} - -fn test_array_mut_slice() { - mut a := [1, 3, 5, 7, 9] - array_mut_slice(mut a) -} diff --git a/vlib/v/tests/array_slice_clone_test.v b/vlib/v/tests/array_slice_clone_test.v deleted file mode 100644 index ec7065b26f..0000000000 --- a/vlib/v/tests/array_slice_clone_test.v +++ /dev/null @@ -1,11 +0,0 @@ -fn test_array_slice_clone() { - arr := [1, 2, 3, 4, 5] - cl := arr[1..].clone() - assert cl == [2, 3, 4, 5] -} - -fn test_array_slice_clone2() { - arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - cl := arr[1..].clone()[2..].clone() - assert cl == [4, 5, 6, 7, 8, 9, 10] -} diff --git a/vlib/v/tests/array_slice_test.v b/vlib/v/tests/array_slice_test.v new file mode 100644 index 0000000000..4d06e3ce71 --- /dev/null +++ b/vlib/v/tests/array_slice_test.v @@ -0,0 +1,31 @@ +fn array_mut_slice(mut a []int) { + assert a[1..3].map(it) == [3, 5] +} + +fn test_array_mut_slice() { + mut a := [1, 3, 5, 7, 9] + array_mut_slice(mut a) +} + +fn test_array_slice_clone() { + arr := [1, 2, 3, 4, 5] + cl := arr[1..].clone() + assert cl == [2, 3, 4, 5] +} + +fn test_array_slice_clone2() { + arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + cl := arr[1..].clone()[2..].clone() + assert cl == [4, 5, 6, 7, 8, 9, 10] +} + +fn access_slice_attribute(mut arr []int) int { + slice := arr[..arr.len - 1] + return slice.len +} + +fn test_access_slice_attribute() { + mut arr := [1, 2, 3, 4, 5] + assert access_slice_attribute(mut arr) == 4 +} +