gen: fix slicing mutable arguments (#6596)

pull/6597/head
Enzo 2020-10-10 12:03:23 +02:00 committed by GitHub
parent d8d80fbf42
commit 6038264a4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 32 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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]
}

View 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
}