Skip to content

Commit

Permalink
fix: can assign an emptytable to an emptytable
Browse files Browse the repository at this point in the history
This should also avoid inferring types as `{} | {}`.
  • Loading branch information
hishamhm committed Sep 19, 2024
1 parent 58ebd4b commit af5aed5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/declaration/union_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,12 @@ describe("union declaration", function()
{ msg = "cannot discriminate a union between multiple function types" },
}))

it("collapses multiple emptytables on declaration", util.check([[
local function count_sea_monsters(image: {string})
local c, m, n = 0, {{}, {}}, 0
for row = 1, #image - 2 do
m[row + 2] = {}
end
end
]]))
end)
13 changes: 13 additions & 0 deletions spec/inference/emptytable_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,17 @@ describe("empty table without type annotation", function()
end
]]))

it("can assign an emptytable to an emptytable", util.check([[
local x = {}
for i = 1, 20 do
if math.random(2) == 1 then
x = {}
else
x = {"hello"}
end
print(#x)
end
]]))

end)
1 change: 1 addition & 0 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8476,6 +8476,7 @@ do


local emptytable_relations = {
["emptytable"] = compare_true,
["array"] = compare_true,
["map"] = compare_true,
["tupletable"] = compare_true,
Expand Down
1 change: 1 addition & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -8476,6 +8476,7 @@ do

-- emptytable rules are the same in eqtype_relations and subtype_relations
local emptytable_relations: {TypeName:CompareTypes} = {
["emptytable"] = compare_true,
["array"] = compare_true,
["map"] = compare_true,
["tupletable"] = compare_true,
Expand Down

0 comments on commit af5aed5

Please sign in to comment.