Repository navigation
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a58637d2-8e4f-4928-be81-7b4add6d7c25
Copy the base expression into each conditional branch. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a58637d2-8e4f-4928-be81-7b4add6d7c25
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Chained postfix directives cause exponential base duplication that can exhaust extraction memory or time, while empty and synthetic branches report incorrect source ranges.
3 open issues
What changed in this PR
Adds Swift postfix conditional-compilation support to the unified extractor by lowering it into existing if_expr nodes, without adding special cases to the common AST.
Changes:
- Copies the base expression into each branch and synthesizes a fallback when
#elseis absent. - Expands extractor fixtures for multiple branches, empty branches, nesting, and chaining.
- Adds a conditional field-write dataflow test and updates generated results.
| File | Description |
|---|---|
unified/ql/test/library-tests/dataflow/test.expected |
Updates generated dataflow results. |
unified/ql/test/library-tests/dataflow/fields.swift |
Adds a setter and conditional field-write flow test. |
unified/extractor/tests/corpus/swift/conditional-compilation/conditional-compilation-postfix-expression.swift |
Adds branch, fallback, nesting, and chaining cases. |
unified/extractor/tests/corpus/swift/conditional-compilation/conditional-compilation-postfix-expression.output |
Updates generated syntax and unified AST snapshots. |
unified/extractor/src/languages/swift/swift.rs |
Implements base substitution and fallback lowering. |
Review effort: Max (adaptive)
| // `base #if .method() #endif` | ||
| // gets translated as had it been | ||
| // `#if base.method() #else base #endif`. | ||
| ctx.conditional_compilation_tail = Some(tree!((block stmt: {base}))); |
| statements | ||
| } else { | ||
| // On empty branches, just translate the base as the body. | ||
| ctx.translate(base) |
| // `base #if .method() #endif` | ||
| // gets translated as had it been | ||
| // `#if base.method() #else base #endif`. | ||
| ctx.conditional_compilation_tail = Some(tree!((block stmt: {base}))); |
This branch has not been deployed
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.


Translate Swift's postfix
#ifconditional compilation expressions into "ordinary" conditional-compilationifexpressions by copying the base into each branch.This approach allows us to handle the feature without introducing special casing for it in the common AST. The downside is that we end up copying parts of the AST, and when postfix conditional compilation expressions are chained or nested this is not linear in the original AST size.
Examples
Here are some examples demonstrating the translation.
The following
is translated into the same as what the following would translate into.
When there's no
#elsebranchan else branch is synthesized such that it becomes
This gets a bit tricky with nesting and chaining. The following example from the tests:
gets turned into