mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: improve mut arg error msg (#16540)
This commit is contained in:
parent
ef5be22f81
commit
8543d5e055
@ -1028,7 +1028,12 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
|
||||
if call_arg.is_mut {
|
||||
to_lock, pos := c.fail_if_immutable(call_arg.expr)
|
||||
if !call_arg.expr.is_lvalue() {
|
||||
c.error('cannot pass expression as `mut`', call_arg.expr.pos())
|
||||
if call_arg.expr is ast.StructInit {
|
||||
c.error('cannot pass a struct initialization as `mut`, you may want to use a variable `mut var := ${call_arg.expr}`',
|
||||
call_arg.expr.pos())
|
||||
} else {
|
||||
c.error('cannot pass expression as `mut`', call_arg.expr.pos())
|
||||
}
|
||||
}
|
||||
if !param.is_mut {
|
||||
tok := call_arg.share.str()
|
||||
|
6
vlib/v/checker/tests/check_fn_init_as_mutable.out
Normal file
6
vlib/v/checker/tests/check_fn_init_as_mutable.out
Normal file
@ -0,0 +1,6 @@
|
||||
vlib/v/checker/tests/check_fn_init_as_mutable.vv:6:14: error: cannot pass a struct initialization as `mut`, you may want to use a variable `mut var := MyStruct{....}`
|
||||
4 |
|
||||
5 | fn main() {
|
||||
6 | process(mut MyStruct{})
|
||||
| ~~~~~~~~~~
|
||||
7 | }
|
7
vlib/v/checker/tests/check_fn_init_as_mutable.vv
Normal file
7
vlib/v/checker/tests/check_fn_init_as_mutable.vv
Normal file
@ -0,0 +1,7 @@
|
||||
struct MyStruct {}
|
||||
|
||||
fn process(mut ms MyStruct) {}
|
||||
|
||||
fn main() {
|
||||
process(mut MyStruct{})
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
vlib/v/checker/tests/pass_mut_lit.vv:10:12: error: cannot pass expression as `mut`
|
||||
vlib/v/checker/tests/pass_mut_lit.vv:10:12: error: cannot pass a struct initialization as `mut`, you may want to use a variable `mut var := Box{....}`
|
||||
8 | }
|
||||
9 |
|
||||
10 | modify(mut Box{}, 10)
|
||||
|
Loading…
Reference in New Issue
Block a user