luau icon indicating copy to clipboard operation
luau copied to clipboard

Syntax error when no parens around function union return type

Open JohnnyMorganz opened this issue 2 years ago • 1 comments

I am not sure if this is intentional

type X = () -> () | () -> () -- no syntax error

local function foo(): (() -> () | () -> ()) -- no syntax error
	return function() end
end

local function bar(): X -- no error
	return function() end
end


local function baz(): () -> () | () -> () -- syntax error
	return function() end
end

JohnnyMorganz avatar Sep 03 '23 15:09 JohnnyMorganz

I think it's because the union is interpreted as being a union of the return type of the first function type (instead of a return type of the main function baz). Though I'm unure why the same error doesn't occur with type X

local function boo(): () -> (string) | () -> () -- Interpreted as boo(): () -> (string | () -> ())
    return function() end -- Must return either a string or a function
end
local function far(): () -> () | T
-- is interpreted as...
local function far(): () -> (() | T) -- Nested empty parens causes syntax error

Interestingly, the problem is resolved by adding parens around only the first function

local function faz(): (() -> ()) | () -> () -- No syntax error and is interpreted intuitively (returns either a function or a function)
    return function() end
end

BizzarBlitz avatar Sep 06 '23 15:09 BizzarBlitz