diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-11-11 14:36:16 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-11-11 14:36:16 -0300 |
| commit | 81f4def54f440e045b1401f11ef78b65b56b7abe (patch) | |
| tree | fe3c6be94cd65269bd4e22d0a737475eb4b79f31 | |
| parent | e44f3a2ffc7ced5e75cca7657aaa60ef27da89aa (diff) | |
| download | lua-81f4def54f440e045b1401f11ef78b65b56b7abe.tar.gz lua-81f4def54f440e045b1401f11ef78b65b56b7abe.tar.bz2 lua-81f4def54f440e045b1401f11ef78b65b56b7abe.zip | |
Correction in line info for semantic errors
Semantic errors should refer the last used token, not the next one.
Diffstat (limited to '')
| -rw-r--r-- | lcode.c | 1 | ||||
| -rw-r--r-- | testes/errors.lua | 64 |
2 files changed, 43 insertions, 22 deletions
| @@ -45,6 +45,7 @@ l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { | |||
| 45 | va_list argp; | 45 | va_list argp; |
| 46 | pushvfstring(ls->L, argp, fmt, msg); | 46 | pushvfstring(ls->L, argp, fmt, msg); |
| 47 | ls->t.token = 0; /* remove "near <token>" from final message */ | 47 | ls->t.token = 0; /* remove "near <token>" from final message */ |
| 48 | ls->linenumber = ls->lastline; /* back to line of last used token */ | ||
| 48 | luaX_syntaxerror(ls, msg); | 49 | luaX_syntaxerror(ls, msg); |
| 49 | } | 50 | } |
| 50 | 51 | ||
diff --git a/testes/errors.lua b/testes/errors.lua index 00a43fc6..c9d85099 100644 --- a/testes/errors.lua +++ b/testes/errors.lua | |||
| @@ -418,28 +418,28 @@ end | |||
| 418 | 418 | ||
| 419 | -- testing line error | 419 | -- testing line error |
| 420 | 420 | ||
| 421 | local function lineerror (s, l) | 421 | local function lineerror (s, l, w) |
| 422 | local err,msg = pcall(load(s)) | 422 | local err,msg = pcall(load(s)) |
| 423 | local line = tonumber(string.match(msg, ":(%d+):")) | 423 | local line = tonumber(string.match(msg, ":(%d+):")) |
| 424 | assert(line == l or (not line and not l)) | 424 | assert((line == l or (not line and not l)) and string.find(msg, w)) |
| 425 | end | 425 | end |
| 426 | 426 | ||
| 427 | lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) | 427 | lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2, "limit") |
| 428 | lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3) | 428 | lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3, "to call") |
| 429 | lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4) | 429 | lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4, "to call") |
| 430 | lineerror("function a.x.y ()\na=a+1\nend", 1) | 430 | lineerror("function a.x.y ()\na=a+1\nend", 1, "index") |
| 431 | 431 | ||
| 432 | lineerror("a = \na\n+\n{}", 3) | 432 | lineerror("a = \na\n+\n{}", 3, "arithmetic") |
| 433 | lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6) | 433 | lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6, "arithmetic") |
| 434 | lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3) | 434 | lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3, "arithmetic") |
| 435 | 435 | ||
| 436 | lineerror("a\n=\n-\n\nprint\n;", 3) | 436 | lineerror("a\n=\n-\n\nprint\n;", 3, "arithmetic") |
| 437 | 437 | ||
| 438 | lineerror([[ | 438 | lineerror([[ |
| 439 | a | 439 | a |
| 440 | ( -- << | 440 | ( -- << |
| 441 | 23) | 441 | 23) |
| 442 | ]], 2) | 442 | ]], 2, "call") |
| 443 | 443 | ||
| 444 | lineerror([[ | 444 | lineerror([[ |
| 445 | local a = {x = 13} | 445 | local a = {x = 13} |
| @@ -449,7 +449,7 @@ x | |||
| 449 | ( -- << | 449 | ( -- << |
| 450 | 23 | 450 | 23 |
| 451 | ) | 451 | ) |
| 452 | ]], 5) | 452 | ]], 5, "call") |
| 453 | 453 | ||
| 454 | lineerror([[ | 454 | lineerror([[ |
| 455 | local a = {x = 13} | 455 | local a = {x = 13} |
| @@ -459,17 +459,17 @@ x | |||
| 459 | ( | 459 | ( |
| 460 | 23 + a | 460 | 23 + a |
| 461 | ) | 461 | ) |
| 462 | ]], 6) | 462 | ]], 6, "arithmetic") |
| 463 | 463 | ||
| 464 | local p = [[ | 464 | local p = [[ |
| 465 | function g() f() end | 465 | function g() f() end |
| 466 | function f(x) error('a', XX) end | 466 | function f(x) error('a', XX) end |
| 467 | g() | 467 | g() |
| 468 | ]] | 468 | ]] |
| 469 | XX=3;lineerror((p), 3) | 469 | XX=3;lineerror((p), 3, "a") |
| 470 | XX=0;lineerror((p), false) | 470 | XX=0;lineerror((p), false, "a") |
| 471 | XX=1;lineerror((p), 2) | 471 | XX=1;lineerror((p), 2, "a") |
| 472 | XX=2;lineerror((p), 1) | 472 | XX=2;lineerror((p), 1, "a") |
| 473 | _G.XX, _G.g, _G.f = nil | 473 | _G.XX, _G.g, _G.f = nil |
| 474 | 474 | ||
| 475 | 475 | ||
| @@ -477,7 +477,7 @@ lineerror([[ | |||
| 477 | local b = false | 477 | local b = false |
| 478 | if not b then | 478 | if not b then |
| 479 | error 'test' | 479 | error 'test' |
| 480 | end]], 3) | 480 | end]], 3, "test") |
| 481 | 481 | ||
| 482 | lineerror([[ | 482 | lineerror([[ |
| 483 | local b = false | 483 | local b = false |
| @@ -487,7 +487,7 @@ if not b then | |||
| 487 | error 'test' | 487 | error 'test' |
| 488 | end | 488 | end |
| 489 | end | 489 | end |
| 490 | end]], 5) | 490 | end]], 5, "test") |
| 491 | 491 | ||
| 492 | lineerror([[ | 492 | lineerror([[ |
| 493 | _ENV = 1 | 493 | _ENV = 1 |
| @@ -495,7 +495,7 @@ global function foo () | |||
| 495 | local a = 10 | 495 | local a = 10 |
| 496 | return a | 496 | return a |
| 497 | end | 497 | end |
| 498 | ]], 2) | 498 | ]], 2, "index") |
| 499 | 499 | ||
| 500 | 500 | ||
| 501 | -- bug in 5.4.0 | 501 | -- bug in 5.4.0 |
| @@ -503,17 +503,37 @@ lineerror([[ | |||
| 503 | local a = 0 | 503 | local a = 0 |
| 504 | local b = 1 | 504 | local b = 1 |
| 505 | local c = b % a | 505 | local c = b % a |
| 506 | ]], 3) | 506 | ]], 3, "perform") |
| 507 | 507 | ||
| 508 | do | 508 | do |
| 509 | -- Force a negative estimate for base line. Error in instruction 2 | 509 | -- Force a negative estimate for base line. Error in instruction 2 |
| 510 | -- (after VARARGPREP, GETGLOBAL), with first absolute line information | 510 | -- (after VARARGPREP, GETGLOBAL), with first absolute line information |
| 511 | -- (forced by too many lines) in instruction 0. | 511 | -- (forced by too many lines) in instruction 0. |
| 512 | local s = string.format("%s return __A.x", string.rep("\n", 300)) | 512 | local s = string.format("%s return __A.x", string.rep("\n", 300)) |
| 513 | lineerror(s, 301) | 513 | lineerror(s, 301, "index") |
| 514 | end | 514 | end |
| 515 | 515 | ||
| 516 | 516 | ||
| 517 | local function stxlineerror (s, l, w) | ||
| 518 | local err,msg = load(s) | ||
| 519 | local line = tonumber(string.match(msg, ":(%d+):")) | ||
| 520 | assert((line == l or (not line and not l)) and string.find(msg, w, 1, true)) | ||
| 521 | end | ||
| 522 | |||
| 523 | stxlineerror([[ | ||
| 524 | ::L1:: | ||
| 525 | ::L1:: | ||
| 526 | ]], 2, "already defined") | ||
| 527 | |||
| 528 | stxlineerror([[ | ||
| 529 | global none | ||
| 530 | local x = b | ||
| 531 | ]], 2, "not declared") | ||
| 532 | |||
| 533 | stxlineerror([[ | ||
| 534 | local <close> a, b | ||
| 535 | ]], 1, "multiple") | ||
| 536 | |||
| 517 | if not _soft then | 537 | if not _soft then |
| 518 | -- several tests that exhaust the Lua stack | 538 | -- several tests that exhaust the Lua stack |
| 519 | collectgarbage() | 539 | collectgarbage() |
