Skip to content

Commit

Permalink
return: module returns the nominal's type, including typeargs
Browse files Browse the repository at this point in the history
Fixes #804.
  • Loading branch information
hishamhm committed Sep 19, 2024
1 parent ccd2417 commit 58ebd4b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
88 changes: 88 additions & 0 deletions spec/statement/return_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,94 @@ describe("return", function()
assert.same({}, result.syntax_errors)
assert.same({}, result.type_errors)
end)

it("when exporting a generic (regression test for #804)", function ()
util.mock_io(finally, {
["foo.tl"] = [[
local record Foo<T>
bar: T
end
return Foo
]],
["main.tl"] = [[
local Foo = require("foo")
local foo: Foo<integer>
foo = {
bar = 5
}
print(string.format("bar: %d", foo.bar + 1))
]],
})

local tl = require("tl")
local result, err = tl.process("main.tl", assert(tl.init_env()))

assert.same(nil, err)
assert.same({}, result.syntax_errors)
assert.same({}, result.type_errors)
end)

it("when exporting a typealias (variation on regression test for #804)", function ()
util.mock_io(finally, {
["foo.tl"] = [[
local record Foo<T>
bar: T
end
local type FooInteger = Foo<integer>
return FooInteger
]],
["main.tl"] = [[
local Foo = require("foo")
local foo: Foo
foo = {
bar = 5
}
print(string.format("bar: %d", foo.bar + 1))
]],
})

local tl = require("tl")
local result, err = tl.process("main.tl", assert(tl.init_env()))

assert.same(nil, err)
assert.same({}, result.syntax_errors)
assert.same({}, result.type_errors)
end)

it("when exporting a non-generic (variation on regression test for #804)", function ()
util.mock_io(finally, {
["foo.tl"] = [[
local record Foo
bar: integer
end
return Foo
]],
["main.tl"] = [[
local Foo = require("foo")
local foo: Foo
foo = {
bar = 5
}
print(string.format("bar: %d", foo.bar + 1))
]],
})

local tl = require("tl")
local result, err = tl.process("main.tl", assert(tl.init_env()))

assert.same(nil, err)
assert.same({}, result.syntax_errors)
assert.same({}, result.type_errors)
end)
end)

it("when exporting type alias through multiple levels", function ()
Expand Down
8 changes: 7 additions & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11527,7 +11527,13 @@ self:expand_type(node, values, elements) })
if not expected then

expected = self:infer_at(node, got)
self.module_type = drop_constant_value(self:to_structural(resolve_tuple(expected)))
local module_type = resolve_tuple(expected)
if module_type.typename == "nominal" then
self:resolve_nominal(module_type)
self.module_type = module_type.found
else
self.module_type = drop_constant_value(module_type)
end
self.st[2].vars["@return"] = { t = expected }
end
local expected_t = expected.tuple
Expand Down
8 changes: 7 additions & 1 deletion tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -11527,7 +11527,13 @@ do
if not expected then
-- if at the toplevel
expected = self:infer_at(node, got)
self.module_type = drop_constant_value(self:to_structural(resolve_tuple(expected)))
local module_type = resolve_tuple(expected)
if module_type is NominalType then
self:resolve_nominal(module_type)
self.module_type = module_type.found
else
self.module_type = drop_constant_value(module_type)
end
self.st[2].vars["@return"] = { t = expected }
end
local expected_t = expected.tuple
Expand Down

0 comments on commit 58ebd4b

Please sign in to comment.