Skip to content

Commit

Permalink
avoid "got foo, expected foo" errors
Browse files Browse the repository at this point in the history
Do not produce a confusing message when reporting on incompatible types with
the same local names. Before this commit, the included test case would produce
an error message saying "got X | integer, expected X | integer"; which was
technically correct because it referred to different nested records called X,
but it was confusing. Since we don't "resolve path names" based on the
position of the error, bailing out with a generic error message is better than
producing a confusing one.
  • Loading branch information
hishamhm committed Jan 28, 2025
1 parent 90293d0 commit 85972a2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
20 changes: 20 additions & 0 deletions spec/lang/assignment/to_union_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,24 @@ describe("assignment to union", function()
local x: {string | Item} = {"name1", {name="myname"}}
]]))

it("do not produce a confusing message when reporting on incompatible types with the same local names", util.check_type_error([[
local record A
record X
end
f: function(X | integer)
end
local record B
record X
end
g: function(): X | integer
end
A.f(B.g())
]], {
{ msg = "argument 1: types are incompatible" }
}))

end)
11 changes: 10 additions & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9621,7 +9621,16 @@ a.types[i], b.types[i]), }
end

if (not ok) and not err then
return false, { Err("got %s, expected %s", t1, t2) }
if t1.typename == "invalid" or t2.typename == "invalid" then
return false, {}
end
local show_t1 = show_type(t1)
local show_t2 = show_type(t2)
if show_t1 == show_t2 then
return false, { Err_at(t1, "types are incompatible") }
else
return false, { Err_at(t1, "got " .. show_t1 .. ", expected " .. show_t2) }
end
end
return ok, err
end
Expand Down
11 changes: 10 additions & 1 deletion tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -9621,7 +9621,16 @@ do
end

if (not ok) and not err then
return false, { Err("got %s, expected %s", t1, t2) }
if t1 is InvalidType or t2 is InvalidType then
return false, {}
end
local show_t1 = show_type(t1)
local show_t2 = show_type(t2)
if show_t1 == show_t2 then
return false, { Err_at(t1, "types are incompatible") }
else
return false, { Err_at(t1, "got " .. show_t1 .. ", expected " .. show_t2) }
end
end
return ok, err
end
Expand Down

0 comments on commit 85972a2

Please sign in to comment.