Skip to content

Commit

Permalink
continue allowing plain local nominal aliases as types
Browse files Browse the repository at this point in the history
See #891 and test cases for description.
  • Loading branch information
hishamhm committed Jan 3, 2025
1 parent 236c637 commit dab4155
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 deletions.
72 changes: 72 additions & 0 deletions spec/lang/declaration/local_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,76 @@ describe("local", function()
]], {
{ y = 5, x = 36, msg = "number of types exceeds number of variables" },
}))

-- behaviors to be deprecated, once we can implement
-- proper detection and warnings.
describe("behavior to deprecate", function()
it("plain `local` aliasing of enums works (regression test for #891)", util.check([[
local enum MyEnum1
"A"
"B"
end
-- this should require `local type`...
local MyEnum2 = MyEnum1
local record MyRecord
x: MyEnum2
end
local x: MyRecord = {x = "A"}
assert(x)
local function f(): string, string
return "hello", "world"
end
local x, y: integer, string, string = 1, ""
local z, w = 0, f()
z, w = 0, f()
]]))
it("plain `local` aliasing of records works (regression test for #891)", util.check([[
local record MyRecord1 end
local MyRecord2 = MyRecord1
local record MyRecord
x: MyRecord2
end
local x: MyRecord = {x = {}}
assert(x)
]]))

it("plain `local` aliasing of records works across `require` (regression test for #891)", function()
util.mock_io(finally, {
["foo.tl"] = [[
local enum MyEnum
"A"
"B"
end
return { MyEnum = MyEnum }
]],
["bar.tl"] = [[
local foo = require "foo"
local MyEnum = foo.MyEnum
local record MyRecord
x: MyEnum
end
local x: MyRecord = {x = "A"}
assert(x)
]],
})
local result, err = tl.process("bar.tl", assert(tl.init_env()))
assert.same(nil, err)
assert.same({}, result.syntax_errors)
assert.same({}, result.type_errors)
end)
end)
end)
16 changes: 10 additions & 6 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7467,19 +7467,23 @@ do
end
for i = 2, #names do
typ = unwrap_for_find_type(typ)
if typ == nil then
return nil
end

local fields = typ.fields and typ.fields
if not fields then
return nil
end

typ = fields[names[i]]
if typ and typ.typename == "nominal" then
typ = typ.found
end
if typ == nil then
return nil
end
end

if typ and typ.typename == "nominal" then
typ = typ.found
end
if typ == nil then
return nil
end

if typ.typename == "typedecl" then
Expand Down
17 changes: 11 additions & 6 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -7467,19 +7467,24 @@ do
end
for i = 2, #names do
typ = unwrap_for_find_type(typ)
if typ == nil then
return nil
end

local fields = typ is RecordLikeType and typ.fields
if not fields then
return nil
end

typ = fields[names[i]]
if typ and typ is NominalType then
typ = typ.found
end
if typ == nil then
return nil
end
end

-- FIXME doing this at the and lets all nominals be used as types (see #891)
if typ and typ is NominalType then
typ = typ.found
end
if typ == nil then
return nil
end

if typ is TypeDeclType then
Expand Down

0 comments on commit dab4155

Please sign in to comment.