Skip to content

Commit 4108107

Browse files
committed
fix contract not widening certain literal fields
1 parent db980e9 commit 4108107

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

nattlua/analyzer/operators/index.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ local function index_table(analyzer, self, key, raw)
8787

8888
if not val then return val, err end
8989

90+
local contract_val = val
91+
9092
do
9193
local val = self:GetMutatedValue(key, analyzer:GetScope())
9294

9395
if val then
9496
if val.Type == "union" then val = val:Copy(nil, true) end
9597

96-
if not val:GetContract() then val:SetContract(val) end
98+
if not val:GetContract() then val:SetContract(contract_val) end
9799

98100
if val.Type == "union" then analyzer:TrackTableIndex(self, key, val) end
99101

@@ -107,7 +109,11 @@ local function index_table(analyzer, self, key, raw)
107109
if self:HasMutations() then
108110
local tracked = self:GetMutatedValue(key, analyzer:GetScope())
109111

110-
if tracked then val = tracked end
112+
if tracked then
113+
val = tracked
114+
115+
if not val:GetContract() then val:SetContract(contract_val) end
116+
end
111117
end
112118

113119
if val.Type == "union" then analyzer:TrackTableIndex(self, key, val) end

test/tests/nattlua/analyzer/table.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ test("typed table correct assignment not should error", function()
6262
]])
6363
end)
6464

65+
test("nested typed list field should widen to contract", function()
66+
analyze([[
67+
local tbl: {loadings = List<|boolean|>} = {
68+
loadings = {true},
69+
}
70+
71+
tbl.loadings[1] = false
72+
attest.equal(tbl.loadings[1], false)
73+
]])
74+
end)
75+
76+
test("nested typed table field should widen to contract", function()
77+
analyze([[
78+
local tbl: {inner = {flag = boolean}} = {
79+
inner = {flag = true},
80+
}
81+
82+
tbl.inner.flag = false
83+
attest.equal(tbl.inner.flag, false)
84+
]])
85+
end)
86+
6587
test("self referenced tables should be equal", function()
6688
local analyzer = analyze([[
6789
local a = {a=true}

0 commit comments

Comments
 (0)