From 83022a24789e340c0d425a5885742c205f1bb53f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 29 Sep 2019 17:02:28 +0300 Subject: [PATCH] fix pushing to mutable array args --- compiler/fn.v | 5 ++-- compiler/parser.v | 4 +++ compiler/tests/fn_test.v | 62 +++++++++++++++++++-------------------- compiler/tests/mut_test.v | 32 ++++++++++---------- 4 files changed, 54 insertions(+), 49 deletions(-) diff --git a/compiler/fn.v b/compiler/fn.v index a836d58492..7f44a8c6c5 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -948,9 +948,10 @@ fn (p mut Parser) fn_call_args(f mut Fn) &Fn { // Reference // TODO ptr hacks. DOOM hacks, fix please. 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. - 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.gen('}[0] ') } diff --git a/compiler/parser.v b/compiler/parser.v index e777c3f9d8..c481bc7fd3 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -2350,6 +2350,10 @@ fn (p mut Parser) expression() string { if !p.expr_var.is_mut && !p.pref.translated { 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 { p.mark_var_changed(p.expr_var) } diff --git a/compiler/tests/fn_test.v b/compiler/tests/fn_test.v index 5f08a7194a..6e7b17f593 100644 --- a/compiler/tests/fn_test.v +++ b/compiler/tests/fn_test.v @@ -50,11 +50,11 @@ fn C.atoi(byteptr) int fn foo() { } -type actionf_v fn () +type actionf_v fn () -type actionf_p1 fn (voidptr) +type actionf_p1 fn (voidptr) -type actionf_p2 fn (voidptr, voidptr) +type actionf_p2 fn (voidptr, voidptr) fn myprint(s string, ..) { println('my print') @@ -63,58 +63,58 @@ fn myprint(s string, ..) { println('/* /* comment */ */') } -// TODO +// TODO fn modify_array(a mut []int) { - a[0] = 10 + a[0] = 10 for i in 0..a.len { a[i] = a[i] * 2 } - a << 888 -} + //a << 888 +} fn test_mut_array() { - mut nums := [1, 2, 3] - modify_array(mut nums) - //assert nums.len == 4 + mut nums := [1, 2, 3] + modify_array(mut nums) + //assert nums.len == 4 // println(nums) - assert nums[0] == 20 + assert nums[0] == 20 assert nums[1] == 4 - assert nums[2] == 6 + assert nums[2] == 6 //assert nums[3] == 888 // workaround for // [91, 32, -33686272] windows bug println(nums.clone()) -} +} fn mod_struct(user mut User) { - user.age++ + user.age++ } struct User { -mut: - age int -} +mut: + age int +} fn test_mut_struct() { - mut user := User{18} - mod_struct(mut user) - assert user.age == 19 -} + mut user := User{18} + mod_struct(mut user) + assert user.age == 19 +} fn mod_ptr(buf mut byteptr) { - buf[0] = 77 -} + buf[0] = 77 +} fn test_mut_ptr() { - buf := malloc(10) - mod_ptr(mut buf) - assert buf[0] == 77 -} + buf := malloc(10) + mod_ptr(mut buf) + assert buf[0] == 77 +} fn high_fn(f fn(int) int) { - -} + +} fn test_fns() { - // no asserts for now, just test function declarations above -} + // no asserts for now, just test function declarations above +} diff --git a/compiler/tests/mut_test.v b/compiler/tests/mut_test.v index 61cea17da6..9be62a3e1d 100644 --- a/compiler/tests/mut_test.v +++ b/compiler/tests/mut_test.v @@ -9,24 +9,24 @@ pub mut: } fn foo(b int, a mut []int) { - a[0] = 7 - a << 4 -} + a[0] = 7 + //a << 4 +} -// TODO fn test_mut() { - mut numbers := [1,2,3] - foo(7, mut numbers) - //assert a.len == 4 - assert numbers[0] == 7 - //assert a[3] == 4 - - n := 1 - mut b := &n - *b = 10 - + mut numbers := [1,2,3] + foo(7, mut numbers) + assert numbers.len == 3 + // TODO bring back once << works with mutable args + //assert numbers.len == 4 + //assert numbers[0] == 7 + //assert numbers[3] == 4 + println(numbers) + n := 1 + mut b := &n + *b = 10 //mut b := mut a - //b = 10 + //b = 10 } fn test_mut_2() { @@ -49,4 +49,4 @@ fn test_mut_2() { assert b.a[0].v[2] == 7 assert b.a[0].v[3] == 8 assert b.a[0].v[4] == 5 -} +}