checker: do not allow dereferencing function calls on the left side of an =

pull/10430/head
Alexander Medvednikov 2021-06-11 16:07:41 +03:00
parent daeeaef030
commit 6761697088
4 changed files with 24 additions and 1 deletions

View File

@ -3266,8 +3266,15 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
is_decl := node.op == .decl_assign is_decl := node.op == .decl_assign
for i, left in node.left { for i, left in node.left {
if left is ast.CallExpr { if left is ast.CallExpr {
// ban `foo() = 10`
c.error('cannot call function `${left.name}()` on the left side of an assignment', c.error('cannot call function `${left.name}()` on the left side of an assignment',
left.pos) left.pos)
} else if left is ast.PrefixExpr {
// ban `*foo() = 10`
if left.right is ast.CallExpr && left.op == .mul {
c.error('cannot dereference a function call on the left side of an assignment, use a temporary variable',
left.pos)
}
} else if left is ast.IndexExpr { } else if left is ast.IndexExpr {
if left.index is ast.RangeExpr { if left.index is ast.RangeExpr {
c.error('cannot reassign using range expression on the left side of an assignment', c.error('cannot reassign using range expression on the left side of an assignment',

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/assign_deref_fn_call_on_left_side_err.vv:8:2: error: cannot dereference a function call on the left side of an assignment, use a temporary variable
6 |
7 | fn main() {
8 | *foo('s') = 1
| ^
9 | }

View File

@ -0,0 +1,9 @@
struct Foo{}
fn foo(s string) &Foo {
return &Foo{}
}
fn main() {
*foo('s') = 1
}

View File

@ -50,6 +50,7 @@ static inline void __sort_ptr(uintptr_t a[], bool b[], int l) {
' '
const c_common_macros = ' const c_common_macros = '
typedef unsigned char u8;
#define EMPTY_VARG_INITIALIZATION 0 #define EMPTY_VARG_INITIALIZATION 0
#define EMPTY_STRUCT_DECLARATION #define EMPTY_STRUCT_DECLARATION
#define EMPTY_STRUCT_INITIALIZATION #define EMPTY_STRUCT_INITIALIZATION