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 /manual | |
parent | 6d04537ea660fd12fc16c328366b701fabaf4919 (diff) | |
download | lua-github-28d829c86712ce5bc453feccc5129a32f78d80c0.tar.gz |
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 'manual')
-rw-r--r-- | manual/manual.of | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/manual/manual.of b/manual/manual.of index 3902f2f3..8b5e5d93 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1538,9 +1538,6 @@ except that its value is @emph{closed} whenever the variable goes out of scope, including normal block termination, exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, or exiting by an error. -If a block ends in a tail call @see{functioncall}, -all variables of the caller function go out of scope -before the start of the callee function. To \emph{close} a value has the following meaning here: If the value of the variable when it goes out of scope is a function, @@ -2038,8 +2035,8 @@ A call of the form @T{f'@rep{string}'} is syntactic sugar for @T{f('@rep{string}')}; that is, the argument list is a single literal string. -A call of the form @T{return @rep{functioncall}} is called -a @def{tail call}. +A call of the form @T{return @rep{functioncall}} not in the +scope of a to-be-closed variable is called a @def{tail call}. Lua implements @def{proper tail calls} (or @emph{proper tail recursion}): in a tail call, @@ -2049,13 +2046,15 @@ a program can execute. However, a tail call erases any debug information about the calling function. Note that a tail call only happens with a particular syntax, -where the @Rw{return} has one single function call as argument; -this syntax makes the calling function return exactly -the returns of the called function. +where the @Rw{return} has one single function call as argument, +and it is outside the scope of any to-be-closed variable. +This syntax makes the calling function return exactly +the returns of the called function, +without any intervening action. So, none of the following examples are tail calls: @verbatim{ return (f(x)) -- results adjusted to 1 -return 2 * f(x) +return 2 * f(x) -- result multiplied by 2 return x, f(x) -- additional results f(x); return -- results discarded return x or f(x) -- results adjusted to 1 |