Skip to content

unified: Add or_pattern and fix 'if case let' translation#22080

Merged
asgerf merged 4 commits into
github:mainfrom
asgerf:unified/commonast-followups
Jun 29, 2026
Merged

unified: Add or_pattern and fix 'if case let' translation#22080
asgerf merged 4 commits into
github:mainfrom
asgerf:unified/commonast-followups

Conversation

@asgerf

@asgerf asgerf commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

switch_case used to contain pattern*: pattern which a comment stating that:

  • case p1, p2: translates to multiple patterns, and
  • default: is translated to an empty list of patterns

In the first case, the patterns should be treated as a disjunction (match if any pattern matches), but this would mean that an empty list of patterns should never match. The empty list accidentally became a special case in order for default: to work.

This PR changes the field to pattern?: pattern and introduces or_pattern to handle multiple patterns. We'll need or_pattern eventually anyway.

Also fixes an issue with how if case let PATTERN = expr was translated. The previous pattern match was too specific.

@asgerf asgerf requested a review from a team as a code owner June 29, 2026 09:15
Copilot AI review requested due to automatic review settings June 29, 2026 09:15
@asgerf asgerf added the no-change-note-required This PR does not need a change note label Jun 29, 2026
final override string getAPrimaryQlClass() { result = "OrPattern" }

/** Gets the node corresponding to the field `modifier`. */
final Modifier getModifier(int i) { unified_or_pattern_modifier(this, i, result) }

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Unified AST schema and Swift extractor translation to correctly represent comma-separated switch case patterns as a disjunction (or_pattern) while keeping default: as “no pattern,” and fixes Swift if case let PATTERN = expr translation to preserve the parsed pattern rather than re-synthesizing a more specific one.

Changes:

  • Change switch_case.pattern from repeated to optional, and introduce or_pattern to represent disjunctions (case p1, p2:).
  • Update the Swift translation rules to emit or_pattern for multi-pattern cases and to translate if case PATTERN = expr by preserving the pattern node.
  • Extend the Swift corpus tests to cover the new or_pattern output and the if case let shadowing scenario.
Show a summary per file
File Description
unified/ql/lib/unified.dbscheme Adds unified_or_pattern relations/types and makes switch_case have a single optional pattern reference.
unified/ql/lib/codeql/unified/Ast.qll Exposes OrPattern in the QL AST API and updates SwitchCase.getPattern to match the schema change.
unified/extractor/tests/corpus/swift/control-flow.txt Updates expected Unified AST output for multi-pattern switch cases and adds an if case let regression test.
unified/extractor/src/languages/swift/swift.rs Emits or_pattern for comma-separated switch patterns and preserves if case patterns directly.
unified/extractor/ast_types.yml Defines the new or_pattern node type and updates switch_case.pattern to be optional.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 0
  • Review effort level: Low

@hvitved hvitved left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@asgerf asgerf merged commit 2ef06c9 into github:main Jun 29, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants