Skip to content

Commit

Permalink
fix: localizing a record does not make the new local a type
Browse files Browse the repository at this point in the history
This allows for a pattern where we can localize a record
and load and alternative implementation on top of it.

Fixes #759.
  • Loading branch information
hishamhm committed Aug 7, 2024
1 parent 94bbab0 commit 181d2a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
5 changes: 3 additions & 2 deletions spec/call/record_method_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ describe("record method call", function()
end
m.a.add(first)
]], {
-- FIXME this warning needs to go away when we detect that "m.a" and "first" are not the same
{ y = 14, msg = "invoked method as a regular function: consider using ':' instead of '.'" }
-- FIXME these warnings need to go away when we detect that the arities are correct
{ y = 10, msg = "invoked method as a regular function: consider using ':' instead of '.'" },
{ y = 14, msg = "invoked method as a regular function: consider using ':' instead of '.'" },
}, {}))

it("for function declared in record body with self as different type from receiver", util.check_warnings([[
Expand Down
13 changes: 13 additions & 0 deletions spec/declaration/local_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,19 @@ describe("local", function()
local b <total>: A = { v = 10 }
]]))

it("localizing a record does not make the new local a type (#759)", util.check([[
local record k
end
local kk: k = {}
local k = k
k = {}
kk = {}
]]))
end)

describe("<close>", function()
Expand Down
27 changes: 27 additions & 0 deletions spec/stdlib/require_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1249,4 +1249,31 @@ describe("require", function()
{ filename = "main.tl", x = 30, y = 1, msg = "require() in type declarations cannot be part of larger expressions" }
}, result.syntax_errors)
end)

it("can localize a record and load and alternative implementation (#759)", function ()
util.mock_io(finally, {
["my-lua-utf8.tl"] = [[
local record my_lua_utf8
end
return my_lua_utf8
]],
["main.tl"] = [[
local type Utf8 = utf8
local utf8: Utf8 = utf8
if not utf8 then
local ok, lutf8 = pcall(require, 'my-lua-utf8') as (boolean, Utf8)
if ok then
utf8 = lutf8
end
end
print(utf8.charpattern)
]],
})
local result, err = tl.process("main.tl")

assert.same({}, result.syntax_errors)
assert.same({}, result.type_errors)
end)

end)
6 changes: 5 additions & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6979,7 +6979,7 @@ do
local scope = self.st[i]
local var = scope.vars[name]
if var then
if use == "lvalue" and var.is_narrowed then
if use == "lvalue" and var.is_narrowed and var.is_narrowed ~= "localizing" then
if var.narrowed_from then
var.used = true
return { t = var.narrowed_from, attribute = var.attribute }, i, var.attribute
Expand Down Expand Up @@ -10595,6 +10595,10 @@ self:expand_type(node, values, elements) })
t.inferred_len = nil
elseif t.typename == "nominal" then
self:resolve_nominal(t)
local rt = t.resolved
if rt.typename == "typedecl" then
t.resolved = rt.def
end
end

return ok, t, infertype ~= nil
Expand Down
6 changes: 5 additions & 1 deletion tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -6979,7 +6979,7 @@ do
local scope = self.st[i]
local var = scope.vars[name]
if var then
if use == "lvalue" and var.is_narrowed then
if use == "lvalue" and var.is_narrowed and var.is_narrowed ~= "localizing" then
if var.narrowed_from then
var.used = true
return { t = var.narrowed_from, attribute = var.attribute }, i, var.attribute
Expand Down Expand Up @@ -10595,6 +10595,10 @@ do
t.inferred_len = nil
elseif t is NominalType then
self:resolve_nominal(t)
local rt = t.resolved
if rt is TypeDeclType then
t.resolved = rt.def
end
end

return ok, t, infertype ~= nil
Expand Down

0 comments on commit 181d2a5

Please sign in to comment.