Skip to content

Commit

Permalink
fix: avoid assertion error if typevar resolution fails when setting i…
Browse files Browse the repository at this point in the history
…nference location
  • Loading branch information
hishamhm committed Oct 21, 2023
1 parent a5dd26b commit 38e23ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
19 changes: 19 additions & 0 deletions spec/statement/return_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,23 @@ describe("return", function()
assert.same({}, result.type_errors)
end)

it("does not crash when a return type inference causes an error", util.check_type_error([[
local type A = record
end
local type B = record
end
local function f(): A
end
local function fail(): A | B
return f()
end
]], {
-- the duplicated error is not ideal, but it's harmless, and better than a crash
{ y = 10, msg = "cannot discriminate a union between multiple table types" },
{ y = 11, msg = "cannot discriminate a union between multiple table types" },
}))

end)
3 changes: 3 additions & 0 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6234,6 +6234,9 @@ tl.type_check = function(ast, opts)

local function infer_at(where, t)
local ret = resolve_typevars_at(where, t)
if ret.typename == "invalid" then
ret = t -- errors are produced by resolve_typevars_at
end
ret = (ret ~= t) and ret or shallow_copy_type(t)
ret.inferred_at = where
ret.inferred_at_file = filename
Expand Down
3 changes: 3 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -6234,6 +6234,9 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string

local function infer_at(where: Node, t: Type): Type
local ret = resolve_typevars_at(where, t)
if ret.typename == "invalid" then
ret = t -- errors are produced by resolve_typevars_at
end
ret = (ret ~= t) and ret or shallow_copy_type(t)
ret.inferred_at = where
ret.inferred_at_file = filename
Expand Down

0 comments on commit 38e23ca

Please sign in to comment.