Skip to content

Commit

Permalink
fix: ignore non-literal booleans in table key redeclaration check
Browse files Browse the repository at this point in the history
Fixes #816.
  • Loading branch information
hishamhm committed Oct 13, 2024
1 parent ba54466 commit 1ed3598
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
21 changes: 21 additions & 0 deletions spec/lang/declaration/map_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,25 @@ describe("maps", function()
{ y = 5, msg = "redeclared key false" },
{ y = 6, msg = "redeclared key true" },
}))

it("redeclaration check does not fail on non-literal boolean keys (regression test for #816)", util.check([[
local KEY_FALSE = false
local KEY_TRUE = true
local foo = {
[false] = "false",
[true] = "true",
}
local bar = {
[KEY_FALSE] = "false",
[KEY_TRUE] = "true",
}
print(foo[false])
print(foo[true])
print(bar[KEY_FALSE])
print(bar[KEY_TRUE])
]]))
end)
13 changes: 8 additions & 5 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10827,13 +10827,16 @@ a.types[i], b.types[i]), }
for i, child in ipairs(children) do
local ck = child.kname
local cktype = child.ktype
local n = node[i].key.constnum
local b = nil
if cktype.typename == "boolean" then
b = (node[i].key.tk == "true")
local key = ck
local n
if not key then
n = node[i].key.constnum
key = n
if not key and node[i].key.kind == "boolean" then
key = (node[i].key.tk == "true")
end
end

local key = ck or n or b
self.errs:check_redeclared_key(node[i], nil, seen_keys, key)

local uvtype = resolve_tuple(child.vtype)
Expand Down
13 changes: 8 additions & 5 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -10827,13 +10827,16 @@ do
for i, child in ipairs(children) do
local ck = child.kname
local cktype = child.ktype
local n = node[i].key.constnum
local b: boolean = nil
if cktype is BooleanType then
b = (node[i].key.tk == "true")
local key: CheckableKey = ck
local n: number
if not key then
n = node[i].key.constnum
key = n
if not key and node[i].key.kind == "boolean" then
key = (node[i].key.tk == "true")
end
end

local key: CheckableKey = ck or n or b
self.errs:check_redeclared_key(node[i], nil, seen_keys, key)

local uvtype = resolve_tuple(child.vtype)
Expand Down

0 comments on commit 1ed3598

Please sign in to comment.