Skip to content

Commit

Permalink
refactor: narrowed_from -> specialized_from
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Jan 28, 2025
1 parent a0778ac commit 09add09
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
19 changes: 11 additions & 8 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7449,9 +7449,9 @@ do
local var = scope.vars[name]
if var then
if use == "lvalue" and var.is_specialized and var.is_specialized ~= "localizing" then
if var.narrowed_from then
if var.specialized_from then
var.has_been_written_to = true
return { t = var.narrowed_from, attribute = var.attribute }, i, var.attribute
return { t = var.specialized_from, attribute = var.attribute }, i, var.attribute
end
else
if i == 1 and var.needs_compat then
Expand Down Expand Up @@ -8072,7 +8072,7 @@ do
end

var.is_specialized = specialization
var.narrowed_from = var.t
var.specialized_from = var.t
var.t = t
else
var = { t = t, attribute = attribute, is_specialized = specialization, declared_at = node }
Expand Down Expand Up @@ -10250,14 +10250,17 @@ a.types[i], b.types[i]), }
function TypeChecker:widen_in_scope(scope, var)
local v = scope.vars[var]
assert(v, "no " .. var .. " in scope")
local narrow_mode = scope.vars[var].is_specialized
if (not narrow_mode) or narrow_mode == "localizing" then
local specialization = scope.vars[var].is_specialized
if (not specialization) or
not (specialization == "narrow" or
specialization == "narrowed_declaration") then

return false
end

if v.narrowed_from then
v.t = v.narrowed_from
v.narrowed_from = nil
if v.specialized_from then
v.t = v.specialized_from
v.specialized_from = nil
v.is_specialized = nil
else
scope.vars[var] = nil
Expand Down
21 changes: 12 additions & 9 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ local record Variable
t: Type
attribute: Attribute
needs_compat: boolean
narrowed_from: Type
specialized_from: Type
is_specialized: Specialization
declared_at: Node
is_func_arg: boolean
Expand Down Expand Up @@ -7449,9 +7449,9 @@ do
local var = scope.vars[name]
if var then
if use == "lvalue" and var.is_specialized and var.is_specialized ~= "localizing" then
if var.narrowed_from then
if var.specialized_from then
var.has_been_written_to = true
return { t = var.narrowed_from, attribute = var.attribute }, i, var.attribute
return { t = var.specialized_from, attribute = var.attribute }, i, var.attribute
end
else
if i == 1 and var.needs_compat then
Expand Down Expand Up @@ -8072,7 +8072,7 @@ do
end

var.is_specialized = specialization
var.narrowed_from = var.t
var.specialized_from = var.t
var.t = t
else
var = { t = t, attribute = attribute, is_specialized = specialization, declared_at = node }
Expand Down Expand Up @@ -10250,14 +10250,17 @@ do
function TypeChecker:widen_in_scope(scope: Scope, var: string): boolean
local v = scope.vars[var]
assert(v, "no " .. var .. " in scope")
local narrow_mode = scope.vars[var].is_specialized
if (not narrow_mode) or narrow_mode == "localizing" then
local specialization = scope.vars[var].is_specialized
if (not specialization)
or not (specialization == "narrow"
or specialization == "narrowed_declaration")
then
return false
end

if v.narrowed_from then
v.t = v.narrowed_from
v.narrowed_from = nil
if v.specialized_from then
v.t = v.specialized_from
v.specialized_from = nil
v.is_specialized = nil
else
scope.vars[var] = nil
Expand Down

0 comments on commit 09add09

Please sign in to comment.