From 8d9a26fec09d94b5474f7e0c3fd91123a17e9b55 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 3 Jan 2025 12:05:35 -0300 Subject: [PATCH] fix: do not get file names and locations out of sync in error messages --- .../error_reporting/typecheck_error_spec.lua | 29 +++++++++++++++++++ tl.lua | 18 +++++------- tl.tl | 17 +++++------ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/spec/lang/error_reporting/typecheck_error_spec.lua b/spec/lang/error_reporting/typecheck_error_spec.lua index 5afa15723..73928f93e 100644 --- a/spec/lang/error_reporting/typecheck_error_spec.lua +++ b/spec/lang/error_reporting/typecheck_error_spec.lua @@ -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 = table.SortFunction + 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) diff --git a/tl.lua b/tl.lua index 4f9861d81..26216861a 100644 --- a/tl.lua +++ b/tl.lua @@ -1680,9 +1680,6 @@ local table_types = { - - - local function is_numeric_type(t) return t.typename == "number" or t.typename == "integer" @@ -6250,10 +6247,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") @@ -6265,7 +6262,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 @@ -6292,14 +6289,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 @@ -6309,7 +6306,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 @@ -6384,7 +6381,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 @@ -7482,6 +7479,7 @@ do typ = fields[names[i]] end + if typ and typ.typename == "nominal" then typ = typ.found end diff --git a/tl.tl b/tl.tl index f04b2395f..f4d8d47b3 100644 --- a/tl.tl +++ b/tl.tl @@ -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 @@ -6250,10 +6247,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") @@ -6265,7 +6262,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 @@ -6292,14 +6289,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 @@ -6309,7 +6306,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 @@ -6384,7 +6381,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