Skip to content

Commit

Permalink
more informative error message when a field assignment fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Oct 30, 2022
1 parent 8ec56f1 commit eeaaa72
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion spec/declaration/record_method_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ describe("record method", function()
function b.stop(self: Rec.Plugin)
end
]], {
{ y = 15, msg = "type signature of 'start' does not match its declaration in Rec.Plugin" }
{ y = 15, msg = "type signature of 'start' does not match its declaration in Rec.Plugin: incompatible number of arguments: " }
}))

end)
20 changes: 12 additions & 8 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8965,23 +8965,27 @@ node.exps[3] and node.exps[3].type, }
filename = filename,
})

local ok = true
if rtype.fields[node.name.tk] and is_a(fn_type, rtype.fields[node.name.tk]) then
ok = true
elseif lax or owner == rtype then
local rfieldtype = rtype.fields[node.name.tk]
local ok = false
local err = nil
if rfieldtype then
ok, err = is_a(fn_type, rfieldtype)
end

if not ok and (lax or owner == rtype) then
rtype.fields[node.name.tk] = fn_type
table.insert(rtype.field_order, node.name.tk)
ok = true
else
ok = false
end

if ok then
node.name.type = fn_type
else
local name = tl.pretty_print_ast(node.fn_owner, opts.gen_target, { preserve_indent = true, preserve_newlines = false })
if rtype.fields[node.name.tk] then
node_error(node, "type signature of '" .. node.name.tk .. "' does not match its declaration in " .. show_type(node.fn_owner.type))
if rfieldtype then
local shortname = node.fn_owner.type.typename == "nominal" and show_type(node.fn_owner.type) or name
local msg = "type signature of '" .. node.name.tk .. "' does not match its declaration in " .. shortname .. ": "
add_errs_prefixing(node, err, errors, msg)
else
node_error(node, "cannot add undeclared function '" .. node.name.tk .. "' outside of the scope where '" .. name .. "' was originally declared")
end
Expand Down
20 changes: 12 additions & 8 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -8965,23 +8965,27 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string
filename = filename,
}

local ok = true
if rtype.fields[node.name.tk] and is_a(fn_type, rtype.fields[node.name.tk]) then
ok = true
elseif lax or owner == rtype then
local rfieldtype = rtype.fields[node.name.tk]
local ok = false
local err: {Error} = nil
if rfieldtype then
ok, err = is_a(fn_type, rfieldtype)
end

if not ok and (lax or owner == rtype) then
rtype.fields[node.name.tk] = fn_type
table.insert(rtype.field_order, node.name.tk)
ok = true
else
ok = false
end

if ok then
node.name.type = fn_type
else
local name = tl.pretty_print_ast(node.fn_owner, opts.gen_target, { preserve_indent = true, preserve_newlines = false })
if rtype.fields[node.name.tk] then
node_error(node, "type signature of '" .. node.name.tk .. "' does not match its declaration in " .. show_type(node.fn_owner.type))
if rfieldtype then
local shortname = node.fn_owner.type.typename == "nominal" and show_type(node.fn_owner.type) or name
local msg = "type signature of '" .. node.name.tk .. "' does not match its declaration in " .. shortname .. ": "
add_errs_prefixing(node, err, errors, msg)
else
node_error(node, "cannot add undeclared function '" .. node.name.tk .. "' outside of the scope where '" .. name .. "' was originally declared")
end
Expand Down

0 comments on commit eeaaa72

Please sign in to comment.