teal-language / tl

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

edge case of checking of the number of return value #741

Open fperrad opened 6 months ago

fperrad commented 6 months ago
local function bar()
end

local function foo()
   if true then
      return bar()  -- ERROR: in return value: excess return values, expected 0 (), got 1 (())
   else
      bar()
      return  -- OK but not idiomatic
   end
end

Note: with nil as explicit return, the checker is happy

local function bar(): nil
end

local function foo(): nil
   return bar()
end
hishamhm commented 6 months ago

Thanks for the report! This is definitely a bug that needs fixing.

return bar()

Is the goal here to trigger a tail call? It surprised me a bit which of the two options you marked as idiomatic — I usually never return f() from 0-ary functions, to help the reader spot that nothing is being returned.

catwell commented 6 months ago

I guess it varies but I tend to agree with @fperrad: I always return f() from 0-ary functions in Lua, in part because of the tail call.