diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-04 15:01:42 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-04 15:01:42 -0200 |
| commit | 28d829c86712ce5bc453feccc5129a32f78d80c0 (patch) | |
| tree | 58197e763ca643bbe4a042372927bf81092b141c /testes | |
| parent | 6d04537ea660fd12fc16c328366b701fabaf4919 (diff) | |
| download | lua-28d829c86712ce5bc453feccc5129a32f78d80c0.tar.gz lua-28d829c86712ce5bc453feccc5129a32f78d80c0.tar.bz2 lua-28d829c86712ce5bc453feccc5129a32f78d80c0.zip | |
Calls cannot be tail in the scope of a to-be-closed variable
A to-be-closed variable must be closed when a block ends, so even
a 'return foo()' cannot directly returns the results of 'foo'; the
function must close the scope before returning.
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/locals.lua | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/testes/locals.lua b/testes/locals.lua index 24681dd9..340af61c 100644 --- a/testes/locals.lua +++ b/testes/locals.lua | |||
| @@ -225,21 +225,24 @@ end | |||
| 225 | 225 | ||
| 226 | 226 | ||
| 227 | do | 227 | do |
| 228 | -- to-be-closed variables must be closed in tail calls | 228 | -- calls cannot be tail in the scope of to-be-closed variables |
| 229 | local X, Y | 229 | local X, Y |
| 230 | local function foo () | 230 | local function foo () |
| 231 | local *toclose _ = function () Y = 10 end | 231 | local *toclose _ = function () Y = 10 end |
| 232 | assert(X == 20 and Y == nil) | 232 | assert(X == true and Y == nil) -- 'X' not closed yet |
| 233 | return 1,2,3 | 233 | return 1,2,3 |
| 234 | end | 234 | end |
| 235 | 235 | ||
| 236 | local function bar () | 236 | local function bar () |
| 237 | local *toclose _ = function () X = 20 end | 237 | local *toclose _ = function () X = false end |
| 238 | return foo() | 238 | X = true |
| 239 | do | ||
| 240 | return foo() -- not a tail call! | ||
| 241 | end | ||
| 239 | end | 242 | end |
| 240 | 243 | ||
| 241 | local a, b, c, d = bar() | 244 | local a, b, c, d = bar() |
| 242 | assert(a == 1 and b == 2 and c == 3 and X == 20 and Y == 10 and d == nil) | 245 | assert(a == 1 and b == 2 and c == 3 and X == false and Y == 10 and d == nil) |
| 243 | end | 246 | end |
| 244 | 247 | ||
| 245 | 248 | ||
