Skip to content

Commit cba0657

Browse files
authored
fix: resolve type aliases when lowering 'is' to Lua (#1149) (#1169)
1 parent 80d3169 commit cba0657

7 files changed

Lines changed: 77 additions & 16 deletions

File tree

spec/lang/operator/is_spec.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,23 @@ if not (r == nil) then
452452
r.f()
453453
end]]))
454454

455+
it("lowers 'is' on a type alias like its underlying type (regression test for #1149)", util.gen([[
456+
local x: any = nil
457+
local type MyInt = integer
458+
local type MyNil = nil
459+
print(x is MyInt)
460+
print(x is MyNil)
461+
print(x is integer)
462+
print(x is nil)
463+
]], [[
464+
local x = nil
465+
466+
467+
print(math.type(x) == "integer")
468+
print(x == nil)
469+
print(math.type(x) == "integer")
470+
print(x == nil)]], "5.4"))
471+
455472
end)
456473

457474
describe("on while", function()

teal.lua

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11067,6 +11067,9 @@ local traverse_nodes = traversal.traverse_nodes
1106711067
local util = require("teal.util")
1106811068
local sorted_keys = util.sorted_keys
1106911069

11070+
local types = require("teal.types")
11071+
11072+
1107011073
local lua_compat = {}
1107111074

1107211075

@@ -11262,9 +11265,13 @@ local function adjust_code(ast, needs_compat, gen_compat, gen_target)
1126211265
visit_node.cbs["op"] = {
1126311266
after = function(_, node, _children)
1126411267
if node.op.op == "is" then
11265-
if node.e2.casttype.typename == "integer" then
11268+
local ct = node.e2.casttype
11269+
while ct.typename == "nominal" and ct.resolved do
11270+
ct = ct.resolved
11271+
end
11272+
if ct.typename == "integer" then
1126611273
needs_compat["math"] = true
11267-
elseif node.e2.casttype.typename ~= "nil" then
11274+
elseif ct.typename ~= "nil" then
1126811275
needs_compat["type"] = true
1126911276
end
1127011277
elseif node.op.op == "." then
@@ -11985,11 +11992,15 @@ function lua_generator.generate(ast, gen_target, opts)
1198511992
elseif node.op.op == "as" then
1198611993
add_child(out, children[1], "", indent)
1198711994
elseif node.op.op == "is" then
11988-
if node.e2.casttype.typename == "integer" then
11995+
local ct = node.e2.casttype
11996+
while ct.typename == "nominal" and ct.resolved do
11997+
ct = ct.resolved
11998+
end
11999+
if ct.typename == "integer" then
1198912000
table.insert(out, "math.type(")
1199012001
add_child(out, children[1], "", indent)
1199112002
table.insert(out, ") == \"integer\"")
11992-
elseif node.e2.casttype.typename == "nil" then
12003+
elseif ct.typename == "nil" then
1199312004
add_child(out, children[1], "", indent)
1199412005
table.insert(out, " == nil")
1199512006
else

teal/gen/lua_compat.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ local traverse_nodes = traversal.traverse_nodes
2121
local util = require("teal.util")
2222
local sorted_keys = util.sorted_keys
2323

24+
local types = require("teal.types")
25+
26+
2427
local lua_compat = {}
2528

2629

@@ -216,9 +219,13 @@ local function adjust_code(ast, needs_compat, gen_compat, gen_target)
216219
visit_node.cbs["op"] = {
217220
after = function(_, node, _children)
218221
if node.op.op == "is" then
219-
if node.e2.casttype.typename == "integer" then
222+
local ct = node.e2.casttype
223+
while ct.typename == "nominal" and ct.resolved do
224+
ct = ct.resolved
225+
end
226+
if ct.typename == "integer" then
220227
needs_compat["math"] = true
221-
elseif node.e2.casttype.typename ~= "nil" then
228+
elseif ct.typename ~= "nil" then
222229
needs_compat["type"] = true
223230
end
224231
elseif node.op.op == "." then

teal/gen/lua_compat.tl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ local traverse_nodes = traversal.traverse_nodes
2121
local util = require("teal.util")
2222
local sorted_keys = util.sorted_keys
2323

24+
local types = require("teal.types")
25+
local type NominalType = types.NominalType
26+
2427
local record lua_compat
2528
end
2629

@@ -216,9 +219,13 @@ local function adjust_code(ast: Node, needs_compat: {string:boolean}, gen_compat
216219
visit_node.cbs["op"] = {
217220
after = function(_: nil, node: Node, _children: {nil}): nil
218221
if node.op.op == "is" then
219-
if node.e2.casttype.typename == "integer" then
222+
local ct = node.e2.casttype
223+
while ct is NominalType and ct.resolved do
224+
ct = ct.resolved
225+
end
226+
if ct.typename == "integer" then
220227
needs_compat["math"] = true
221-
elseif node.e2.casttype.typename ~= "nil" then
228+
elseif ct.typename ~= "nil" then
222229
needs_compat["type"] = true
223230
end
224231
elseif node.op.op == "." then

teal/gen/lua_generator.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,15 @@ function lua_generator.generate(ast, gen_target, opts)
626626
elseif node.op.op == "as" then
627627
add_child(out, children[1], "", indent)
628628
elseif node.op.op == "is" then
629-
if node.e2.casttype.typename == "integer" then
629+
local ct = node.e2.casttype
630+
while ct.typename == "nominal" and ct.resolved do
631+
ct = ct.resolved
632+
end
633+
if ct.typename == "integer" then
630634
table.insert(out, "math.type(")
631635
add_child(out, children[1], "", indent)
632636
table.insert(out, ") == \"integer\"")
633-
elseif node.e2.casttype.typename == "nil" then
637+
elseif ct.typename == "nil" then
634638
add_child(out, children[1], "", indent)
635639
table.insert(out, " == nil")
636640
else

teal/gen/lua_generator.tl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,15 @@ function lua_generator.generate(ast: Node, gen_target: GenTarget, opts?: Options
626626
elseif node.op.op == "as" then
627627
add_child(out, children[1], "", indent)
628628
elseif node.op.op == "is" then
629-
if node.e2.casttype.typename == "integer" then
629+
local ct = node.e2.casttype
630+
while ct is NominalType and ct.resolved do
631+
ct = ct.resolved
632+
end
633+
if ct.typename == "integer" then
630634
table.insert(out, "math.type(")
631635
add_child(out, children[1], "", indent)
632636
table.insert(out, ") == \"integer\"")
633-
elseif node.e2.casttype.typename == "nil" then
637+
elseif ct.typename == "nil" then
634638
add_child(out, children[1], "", indent)
635639
table.insert(out, " == nil")
636640
else

tl.lua

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11321,6 +11321,9 @@ local traverse_nodes = traversal.traverse_nodes
1132111321
local util = require("teal.util")
1132211322
local sorted_keys = util.sorted_keys
1132311323

11324+
local types = require("teal.types")
11325+
11326+
1132411327
local lua_compat = {}
1132511328

1132611329

@@ -11516,9 +11519,13 @@ local function adjust_code(ast, needs_compat, gen_compat, gen_target)
1151611519
visit_node.cbs["op"] = {
1151711520
after = function(_, node, _children)
1151811521
if node.op.op == "is" then
11519-
if node.e2.casttype.typename == "integer" then
11522+
local ct = node.e2.casttype
11523+
while ct.typename == "nominal" and ct.resolved do
11524+
ct = ct.resolved
11525+
end
11526+
if ct.typename == "integer" then
1152011527
needs_compat["math"] = true
11521-
elseif node.e2.casttype.typename ~= "nil" then
11528+
elseif ct.typename ~= "nil" then
1152211529
needs_compat["type"] = true
1152311530
end
1152411531
elseif node.op.op == "." then
@@ -12239,11 +12246,15 @@ function lua_generator.generate(ast, gen_target, opts)
1223912246
elseif node.op.op == "as" then
1224012247
add_child(out, children[1], "", indent)
1224112248
elseif node.op.op == "is" then
12242-
if node.e2.casttype.typename == "integer" then
12249+
local ct = node.e2.casttype
12250+
while ct.typename == "nominal" and ct.resolved do
12251+
ct = ct.resolved
12252+
end
12253+
if ct.typename == "integer" then
1224312254
table.insert(out, "math.type(")
1224412255
add_child(out, children[1], "", indent)
1224512256
table.insert(out, ") == \"integer\"")
12246-
elseif node.e2.casttype.typename == "nil" then
12257+
elseif ct.typename == "nil" then
1224712258
add_child(out, children[1], "", indent)
1224812259
table.insert(out, " == nil")
1224912260
else

0 commit comments

Comments
 (0)