Skip to content

Commit

Permalink
fix: do not get file names and locations out of sync in error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Jan 3, 2025
1 parent adc3363 commit 8cdb640
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
29 changes: 29 additions & 0 deletions spec/lang/error_reporting/typecheck_error_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,33 @@ describe("typecheck errors", function()
})
end)

it("do not confuse filenames", function ()
util.mock_io(finally, {
["ordering.tl"] = [[
local record ordering
type SortBy<K> = table.SortFunction<K>
end
return ordering
]],
["util.tl"] = [[
local type SortBy = require("ordering").SortBy
local record util
func: function(sort_by?: SortBy)
end
return util
]]
})
util.run_check_type_error([[
local util = require("util")
util.func(function() end)
print("this is where the error should not be")
]], {
{ y = 4, x = 41, filename = "./util.tl", msg = "missing type argument" }
})
end)

end)
18 changes: 8 additions & 10 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1680,9 +1680,6 @@ local table_types = {







local function is_numeric_type(t)
return t.typename == "number" or t.typename == "integer"
Expand Down Expand Up @@ -6247,10 +6244,10 @@ local function Err_at(w, msg)
}
end

local function insert_error(self, y, x, err)
local function insert_error(self, y, x, f, err)
err.y = assert(y)
err.x = assert(x)
err.filename = self.filename
err.filename = assert(f)

if TL_DEBUG then
io.stderr:write("ERROR:" .. err.y .. ":" .. err.x .. ": " .. err.msg .. "\n")
Expand All @@ -6262,7 +6259,7 @@ end
function Errors:add(w, msg, ...)
local e = Err(msg, ...)
if e then
insert_error(self, w.y, w.x, e)
insert_error(self, w.y, w.x, w.f, e)
end
end

Expand All @@ -6289,14 +6286,14 @@ function Errors:add_in_context(w, ctx, msg, ...)

local e = Err(msg, ...)
if e then
insert_error(self, w.y, w.x, e)
insert_error(self, w.y, w.x, w.f, e)
end
end


function Errors:collect(errs)
for _, e in ipairs(errs) do
insert_error(self, e.y, e.x, e)
insert_error(self, e.y, e.x, e.filename, e)
end
end

Expand All @@ -6306,7 +6303,7 @@ function Errors:add_warning(tag, w, fmt, ...)
y = w.y,
x = w.x,
msg = fmt:format(...),
filename = self.filename,
filename = assert(w.f),
tag = tag,
})
end
Expand Down Expand Up @@ -6381,7 +6378,7 @@ function Errors:add_prefixing(w, src, prefix, dst)
if dst then
table.insert(dst, err)
else
insert_error(self, err.y, err.x, err)
insert_error(self, err.y, err.x, err.filename, err)
end
end
end
Expand Down Expand Up @@ -7479,6 +7476,7 @@ do
typ = fields[names[i]]
end


if typ and typ.typename == "nominal" then
typ = typ.found
end
Expand Down
17 changes: 7 additions & 10 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -1668,9 +1668,6 @@ local interface Type
is Where
where self.typename

y: integer
x: integer

typename: TypeName -- discriminator
typeid: integer -- unique identifier
inferred_at: Where -- for error messages
Expand Down Expand Up @@ -6247,10 +6244,10 @@ local function Err_at(w: Where, msg: string): Error
}
end

local function insert_error(self: Errors, y: integer, x: integer, err: Error)
local function insert_error(self: Errors, y: integer, x: integer, f: string, err: Error)
err.y = assert(y)
err.x = assert(x)
err.filename = self.filename
err.filename = assert(f)

if TL_DEBUG then
io.stderr:write("ERROR:" .. err.y .. ":" .. err.x .. ": " .. err.msg .. "\n")
Expand All @@ -6262,7 +6259,7 @@ end
function Errors:add(w: Where, msg: string, ...:Type)
local e = Err(msg, ...)
if e then
insert_error(self, w.y, w.x, e)
insert_error(self, w.y, w.x, w.f, e)
end
end

Expand All @@ -6289,14 +6286,14 @@ function Errors:add_in_context(w: Where, ctx: Node, msg: string, ...:Type)

local e = Err(msg, ...)
if e then
insert_error(self, w.y, w.x, e)
insert_error(self, w.y, w.x, w.f, e)
end
end


function Errors:collect(errs: {Error})
for _, e in ipairs(errs) do
insert_error(self, e.y, e.x, e)
insert_error(self, e.y, e.x, e.filename, e)
end
end

Expand All @@ -6306,7 +6303,7 @@ function Errors:add_warning(tag: WarningKind, w: Where, fmt: string, ...: any)
y = w.y,
x = w.x,
msg = fmt:format(...),
filename = self.filename,
filename = assert(w.f),
tag = tag,
})
end
Expand Down Expand Up @@ -6381,7 +6378,7 @@ function Errors:add_prefixing(w: Where, src: {Error}, prefix: string, dst?: {Err
if dst then
table.insert(dst, err)
else
insert_error(self, err.y, err.x, err)
insert_error(self, err.y, err.x, err.filename, err)
end
end
end
Expand Down

0 comments on commit 8cdb640

Please sign in to comment.