lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, 15 Nov 2018 at 11:55, Philippe Verdy <verdy_p@wanadoo.fr> wrote:
>
>
>
> Le mer. 14 nov. 2018 à 21:55, Dibyendu Majumdar <mobile@majumdar.org.uk> a écrit :
>>
>> On Wed, 14 Nov 2018 at 19:55, Egor Skriptunoff
>> <egor.skriptunoff@gmail.com> wrote:
>> >
>> >
>> > OK, let's test it!
>> >
>> > 1)
>> > A bug: correct Lua 5.3 program doesn't run on Ravi.
>> >    C:\Software\ravi\bin>ravi -e"for x = 1, 2 do x = 42.0 end"
>> >    ravi: (command line):1: Invalid assignment: integer expected near 'end'
>> >
>>
>> In this case Ravi is deciding that x is an integer type and hence
>> doesn't like that you are assigning a floating point value.
>
>
> That's because it attempts to optimize the integer loop unsafely: when compiling x=42.0, which changes the loop variable, it forgets to evaluate the loop condition (x<=42) which would normally imply that this assignment to x causes a break to the loop; but as this x variable is local to the loop and this assigned value is then not used after breaking, the assignment has no other effect than breaking the loop, so this code would be nearly like "for x = 1, 2 do break end" i.e. it will do nothing if compiled properly; if the code is interpreted, the for loop will be executed only once (local x=1 when entering the first loop which sets x=42.0, and exiting immediately which deletes the x variable).
> There is code where modifying the loop control variable is perfectly valid, even if in general this is done by assigning a value of the same type.

Hi, to be honest the for num loop variable should be treated as
'readonly' inside the loop body as it is a copy of the real index
variable, and changing it inside the body doesn't affect the loop, and
is just bad practice in my view. Anyway I take the liberty of
optimising the for num index here, and require that the variable is
not assigned a non-integer value. I would forbid assignment but this
is harder to do.

Regards
Dibyendu