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

[nil] union type does not specialize with and-or idiom #776

Open
hishamhm opened this issue Aug 5, 2024 · 1 comment
Open

[nil] union type does not specialize with and-or idiom #776

hishamhm opened this issue Aug 5, 2024 · 1 comment
Labels
semantics Unexpected or unsound behaviors

Comments

@hishamhm
Copy link
Member

hishamhm commented Aug 5, 2024

do
   local record vers
      record NestedVersion
         string: string
      end
   end

   local type AliasVersion = vers.NestedVersion

   local cv: AliasVersion | string
   local a = cv is vers.NestedVersion and cv.string or cv
   local b = cv is AliasVersion and cv.string or cv
end

do
   local record NotNestedVersion
      string: string
   end

   local cv: NotNestedVersion | string
   local a = cv is NotNestedVersion and cv.string or cv
end

Local variables are not being inferred to string, even though both sides of the and-or "ternary" resolve to string.

@hishamhm hishamhm added the semantics Unexpected or unsound behaviors label Aug 5, 2024
@hishamhm hishamhm changed the title [next] union type does not specialize with and-or idiom union type does not specialize with and-or idiom Sep 2, 2024
@hishamhm
Copy link
Member Author

hishamhm commented Sep 2, 2024

Local variables are not being inferred to string, even though both sides of the and-or "ternary" resolve to string.

This is not true. Consider the case where cv is a record, but cv.string resolves to nil:

   local cv: AliasVersion | string = {}
   local a = cv is vers.NestedVersion and cv.string or cv
   local b = cv is AliasVersion and cv.string or cv

   print(type(a)) --> "table"
   print(type(b)) --> "table"

This can only be solved when nil is handled separately from other types. If we can guarantee that cv.string is a non-nil string, then we'd know for a fact that all types other than nil and boolean are always truthy, and then we could propagate the inference confidently.

@hishamhm hishamhm changed the title union type does not specialize with and-or idiom [nil] union type does not specialize with and-or idiom Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semantics Unexpected or unsound behaviors
Projects
None yet
Development

No branches or pull requests

1 participant