Skip to content

Commit

Permalink
checker: check mismatch of fn call mut argument (fix #21857) (#21873)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 15, 2024
1 parent 9088970 commit 64430f9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/checker/check_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
}
}

// `fn foo(mut p &Expr); mut expr := Expr{}; foo(mut expr)`
if arg.is_mut && expected.nr_muls() > 1 && !got.is_ptr() {
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
return error('cannot use `${got_typ_str}` as `${expected_typ_str}`')
}

exp_sym_idx := c.table.sym(expected).idx
got_sym_idx := c.table.sym(got).idx

Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/fn_call_mut_arg_mismatch_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vlib/v/checker/tests/fn_call_mut_arg_mismatch_err.vv:15:10: error: cannot use `Expr1` as `&&Expr1` in argument 1 to `foo`
13 | name: '123'
14 | }
15 | foo(mut expr)
| ~~~~
16 | }
16 changes: 16 additions & 0 deletions vlib/v/checker/tests/fn_call_mut_arg_mismatch_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module main

pub struct Expr1 {
name string
}

fn foo(mut expr &Expr1) { // <---- &
println(expr.name)
}

fn main() {
mut expr := Expr1{
name: '123'
}
foo(mut expr)
}

0 comments on commit 64430f9

Please sign in to comment.