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

is should imply truthiness for non-boolean non-nil types #878

Closed
euclidianAce opened this issue Dec 20, 2024 · 1 comment · Fixed by #894
Closed

is should imply truthiness for non-boolean non-nil types #878

euclidianAce opened this issue Dec 20, 2024 · 1 comment · Fixed by #894
Labels
nil semantics Unexpected or unsound behaviors

Comments

@euclidianAce
Copy link
Member

(Possibly related to/duplicate of #776, or maybe a special case of it?)

Consider the following:

local record R end

local function convert(_: string): R end

local u: string | R
local _r: R = u is R
	and u
	or convert(u)
$ tl check example.tl
example.tl:8:13: argument 1: got string | R, expected string

The only case where the argument to convert will not be a string is when u itself is nil. But since u is R and u (-> type(u) == "table" and u) will never produce a nil, convert's argument is a string (well, string | nil).

In fact this error can be avoided by using and assert(u) instead of just and u.

I haven't dug around much in the fact inference code so I'm not sure how feasible it is to say a given variable should be truthy in a given context. Also with custom is implementations it may not be as simple as this, but it would be nice to not need a runtime assert for this.

@hishamhm
Copy link
Member

hishamhm commented Jan 2, 2025

local _r: R = u is R
	and u
	or convert(u)

To make this work (i.e, given a , we would have to special-case the var is T and var or __ construct.

If the and part contains anything other than the plain variable tested with is, that part may be nil, and then the or part would still run when var is T (as shown in #776), so if var was of the type T | U, we can't assume U in the or part if the and part is an arbitrary expression. But we could for this special-cased construct you described.

@hishamhm hishamhm added the semantics Unexpected or unsound behaviors label Jan 3, 2025
hishamhm added a commit that referenced this issue Jan 3, 2025
Only for "truthy types" (i.e. all except boolean and nil).

Closes #878.
@hishamhm hishamhm added the nil label Jan 3, 2025
hishamhm added a commit that referenced this issue Jan 3, 2025
Only for "truthy types" (i.e. all except boolean and nil).

Closes #878.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nil semantics Unexpected or unsound behaviors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants