Skip to content

Unified: Support Swift postfix conditional compilation - #23228

Open
paldepind wants to merge 2 commits into
mainfrom
unified/swift-postfixIfConfigExpr
Open

paldepind wants to merge 2 commits into
mainfrom
unified/swift-postfixIfConfigExpr

Conversation

@paldepind

@paldepind paldepind commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Translate Swift's postfix #if conditional compilation expressions into "ordinary" conditional-compilation if expressions 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

request
  #if USE_CACHE
      .cached()
  #else
      .fresh()
  #endif

is translated into the same as what the following would translate into.

#if USE_CACHE
    request.cached()
#else
    request.fresh()
#endif

When there's no #else branch

view
  #if DEBUG
    .border(.red)
  #endif

an else branch is synthesized such that it becomes

#if DEBUG
    view.border(.red)
#else
    view
#endif

This gets a bit tricky with nesting and chaining. The following example from the tests:

let postfixChain = baseExpr
    #if CONDITION_1
        #if CONDITION_2
            .someMethod()
        #endif
        #if CONDITION_3
            .otherMethod()
        #endif
    #endif

gets turned into

let postfixChain =
    #if CONDITION_1
        #if CONDITION_3
            (
                #if CONDITION_2
                    baseExpr.someMethod()
                #else
                    baseExpr
                #endif
            ).otherMethod()
        #else
            #if CONDITION_2
                baseExpr.someMethod()
            #else
                baseExpr
            #endif
        #endif
    #else
        baseExpr
    #endif

paldepind and others added 2 commits November 24, 2026 15:20
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
@paldepind
paldepind marked this pull request as ready for review November 24, 2026 14:52
@paldepind
paldepind requested review from a team as code owners November 24, 2026 14:52
Copilot AI adaptive review requested due to automatic review settings November 24, 2026 14:52

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.

🟡 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 #else is 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants