Skip to content

Support labeled statements #2889

Open
@wrnrlr

Description

@wrnrlr

Feature suggestion

Labeled statements can come in handy when implementing complex control flow with multiple nested loops and if statements.

Currently parser will complain

function test1():void {
  let str = '';
  loop1: for (let i = 0; i < 5; i++) {
    if (i === 1) {
      continue loop1;
    }
    str = str + i;
  }
}
ERROR TS1109: Expression expected.
    :
 11 │ loop1: for (let i = 0; i < 5; i++) {
    │      ~
    └─ in assembly/index.ts(11,8)

Activity

KazChe

KazChe commented on Dec 13, 2024

@KazChe
Contributor

If it's decided to move ahead with this, I was curious and had some ideas and questions

I've been digging into the codebase around parser, complier and how AST implementation looks like when I came across this issue and wanted to see if the potential implementation approaches make sense:

  1. Quick Fix:

    • Basic label-to-break or continue functionality
    • simple label tracking without full scope validation
    • focus on common loop label use cases first
  2. Full Implementation:

    • Complete label scope management and validation
    • Support for all label contexts (loops, blocks)
    • full integration with existing control flow system

and some followup questions:
What limitations or constraints should we be aware of before proceeding?
- for example should this support just loops or blocks as well
- should we restrict what control flow statements (break/continue) can target labeled statements?

Thanks.

wrnrlr

wrnrlr commented on Dec 15, 2024

@wrnrlr
Author

That's funny, I've also encounter this when implementing a parser.
For my current usecase I would only need to break out of a nested for loops.
This is however not a insurmountable problem as my situation can be circumvented with some extra if statements.

I do know that in some code-bases, like the Linux kernel, they use labeled statements to avoid having to execute certain instructions for performance reasons. For example to jump directly to the error handling logic or return statement.

However you need full goto statements for that and those are not available in the JS/TS syntax.
JavaScript semantics also do not allow for a statement to break or continue to an outer labeled statement.

Maybe AssemblyScript can reinterpret an if statement with the true condition followed by a break statement to an outer label as a goto statement.

function example() {
  if (true) break ending;

  ending: console.log('LOL')
}

But I don't know the further implications of this hack or even claim this is a good idea, just an idea I had when thinking about this issue.

JairusSW

JairusSW commented on Dec 15, 2024

@JairusSW
Contributor

I would love to see this implemented. Would help solve a lot of my headaches with https://github.com/JairusSW/as-json

The support is there--built into wasm itself, so it shouldn't be that hard to get a POC going

pinned this issue on Dec 15, 2024
HerrCai0907

HerrCai0907 commented on Dec 15, 2024

@HerrCai0907
Member

In theory, it is not so hard to do it.
From language design perspective, TS also support label based break statement.
For WASM perspective, br to special labeled block is also possible.

CountBleck

CountBleck commented on Dec 15, 2024

@CountBleck
Member

Maybe AssemblyScript can reinterpret an if statement with the true condition followed by a break statement to an outer label as a goto statement.

You can probably just do this right now, with the disadvantage of requiring you wrap the code in a block:

export function example(): void {
  do {
    console.log("Hey")
    if (true) break;
    console.log("skipped")
  } while (0)

  console.log('LOL')
}

I don't think adding more syntax differences between AS and JS is a good idea. As an aside: in JS, you can use a labeled block and a labeled break instead of the do { /* ... */ } while (0).

As for labeled statements...there's probably enough infrastructure to support labeled breaks in the Compiler (since unlabeled breaks are working just fine), but it'd definitely require parser and AST changes. I think an additional field to the AST nodes for for, while, do-while, and blocks themselves is enough, since labeling regular statements isn't very useful.

self-assigned this
on Dec 16, 2024
added 4 commits that reference this issue on Dec 16, 2024
43d7a99
60e57da
3a29149
b47dc99
JairusSW

JairusSW commented on Jun 7, 2025

@JairusSW
Contributor

@CountBleck, any update on this?

added a commit that references this issue on Jun 22, 2025
300a0b4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @KazChe@wrnrlr@JairusSW@HerrCai0907@CountBleck

    Issue actions

      Support labeled statements · Issue #2889 · AssemblyScript/assemblyscript