diff --git a/compiler/fn.v b/compiler/fn.v index e5a934b229..5953d4804e 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -853,7 +853,7 @@ fn (p mut Parser) fn_call_args(f mut Fn) *Fn { p.error(err) } is_interface := p.table.is_interface(arg.typ) - // Add & or * before arg? + // Add `&` or `*` before an argument? if !is_interface { // Dereference if got.contains('*') && !expected.contains('*') { @@ -862,11 +862,17 @@ fn (p mut Parser) fn_call_args(f mut Fn) *Fn { // Reference // TODO ptr hacks. DOOM hacks, fix please. if !got.contains('*') && expected.contains('*') && got != 'voidptr' { + // Special case for mutable arrays. We can't & function results, + // have to use `(array[]){ expr }` hack. + if expected.starts_with('array_') && expected.ends_with('*') { + p.cgen.set_placeholder(ph, '& /*111*/ (array_int[]){') + p.gen('} ') + } // println('\ne:"$expected" got:"$got"') - if ! (expected == 'void*' && got == 'int') && + else if ! (expected == 'void*' && got == 'int') && ! (expected == 'byte*' && got.contains(']byte')) && ! (expected == 'byte*' && got == 'string') { - p.cgen.set_placeholder(ph, '& /*11 EXP:"$expected" GOT:"$got" */') + p.cgen.set_placeholder(ph, '& /*112 EXP:"$expected" GOT:"$got" */') } } } diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 482e794d90..2e9315b414 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -177,3 +177,15 @@ fn test_fixed() { nums2 := [N]int assert nums2[N - 1] == 0 } + +fn modify (numbers mut []int) { + numbers[0] = 777 +} + +fn test_mut_slice() { + mut n := [1,2,3] + modify(mut n.left(2)) + assert n[0] == 777 + println(n) +} +