[swiftsrc2cpg] De-sugar tuple patterns in switch-case statements#5913
Merged
max-leuthaeuser merged 2 commits intomasterfrom Apr 1, 2026
Merged
[swiftsrc2cpg] De-sugar tuple patterns in switch-case statements#5913max-leuthaeuser merged 2 commits intomasterfrom
max-leuthaeuser merged 2 commits intomasterfrom
Conversation
When a switch-case contains tuple patterns, the subject expression is assigned to a temporary variable evaluated once: - Expression tuple patterns (e.g. `case (1, 2):`) are de-sugared into component-wise equality chains (`<subject>.0 == 1 && <subject>.1 == 2`) - Binding tuple patterns (e.g. `case let (a, b):` or `case (var a, var b):`) are de-sugared into component-wise assignments (`a = <subject>.0`) - `is Type` patterns in tuples (e.g. `case (is String, 42):`) produce `instanceOf` checks - Enum case patterns (e.g. `case (.min, 0):`) produce equality checks - Mixed binding and non-binding elements (e.g. `case (var a, .min):`) are handled correctly - Nested tuples are handled recursively Tuple elements are accessed using Swift's 0-based numeric labels (`.0`, `.1`, ...) rather than Scala-style `._1`, `._2`.
b3a4b99 to
8671b83
Compare
- Add data-flow tests for equality chain (source flows through equals checks and into sink) in DataFlowTests - Replace .size shouldBe X assertions with content checks in SwitchTests - Set typeFullName of subject temp local/identifiers to Defines.Tuple - Fix missing scope.addVariable for subject temp (prevented spurious second local from being created by createVariableReferenceLinks) - Add type assertions for subject temp
ml86
approved these changes
Mar 31, 2026
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.
When a switch-case contains tuple patterns, the subject expression is
assigned to a temporary variable evaluated once:
case (1, 2):) are de-sugared intocomponent-wise equality chains (
<subject>.0 == 1 && <subject>.1 == 2)case let (a, b):orcase (var a, var b):)are de-sugared into component-wise assignments (
a = <subject>.0)is Typepatterns in tuples (e.g.case (is String, 42):) produceinstanceOfcheckscase (.min, 0):) produce equality checkscase (var a, .min):)are handled correctly
.0,.1, etc.