full-moon icon indicating copy to clipboard operation
full-moon copied to clipboard

Incorrect type intersections

Open jeparlefrancais opened this issue 1 year ago • 2 comments

I'm working on getting Luau types into darklua and I noticed that full-moon does not build some syntax constructs right.

Here is a confusing thing that I derived from the grammar that full-moon does not represent correctly:

-- this reads as a function that returns a variable amount of values typed as either:
--     * `string`
--     * `T`
type Fn = () -> ...string & T
-- issue: full-moon parses it as an intersection of `...string` and `T`

If you slightly change this snippet to something like the following, you get a totally different thing, which is also not represented correctly:

-- this reads as an intersection of:
--    * a function that returns a variable amount of values typed as different types (represented by U...) 
--    * a value of type T
type Fn<U...> = () -> U... & T
-- issue: full-moon parses it as a function that returns a value typed as an intersection of `U...` and `T`

Repro

To verify this, you can use luau-lsp VS code extension to hover over the variables in these examples.

To reproduce the variadic type of an intersection:

type T = { bubble: number }
type Fn = () -> ...string & T

local fn: Fn = nil :: any

-- a, b and c are inferred as `string & T` variables
local a, b, c = fn()

To reproduce the intersection of a function returning a generic type pack and another type:

type T = { bubble: number }
type Fn<U...> = () -> U... & T

local fn: Fn<(string, boolean)> = nil :: any

-- `fn` has a bubble property because of the intersection with `T` 
local bubble = fn.bubble
-- `a` is a string, `b` a boolean and `c` has no type, as given by the type pack given to `Fn`
local a, b, c = fn()

jeparlefrancais avatar Sep 29 '23 17:09 jeparlefrancais

I think all the errors you report should be solved by the parser rewrite. If I get round to it, I'm gonna take another look this weekend. It was blocked on a stack overflow though

JohnnyMorganz avatar Sep 29 '23 22:09 JohnnyMorganz

Can confirm, these are fixed in the parser rewrite, I have added a test case for this

JohnnyMorganz avatar Sep 30 '23 14:09 JohnnyMorganz