Skip to content

Improve error when break/continue label is in the same function but not enclosing - #64501

Open
Musfiqur Rahman Saimon (britsync07-prog) wants to merge 2 commits into
microsoft:mainfrom
britsync07-prog:fix/label-jump-target-message
Open

Musfiqur Rahman Saimon (britsync07-prog) wants to merge 2 commits into
microsoft:mainfrom
britsync07-prog:fix/label-jump-target-message

Conversation

@britsync07-prog

Copy link
Copy Markdown

Fixes #30408.

When a �reak/continue targets a label declared in the same function that does not enclose the statement (e.g. a label placed after the loop), the checker reported Jump target cannot cross function boundary, which is misleading since no function boundary is crossed. checkGrammarBreakOrContinueStatement now detects this case (via a same-function label search that does not cross nested functions) and reports the specific A 'continue'/'break' statement can only jump to a label of an enclosing ... diagnostic instead. Labels declared outside the function still produce the original error (�reakTarget5/continueTarget5 baselines unchanged).

Verified with the tsgo test runner: new continueTargetSameFunctionLabel test plus the full break/continue/label sweep green.

…ot enclosing

When a break/continue targets a label declared in the same function that does not enclose the statement (e.g. a label placed after the loop), the checker reported 'Jump target cannot cross function boundary', which is misleading since no function boundary is crossed. Detect this case and report the specific 'can only jump to a label of an enclosing ...' diagnostic instead. Labels declared outside the function still produce the original error.

Fixes microsoft#30408
Copilot AI balanced review requested due to automatic review settings September 28, 2026 15:45
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 28, 2026
@typescript-automation

Copy link
Copy Markdown
Contributor

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Repeated full-function scans risk quadratic checking time, and newly added branches lack regression coverage.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Improves diagnostics for non-enclosing labels declared within the same function.

Changes:

  • Detects same-function labels without crossing nested function boundaries.
  • Reports label-specific diagnostics instead of function-boundary errors.
  • Adds a regression test and baselines.
File Description
tsc/​internal/​checker/​grammarchecks.go Adds label discovery and diagnostic selection.
tsc/​testdata/​tests/​cases/​compiler/​continueTargetSameFunctionLabel.ts Adds the regression case.
tsc/​testdata/​baselines/​reference/​compiler/​continueTargetSameFunctionLabel.errors.txt Captures the corrected diagnostic.
tsc/​testdata/​baselines/​reference/​compiler/​continueTargetSameFunctionLabel.js Captures emitted JavaScript.
tsc/​testdata/​baselines/​reference/​compiler/​continueTargetSameFunctionLabel.symbols Captures symbol output.
tsc/​testdata/​baselines/​reference/​compiler/​continueTargetSameFunctionLabel.types Captures type output.

Comment thread tsc/internal/checker/grammarchecks.go Outdated
// "cannot cross function boundary" is misleading, so report the
// more specific message instead.
// See https://github.com/microsoft/TypeScript/issues/30408
if targetLabel != nil && functionContainsLabel(current, targetLabel.Text()) {
Comment thread tsc/internal/checker/grammarchecks.go Outdated
Comment on lines +1498 to +1502
if targetLabel != nil && functionContainsLabel(current, targetLabel.Text()) {
if node.Kind == ast.KindBreakStatement {
return c.grammarErrorOnNode(node, diagnostics.A_break_statement_can_only_jump_to_a_label_of_an_enclosing_statement)
}
return c.grammarErrorOnNode(node, diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement)
@britsync07-prog

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Cache per-function label sets on the checker so bodies are walked at most once. Add breakTargetSameFunctionLabel test covering break in a function and in a class static block.
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 29, 2026
@britsync07-prog

Copy link
Copy Markdown
Author

Addressed both findings: label sets are now collected once per function and cached on the checker (no repeated subtree scans), and added �reakTargetSameFunctionLabel covering �reak in a function plus a class static block. CLA signed. Full break/continue/label sweep green; �reakTarget5/continueTarget5 baselines unchanged.

This branch has not been deployed

No deployments
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: Not started

Development

Successfully merging this pull request may close these issues.

Confusing error message for labels used before definition

2 participants