1
0
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:
Vincenzo Palazzo 2022-11-27 05:07:35 +01:00 committed by GitHub
parent ef5be22f81
commit 8543d5e055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 3 deletions

View File

@ -1028,7 +1028,12 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
if call_arg.is_mut { if call_arg.is_mut {
to_lock, pos := c.fail_if_immutable(call_arg.expr) to_lock, pos := c.fail_if_immutable(call_arg.expr)
if !call_arg.expr.is_lvalue() { 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 { if !param.is_mut {
tok := call_arg.share.str() tok := call_arg.share.str()

View 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 | }

View File

@ -0,0 +1,7 @@
struct MyStruct {}
fn process(mut ms MyStruct) {}
fn main() {
process(mut MyStruct{})
}

View File

@ -1,5 +1,5 @@
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 | } 8 | }
9 | 9 |
10 | modify(mut Box{}, 10) 10 | modify(mut Box{}, 10)
| ~~~~~ | ~~~~~