Skip to content

Add labeled break and continue statements#219

Open
SPY wants to merge 2 commits into
luau-lang:masterfrom
SPY:syntax-labeled-break-continue
Open

Add labeled break and continue statements#219
SPY wants to merge 2 commits into
luau-lang:masterfrom
SPY:syntax-labeled-break-continue

Conversation

@SPY

@SPY SPY commented Jul 7, 2026

Copy link
Copy Markdown

@aatxe aatxe changed the title Add labeled break and return statements Add labeled break and continue statements Jul 7, 2026
@Dekkonot

Dekkonot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

My preference would be to have the label follow the do I think, since it makes it more obvious that it's attached to that scope rather than just the loop. It also solves some of the ambiguity problems of less ugly syntax (my apologies to anyone who is in love with ::label::) because the position is already a syntax error with something as simple as a colon:

for i = 1, 10 do: label
  if i == 8 then
    continue label
  end
end

While I'm not strictly proposing this syntax (it gets weird due to e.g. do:label("foo"):lower()), it gives us more options than making it a prefix to the keywords.

repeat has the same property, which is that a lot of symbols after it are just outright syntax errors right now.

@bradsharp

Copy link
Copy Markdown
Contributor

While this would be a nice feature I am not sure about the syntax to to add a label. Could we lean into attributes here with something like @label <name>? Alternately, could this be a number where break 1 is equivalent to break and break 2 would break the outer loop?

@nnullcolumn

nnullcolumn commented Jul 8, 2026

Copy link
Copy Markdown

While this would be a nice feature I am not sure about the syntax to to add a label. Could we lean into attributes here with something like @label <name>? Alternately, could this be a number where break 1 is equivalent to break and break 2 would break the outer loop?

break 1 or break 2, albeit admirably minimal, seem like they could cause mistakes - e.g. moving nested code from one spot to another. Attributes historically haven't changed the runtime semantics of programs (apart from arguably native in niche float ways), so it feels bad to overload them for control flow. I like the solution dekkonot proposed even if it allows people to mangle their syntax a bit.

@SPY

SPY commented Jul 8, 2026

Copy link
Copy Markdown
Author

My preference would be to have the label follow the do I think, since it makes it more obvious that it's attached to that scope rather than just the loop.

I like it.

my apologies to anyone who is in love with ::label::

Never was attached to it. It looks ugly :)

@Nicant02

Nicant02 commented Jul 8, 2026

Copy link
Copy Markdown

I like this, something like this would be nice to have.

I enjoy the extended do block syntax, an uncommon pattern I do sometimes when I need to check a bunch of different things that chain off of each other is something like:

-- execution starts
repeat
	local character = Player.Character
	if not character then
		break
	end
	
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then
		break
	end
	ApplyDamage(humanoid, 10)
until true
-- execution continues

@InfraredGodYT

Copy link
Copy Markdown

My preference would be to have the label follow the do I think, since it makes it more obvious that it's attached to that scope rather than just the loop.

I like it.

my apologies to anyone who is in love with ::label::

Never was attached to it. It looks ugly :)

I was unsure about the syntax at first, but using do: label sealed the deal for me. If this isn’t too complex to add, it’ll definitely be a nice addition.

@alexmccord

alexmccord commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Even label: (do|for|while|repeat) and break label are syntax that can be supported retroactively. No need for ::label:: gunk or postfix kw: label. And quite frankly, I don't want to read for k, v in t do: label, I want to read label: for k, v in t do.

@nnullcolumn

Copy link
Copy Markdown

Even label: (do|for|while|repeat) and break label are syntax that can be supported retroactively. No need for ::label:: gunk or postfix kw: label. And quite frankly, I don't want to read for k, v in t do: label, I want to read label: for k, v in t do.

imo, label: for is crazy visual noise and looks like a type annotation in all the wrong ways

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants