Skip to content

Commit

Permalink
types: fix reporting of self in record functions
Browse files Browse the repository at this point in the history
Fixes #884.
  • Loading branch information
hishamhm committed Dec 31, 2024
1 parent 5da9b91 commit fd5c41d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions spec/api/get_types_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,39 @@ describe("tl.get_types", function()
assert(ti ~= ti.ref)
assert.same(ti_ref.str, "Operator")
end)

it("reports self of a record function (#884)", function()
local env = tl.init_env()
env.report_types = true
local result = assert(tl.check_string([[
local record mod
foo1: function(self)
foo2: function(self)
end
function mod:foo1()
end
function mod.foo2(self: mod)
end
]], env))

local tr, trenv = tl.get_types(result)
assert.same(#tr.symbols, 9)
local syms = {
{ 1, "@{" },
{ 1, "mod" },
{ 6, "@{" },
{ 6, "self" },
{ 7, "@}" },
{ 9, "@{" },
{ 9, "self" },
{ 10, "@}" },
{ 11, "@}" }
}
for i, s in ipairs(tr.symbols) do
assert.same(s[1], syms[i][1])
assert.same(s[3], syms[i][2])
end
end)
end)
3 changes: 3 additions & 0 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12173,6 +12173,9 @@ self:expand_type(node, values, elements) })
args.tuple[1] = a_type(node, "self", { display_type = selftype })
self:add_var(nil, "self", selftype)
self:add_var(nil, "@self", a_type(node, "typedecl", { def = selftype }))
if self.collector then
self.collector.add_to_symbol_list(node.fn_owner, "self", selftype)
end
end

local fn_type = self:ensure_fresh_typeargs(a_function(node, {
Expand Down
3 changes: 3 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -12173,6 +12173,9 @@ do
args.tuple[1] = a_self(node, selftype)
self:add_var(nil, "self", selftype)
self:add_var(nil, "@self", a_typedecl(node, selftype))
if self.collector then
self.collector.add_to_symbol_list(node.fn_owner, "self", selftype)
end
end

local fn_type = self:ensure_fresh_typeargs(a_function(node, {
Expand Down

0 comments on commit fd5c41d

Please sign in to comment.