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

@ -50,11 +50,11 @@ fn C.atoi(byteptr) int
fn foo() { 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, ..) { fn myprint(s string, ..) {
println('my print') println('my print')
@ -63,58 +63,58 @@ fn myprint(s string, ..) {
println('/* /* comment */ */') println('/* /* comment */ */')
} }
// TODO // TODO
fn modify_array(a mut []int) { fn modify_array(a mut []int) {
a[0] = 10 a[0] = 10
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() {
mut nums := [1, 2, 3] mut nums := [1, 2, 3]
modify_array(mut nums) modify_array(mut nums)
//assert nums.len == 4 //assert nums.len == 4
// println(nums) // println(nums)
assert nums[0] == 20 assert nums[0] == 20
assert nums[1] == 4 assert nums[1] == 4
assert nums[2] == 6 assert nums[2] == 6
//assert nums[3] == 888 //assert nums[3] == 888
// workaround for // [91, 32, -33686272] windows bug // workaround for // [91, 32, -33686272] windows bug
println(nums.clone()) println(nums.clone())
} }
fn mod_struct(user mut User) { fn mod_struct(user mut User) {
user.age++ user.age++
} }
struct User { struct User {
mut: mut:
age int age int
} }
fn test_mut_struct() { fn test_mut_struct() {
mut user := User{18} mut user := User{18}
mod_struct(mut user) mod_struct(mut user)
assert user.age == 19 assert user.age == 19
} }
fn mod_ptr(buf mut byteptr) { fn mod_ptr(buf mut byteptr) {
buf[0] = 77 buf[0] = 77
} }
fn test_mut_ptr() { fn test_mut_ptr() {
buf := malloc(10) buf := malloc(10)
mod_ptr(mut buf) mod_ptr(mut buf)
assert buf[0] == 77 assert buf[0] == 77
} }
fn high_fn(f fn(int) int) { fn high_fn(f fn(int) int) {
} }
fn test_fns() { fn test_fns() {
// no asserts for now, just test function declarations above // no asserts for now, just test function declarations above
} }

View File

@ -9,24 +9,24 @@ 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
n := 1 //assert numbers[3] == 4
mut b := &n println(numbers)
*b = 10 n := 1
mut b := &n
*b = 10
//mut b := mut a //mut b := mut a
//b = 10 //b = 10
} }
fn test_mut_2() { fn test_mut_2() {
@ -49,4 +49,4 @@ fn test_mut_2() {
assert b.a[0].v[2] == 7 assert b.a[0].v[2] == 7
assert b.a[0].v[3] == 8 assert b.a[0].v[3] == 8
assert b.a[0].v[4] == 5 assert b.a[0].v[4] == 5
} }