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

Regression involving Math Overloads & Intersection Types #1003

Open
AmberGraceSoftware opened this issue Aug 6, 2023 · 2 comments
Open

Regression involving Math Overloads & Intersection Types #1003

AmberGraceSoftware opened this issue Aug 6, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@AmberGraceSoftware
Copy link
Contributor

AmberGraceSoftware commented Aug 6, 2023

I am noticing a regression in the code for the reactive UI library I'm in the process of writing (Dec), which uses a base class called "Observable", and subclasses (like "State") that inherit this base class.

Essentially, in my library, this code sample used to work:

local a = Dec.State(3)
local b = Dec.State(4)
local sum = a + b
print(sum:Current()) -- 7
a:Set(4)
print(sum:Current()) -- 8

But no longer does due to the way I use inheritance.

Code to reproduce this issue:

--!strict
type BaseClass = typeof(setmetatable(
    {},
    ({} :: any) :: {__add: (BaseClass, BaseClass) -> BaseClass})
)
type SubClass = BaseClass & {extraField: string}

-- OK
local function add1(x: BaseClass, y: BaseClass): BaseClass
    return x + y
end

-- OK
local function add2(x: SubClass, y: BaseClass): BaseClass
    return x + y
end

-- OK
local function add3(x: BaseClass, y: SubClass): BaseClass
    return x + y
end

-- Emits an Error: "Type Error: (22,9) Type 't1 & {| extraField: string |} where
-- t1 = { @metatable {| __add: (t1, t1) -> t1 |}, {  } }' could not be converted
-- into 'number'; none of the intersection parts are compatible
local function add4(x: SubClass, y: SubClass): BaseClass
    return x + y
end

The first three examples correctly emit no warnings, as SubClass structurally relates to BaseClass in the overload for the __add metamethod. However, if there is not at least one operands which is exactly equal to BaseClass, even if both operands are structurally relatable to the BaseClass, an error is emitted.

The expected behavior is for all four functions to not emit any static analysis errors.

@AmberGraceSoftware AmberGraceSoftware added the bug Something isn't working label Aug 6, 2023
@AmberGraceSoftware AmberGraceSoftware changed the title Regression involving Math Overloads & Duck Typing Regression involving Math Overloads & Intersection Types Aug 7, 2023
@AmberGraceSoftware
Copy link
Contributor Author

AmberGraceSoftware commented Aug 12, 2023

I simplified the repro example in the original issue. Hopefully this can help in developing tests for fixing this regression and making sure it doesn't happen again.

@AmberGraceSoftware
Copy link
Contributor Author

Made a PR to fix this edge case with #1009 !

Looking at the git blame for the code I edited, it seems like this may have not actually been a recent regression, but rather I failed to notice this being an issue originally? Hard to say.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
1 participant