|
Correct me if I'm wrong but at https://github.com/lua/lua/blob/master/lparser.c#L1814 the tailcall is inserted. For that fs->bl->insidetbc needs to be zero. It is set at https://github.com/lua/lua/blob/master/lparser.c#L1710 in checktoclose called from localstat but not in the for loop case at https://github.com/lua/lua/blob/master/lparser.c#L1602. In for loop case the fs->bl->insidetbc is never set and therfore the call is a tailcall.
The problem is that 'tail' is no longer a tail call. In the general case the file can only be closed after 'tail' returns, like in this convoluted example: