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

checker: forbid non-reference mut arg or receiver of go function

This commit is contained in:
Uwe Krüger 2020-06-26 23:31:38 +02:00 committed by GitHub
parent 993cd1467b
commit 8fe70a24a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 0 deletions

View File

@ -1826,6 +1826,19 @@ fn (mut c Checker) stmt(node ast.Stmt) {
c.error('expression in `go` must be a function call', it.call_expr.position())
}
c.expr(it.call_expr)
if it.call_expr is ast.CallExpr {
call_expr := it.call_expr as ast.CallExpr
// Make sure there are no mutable arguments
for arg in call_expr.args {
if arg.is_mut && !arg.typ.is_ptr() {
c.error('function in `go` statement cannot contain mutable non-reference arguments', arg.expr.position())
}
}
if call_expr.is_method && call_expr.receiver_type.is_ptr() && !call_expr.left_type.is_ptr() {
c.error('method in `go` statement cannot have non-reference mutable receiver', call_expr.left.position())
}
}
}
// ast.HashStmt {}
ast.Import {}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/go_mut_arg.v:14:16: error: function in `go` statement cannot contain mutable non-reference arguments
12 | a: 0
13 | }
14 | go change(mut x)
| ^
15 | }

View File

@ -0,0 +1,15 @@
struct St {
mut:
a int
}
fn change(mut a St) {
a.a++
}
fn main() {
mut x := St{
a: 0
}
go change(mut x)
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/go_mut_receiver.v:14:5: error: method in `go` statement cannot have non-reference mutable receiver
12 | a: 0
13 | }
14 | go x.change()
| ^
15 | }

View File

@ -0,0 +1,15 @@
struct St {
mut:
a int
}
fn (mut a St) change() {
a.a++
}
fn main() {
mut x := St{
a: 0
}
go x.change()
}