Skip to content

[swiftsrc2cpg] De-sugar tuple patterns in switch-case statements#5913

Merged
max-leuthaeuser merged 2 commits intomasterfrom
max/switchPatternHandling
Apr 1, 2026
Merged

[swiftsrc2cpg] De-sugar tuple patterns in switch-case statements#5913
max-leuthaeuser merged 2 commits intomasterfrom
max/switchPatternHandling

Conversation

@max-leuthaeuser
Copy link
Copy Markdown
Contributor

@max-leuthaeuser max-leuthaeuser commented Mar 31, 2026

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
  • Swift tuple elements are indeed accessed with .0, .1, etc.

@max-leuthaeuser max-leuthaeuser requested a review from ml86 March 31, 2026 11:11
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`.
@max-leuthaeuser max-leuthaeuser force-pushed the max/switchPatternHandling branch from b3a4b99 to 8671b83 Compare March 31, 2026 11:38
- 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
@max-leuthaeuser max-leuthaeuser merged commit f4d5049 into master Apr 1, 2026
8 checks passed
@max-leuthaeuser max-leuthaeuser deleted the max/switchPatternHandling branch April 1, 2026 05:54
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.

2 participants