fix(checker): emit precise error when labeled continue/break targets a non-enclosing label#63438
Open
creazyfrog wants to merge 1 commit intomicrosoft:mainfrom
Open
fix(checker): emit precise error when labeled continue/break targets a non-enclosing label#63438creazyfrog wants to merge 1 commit intomicrosoft:mainfrom
creazyfrog wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…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>
|
@creazyfrog please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #30408
Problem
When a labeled
continueorbreakreferences a label that is in the same function but not an ancestor (e.g. a forward/sibling reference), TypeScript emits:This message is wrong — no function boundary is crossed. The label lives in the same function; it's just not enclosing the jump statement.
Fix
Adds a helper
labelExistsInScope(scope, labelName)that walks the children of the enclosing function looking for a matchingLabeledStatement(without descending into nested functions).In
checkGrammarBreakOrContinueStatement, before emitting TS1107 at a function boundary, the fix checks whether the named label exists inside the function. If it does, it emits the more accurate diagnostic instead:continue: "A 'continue' statement can only jump to a label of an enclosing iteration statement."break: "A 'break' statement can only jump to a label of an enclosing statement."TS1107 is still emitted for the genuine case where the label is in an outer function (truly crossing a function boundary).
Before / After
The genuine cross-function case still produces TS1107: