Skip to content

Commit

Permalink
fix: don't confuse localized global for a narrow when reporting unuse…
Browse files Browse the repository at this point in the history
…d var

Fixes #677.
  • Loading branch information
hishamhm committed Jul 19, 2023
1 parent d4b1763 commit dd7f845
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
16 changes: 16 additions & 0 deletions spec/error_reporting/warning_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ describe("warnings", function()
foo()
]], { }))

it("should report a unused localized global (regression test for #677)", util.check_warnings([[
local print = print
local type = type
local _ENV = nil
return {
say = function (msg: any)
if msg is string then
print(msg)
end
end,
}
]], {
{ y = 2, msg = "unused function type" },
}))

it("reports when implicitly declared variables redeclare a local (for loop)", util.check_warnings([[
local i = 1
for i = 1, 10 do
Expand Down
12 changes: 7 additions & 5 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4873,6 +4873,7 @@ end





local function sorted_keys(m)
local keys = {}
Expand Down Expand Up @@ -6171,7 +6172,7 @@ tl.type_check = function(ast, opts)
local function unused_warning(name, var)
local prefix = name:sub(1, 1)
if var.declared_at and
not (var.is_narrowed == "is") and
var.is_narrowed ~= "narrow" and
prefix ~= "_" and
prefix ~= "@" then

Expand Down Expand Up @@ -7656,7 +7657,8 @@ tl.type_check = function(ast, opts)

local function widen_in_scope(scope, var)
assert(scope[var], "no " .. var .. " in scope")
if scope[var].is_narrowed then
local narrow_mode = scope[var].is_narrowed
if narrow_mode and narrow_mode ~= "declaration" then
if scope[var].narrowed_from then
scope[var].t = scope[var].narrowed_from
scope[var].narrowed_from = nil
Expand Down Expand Up @@ -8436,7 +8438,7 @@ tl.type_check = function(ast, opts)
if not f.where then
t.inferred_at = nil
end
add_var(nil, v, t, "const", "is")
add_var(nil, v, t, "const", "narrow")
end
end
end
Expand Down Expand Up @@ -9062,7 +9064,7 @@ tl.type_check = function(ast, opts)

local rt = resolve_tuple_and_nominal(t)
if rt.typename ~= "enum" and not same_type(t, infertype) then
add_var(where, var.tk, infer_at(where, infertype), "const", "declaration")
add_var(where, var.tk, infer_at(where, infertype), "const", "narrowed_declaration")
end
end

Expand Down Expand Up @@ -9123,7 +9125,7 @@ tl.type_check = function(ast, opts)

if varnode.kind == "variable" and vartype.typename == "union" then

add_var(varnode, varnode.tk, val, nil, "is")
add_var(varnode, varnode.tk, val, nil, "narrow")
end
else
node_error(varnode, "variable is not being assigned a value")
Expand Down
14 changes: 8 additions & 6 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -4856,7 +4856,8 @@ function tl.search_module(module_name: string, search_dtl: boolean): string, FIL
end

local enum Narrow
"is"
"narrow"
"narrowed_declaration"
"declaration"
end

Expand Down Expand Up @@ -6171,7 +6172,7 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string
local function unused_warning(name: string, var: Variable)
local prefix <const> = name:sub(1,1)
if var.declared_at
and not (var.is_narrowed == "is")
and var.is_narrowed ~= "narrow"
and prefix ~= "_"
and prefix ~= "@"
then
Expand Down Expand Up @@ -7656,7 +7657,8 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string

local function widen_in_scope(scope: Scope, var: string): boolean
assert(scope[var], "no " .. var .. " in scope")
if scope[var].is_narrowed then
local narrow_mode = scope[var].is_narrowed
if narrow_mode and narrow_mode ~= "declaration" then
if scope[var].narrowed_from then
scope[var].t = scope[var].narrowed_from
scope[var].narrowed_from = nil
Expand Down Expand Up @@ -8436,7 +8438,7 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string
if not f.where then
t.inferred_at = nil
end
add_var(nil, v, t, "const", "is")
add_var(nil, v, t, "const", "narrow")
end
end
end
Expand Down Expand Up @@ -9062,7 +9064,7 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string

local rt = resolve_tuple_and_nominal(t)
if rt.typename ~= "enum" and not same_type(t, infertype) then
add_var(where, var.tk, infer_at(where, infertype), "const", "declaration")
add_var(where, var.tk, infer_at(where, infertype), "const", "narrowed_declaration")
end
end

Expand Down Expand Up @@ -9123,7 +9125,7 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string

if varnode.kind == "variable" and vartype.typename == "union" then
-- narrow union
add_var(varnode, varnode.tk, val, nil, "is")
add_var(varnode, varnode.tk, val, nil, "narrow")
end
else
node_error(varnode, "variable is not being assigned a value")
Expand Down

0 comments on commit dd7f845

Please sign in to comment.