1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: disallow invalid prefix on left side of assign stmt (#18750)

This commit is contained in:
Swastik Baranwal 2023-07-03 21:24:23 +05:30 committed by GitHub
parent df3c85eb36
commit ab258aebfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 1 deletions

View File

@ -392,6 +392,11 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
left.right.obj.is_used = true
}
}
} else if left.op == .amp {
c.error('cannot use a reference on the left side of `${node.op}`',
left.pos)
} else {
c.error('cannot use `${left.op}` on the left of `${node.op}`', left.pos)
}
if is_decl {
c.error('non-name on the left side of `:=`', left.pos)

View File

@ -0,0 +1,14 @@
vlib/v/checker/tests/invalid_prefix_left_side_assign_stmt_err.vv:3:2: error: cannot use `~` on the left of `=`
1 | fn main() {
2 | mut x := 0
3 | ~x = 34
| ^
4 | &x = unsafe { nil }
5 | println(x)
vlib/v/checker/tests/invalid_prefix_left_side_assign_stmt_err.vv:4:2: error: cannot use a reference on the left side of `=`
2 | mut x := 0
3 | ~x = 34
4 | &x = unsafe { nil }
| ^
5 | println(x)
6 | }

View File

@ -0,0 +1,6 @@
fn main() {
mut x := 0
~x = 34
&x = unsafe { nil }
println(x)
}

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/prefix_expr_decl_assign_err.vv:2:5: error: non-name on the left side of `:=`
vlib/v/checker/tests/prefix_expr_decl_assign_err.vv:2:5: error: cannot use a reference on the left side of `:=`
1 | fn main() {
2 | &a := 12
| ^