Skip to content

Recap: May 2022

This month Luau team has worked to bring you a new language feature together with more typechecking improvements and bugfixes!

Generalized iteration

We have extended the semantics of standard Lua syntax for iterating through containers, for vars in values with support for generalized iteration.
In Lua, to iterate over a table you need to use an iterator like next or a function that returns one like pairs or ipairs. In Luau, you can now simply iterate over a table:

This works for tables but can also be customized for tables or userdata by implementing __iter metamethod. It is called before the iteration begins, and should return an iterator function like next (or a custom one):

The default iteration order for tables is specified to be consecutive for elements 1..#t and unordered after that, visiting every element.
Similar to iteration using pairs, modifying the table entries for keys other than the current one results in unspecified behavior.

Typechecking improvements

We have added a missing check to compare implicit table keys against the key type of the table indexer:

Rules for == and ~= have been relaxed for union types, if any of the union parts can be compared, operation succeeds:

Table value type propagation now correctly works with [any] key type:

If a generic function doesn’t provide type annotations for all arguments and the return value, additional generic type parameters might be added automatically:

We have also fixed various issues that have caused crashes, with many of them coming from your bug reports.

Linter improvements

GlobalUsedAsLocal lint warning has been extended to notice when global variable writes always happen before their use in a local scope, suggesting that they can be replaced with a local variable:

Performance improvements

Garbage collection CPU utilization has been tuned to further reduce frame time spikes of individual collection steps and to bring different GC stages to the same level of CPU utilization.

Returning a type-cast local (return a :: type) as well as returning multiple local variables (return a, b, c) is now a little bit more efficient.

Function inlining and loop unrolling

In the open-source release of Luau, when optimization level 2 is enabled, the compiler will now perform function inlining and loop unrolling.

Only loops with loop bounds known at compile time, such as for i=1,4 do, can be unrolled. The loop body must be simple enough for the optimization to be profitable; compiler uses heuristics to estimate the performance benefit and automatically decide if unrolling should be performed.

Only local functions (defined either as local function foo or local foo = function) can be inlined. The function body must be simple enough for the optimization to be profitable; compiler uses heuristics to estimate the performance benefit and automatically decide if each call to the function should be inlined instead. Additionally recursive invocations of a function can’t be inlined at this time, and inlining is completely disabled for modules that use getfenv/setfenv functions.