Error object cannot be nil

Lua will change a nil as error object to a string message, so that it
never reports an error with nil as the error object.
This commit is contained in:
Roberto Ierusalimschy
2025-02-28 14:53:58 -03:00
parent 127a8e80fe
commit ee99452158
4 changed files with 23 additions and 8 deletions

View File

@@ -46,7 +46,7 @@ end
assert(doit("error('hi', 0)") == 'hi')
-- test nil error message
assert(doit("error()") == nil)
assert(doit("error()") == "<error object is nil>")
-- test common errors/errors that crashed in the past
@@ -614,7 +614,7 @@ do
assert(not res and msg == t)
res, msg = pcall(function () error(nil) end)
assert(not res and msg == nil)
assert(not res and msg == "<error object is nil>")
local function f() error{msg='x'} end
res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
@@ -634,7 +634,7 @@ do
assert(not res and msg == t)
res, msg = pcall(assert, nil, nil)
assert(not res and msg == nil)
assert(not res and type(msg) == "string")
-- 'assert' without arguments
res, msg = pcall(assert)