Skip to content

Improve label reference diagnostics#63472

Open
MukundaKatta wants to merge 1 commit into
microsoft:mainfrom
MukundaKatta:codex/fix-label-diagnostics
Open

Improve label reference diagnostics#63472
MukundaKatta wants to merge 1 commit into
microsoft:mainfrom
MukundaKatta:codex/fix-label-diagnostics

Conversation

@MukundaKatta
Copy link
Copy Markdown

Fixes #30408.

This updates break/continue grammar checking so a labeled jump that references a label declared later in the same function scope reports a targeted diagnostic instead of the generic function-boundary error. The new error includes a related span pointing at the label declaration.

The test coverage also keeps the existing behavior for missing, non-enclosing, and cross-function labels.

Validation:

  • npx -p node@22 -c './node_modules/.bin/hereby runtests --tests=labelReferenceDiagnostics'\n- npx -p node@22 -c 'npm run build:compiler'

Copilot AI review requested due to automatic review settings May 11, 2026 22:35
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog May 11, 2026
@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug labels May 11, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates TypeScript’s grammar checking for break/continue with labels so that referencing a label declared later in the same function reports a targeted diagnostic (with related info pointing at the label declaration), while preserving existing diagnostics for other invalid label scenarios.

Changes:

  • Added new compiler test + baselines covering “label declared later”, missing labels, non-enclosing labels, and cross-function label jumps.
  • Introduced a new diagnostic (TS18001) and wired it into break/continue grammar checking with related info on the label declaration.
  • Added helper functions in the checker to detect “label declared later in same function” vs cross-function label containment.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/cases/compiler/labelReferenceDiagnostics.ts New test cases for improved label reference diagnostics.
tests/baselines/reference/labelReferenceDiagnostics.types Baseline update for type display for the new test.
tests/baselines/reference/labelReferenceDiagnostics.symbols Baseline update for symbol display for the new test.
tests/baselines/reference/labelReferenceDiagnostics.js Baseline update for JS emit for the new test.
tests/baselines/reference/labelReferenceDiagnostics.errors.txt Baseline update to validate the new targeted diagnostic + related info.
src/compiler/diagnosticMessages.json Adds the new TS18001 diagnostic message.
src/compiler/checker.ts Implements the new diagnostic behavior and related-info reporting for later-declared labels.
Comments suppressed due to low confidence (2)

src/compiler/checker.ts:1

  • The label.pos > node.pos comparison is trivia-sensitive (pos includes leading comments/whitespace). This can misclassify cases where the label’s leading trivia starts earlier than the jump statement even though the label token is later. Compare using token starts instead (e.g., getStart(sourceFile) / getTokenPosOfNode) for both the jump and the label identifier so the diagnostic triggers based on actual syntax positions.
    src/compiler/checker.ts:1
  • The containment check mixes manual pos/end range comparisons (also trivia-sensitive) with label matching. Using an existing range helper (e.g., rangeContainsRange / containsRange) and comparing via getStart/getEnd (or token positions) would make the intent clearer and reduce the risk of subtle off-by-trivia issues when future refactors introduce comments/formatting changes.

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

3 participants