Skip to content

Commit

Permalink
fix: type variables from returns resolve for arguments
Browse files Browse the repository at this point in the history
Fixes #838.
  • Loading branch information
hishamhm committed Oct 25, 2024
1 parent 374bd99 commit 0d34326
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 24 deletions.
70 changes: 70 additions & 0 deletions spec/lang/inference/function_call_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
local util = require("spec.util")

describe("function call", function()
describe("results", function()
it("should be adjusted down to 1 result in an expression list", util.check([[
local function f(): string, number
end
local a, b = f(), "hi"
a = "hey"
]]))

it("can resolve type arguments based on expected type at use site (#512)", util.check([[
local function get_foos<T>():{T}
return {}
end
local foos:{integer} = get_foos()
print(foos)
]]))
end)

describe("arguments", function()
it("type variables from returns resolve for arguments (regression test for #838)", util.check([[
local fcts: {integer:function(val: any, opt?: string): any}
local function bar (val: number): number
print(val)
return val
end
local function bar2 (val: number, val2: string): number
print(val, val2)
return val
end
fcts = { -- OK, with table constructor
[11] = function (val: string): string
print(val)
return val
end,
[12] = function (val: string, val2: string): string
print(val, val2)
return val
end,
[21] = bar,
[22] = bar2,
}
setmetatable(fcts, {
__tostring = function(): string return 'fcts' end
})
fcts = setmetatable({ -- Ok, as an argument via type variable
[11] = function (val: string): string
print(val)
return val
end,
[12] = function (val: string, val2: string): string
print(val, val2)
return val
end,
[21] = bar,
[22] = bar2,
}, {
__tostring = function(): string return 'fcts' end
})
print(fcts)
]]))
end)
end)
19 changes: 0 additions & 19 deletions spec/lang/inference/function_result_spec.lua

This file was deleted.

2 changes: 1 addition & 1 deletion spec/lang/statement/forin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("forin", function()
it("with a callable record iterator", util.check([[
local record R
incr: integer
metamethod __call: function(): integer
metamethod __call: function(self): integer
end
local function foo(incr: integer): R
Expand Down
4 changes: 2 additions & 2 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12267,13 +12267,13 @@ self:expand_type(node, values, elements) })
for _, typ in ipairs(e1args) do
at = at + 1
if node.e2[at] then
node.e2[at].expected = typ
node.e2[at].expected = self:infer_at(node.e2[at], typ)
end
end
if e1type.args.is_va then
local typ = e1args[#e1args]
for i = at + 1, #node.e2 do
node.e2[i].expected = typ
node.e2[i].expected = self:infer_at(node.e2[i], typ)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -12267,13 +12267,13 @@ do
for _, typ in ipairs(e1args) do
at = at + 1
if node.e2[at] then
node.e2[at].expected = typ
node.e2[at].expected = self:infer_at(node.e2[at], typ)
end
end
if e1type.args.is_va then
local typ = e1args[#e1args]
for i = at + 1, #node.e2 do
node.e2[i].expected = typ
node.e2[i].expected = self:infer_at(node.e2[i], typ)
end
end
end
Expand Down

0 comments on commit 0d34326

Please sign in to comment.