Skip to content

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

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 +1print count
count, datum =3,"ab"
count -=#datum +1print count

When using MoonScript version 0.5.0, moonc generates this:

localcount, minuend=3, 2count=count- (minuend+1)
print(count)
localdatumcount, datum=3, "ab"count=count-#datum+1returnprint(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