Skip to content

Commit

Permalink
fix: resolve generic __call metamethod
Browse files Browse the repository at this point in the history
Fixes #904.

This reiterates the need for a better split of the various
Type types (see comment in 100748b).
  • Loading branch information
hishamhm committed Jan 13, 2025
1 parent 93e94a1 commit 32e5370
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions spec/lang/metamethods/call_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ describe("metamethod __call", function()
{ msg = "argument 2: got Rec, expected number" },
}))

it("works with generic function (#904)", util.check([[
local record Rec
record Nest<T>
field: T
end
metamethod __call: function<T>(self, Nest<T>):T
end
local function create_rec(): Rec
return setmetatable({}, {
__call = function<T>(_, Nest: Rec.Nest<T>):T
return Nest.field
end
})
end
local rec = create_rec()
local value = rec({
field = 5
})
print("value = " .. tostring(value))
]]))

it("cannot be typechecked if the metamethod is not defined in the record", util.check_type_error([[
local type Rec = record
x: number
Expand Down
4 changes: 4 additions & 0 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9591,6 +9591,10 @@ a.types[i], b.types[i]), }
is_method = true
end

if func.typename == "generic" then
func = self:apply_generic(func, func)
end

return func, is_method
end

Expand Down
4 changes: 4 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -9591,6 +9591,10 @@ do
is_method = true
end

if func is GenericType then
func = self:apply_generic(func, func)
end

return func, is_method
end

Expand Down

0 comments on commit 32e5370

Please sign in to comment.