Skip to content

fix(checker): emit precise error when labeled continue/break targets a non-enclosing label#63438

Closed
creazyfrog wants to merge 1 commit intomicrosoft:mainfrom
creazyfrog:fix/label-forward-ref-error-message
Closed

fix(checker): emit precise error when labeled continue/break targets a non-enclosing label#63438
creazyfrog wants to merge 1 commit intomicrosoft:mainfrom
creazyfrog:fix/label-forward-ref-error-message

Conversation

@creazyfrog
Copy link
Copy Markdown

@creazyfrog creazyfrog commented Apr 26, 2026

What this change intends to do

Fixes #30408

When a labeled continue or break references a label that exists in the same function but is not an ancestor of the jump statement, TypeScript incorrectly emits:

TS1107: Jump target cannot cross function boundary.

No function boundary is crossed — the label simply is not enclosing the jump. The misleading message causes developers to think they have a cross-function scoping problem when they actually have a non-enclosing label reference.

function foo() {
    for (let i = 0; i < 10; i++) {
        continue loopend; // ❌ TS1107 before fix — misleading
    }
    loopend:              // label exists in same function, just not enclosing
    console.log('done');
}

Fix

Adds a helper labelExistsInScope(scope, labelName) that walks the AST children of the enclosing function-like node looking for a matching LabeledStatement, without descending into nested function boundaries.

In checkGrammarBreakOrContinueStatement, when a function boundary is reached, the fix checks whether the named label exists anywhere in that function. If it does, it emits the accurate diagnostic instead of TS1107:

  • TS1115 for continue"A 'continue' statement can only jump to a label of an enclosing iteration statement."
  • TS1116 for break"A 'break' statement can only jump to a label of an enclosing statement."

TS1107 is preserved for the genuine case where the label is defined in an outer function (a real function-boundary violation).

Before / After

function foo() {
    for (let i = 0; i < 10; i++) {
        continue loopend;
        //       ~~~~~~~ Before: TS1107 "Jump target cannot cross function boundary"
        //               After:  TS1115 "A 'continue' statement can only jump to a label of an enclosing iteration statement"
    }
    loopend:
    console.log('done');
}

The genuine cross-function case still produces TS1107 correctly:

function outer() {
    myLabel:
    function inner() {
        continue myLabel; // TS1107 — still correctly reported
    }
}

File changed: src/compiler/checker.ts — 22 insertions, 0 deletions

Tests

This PR does not currently include a new compiler baseline test. A test in tests/cases/compiler/ covering:

  • a same-function non-enclosing label (should produce TS1115/TS1116, not TS1107), and
  • a genuine cross-function label (should still produce TS1107)

would be the right addition. I can add one if a maintainer confirms this approach before I invest the time, since baseline-accept changes require a full local build.

AI Assistance Disclosure

This PR was developed with the assistance of Claude Code (Anthropic), as required to be disclosed per the CONTRIBUTING.md guidelines.

…a non-enclosing label

When a labeled continue or break statement references a label that exists
in the same function but is not an ancestor (e.g. a forward reference or
a sibling statement), the checker walked up to the function boundary and
emitted TS1107 "Jump target cannot cross function boundary".

This message is misleading: the label is inside the same function, so
no function boundary is actually being crossed. The real problem is that
the label is not an *enclosing* statement.

Fix: before emitting TS1107 at a function boundary, check whether the
named label exists anywhere within that function scope via a new helper
`labelExistsInScope`. If it does, emit the more accurate diagnostic:
- TS1115: "A 'continue' statement can only jump to a label of an
          enclosing iteration statement."
- TS1116: "A 'break' statement can only jump to a label of an enclosing
          statement."

TS1107 is still emitted for the genuine cross-function case (label is in
an outer function).

Fixes microsoft#30408

Signed-off-by: creazyfrog <rohitcse.gec@gmail.com>
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Apr 26, 2026
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Apr 26, 2026
@jakebailey
Copy link
Copy Markdown
Member

Can you please tell me what tool you used to send this PR, and how you chose this issue to fix?

@creazyfrog
Copy link
Copy Markdown
Author

creazyfrog commented Apr 26, 2026 via email

@jakebailey
Copy link
Copy Markdown
Member

Please read CONTRIBUTING.md

@creazyfrog creazyfrog closed this Apr 27, 2026
@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Confusing error message for labels used before definition

3 participants