--- source: src/main.rs expression: compiled input_file: test-data/lua5.4-tests/errors.lua --- print("testing errors"); local debug = require("debug") local mt = getmetatable(_G) || {} local oldmm = mt.__index mt.__index = nil local fn checkerr(msg, f, ...) { local st, err = pcall(f, ...) assert(!st && string.find(err, msg)); } local fn doit(s) { local f, msg = load(s) if !f { return msg } local cond, msg = pcall(f) return (!cond) && msg } local fn checkmessage(prog, msg, debug) { local m = doit(prog) if debug { print(m, msg); } assert(string.find(m, msg, 1, true)); } local fn checksyntax(prog, extra, token, line) { local msg = doit(prog) if !string.find(token, "^<%a") && !string.find(token, "^char%(") { token = "'" .. token .. "'" } token = string.gsub(token, "(%p)", "%%%1") local pt = string.format(`^%%[string ".*"%%]:%d: .- near %s$`, line, token) assert(string.find(msg, pt)); assert(string.find(msg, msg, 1, true)); } assert(doit("error('hi', 0)") == 'hi'); assert(doit("error()") == nil); assert(doit("table.unpack({}, 1, n=2^30)")); assert(doit("a=math.sin()")); assert(!doit("tostring(1)") && doit("tostring()")); assert(doit("tonumber()")); assert(doit("repeat until 1; a")); assert(doit("return;;")); assert(doit("assert(false)")); assert(doit("assert(nil)")); assert(doit("function a (... , ...) end")); assert(doit("function a (, ...) end")); assert(doit("local t={}; t = t[#t] + 1")); checksyntax(` local a = {4 `, "'}' expected (to close '{' at line 1)", "", 3); { local fn checksyntax(prog, msg, line) { local st, err = load(prog) assert(string.find(err, "line " .. line)); assert(string.find(err, msg, 1, true)); } checksyntax(` a = 1 `, "label 'A' already defined", 1); checksyntax(` a = 1 do end `, "no visible label 'A'", 2); } if !T { (Message || print)('\n >>> testC not active: skipping memory message test <<<\n'); } else { print("testing memory error message"); local a = {} for i = 1, 10000 { a[(i)] = true } collectgarbage(); T.totalmem(T.totalmem() + 10000); local st, msg = pcall(fn () { for i = 1, 100000 { a[(i)] = tostring(i) } }) T.totalmem(0); assert(!st && msg == "not enough" .. " memory"); } checkmessage("a = {} + 1", "arithmetic"); checkmessage("a = {} | 1", "bitwise operation"); checkmessage("a = {} < 1", "attempt to compare"); checkmessage("a = {} <= 1", "attempt to compare"); checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'"); checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'"); checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'"); assert(!string.find(doit("a={13}; local bbbb=1; a[bbbb](3)"), "'bbbb'")); checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number"); checkmessage("a=(1)..{}", "a table value"); checkmessage("local a; a(13)", "local 'a'"); checkmessage(` local a = setmetatable({}, {__add = 34}) a = a + 1 `, "metamethod 'add'"); checkmessage(` local a = setmetatable({}, {__lt = {}}) a = a > a `, "metamethod 'lt'"); checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'"); checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'"); checkmessage("a = #print", "length of a function value"); checkmessage("a = #3", "length of a number value"); aaa = nil checkmessage("aaa.bbb:ddd(9)", "global 'aaa'"); checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'"); checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'"); checkmessage("local a,b,c; (function () a = b+1.1 end)()", "upvalue 'b'"); assert(!doit("local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")); checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'"); checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'"); checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'"); checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'"); checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'"); checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'"); checkmessage("aaa={}; x=-aaa", "global 'aaa'"); checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", "local 'bbbb'"); checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'"); checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'"); checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value"); assert(!string.find(doit("aaa={}; x=(aaa or aaa)+(aaa and aaa)"), "'aaa'")); assert(!string.find(doit("aaa={}; (aaa or aaa)()"), "'aaa'")); checkmessage("print(print < 10)", "function with number"); checkmessage("print(print < print)", "two function values"); checkmessage("print('10' < 10)", "string with number"); checkmessage("print(10 < '23')", "number with string"); checkmessage("local a = 2.0^100; x = a << 2", "local a"); checkmessage("local a = 1 >> 2.0^100", "has no integer representation"); checkmessage("local a = 10.1 << 2.0^100", "has no integer representation"); checkmessage("local a = 2.0^100 & 1", "has no integer representation"); checkmessage("local a = 2.0^100 & 1e100", "has no integer representation"); checkmessage("local a = 2.0 | 1e40", "has no integer representation"); checkmessage("local a = 2e100 ~ 1", "has no integer representation"); checkmessage("string.sub('a', 2.0^100)", "has no integer representation"); checkmessage("string.rep('a', 3.3)", "has no integer representation"); checkmessage("return 6e40 & 7", "has no integer representation"); checkmessage("return 34 << 7e30", "has no integer representation"); checkmessage("return ~-3e40", "has no integer representation"); checkmessage("return ~-3.009", "has no integer representation"); checkmessage("return 3.009 & 1", "has no integer representation"); checkmessage("return 34 >> {}", "table value"); checkmessage("a = 24 // 0", "divide by zero"); checkmessage("a = 1 % 0", "'n%0'"); checkmessage("local a = setmetatable({}, {__index = 10}).x", "attempt to index a number value"); checkmessage("for i = {}, 10 do end", "table"); checkmessage("for i = io.stdin, 10 do end", "FILE"); checkmessage("for i = {}, 10 do end", "initial value"); checkmessage("for i = 1, 'x', 10 do end", "string"); checkmessage("for i = 1, {}, 10 do end", "limit"); checkmessage("for i = 1, {} do end", "limit"); checkmessage("for i = 1, 10, print do end", "step"); checkmessage("for i = 1, 10, print do end", "function"); _G.D = debug checkmessage(` -- create light udata local x = D.upvalueid(function () return debug end, 1) D.setuservalue(x, {}) `, "light userdata"); _G.D = nil { checkmessage("math.sin(io.input())", "(number expected, got FILE*)"); _G.XX = setmetatable({}, { __name = "My Type" }) assert(string.find(tostring(XX), "^My Type")); checkmessage("io.input(XX)", "(FILE* expected, got My Type)"); checkmessage("return XX + 1", "on a My Type value"); checkmessage("return ~io.stdin", "on a FILE* value"); checkmessage("return XX < XX", "two My Type values"); checkmessage("return {} < XX", "table with My Type"); checkmessage("return XX < io.stdin", "My Type with FILE*"); _G.XX = nil if T { local x = setmetatable({}, { __name = "TABLE" }) assert(T.testC("Ltolstring -1; return 1", x) == tostring(x)); local a, b = T.testC("pushint 10; Ltolstring -2; return 2", x) assert(a == 10 && b == tostring(x)); setmetatable(x, { __tostring = fn (o) { assert(o == x); return "ABC" } }); local a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x) assert(a == x && b == 10 && c == "ABC"); } } checkmessage("(io.write or print){}", "io.write"); checkmessage("(collectgarbage or print){}", "collectgarbage"); { local f = fn (a) { return a + 1 } f = assert(load(string.dump(f, true))) assert(f(3) == 4); checkerr("^%?:%-1:", f, {}); f = fn () { local a a = {} return a + 2 } f = assert(load(string.dump(f, true))) checkerr("^%?:%-1:.*table value", f); } local t = {} for i = 1, 1000 { t[(i)] = "a = x" .. i } local s = table.concat(t, "; ") t = nil checkmessage(s .. "; a = bbb + 1", "global 'bbb'"); checkmessage("local _ENV=_ENV;" .. s .. "; a = bbb + 1", "global 'bbb'"); checkmessage(s .. "; local t = {}; a = t.bbb + 1", "field 'bbb'"); checkmessage(s .. "; local t = {}; t:bbb()", "method 'bbb'"); checkmessage(`aaa=9 repeat until 3==3 local x=math.sin(math.cos(3)) if math.sin(1) == x then return math.sin(1) end -- tail call local a,b = 1, { {x='a'..'b'..'c', y='b', z=x}, {1,2,3,4,5} or 3+3<=3+3, 3+1>3+1, {d = x and aaa[x or y]}} `, "global 'aaa'"); checkmessage(` local x,y = {},1 if math.sin(1) == 0 then return 3 end -- return x.a()`, "field 'a'"); checkmessage(` prefix = nil insert = nil while 1 do local a if nil then break end insert(prefix, a) end`, "global 'insert'"); checkmessage(` -- tail call return math.sin("a") `, "sin"); checkmessage(`collectgarbage("nooption")`, "invalid option"); checkmessage(`x = print .. "a"`, "concatenate"); checkmessage(`x = "a" .. false`, "concatenate"); checkmessage(`x = {} .. 2`, "concatenate"); checkmessage("getmetatable(io.stdin).__gc()", "no value"); checkmessage(` local Var local function main() NoSuchName (function() Var=0 end) end main() `, "global 'NoSuchName'"); print('+'); a = {} setmetatable(a, { __index = string }); checkmessage("a:sub()", "bad self"); checkmessage("string.sub('a', {})", "#2"); checkmessage("('a'):sub{}", "#1"); checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'"); checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'"); local fn f(n) { local c = coroutine.create(f) local a, b = coroutine.resume(c) return b } assert(string.find(f(), "C stack overflow")); checkmessage("coroutine.yield()", "outside a coroutine"); f = coroutine.wrap(fn () { table.sort({ 1, 2, 3 }, coroutine.yield); }) checkerr("yield across", f); idsize = 60 - 1 local fn checksize(source) { local _, msg = load("x", source) msg = string.match(msg, "^([^:]*):") assert(msg::len() <= idsize); } for i = 60 - 10, 60 + 10 { checksize("@" .. string.rep("x", i)); checksize(string.rep("x", i - 10)); checksize("=" .. string.rep("x", i)); } local fn lineerror(s, l) { local err, msg = pcall(load(s)) local line = tonumber(string.match(msg, ":(%d+):")) assert(line == l || (!line && !l)); } lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2); lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3); lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4); lineerror("function a.x.y ()\na=a+1\nend", 1); lineerror("a = \na\n+\n{}", 3); lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6); lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3); lineerror("a\n=\n-\n\nprint\n;", 3); lineerror(` a ( 23) `, 1); lineerror(` local a = {x = 13} a . x ( 23 ) `, 2); lineerror(` local a = {x = 13} a . x ( 23 + a ) `, 6); local p = ` function g() f() end function f(x) error('a', X) end g() ` X = 3 lineerror((p), 3); X = 0 lineerror((p), false); X = 1 lineerror((p), 2); X = 2 lineerror((p), 1); lineerror(` local b = false if not b then error 'test' end`, 3); lineerror(` local b = false if not b then if not b then if not b then error 'test' end end end`, 5); { local s = string.format("%s return __A.x", string.rep("\n", 300)) lineerror(s, 301); } if !_soft { collectgarbage(); print("testing stack overflow"); C = 0 local l = debug.getinfo(1, "l").currentline + 1 local fn auxy() { C = C + 1 auxy(); } global fn y() { collectgarbage("stop"); auxy(); collectgarbage("restart"); } local fn checkstackmessage(m) { print("(expected stack overflow after " .. C .. " calls)"); C = 0 return (string.find(m, "stack overflow")) } assert(checkstackmessage(doit('y()'))); assert(checkstackmessage(doit('y()'))); assert(checkstackmessage(doit('y()'))); local l1 local fn g(x) { l1 = debug.getinfo(x, "l").currentline + 2 collectgarbage("stop"); auxy(); collectgarbage("restart"); } local _, stackmsg = xpcall(g, debug.traceback, 1) print('+'); local stack = {} for line with string.gmatch(stackmsg, "[^\n]*") { local curr = string.match(line, ":(%d+):") if curr { table.insert(stack, tonumber(curr)); } } local i = 1 while stack[(i)] != l1 { assert(stack[(i)] == l); i = i + 1 } assert(i > 15); local res, msg = xpcall(error, error) assert(!res && type(msg) == 'string'); print('+'); local fn f(x) { if x == 0 { error('a\n'); } else { local aux = fn () { return f(x - 1) } local a, b = xpcall(aux, aux) return a, b } } f(3); local fn loop(x, y, z) { return 1 + loop(x, y, z) } local res, msg = xpcall(loop, fn (m) { assert(string.find(m, "stack overflow")); checkerr("error handling", loop); assert(math.sin(0) == 0); return 15 }) assert(msg == 15); local f = fn () { for i = 999900, 1000000, 1 { table.unpack({}, 1, i); } } checkerr("too many results", f); } { local t = {} local res, msg = pcall(fn () { error(t); }) assert(!res && msg == t); res, msg = pcall(fn () { error(nil); }) assert(!res && msg == nil); local fn f() { error({ msg = 'x' }); } res, msg = xpcall(f, fn (r) { return { msg = r.msg .. 'y' } }) assert(msg.msg == 'xy'); res, msg = pcall(assert, false, "X", t) assert(!res && msg == "X"); res, msg = pcall(fn () { assert(false); }) local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$") assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2); res, msg = pcall(assert, false, t) assert(!res && msg == t); res, msg = pcall(assert, nil, nil) assert(!res && msg == nil); res, msg = pcall(assert) assert(!res && string.find(msg, "value expected")); } a, b, c = xpcall(string.find, error, "alo", "al") assert(a && b == 1 && c == 2); a, b, c = xpcall(string.find, fn (x) { return {} }, true, "al") assert(!a && type(b) == "table" && c == nil); print("testing tokens in error messages"); checksyntax("syntax error", "", "error", 1); checksyntax("1.000", "", "1.000", 1); checksyntax("[[a]]", "", "[[a]]", 1); checksyntax("'aa'", "", "'aa'", 1); checksyntax("while << do end", "", "<<", 1); checksyntax("for >> do end", "", ">>", 1); checksyntax("a\1a = 1", "", "<\\1>", 1); checksyntax("\255a = 1", "", "<\\255>", 1); doit('I = load("a=9+"); a=3'); assert(a == 3 && !I); print('+'); lim = 1000 if _soft { lim = 100 } for i = 1, lim { doit('a = '); doit('a = 4+nil'); } local fn testrep(init, rep, close, repc, finalresult) { local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100) local res, msg = load(s) assert(res); if (finalresult) { assert(res() == finalresult); } s = init .. string.rep(rep, 500) local res, msg = load(s) assert(!res && (string.find(msg, "too many") || string.find(msg, "overflow"))); } testrep("local a; a", ",a", "= 1", ",1"); testrep("local a; a=", "{", "0", "}"); testrep("return ", "(", "2", ")", 2); testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2); testrep("", "do ", "", " end"); testrep("", "while a do ", "", " end"); testrep("local a; ", "if a then else ", "", " end"); testrep("", "function foo () ", "", " end"); testrep("local a = ''; return ", "a..", "'a'", "", "a"); testrep("local a = 1; return ", "a^", "a", "", 1); checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers"); local lim = 127 local s = "local function fooA ()\n local " for j = 1, lim { s = s .. "a" .. j .. ", " } s = s .. "b,c\n" s = s .. "local function fooB ()\n local " for j = 1, lim { s = s .. "b" .. j .. ", " } s = s .. "b\n" s = s .. "function fooC () return b+c" local c = 1 + 2 for j = 1, lim { s = s .. "+a" .. j .. "+b" .. j c = c + 2 } s = s .. "\nend end end" local a, b = load(s) assert(c > 255 && string.find(b, "too many upvalues") && string.find(b, "line 5")); s = "\nfunction foo ()\n local " for j = 1, 300 { s = s .. "a" .. j .. ", " } s = s .. "b\n" local a, b = load(s) assert(string.find(b, "line 2") && string.find(b, "too many local variables")); mt.__index = oldmm print('OK');