mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
gen: fix slicing mutable arguments (#6596)
This commit is contained in:
parent
d8d80fbf42
commit
6038264a4c
@ -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)
|
c.warn('pointer indexing is only allowed in `unsafe` blocks', node.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if node.index !is ast.RangeExpr { // [1]
|
if node.index is ast.RangeExpr as range { // [1..2]
|
||||||
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 range.has_low {
|
if range.has_low {
|
||||||
index_type := c.expr(range.low)
|
index_type := c.expr(range.low)
|
||||||
c.check_index_type(typ_sym, index_type, node.pos)
|
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)
|
idx := c.table.find_or_register_array(elem_type, 1, c.mod)
|
||||||
return table.new_type(idx)
|
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
|
return typ
|
||||||
}
|
}
|
||||||
|
@ -2228,7 +2228,6 @@ fn (mut g Gen) expr(node ast.Expr) {
|
|||||||
}
|
}
|
||||||
ast.SelectorExpr {
|
ast.SelectorExpr {
|
||||||
if node.expr is ast.TypeOf as left {
|
if node.expr is ast.TypeOf as left {
|
||||||
// typeof(expr).name
|
|
||||||
g.typeof_name(left)
|
g.typeof_name(left)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -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)
|
|
||||||
}
|
|
@ -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]
|
|
||||||
}
|
|
31
vlib/v/tests/array_slice_test.v
Normal file
31
vlib/v/tests/array_slice_test.v
Normal file
@ -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
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user