Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next function incorrectly typed #706

Closed
fewkz opened this issue Oct 13, 2022 · 4 comments · Fixed by #742
Closed

next function incorrectly typed #706

fewkz opened this issue Oct 13, 2022 · 4 comments · Fixed by #742
Assignees
Labels
bug Something isn't working

Comments

@fewkz
Copy link

fewkz commented Oct 13, 2022

The following code gets past luau-analyze even though foo prints nil instead of a number, due to next's return type not including nil

local foo: number = next({})
print(foo) -- prints nil
@fewkz fewkz added the bug Something isn't working label Oct 13, 2022
@vegorov-rbx vegorov-rbx self-assigned this Oct 18, 2022
@vegorov-rbx
Copy link
Collaborator

We are preparing a fix for this issue internally, hopefully it gets fixed in one of the upcoming releases.

There are some limitations of the current Luau inference engine that make this more complicated than it should be, but we have a work-around that should cover most cases.

vegorov-rbx added a commit that referenced this issue Nov 10, 2022
* Type inference of `a and b` and `a or b` has been improved (Fixes
#730)
* Improved error message when `for ... in x` loop iterates over a value
that could be 'nil'
* Return type of `next` not includes 'nil' (Fixes
#706)
* Improved type inference of `string` type
* Luau library table type names are now reported as `typeof(string)`/etc
instead of just `string` which was misleading
* Added parsing error when optional type alias type parameter wasn't
provided after `=` token
* Improved tagged union type refinement in conditional expressions, type
in `else` branch should no longer include previously handled union
options
@fewkz
Copy link
Author

fewkz commented Nov 13, 2022

I've tested in luau 0.553 and the code still gets past luau-analyze without any type errors. It doesn't appear to be fixed

local foo: number = next({"a"})

@vegorov-rbx
Copy link
Collaborator

Unfortunately, flag for this fix was temporarily marked as an experimental flag.

  1. You can run luau-analyze --fflags=LuauOptionalNextKey=true to get this check
  2. You can remove this flag from list in Common\include\Luau\ExperimentalFlags.h and rebuild lua-analyze

This fix should be enabled by default in the 0.554 release.

@vegorov-rbx
Copy link
Collaborator

0.554 release is now available and this fix is no longer 'experimental'.

RomanKhafizianov pushed a commit to RomanKhafizianov/luau that referenced this issue Nov 27, 2023
* Type inference of `a and b` and `a or b` has been improved (Fixes
luau-lang/luau#730)
* Improved error message when `for ... in x` loop iterates over a value
that could be 'nil'
* Return type of `next` not includes 'nil' (Fixes
luau-lang/luau#706)
* Improved type inference of `string` type
* Luau library table type names are now reported as `typeof(string)`/etc
instead of just `string` which was misleading
* Added parsing error when optional type alias type parameter wasn't
provided after `=` token
* Improved tagged union type refinement in conditional expressions, type
in `else` branch should no longer include previously handled union
options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants