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

new solver doing weird union stuff when calling functions #1484

Open
LiterallyWize opened this issue Oct 23, 2024 · 3 comments
Open

new solver doing weird union stuff when calling functions #1484

LiterallyWize opened this issue Oct 23, 2024 · 3 comments
Assignees
Labels
bug Something isn't working new solver This issue is specific to the new solver.

Comments

@LiterallyWize
Copy link

this is the simplest repro i could do of this

script:
--!strict

local module = require(script.module)

local func = nil

function start()
	if func then return end
	func = module()
end

function stop()
	if not func then return end
	func()
end
script.module:
function func2() -- 2nd function which is returned
	
end

function func1() -- 1st function which is initially called
	return func2
end

return func1

on line 14 of script, when calling func() the following warning occurs:

(((...any) -> ())?) & (buffer 
| class
| function 
| number
| string
| table
| thread
| true)
Cannot call a value of type (((...any) -> ())?) & (buffer | class | function | number | string | table | thread | true)

apparently it doesn't see it as just a function and fails to call it unless i cast func to () -> () on line 9 of script:

func = module() :: () -> ()
@LiterallyWize LiterallyWize added the bug Something isn't working label Oct 23, 2024
@biseson
Copy link

biseson commented Oct 23, 2024

The system is inferring a broader type for func, which is why it produces that union type in the error message when you try to call it. Your current workaround of casting func to () -> () is a valid way to resolve the issue; telling the type checker explicitly that func is a function.

You can declare func's type at the top of the script to avoid the ambiguity. So, explicitly stating that func is either nil or a function takes any number of args and returns nothing (a.k.a. () -> ()):

--!strict

local module = require(script.module)

local func: (() -> ())? = nil -- declare func's type explicitly

function start()
    if func then return end
    func = module()
end

function stop()
    if not func then return end
    func() -- no need to cast here anymore
end

@aatxe aatxe added the new solver This issue is specific to the new solver. label Oct 24, 2024
@aatxe
Copy link
Collaborator

aatxe commented Oct 24, 2024

The underlying problem here is that type normalization does not seem to handle converting ((...any) -> ()) | nil intersected with ~(false | nil) (the negation expands to that big union you're seeing, which is sort of an orthogonal DX concern) into the particular function type.

@aatxe aatxe self-assigned this Oct 29, 2024
@LiterallyWize
Copy link
Author

The system is inferring a broader type for func, which is why it produces that union type in the error message when you try to call it. Your current workaround of casting func to () -> () is a valid way to resolve the issue; telling the type checker explicitly that func is a function.

You can declare func's type at the top of the script to avoid the ambiguity. So, explicitly stating that func is either nil or a function takes any number of args and returns nothing (a.k.a. () -> ()):

--!strict

local module = require(script.module)

local func: (() -> ())? = nil -- declare func's type explicitly

function start()
    if func then return end
    func = module()
end

function stop()
    if not func then return end
    func() -- no need to cast here anymore
end

doing this was actually the first idea i had but oddly the same warning occurs, i should've mentioned that before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working new solver This issue is specific to the new solver.
Development

No branches or pull requests

3 participants