cgen: fix `array_slice(*a, 0, a.len)` C error, generated for `a[..a.len]`, for a `mut a []int` (#8168)

pull/8172/head
BigBlack 2021-01-17 23:22:23 +08:00 committed by GitHub
parent 334b66b311
commit 28ed4da1ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -3940,6 +3940,12 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write('_ARR_LEN(') g.write('_ARR_LEN(')
g.expr(node.left) g.expr(node.left)
g.write(')') g.write(')')
} else if node.left_type.is_ptr() {
g.write('(')
g.write('*')
g.expr(node.left)
g.write(')')
g.write('.len')
} else { } else {
g.expr(node.left) g.expr(node.left)
g.write('.len') g.write('.len')

View File

@ -37,3 +37,14 @@ fn test_fixed_array_slice() {
arr2 := fixed_array2[0..] arr2 := fixed_array2[0..]
assert arr2 == [[1, 2], [2, 3], [3, 4],[4, 5]] assert arr2 == [[1, 2], [2, 3], [3, 4],[4, 5]]
} }
fn pointer_array_slice(mut a []int) {
assert a[0..] == [1,2,3]
assert a[..a.len] == [1,2,3]
}
fn test_pointer_array_slice() {
mut arr := [1,2,3]
pointer_array_slice(mut arr)
}