teal-language / tl

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

Local function type generates unused local table #489

Closed kikito closed 2 years ago

kikito commented 2 years ago

Given the following example:

local type MyFun = function(integer, integer): integer

local function execute(f: MyFun, x: integer, y: integer): integer
  return f(x, y)
end

local function sum(x:integer, y:integer): integer
  return x + y
end

execute(sum, 1, 2)

The generated Lua code replaces the MyFun local type with an empty local table declaration (note the first line):

local MyFun = {}

local function execute(f, x, y)
   return f(x, y)
end

local function sum(x, y)
   return x + y
end

execute(sum, 1, 2)

This variable is never used by the generated Lua code, and will make linters complain about it.

Expectation: local types should not generate Lua code at all, unless they are used by said Lua code.

hishamhm commented 2 years ago

@kikito This is now implemented! Local types that are never used in a concrete context are now elided from the generated code!