Skip to content

Commit

Permalink
fix: check if number of types in declaration exceeds number of variables
Browse files Browse the repository at this point in the history
Fixes #868.
  • Loading branch information
hishamhm committed Dec 31, 2024
1 parent 44ced6a commit 43def7f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
14 changes: 14 additions & 0 deletions spec/lang/declaration/local_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,18 @@ describe("local", function()
{ y = 9, msg = "_bar1: got number, expected Bar" },
{ y = 10, msg = "_bar2: Foo is not a Bar" },
}))

it("catches excessive types in declaration (regression test for #868)", util.check_type_error([[
local function f(): string, string
return "hello", "world"
end
local x, y: integer, string, string = 1, ""
local z, w = 0, f()
z, w = 0, f()
]], {
{ y = 5, x = 36, msg = "number of types exceeds number of variables" },
}))
end)
12 changes: 10 additions & 2 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10908,7 +10908,7 @@ a.types[i], b.types[i]), }
end
end

local function set_expected_types_to_decltuple(_, node, children)
local function set_expected_types_to_decltuple(self, node, children)
local decltuple = node.kind == "assignment" and children[1] or node.decltuple
assert(decltuple.typename == "tuple")
local decls = decltuple.tuple
Expand All @@ -10919,7 +10919,7 @@ a.types[i], b.types[i]), }
local typ
typ = decls[i]
if typ then
if i == nexps and ndecl > nexps then
if node.kind == "assignment" and i == nexps and ndecl > nexps then
typ = a_type(node, "tuple", { tuple = {} })
for a = i, ndecl do
table.insert(typ.tuple, decls[a])
Expand All @@ -10930,6 +10930,14 @@ a.types[i], b.types[i]), }
end
end
end

if node.decltuple then
local ndecltuple = #node.decltuple.tuple
local nvars = #node.vars
if ndecltuple > nvars then
self.errs:add(node.decltuple.tuple[nvars + 1], "number of types exceeds number of variables")
end
end
end

local function is_positive_int(n)
Expand Down
12 changes: 10 additions & 2 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -10908,7 +10908,7 @@ do
end
end

local function set_expected_types_to_decltuple(_: TypeChecker, node: Node, children: {Type})
local function set_expected_types_to_decltuple(self: TypeChecker, node: Node, children: {Type})
local decltuple = node.kind == "assignment" and children[1] or node.decltuple
assert(decltuple is TupleType)
local decls = decltuple.tuple
Expand All @@ -10919,7 +10919,7 @@ do
local typ: Type
typ = decls[i]
if typ then
if i == nexps and ndecl > nexps then
if node.kind == "assignment" and i == nexps and ndecl > nexps then
typ = a_tuple(node, {})
for a = i, ndecl do
table.insert(typ.tuple, decls[a])
Expand All @@ -10930,6 +10930,14 @@ do
end
end
end

if node.decltuple then
local ndecltuple = #node.decltuple.tuple
local nvars = #node.vars
if ndecltuple > nvars then
self.errs:add(node.decltuple.tuple[nvars + 1], "number of types exceeds number of variables")
end
end
end

local function is_positive_int(n: number): boolean
Expand Down

0 comments on commit 43def7f

Please sign in to comment.