Comments (1)
Here is some context about why this is happening.
There's currently an affordance that indexing a table with an indexer returns a non-nil
type. This is present because (at some point) we believed users would find it too frustrating to have to check for nil
on every index, and we did not have the type refinement and typestate machinery to necessarily support inferring it automatically.
For example,
local x = { 1, 2, 3 }
if x[4] then
fib(x[5])
end
In this case, it's not really clear what behavior is possible with the systems in place that doesn't lead to issues (either DX issues or unsoundness issues). This applies as well to function returns because you can return the results from indexing:
function getFourth<T>(tbl: {T}): T
return tbl[4]
end
The current design point we're at for the new solver here is that indexers (and functions, as a result) can return nil
without the type reflecting that, but we want people to be able to still write guards which require them to be able to compare to nil
, and so, in conditional contexts, the type of function results and indexing results include implicit nil
possibilities.
This lets you still write a check like...
if x[4] ~= nil then
use(x[4])
end
without requiring refinements to be able to support a refinement per-individual-index and so forth.
All that context being said, it seems like the bug here is that it, for whatever reason, is continuing into the ensuing scope which is unexpected. The type shouldn't be changed, only temporarily viewed as containing nil
in a conditional context.
from luau.
Related Issues (20)
- "Free Types and {- -}" causes the "autocomplete type and owning area MainModule" to be appended in the old type solver, and don't work in the new type solver HOT 1
- Unable to Table Assign and Locally Assign Variables at the Same Time While Unpacking HOT 2
- Issue with tabs and the Luau Lexer. HOT 2
- adding @inline tag HOT 1
- [COMPILER OPTIMIZATION] Simplify Conditional Logic Expressions
- "attempt to call a nil value" error message could be improved HOT 1
- Underflow for column count in luau-analyze involving a syntax and return type error in function
- `Luau::findAstAncestryOfPosition` returns short ancestry for global functions with empty blocks
- end keyword doesn't show up in autocomplete suggestions when using a return statement
- Vector XYZW fast path not supported for vectors within immutable globals HOT 1
- Allow removing globals in type definition files
- typeof() not functioning correctly for lightuserdata with a metatable
- Some compiler warnings HOT 9
- Include version in CLI output
- Casting with table indexer doesn't error with new solver
- Type state prevents table key from being changed in new solver
- Reassigning table upvalue errors with new solver
- bad error message for incompatible function types
- Generic function not identifying variadic function parameters correctly
- Create a solution for `__iter` dropping the extra information passed to for loops
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from luau.