Skip to content

Commit 12d2090

Browse files
committed
provide specific error message for redefinition of polys
1 parent 772d2d2 commit 12d2090

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

spec/declaration/record_function_spec.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ describe("record function", function()
4444
{ y = 8, msg = "type signature of 'do_x' does not match its declaration in Y: argument 2: got string, expected integer" },
4545
}))
4646

47+
it("cannot implement a polymorphic function via redeclaration", util.check_type_error([[
48+
local record Y
49+
do_x: function(integer, integer): integer
50+
do_x: function(integer): integer
51+
end
52+
53+
function Y.do_x(a: integer, b: string): integer
54+
return a + math.tointeger(b)
55+
end
56+
]], {
57+
{ y = 6, msg = "type signature does not match declaration: field has multiple function definitions" },
58+
}))
59+
4760
it("a consistent redeclaration produces a warning", util.check_warnings([[
4861
local record Y
4962
end

spec/declaration/record_method_spec.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,19 @@ describe("record method", function()
474474
{ y = 5, msg = "type signature of 'do_x' does not match its declaration in Y: argument 2: got string, expected integer" },
475475
}))
476476

477+
it("cannot implement a polymorphic method via redeclaration", util.check_type_error([[
478+
local record Y
479+
do_x: function(Y, integer, integer): integer
480+
do_x: function(Y, integer): integer
481+
end
482+
483+
function Y:do_x(a: integer, b: string): integer
484+
return a + math.tointeger(b)
485+
end
486+
]], {
487+
{ y = 6, msg = "type signature does not match declaration: field has multiple function definitions" },
488+
}))
489+
477490
it("an inconsistent type in redeclaration produces an error", util.check_type_error([[
478491
local record Y
479492
end

tl.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9557,6 +9557,11 @@ tl.type_check = function(ast, opts)
95579557

95589558
local ok, err = same_type(fn_type, rfieldtype)
95599559
if not ok then
9560+
if rfieldtype.typename == "poly" then
9561+
add_errs_prefixing(node, err, errors, "type signature does not match declaration: field has multiple function definitions (such polymorphic declarations are intended for Lua module interoperability)")
9562+
return
9563+
end
9564+
95609565
local shortname = node.fn_owner.type.typename == "nominal" and
95619566
show_type(node.fn_owner.type) or
95629567
show_fn_owner(node)

tl.tl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9557,6 +9557,11 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string
95579557

95589558
local ok, err = same_type(fn_type, rfieldtype)
95599559
if not ok then
9560+
if rfieldtype.typename == "poly" then
9561+
add_errs_prefixing(node, err, errors, "type signature does not match declaration: field has multiple function definitions (such polymorphic declarations are intended for Lua module interoperability)")
9562+
return
9563+
end
9564+
95609565
local shortname = node.fn_owner.type.typename == "nominal"
95619566
and show_type(node.fn_owner.type)
95629567
or show_fn_owner(node)

0 commit comments

Comments
 (0)