Skip to content

Commit

Permalink
refactor: move if not t test near the bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Jan 28, 2025
1 parent dc0d26c commit ec648bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
36 changes: 18 additions & 18 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13143,9 +13143,6 @@ self:expand_type(node, values, elements) })
if mt_name then
t, meta_on_operator = self:check_metamethod(node, mt_name, ra, nil, ua, nil)
end
if not t then
t = self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' on type %s", ua)
end
end

if ra.typename == "map" then
Expand All @@ -13156,10 +13153,6 @@ self:expand_type(node, values, elements) })
end
end

if t.typename ~= "boolean" and not is_unknown(t) then
node.known = FACT_TRUTHY
end

if node.op.op == "~" and self.gen_target == "5.1" then
if meta_on_operator then
self.all_needs_compat["mt"] = true
Expand All @@ -13170,6 +13163,14 @@ self:expand_type(node, values, elements) })
end
end

if not t then
return self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' on type %s", ua)
end

if not (t.typename == "boolean" or is_unknown(t)) then
node.known = FACT_TRUTHY
end

return t
end

Expand Down Expand Up @@ -13209,22 +13210,11 @@ self:expand_type(node, values, elements) })
ua, ub = ub, ua
end
end
if not t then
t = self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", ua, ub)
if node.op.op == "or" then
local u = unite(node, { ua, ub })
if u.typename == "union" and is_valid_union(u) then
self.errs:add_warning("hint", node, "if a union type was intended, consider declaring it explicitly")
end
end
end
end

if (not t) and ua.typename == "nominal" and ub.typename == "nominal" and not meta_on_operator then
if self:is_a(ua, ub) then
t = ua
else
self.errs:add(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for distinct nominal types %s and %s", ua, ub)
end
end

Expand All @@ -13250,6 +13240,16 @@ self:expand_type(node, values, elements) })
end
end

if not t then
if node.op.op == "or" then
local u = unite(node, { ua, ub })
if u.typename == "union" and is_valid_union(u) then
self.errs:add_warning("hint", node, "if a union type was intended, consider declaring it explicitly")
end
end
return self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", ua, ub)
end

return t
end

Expand Down
36 changes: 18 additions & 18 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -13143,9 +13143,6 @@ do
if mt_name then
t, meta_on_operator = self:check_metamethod(node, mt_name, ra, nil, ua, nil)
end
if not t then
t = self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' on type %s", ua)
end
end

if ra is MapType then
Expand All @@ -13156,10 +13153,6 @@ do
end
end

if t.typename ~= "boolean" and not is_unknown(t) then
node.known = FACT_TRUTHY
end

if node.op.op == "~" and self.gen_target == "5.1" then
if meta_on_operator then
self.all_needs_compat["mt"] = true
Expand All @@ -13170,6 +13163,14 @@ do
end
end

if not t then
return self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' on type %s", ua)
end

if not (t is BooleanType or is_unknown(t)) then
node.known = FACT_TRUTHY
end

return t
end

Expand Down Expand Up @@ -13209,22 +13210,11 @@ do
ua, ub = ub, ua
end
end
if not t then
t = self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", ua, ub)
if node.op.op == "or" then
local u = unite(node, {ua, ub})
if u is UnionType and is_valid_union(u) then
self.errs:add_warning("hint", node, "if a union type was intended, consider declaring it explicitly")
end
end
end
end

if (not t) and ua is NominalType and ub is NominalType and not meta_on_operator then
if self:is_a(ua, ub) then
t = ua
else
self.errs:add(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for distinct nominal types %s and %s", ua, ub)
end
end

Expand All @@ -13250,6 +13240,16 @@ do
end
end

if not t then
if node.op.op == "or" then
local u = unite(node, {ua, ub})
if u is UnionType and is_valid_union(u) then
self.errs:add_warning("hint", node, "if a union type was intended, consider declaring it explicitly")
end
end
return self.errs:invalid_at(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", ua, ub)
end

return t
end

Expand Down

0 comments on commit ec648bc

Please sign in to comment.