checker: forbid non-reference mut arg or receiver of go function
							parent
							
								
									993cd1467b
								
							
						
					
					
						commit
						8fe70a24a8
					
				|  | @ -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 {} | ||||
|  |  | |||
|  | @ -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 | } | ||||
|  | @ -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) | ||||
| } | ||||
|  | @ -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 | } | ||||
|  | @ -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() | ||||
| } | ||||
		Loading…
	
		Reference in New Issue