Skip to content

**= and //= evaluate complex targets twice #42

@ylluminate

Description

@ylluminate

**= and //= expand to target = func(target, val), which evaluates the target expression twice. Fine for simple variables, but wrong if the target has side effects.

def f():
    print("called")
    return 0
arr = [1, 2, 3]
arr[f()] **= 2

transpiles to:

arr[f()] = math.powi(arr[f()], 2)

f() gets called twice. Should use a temp variable for complex targets:

tmp := f()
arr[tmp] = math.powi(arr[tmp], 2)

Only affects **= and //= since those are the operators that expand to function calls. Regular +=, -= etc. use V's native operators and don't have this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions