unified: Add or_pattern and fix 'if case let' translation#22080
Merged
Conversation
| 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) } |
Contributor
There was a problem hiding this comment.
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.patternfrom repeated to optional, and introduceor_patternto represent disjunctions (case p1, p2:). - Update the Swift translation rules to emit
or_patternfor multi-pattern cases and to translateif case PATTERN = exprby preserving the pattern node. - Extend the Swift corpus tests to cover the new
or_patternoutput and theif case letshadowing 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
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.
switch_caseused to containpattern*: patternwhich a comment stating that:case p1, p2:translates to multiple patterns, anddefault:is translated to an empty list of patternsIn 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?: patternand introducesor_patternto handle multiple patterns. We'll needor_patterneventually anyway.Also fixes an issue with how
if case let PATTERN = exprwas translated. The previous pattern match was too specific.