Skip to content

Commit 493061b

Browse files
committed
improve typing of assert function
1 parent b0936f4 commit 493061b

File tree

5 files changed

+2331
-9
lines changed

5 files changed

+2331
-9
lines changed

nattlua/analyzer/operators/function_call_analyzer.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ return function(analyzer, obj, input)
131131
)
132132
end
133133

134+
-- if you call print(SOMEFUNCTION()), SOMEFUNCTION is not defined and thus returns any, which when called
135+
-- results in ((any,)*inf,)
136+
-- when the input signature is also ((TYPE,)*inf,) both will result in safe length being 0
137+
-- so no arguments are passed. This feels wrong, maybe at least 1 argument? (however technically this is also wrong?)
138+
if input:GetElementCount() == math.huge and signature_arguments:GetElementCount() == math.huge then
139+
input = Tuple({input:GetWithNumber(1)})
140+
end
141+
134142
local len = signature_arguments:GetSafeLength(input)
135143
local packed_args = {input:Unpack(len)}
136144

nattlua/definitions/lua/globals.nlua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,15 @@ analyzer function rawget(tbl: {[any] = any} | {}, key: any)
247247
return analyzer:AssertFallback(types.Nil(), analyzer:IndexOperator(tbl, key, true))
248248
end
249249

250-
analyzer function assert(obj: any, msg: string | nil, level: number | nil)
250+
analyzer function assert(...: ...any)
251+
local obj = select(1, ...)
252+
local potential_msg = select(2, ...)
251253
if not analyzer:IsDefinetlyReachable() then
252254
analyzer:ThrowSilentError(obj)
253255

254-
if obj.Type == "union" then return obj:RemoveCertainlyFalsy() end
256+
if obj.Type == "union" then return obj:RemoveCertainlyFalsy(), select(2, ...) end
255257

256-
return obj
258+
return ...
257259
end
258260

259261
if obj.Type == "union" then
@@ -263,21 +265,21 @@ analyzer function assert(obj: any, msg: string | nil, level: number | nil)
263265
end
264266

265267
if obj:IsTruthy() and not obj:IsFalsy() then
266-
if obj.Type == "union" then return obj:RemoveCertainlyFalsy() end
268+
if obj.Type == "union" then return obj:RemoveCertainlyFalsy(), select(2, ...) end
267269
end
268270

269271
if obj:IsFalsy() then
270272
analyzer:AssertError(
271273
obj,
272-
type_errors.plain_error(msg and msg:GetData() or "assertion failed!"),
273-
level and level:GetData() or nil,
274+
type_errors.plain_error(potential_msg and potential_msg:GetData() or "assertion failed!"),
275+
nil,
274276
obj:IsTruthy()
275277
)
276278

277-
if obj.Type == "union" then return obj:RemoveCertainlyFalsy() end
279+
if obj.Type == "union" then return obj:RemoveCertainlyFalsy(), select(2, ...) end
278280
end
279281

280-
return obj
282+
return ...
281283
end
282284

283285
analyzer function error(msg: string, level: number | nil)

0 commit comments

Comments
 (0)