diff --git a/compiler/fn.v b/compiler/fn.v index f0b07d7f6e..a9a1aa343f 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -678,6 +678,7 @@ fn (p mut Parser) fn_args(f mut Fn) { } } +// foo *(1, 2, 3, mut bar)* fn (p mut Parser) fn_call_args(f *Fn) *Fn { // p.gen('(') // println('fn_call_args() name=$f.name args.len=$f.args.len') @@ -725,7 +726,15 @@ fn (p mut Parser) fn_call_args(f *Fn) *Fn { p.error('`$arg.name` is a key_mut argument, you need to provide a variable to modify: `$f.name(... mut a...)`') } p.check(.key_mut) - } + var_name := p.lit + v := p.cur_fn.find_var(var_name) + if v.name == '' { + p.error('`$arg.name` is a key_mut argument, you need to provide a variable to modify: `$f.name(... mut a...)`') + } + if !v.is_changed { + p.cur_fn.mark_var_changed(v) + } + } p.expected_type = arg.typ typ := p.bool_expression() // Optimize `println`: replace it with `printf` to avoid extra allocations and diff --git a/compiler/tests/fn_test.v b/compiler/tests/fn_test.v index de4fcd94cc..2505f8f440 100644 --- a/compiler/tests/fn_test.v +++ b/compiler/tests/fn_test.v @@ -92,7 +92,7 @@ mut: fn test_mut_struct() { mut user := User{18} - mod_struct(mut user) + mod_struct(mut user) assert user.age == 19 }