Skip to content

Take the #else branch when every earlier condition is false - #71

Merged
zienag merged 1 commit into
yandex:mainfrom
zienag:active-else-clause
Oct 26, 2026
Merged

zienag merged 1 commit into
yandex:mainfrom
zienag:active-else-clause

Conversation

@zienag

@zienag zienag commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

With known compilation conditions, an #else reached after false conditions was not treated as the active branch. An #else clause has no condition, and evaluating it gave nil, the same as an unknown condition, so the whole #if stayed unresolved. Reads inside it still worked, but an @Implicit declaration in the #else failed with "Cannot mutate implicit context inside '#if' block with unresolved condition", and the note pointed at a condition that was in fact known. A release build hit this with #if INTERNAL_BUILD ... #else @Implicit() var voiceDialog: VoiceDialog = io #endif when INTERNAL_BUILD was not defined.

Changes

  • filterInactiveIfConfig recognizes an #else by its keyword and counts it as true once reached. An #else after an unknown condition still stays unresolved, as before.
  • parsedCondition uses the same isElse check instead of inferring #else from a missing condition.
  • if_config_code_block.swift covers declarations in an active #else after a false #if and #elseif.

Test plan

  • The new case failed before the fix with the mutation error and notes at the known #if D and #elseif E lines.
  • swift test (Xcode Swift 6.3.2): 121 tests pass.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 12:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
What changed in this PR

Fixes conditional-compilation branch resolution so a reachable #else becomes the active branch when all earlier conditions are known-false, preventing false “unresolved #if” mutation errors for @Implicit declarations.

Changes:

  • Treats #else clauses as active (true) once reached in filterInactiveIfConfig.
  • Adds test cases covering @Implicit declarations inside an active #else (after #if and after #elseif).
File Description
Sources/​ImplicitsTool/​CompilationConditionsConfig.swift Adjusts clause-activity evaluation so #else can resolve as active.
Sources/​TestResources/​test_data/​if_config_code_block.swift Adds regression cases ensuring @Implicit declarations work in active #else branches.

Review effort: Lite

An #else clause has no condition, so evaluating it gave nil, the same as
an unknown condition. With `#if FLAG ... #else ... #endif` and FLAG not
defined, the #else was not recognized as the active branch: the whole
#if stayed unresolved, and an @implicit declaration in #else failed with
"Cannot mutate implicit context inside '#if' block with unresolved
condition", noted at the known condition. Reads in such a block still
worked, so the existing filtering tests missed it.

A clause is now recognized as #else by its keyword and counts as true
once reached. parsedCondition uses the same check instead of inferring
#else from a missing condition.
@zienag
zienag force-pushed the active-else-clause branch from bbace31 to 47a9c22 Compare October 26, 2026 13:35
@zienag
zienag enabled auto-merge (squash) October 26, 2026 13:35
@zienag
zienag merged commit 45b6aff into yandex:main Oct 26, 2026
1 check passed
@zienag
zienag deleted the active-else-clause branch October 26, 2026 15:56
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