checker: error if assigning to a function (#6581)

pull/6585/head
Nick Treleaven 2020-10-08 23:48:39 +01:00 committed by GitHub
parent 7d65e4cb10
commit 92630a2821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -1944,6 +1944,8 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
if assign_stmt.op !in [.assign, .decl_assign] {
c.error('cannot modify blank `_` identifier', left.pos)
}
} else if left.info !is ast.IdentVar {
c.error('cannot assign to $left.kind `$left.name`', left.pos)
} else {
if is_decl {
c.check_valid_snake_case(left.name, 'variable name', left.pos)

View File

@ -4,4 +4,18 @@ vlib/v/checker/tests/assign_multi_immutable_err.vv:4:2: error: `a` is immutable,
4 | a, b = 1, 2
| ^
5 |
6 | println('$a, $b')
6 | println('$a, $b')
vlib/v/checker/tests/assign_multi_immutable_err.vv:18:5: error: cannot assign to function `error`
16 |
17 | fn assign_fn() {
18 | _, error = g()
| ~~~~~
19 | g = f()
20 | }
vlib/v/checker/tests/assign_multi_immutable_err.vv:19:2: error: cannot assign to function `g`
17 | fn assign_fn() {
18 | _, error = g()
19 | g = f()
| ^
20 | }
21 |

View File

@ -4,4 +4,18 @@ fn main() {
a, b = 1, 2
println('$a, $b')
}
}
fn f() int {
return 2
}
fn g() (int, int) {
return 1, 2
}
fn assign_fn() {
_, error = g()
g = f()
}