Skip to content

[Sema] Improve handling of fallthrough in if/switch expressions #75891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 19, 2024

Conversation

hamishknight
Copy link
Contributor

Remove the restriction on fallthrough as the last statement in a switch expression, and prevent fallthrough from jumping out of if expressions.

rdar://127670432
rdar://133845101
Resolves #75880

Make it clear the issue is the transfer of
control flow out of the `if`/`switch` expression.
Technically things like `break` and `continue` are
allowed, as long as they are not jumping out of
the expression.
I don't think this currently matters, but ensure
we re-contextualize BreakStmts and ContinueStmts
in RecontextualizeClosures since these statements
store DeclContexts.
Follow a similar pattern to BreakTargetRequest
and ContinueTargetRequest.
Allow `fallthrough` to appear as the last statement
in the case of a `switch` expression. We already
allowed it in other positions, this was just an
oversight.

rdar://127670432
Impose the same limit we impose on other
forms of control flow statements (e.g `break`,
`continue`, `return`), where it cannot transfer
control out of the expression. This fixes a crash
where we'd fail to find a fallthrough nested in an
`if` expression. It is technically source breaking,
as we would have allowed the case where the `if`
expression is a directly nested result of an outer
`switch` expression, but I would be very surprised
if anyone is relying on that.

rdar://133845101
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight
Copy link
Contributor Author

@swift-ci please test source compatibility

@ahoppen ahoppen removed their request for review August 14, 2024 22:04
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight hamishknight merged commit 840ed6a into swiftlang:main Aug 19, 2024
7 checks passed
@hamishknight hamishknight deleted the fellthrough branch August 19, 2024 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SILGen crash on fallthrough out of if expression
2 participants