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

Incorrect type inferred from and operator #730

Closed
centau opened this issue Oct 29, 2022 · 1 comment · Fixed by #742
Closed

Incorrect type inferred from and operator #730

centau opened this issue Oct 29, 2022 · 1 comment · Fixed by #742
Assignees
Labels
bug Something isn't working

Comments

@centau
Copy link

centau commented Oct 29, 2022

The and operator seems to always infer a and b to be of type boolean | typeof(b) which does not reflect the actual runtime values as can be seen below.

image

This allows code like this to check correctly while in practice will return nil instead of false if i is nil.
image

@centau centau added the bug Something isn't working label Oct 29, 2022
@vegorov-rbx vegorov-rbx self-assigned this Nov 1, 2022
@vegorov-rbx
Copy link
Collaborator

We are working on a fix for this, where we will only take nil and false parts of the left-hand side type if they are possible for that type.
So number? and number will still be number? and number? and boolean will be boolean?.

However, the fix might take some time to get here.

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
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