Skip to content

Order-of-operations confusion when using augmented assignment and length operator #457

Open
@Grissess

Description

@Grissess

Hello!

I was just bitten by a curious little code generation bug: when using an augmented assignment with the length operator, Lua is generated that doesn't follow the order of operations. It's a bit odd, because the generated Lua does follow the order of operations if the length operator isn't involved. Here's a minimal reproduction:

count, minuend = 3, 2
count -= minuend + 1
print count

count, datum = 3, "ab"
count -= #datum + 1
print count

When using MoonScript version 0.5.0, moonc generates this:

local count, minuend = 3, 2
count = count - (minuend + 1)
print(count)
local datum
count, datum = 3, "ab"
count = count - #datum + 1
return print(count)

which, in Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio, prints this when interpreted:

0
2

This behavior is a bit unexpected, as assignment (including augmented assignment) is supposed to have the lowest precedence in ANSI C (apologies for the C++ ref, but the same table is in my book), and I assume the operators here introduced are supposed to be semantically compatible with that interpretation. Indeed, if that's the case, the count = count - #datum + 1 is incorrect by the standard, and it's in general safe to always convert LHS $= RHS to LHS = LHS $ (RHS) for all operators $.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions