teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.03k stars 101 forks source link

Warning when calling method via pcall #656

Closed crai0 closed 1 year ago

crai0 commented 1 year ago

When calling a method via pcall, the Teal compiler produces a warning that the method is being invoked as a regular function.

Example:

local record Test
    someString: string
end

function Test.new(someString: string): Test
    return setmetatable({ someString = someString }, { __index = Test })
end

function Test:print(): nil
    if self.someString == nil then
        error("someString is nil")
    end

    print(self.someString)
end

local test = Test.new()
local ok, err = pcall(test.print, test)
if not ok then
    print(err)
end

Output:

========================================
1 warning:
test.tl:18:22: invoked method as a regular function: consider using ':' instead of '.'
hishamhm commented 1 year ago

@JR-Mitchell this looks like an interesting edge case to the : warnings you implemented — pcall does have a special handling function in the compiler, so it should be possible to detect when a function call goes through that. Could you take a look? Thank you!

JR-Mitchell commented 1 year ago

I'll try and find the time to have a look, feel free to assign this one to me for now and I'll let you know how I get on.

hishamhm commented 1 year ago

@JR-Mitchell thank you!!

JR-Mitchell commented 1 year ago

@hishamhm I've opened #668 which should fix this for pcall and xpcall, as well as the related issue of incorrect error messaging if calling, for example

local ok, err = pcall(test.print, 123)

I haven't taken the time to check if there are any other special functions that might have a similar bug, though I can't think of any off the top of my head.