Skip to content

Commit fe6106f

Browse files
committed
improve tuple slice which catches more errors
1 parent b2152a5 commit fe6106f

File tree

12 files changed

+38
-20
lines changed

12 files changed

+38
-20
lines changed

examples/techniques/data_structures.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ do
202202
view_slice:Set(1, 666)
203203
assert(view_slice:Get(1) == 666)
204204
assert(arr:Get(6) == 666)
205-
local slice = arr:Slice(5, 7)
205+
local slice = assert(arr:Slice(5, 7))
206206
assert(slice:Get(0) == 1)
207207
assert(slice:Get(1) == 666)
208208
assert(slice:Get(2) == 3)

nattlua/analyzer/operators/function_call_body.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ return function(self, obj, input)
472472

473473
if self:IsRuntime() then
474474
if identifier.value.value == "..." then
475-
self:CreateLocalValue(identifier.value.value, input:Slice(argi))
475+
self:CreateLocalValue(identifier.value.value, self:Assert(input:Slice(argi)))
476476
else
477477
local val, err = input:GetWithNumber(argi)
478478

@@ -552,7 +552,7 @@ return function(self, obj, input)
552552
end
553553
end
554554

555-
obj:GetInputSignature():Merge(input:Slice(1, obj:GetInputSignature():GetMinimumLength()))
555+
obj:GetInputSignature():Merge(self:Assert(input:Slice(1, obj:GetInputSignature():GetMinimumLength())))
556556
end
557557

558558
do -- this is for the emitter

nattlua/definitions/lua/globals.nlua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ type xpcall = ReturnIfNonLiteral<|
376376
type select = ReturnIfNonLiteral<|
377377
analyzer function(index: 1 .. inf | "#", ...: ...any): ...any
378378
local x = ...
379-
380379
if x and x.Type == "tuple" then
381380
return select(index:GetData(), x:Unpack())
382381
end
@@ -385,6 +384,15 @@ type select = ReturnIfNonLiteral<|
385384
end
386385
|>
387386

387+
type select_type = analyzer function(index: 1 .. inf | "#", ...: ...any): ...any
388+
local x = ...
389+
if x and x.Type == "tuple" then
390+
return x:Slice(index:GetData())
391+
end
392+
393+
return select(index:GetData(), ...)
394+
end
395+
388396
analyzer function type(obj: any)
389397
if obj.Type == "any" then return types.String() end
390398

nattlua/definitions/utility.nlua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050

5151
analyzer function return_type(func: Function, i: number | nil)
5252
local i = i and i:GetData() or nil
53-
return {func:GetOutputSignature():Slice(i, i)}
53+
return {analyzer:Assert(func:GetOutputSignature():Slice(i, i))}
5454
end
5555

5656
analyzer function set_return_type(func: Function, tup: any)
@@ -61,7 +61,7 @@ end
6161

6262
analyzer function argument_type(func: Function, i: number | nil)
6363
local i = i and i:GetData() or nil
64-
return {func:GetInputSignature():Slice(i, i)}
64+
return {analyzer:Assert(func:GetInputSignature():Slice(i, i))}
6565
end
6666

6767
analyzer function exclude(T: any, U: any)

nattlua/lexer/lexer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ do
647647
META.ReadSingleQuoteString = build_string_reader("single", "'")
648648
end
649649

650-
local ReadSymbolFromTrie = BuildTrieReader(runtime_syntax:GetSymbols())
650+
local ReadSymbolFromTrie = BuildTrieReader(runtime_syntax:GetSymbols(), false)
651651

652652
function META:ReadSymbol()
653653
if ReadSymbolFromTrie(self) then return "symbol" end

nattlua/parser/base.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ do
109109
self:PushContextValue("parent_node", node)
110110
end
111111

112-
function META:GetParentNode(level)
112+
function META:GetParentNode(level--[[#: nil | number]])
113113
return self:GetContextValue("parent_node", level) or false--[[# as Node | false]]
114114
end
115115

nattlua/types/tuple.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,19 @@ function META:Slice(start--[[#: number]], stop--[[#: number]])
564564
local data = {}
565565

566566
for i = start, stop do
567-
table.insert(data, (self:GetData()--[[# as TBaseType]])[i])
567+
local val, err = self:GetWithNumber(i)
568+
569+
if not val then return val, err end
570+
571+
table.insert(data, val)
568572
end
569573

570-
return self:Copy():SetData(data)
574+
local copy = META.New(data)
575+
copy.Repeat = self.Repeat
576+
copy.Remainder = self.Remainder and self.Remainder:Copy() or false
577+
copy.Unpackable = self.Unpackable
578+
copy:CopyInternalsFrom(self)
579+
return copy
571580
end
572581

573582
function META:GetFirstValue()

test/tests/editor_helper.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ do
124124
local code, line_pos, char_pos = get_line_char(code)
125125
local editor = single_file(code)
126126
local new_code = apply_edits(code, editor:GetRenameInstructions(path, line_pos, char_pos, "LOL"))
127-
assert(code:gsub("foo", "LOL") == new_code)
127+
equal(code:gsub("foo", "LOL"), new_code)
128128
end
129129

130130
do
@@ -252,4 +252,5 @@ do
252252
local integers = helper:GetSemanticTokens(path)
253253
assert((#integers / 5) == 2)
254254
end
255-
_G.TEST = false
255+
256+
_G.TEST = false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
local foo = require("test.tests.nattlua.analyzer.file_importing.complex.foo")()
1+
local foo = require("test.tests.nattlua.analyzer.file_importing.complex.foo")(1)
22
local bar = require("test.tests.nattlua.analyzer.file_importing.complex.bar")
33
return foo.get() + bar

test/tests/nattlua/analyzer/load.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ analyze[[
66
attest.equal(assert(load("return " .. 2))(), 2)
77
]]
88
analyze[[
9-
attest.equal(require("test.tests.nattlua.analyzer.file_importing.expect_5")(5), 1337)
9+
attest.equal(require("test.tests.nattlua.analyzer.file_importing.expect_5")(5, nil, nil, nil), 1337)
1010
]]
1111
-- file import
1212
equal(
@@ -24,7 +24,7 @@ equal(
2424
end)
2525
]=]
2626
analyze([[
27-
attest.equal(loadfile("test/tests/nattlua/analyzer/file_importing/complex/main.nlua")(), 14)
27+
attest.equal(loadfile("test/tests/nattlua/analyzer/file_importing/complex/main.nlua")(1), 14)
2828
]])
2929
analyze[[
3030
attest.equal(require("test.tests.nattlua.analyzer.file_importing.complex.adapter"), 14)

0 commit comments

Comments
 (0)