fix pushing to mutable array args

pull/2166/head
Alexander Medvednikov 2019-09-29 17:02:28 +03:00
parent 918edad525
commit 83022a2478
4 changed files with 54 additions and 49 deletions

View File

@ -948,9 +948,10 @@ fn (p mut Parser) fn_call_args(f mut Fn) &Fn {
// Reference // Reference
// TODO ptr hacks. DOOM hacks, fix please. // TODO ptr hacks. DOOM hacks, fix please.
if !got_ptr && exp_ptr && got != 'voidptr' { if !got_ptr && exp_ptr && got != 'voidptr' {
// Special case for mutable arrays. We can't `&` function results, // Special case for mutable arrays. We can't `&` function
// results,
// have to use `(array[]){ expr }` hack. // have to use `(array[]){ expr }` hack.
if expected.starts_with('array_') && exp_ptr { if expected.starts_with('array_') && exp_ptr { //&& !arg.is_mut{
p.cgen.set_placeholder(ph, '& /*111*/ (array[]){') p.cgen.set_placeholder(ph, '& /*111*/ (array[]){')
p.gen('}[0] ') p.gen('}[0] ')
} }

View File

@ -2350,6 +2350,10 @@ fn (p mut Parser) expression() string {
if !p.expr_var.is_mut && !p.pref.translated { if !p.expr_var.is_mut && !p.pref.translated {
p.error('`$p.expr_var.name` is immutable (can\'t <<)') p.error('`$p.expr_var.name` is immutable (can\'t <<)')
} }
if p.expr_var.is_arg && p.expr_var.typ.starts_with('array_') {
p.error("for now it's not possible to append an element to "+
'a mutable array argument `$p.expr_var.name`')
}
if !p.expr_var.is_changed { if !p.expr_var.is_changed {
p.mark_var_changed(p.expr_var) p.mark_var_changed(p.expr_var)
} }

View File

@ -69,7 +69,7 @@ fn modify_array(a mut []int) {
for i in 0..a.len { for i in 0..a.len {
a[i] = a[i] * 2 a[i] = a[i] * 2
} }
a << 888 //a << 888
} }
fn test_mut_array() { fn test_mut_array() {

View File

@ -10,21 +10,21 @@ pub mut:
fn foo(b int, a mut []int) { fn foo(b int, a mut []int) {
a[0] = 7 a[0] = 7
a << 4 //a << 4
} }
// TODO
fn test_mut() { fn test_mut() {
mut numbers := [1,2,3] mut numbers := [1,2,3]
foo(7, mut numbers) foo(7, mut numbers)
//assert a.len == 4 assert numbers.len == 3
assert numbers[0] == 7 // TODO bring back once << works with mutable args
//assert a[3] == 4 //assert numbers.len == 4
//assert numbers[0] == 7
//assert numbers[3] == 4
println(numbers)
n := 1 n := 1
mut b := &n mut b := &n
*b = 10 *b = 10
//mut b := mut a //mut b := mut a
//b = 10 //b = 10
} }