Skip to content

Commit

Permalink
fix: do not try to decide validity of unions too early
Browse files Browse the repository at this point in the history
Looks like this moves some errors around, but overall it looks like
a better behavior.

Fixes #903.
  • Loading branch information
hishamhm committed Jan 28, 2025
1 parent 85972a2 commit e1dfdc5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
3 changes: 2 additions & 1 deletion spec/lang/arguments/union_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("union argument", function()
R.f = function(a: boolean | E)
end
]], {
{ y = 9, msg = "unknown type E" }
{ y = 9, msg = "argument 1: types are incompatible" },
{ y = 9, msg = "unknown type E" },
}))
end)
21 changes: 21 additions & 0 deletions spec/lang/declaration/union_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,25 @@ describe("union declaration", function()
end
end
]]))

it("do not try to resolve validity of unions too early (regression test for #903)", util.check([[
local interface A
where true
end
local record D
v: A | C
end
local interface C
where true
end
local d: D
local v = d.v
if v is A then
end
]]))

end)
4 changes: 2 additions & 2 deletions spec/lang/macroexp/is_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ describe("__is with macroexp", function()

it("can expand self in an expression", util.gen([[
local record R1
metamethod __is: function(self: R1|R2): boolean = macroexp(self: R1|R2): boolean
metamethod __is: function(self: R1): boolean = macroexp(self: R1): boolean
return self.kind == "r1"
end
kind: string
end
local record R2
metamethod __is: function(self: R1|R2): boolean = macroexp(self: R1|R2): boolean
metamethod __is: function(self: R2): boolean = macroexp(self: R2): boolean
return self.kind == "r2"
end
Expand Down
6 changes: 3 additions & 3 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13753,9 +13753,9 @@ self:expand_type(node, values, elements) })
},
["union"] = {
after = function(self, typ, _children)
local ok, err = is_valid_union(typ)
if not ok then
return err and self.errs:invalid_at(typ, err, typ) or a_type(typ, "invalid", {})
local _, err = is_valid_union(typ)
if err then
return self.errs:invalid_at(typ, err, typ)
end
return typ
end,
Expand Down
6 changes: 3 additions & 3 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -13753,9 +13753,9 @@ do
},
["union"] = {
after = function(self: TypeChecker, typ: UnionType, _children: {Type}): Type
local ok, err = is_valid_union(typ)
if not ok then
return err and self.errs:invalid_at(typ, err, typ) or an_invalid(typ)
local _, err = is_valid_union(typ)
if err then
return self.errs:invalid_at(typ, err, typ)
end
return typ
end
Expand Down

0 comments on commit e1dfdc5

Please sign in to comment.