twitter-archive/torch-autograd

Wrong error message due to operation overloading

albanD opened this issue · 1 comments

There is a slight unexpected behaviour in the operation overloading on numbers done here

If we try to add (same with mul and sub) something that does not have an __add in its metatable to a number, we get to an infinite recursion leading to a stack overflow.
Below is an example using nil.

local function add(a,b)
    return a + b
end

local ok, res = pcall(add, nil, 2)
print(res)
-- a.lua:2: attempt to perform arithmetic on local 'a' (a nil value)

require 'autograd'

local ok, res = pcall(add, nil, 2)
print(res)
-- ...e/alban/torch/install/share/lua/5.1/autograd/support.lua:67: stack overflow

This could be solve by checking whether or not the first element has the proper function in its metatable before calling a+b here. Do you guys think there would be a better way to solve this? I can prepare a PR if you agree with the proposed fix.

That seems like the right place to put it. A PR would be very welcome. I'll keep the issue open until the PR is merged.