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

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

This commit is contained in:
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
for i, left in node.left {
if left is ast.CallExpr {
// ban `foo() = 10`
c.error('cannot call function `${left.name}()` on the left side of an assignment',
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 {
if left.index is ast.RangeExpr {
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 = '
typedef unsigned char u8;
#define EMPTY_VARG_INITIALIZATION 0
#define EMPTY_STRUCT_DECLARATION
#define EMPTY_STRUCT_INITIALIZATION
@ -194,7 +195,7 @@ const c_helper_macros = '//============================== HELPER C MACROS ======
//#define tos4(s, slen) ((string){.str=(s), .len=(slen)})
// _SLIT0 is used as NULL string for literal arguments
// `"" s` is used to enforce a string literal argument
#define _SLIT0 (string){.len=0}
#define _SLIT0 (string){.len=0}
#define _SLIT(s) ((string){.str=(byteptr)("" s), .len=(sizeof(s)-1), .is_lit=1})
//#define _SLIT(s) ((string){.str=(byteptr)("" s), .len=(sizeof(s)-1), .is_lit=1})
// take the address of an rvalue