Merge remote-tracking branch 'origin/master' into merge

This commit is contained in:
Rob Blanckaert 2022-06-09 18:24:58 -07:00
commit 44a5cb6d52
5 changed files with 10 additions and 123 deletions

View File

@ -6,6 +6,7 @@
#include <type_traits>
#include <initializer_list>
#include <stddef.h>
#include <utility>
namespace Luau
{

View File

@ -1018,18 +1018,20 @@ static int luauF_tunpack(lua_State* L, StkId res, TValue* arg0, int nresults, St
static int luauF_vector(lua_State* L, StkId res, TValue* arg0, int nresults, StkId args, int nparams)
{
#if LUA_VECTOR_SIZE == 4
if (nparams >= 4 && nresults <= 1 && ttisnumber(arg0) && ttisnumber(args) && ttisnumber(args + 1) && ttisnumber(args + 2))
#else
if (nparams >= 3 && nresults <= 1 && ttisnumber(arg0) && ttisnumber(args) && ttisnumber(args + 1))
#endif
{
double x = nvalue(arg0);
double y = nvalue(args);
double z = nvalue(args + 1);
#if LUA_VECTOR_SIZE == 4
double w = nvalue(args + 2);
double w = 0.0;
if (nparams >= 4)
{
if (!ttisnumber(args + 2))
return -1;
w = nvalue(args + 2);
}
setvvalue(res, float(x), float(y), float(z), float(w));
#else
setvvalue(res, float(x), float(y), float(z), 0.0f);

View File

@ -15,26 +15,12 @@ This document tracks unimplemented RFCs.
**Status**: Needs implementation
## Safe navigation operator
[RFC: Safe navigation postfix operator (?)](https://github.com/Roblox/luau/blob/master/rfcs/syntax-safe-navigation-operator.md)
**Status**: Needs implementation.
**Notes**: We have unresolved issues with interaction between this feature and Roblox instance hierarchy. This may affect the viability of this proposal.
## String interpolation
[RFC: String interpolation](https://github.com/Roblox/luau/blob/master/rfcs/syntax-string-interpolation.md)
**Status**: Needs implementation
## Generalized iteration
[RFC: Generalized iteration](https://github.com/Roblox/luau/blob/master/rfcs/generalized-iteration.md)
**Status**: Implemented but not fully rolled out yet.
## Lower Bounds Calculation
[RFC: Lower bounds calculation](https://github.com/Roblox/luau/blob/master/rfcs/lower-bounds-calculation.md)

View File

@ -1,5 +1,7 @@
# Generalized iteration
**Status**: Implemented
## Summary
Introduce support for iterating over tables without using `pairs`/`ipairs` as well as a generic customization point for iteration via `__iter` metamethod.

View File

@ -1,104 +0,0 @@
# Safe navigation postfix operator (?)
**Note**: We have unresolved issues with interaction between this feature and Roblox instance hierarchy. This may affect the viability of this proposal.
## Summary
Introduce syntax to navigate through `nil` values, or short-circuit with `nil` if it was encountered.
## Motivation
nil values are very common in Lua, and take care to prevent runtime errors.
Currently, attempting to index `dog.name` while caring for `dog` being nil requires some form of the following:
```lua
local dogName = nil
if dog ~= nil then
dogName = dog.name
end
```
...or the unusual to read...
```lua
local dogName = dog and dog.name
```
...which will return `false` if `dog` is `false`, instead of throwing an error because of the index of `false.name`.
Luau provides the if...else expression making this turn into:
```lua
local dogName = if dog == nil then nil else dog.name
```
...but this is fairly clunky for such a common expression.
## Design
The safe navigation operator will make all of these smooth, by supporting `x?.y` to safely index nil values. `dog?.name` would resolve to `nil` if `dog` was nil, or the name otherwise.
The previous example turns into `local dogName = dog?.name` (or just using `dog?.name` elsewhere).
Failing the nil-safety check early would make the entire expression nil, for instance `dog?.body.legs` would resolve to `nil` if `dog` is nil, rather than resolve `dog?.body` into nil, then turning into `nil.legs`.
```lua
dog?.name --[[ is the same as ]] if dog == nil then nil else dog.name
```
The short-circuiting is limited within the expression.
```lua
dog?.owner.name -- This will return nil if `dog` is nil
(dog?.owner).name -- `(dog?.owner)` resolves to nil, of which `name` is then indexed. This will error at runtime if `dog` is nil.
dog?.legs + 3 -- `dog?.legs` is resolved on its own, meaning this will error at runtime if it is nil (`nil + 3`)
```
The operator must be used in the context of either a call or an index, and so:
```lua
local value = x?
```
...would be invalid syntax.
This syntax would be based on expressions, and not identifiers, meaning that `(x or y)?.call()` would be valid syntax.
### Type
If the expression is typed as an optional, then the resulting type would be the final expression, also optional. Otherwise, it'll just be the resulting type if `?` wasn't used.
```lua
local optionalObject: { name: string }?
local optionalObjectName = optionalObject?.name -- resolves to `string?`
local nonOptionalObject: { name: string }
local nonOptionalObjectName = nonOptionalObject?.name -- resolves to `string`
```
### Calling
This RFC only specifies `x?.y` as an index method. `x?:y()` is currently unspecified, and `x?.y(args)` as a syntax will be reserved (will error if you try to use it).
While being able to support `dog?.getName()` is useful, it provides [some logistical issues for the language](https://github.com/Roblox/luau/pull/142#issuecomment-990563536).
`x?.y(args)` will be reserved both so that this can potentially be resolved later down the line if something comes up, but also because it would be a guaranteed runtime error under this RFC: `dog?.getName()` will first index `dog?.getName`, which will return nil, then will attempt to call it.
### Assignment
`x?.y = z` is not supported, and will be reported as a syntax error.
## Drawbacks
As with all syntax additions, this adds complexity to the parsing of expressions, and the execution of cancelling the rest of the expression could prove challenging.
Furthermore, with the proposed syntax, it might lock off other uses of `?` within code (and not types) for the future as being ambiguous.
## Alternatives
Doing nothing is an option, as current standard if-checks already work, as well as the `and` trick in other use cases, but as shown before this can create some hard to read code, and nil values are common enough that the safe navigation operator is welcome.
Supporting optional calls/indexes, such as `x?[1]` and `x?()`, while not out of scope, are likely too fringe to support, while adding on a significant amount of parsing difficulty, especially in the case of shorthand function calls, such as `x?{}` and `x?""`.
It is possible to make `x?.y = z` resolve to only setting `x.y` if `x` is nil, but assignments silently failing can be seen as surprising.