Skip to content

Commit

Permalink
fix: elide code generation of unused local types
Browse files Browse the repository at this point in the history
Unused global types still generate the empty table, as those
declaration may be used from other modules.

Closes #632.
  • Loading branch information
hishamhm committed Jul 19, 2023
1 parent 58d8615 commit 78d9676
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 7 additions & 1 deletion spec/cli/gen_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ local util = require("spec.util")
local input_file = [[
global type1 = 2
global type type_g = record
end
local type type_2 = record
end
Expand Down Expand Up @@ -36,7 +39,10 @@ local c = 100
local output_file = [[
type1 = 2
local type_2 = {}
type_g = {}
local function bla()
Expand Down
7 changes: 5 additions & 2 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6474,7 +6474,7 @@ tl.type_check = function(ast, opts)



local function check_for_unused_vars(vars)
local function check_for_unused_vars(vars, is_global)
if not next(vars) then
return
end
Expand All @@ -6484,6 +6484,9 @@ tl.type_check = function(ast, opts)
if var.used_as_type then
var.declared_at.elide_type = true
else
if is_typetype(var.t) and not is_global then
var.declared_at.elide_type = true
end
table.insert(list, { y = var.declared_at.y, x = var.declared_at.x, name = name, var = var })
end
elseif var.used and is_typetype(var.t) and var.aliasing then
Expand Down Expand Up @@ -10368,7 +10371,7 @@ tl.type_check = function(ast, opts)
recurse_node(ast, visit_node, visit_type)

close_types(st[1])
check_for_unused_vars(st[1])
check_for_unused_vars(st[1], true)

clear_redundant_errors(errors)

Expand Down
7 changes: 5 additions & 2 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -6474,7 +6474,7 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string
var: Variable
end

local function check_for_unused_vars(vars: {string:Variable})
local function check_for_unused_vars(vars: {string:Variable}, is_global: boolean)
if not next(vars) then
return
end
Expand All @@ -6484,6 +6484,9 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string
if var.used_as_type then
var.declared_at.elide_type = true
else
if is_typetype(var.t) and not is_global then
var.declared_at.elide_type = true
end
table.insert(list, { y = var.declared_at.y, x = var.declared_at.x, name = name, var = var })
end
elseif var.used and is_typetype(var.t) and var.aliasing then
Expand Down Expand Up @@ -10368,7 +10371,7 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string
recurse_node(ast, visit_node, visit_type)

close_types(st[1])
check_for_unused_vars(st[1])
check_for_unused_vars(st[1], true)

clear_redundant_errors(errors)

Expand Down

0 comments on commit 78d9676

Please sign in to comment.