lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


No other popular programming language requires break statements to be at the end of the block. I thought the main reason for it was to maintain consistency with the return statement. As the return statement needing to be at the end of the block is a clever way to avoid syntax ambiguities.

Rustum Zia (Rusty) (he/him)
On Jul 26, 2022, 7:36 AM -0400, Lars Müller <appgurulars@gmx.de>, wrote:

Excerpt from the Lua grammar:

block : {stat} [retstat] ;
stat : break | ... ;
retstat : return [explist] [';'] ;

"return" is the only "retstat" and thus the only statement that is syntactically only allowed at the end of the block:

(function() return; a = 1 end)()

Is rejected by all Lua versions with the syntax error

'end' expected near ...

Why is this not extended to "break", which is a normal statement and thus allowed to precede arbitrary other statements?

Currently, Lua 5.2 to Lua 5.4 happily allow abominations such as

while true do break break break end
while true do break a = 1 end

and just execute them as expected; Lua 5.1 & LuaJIT however used to throw the syntax error

'end' expected near ...

Is this change in behavior an oversight (regression?) or an intentional change (assuming the latter since the grammar indicates it)? Why should "break" statements be allowed before the end of a block?