Skip to content

fix: handle ternary expressions in state variable initializers#2974

Open
ep0chzer0 wants to merge 2 commits intocrytic:masterfrom
ep0chzer0:fix/ternary-in-state-var-init
Open

fix: handle ternary expressions in state variable initializers#2974
ep0chzer0 wants to merge 2 commits intocrytic:masterfrom
ep0chzer0:fix/ternary-in-state-var-init

Conversation

@ep0chzer0
Copy link
Contributor

Summary

  • Fixes [Bug-Candidate]: The ternary operator does not support structs. #2836 - SlithIRError: Ternary operator are not convertible to SlithIR when ternary expressions are used in state variable initializers
  • Root cause: State variable initializers are processed via synthetic CONSTRUCTOR_VARIABLES functions created in add_constructor_variables(), which runs after the normal function parsing pipeline. These synthetic functions never go through _rewrite_ternary_as_if_else(), so ConditionalExpression nodes reach SlithIR conversion unsplit
  • Fix: Added _rewrite_ternary_in_constructor_variables() to Contract that performs the equivalent IF/EXPRESSION/ENDIF node splitting for constructor variable nodes, called after all nodes are created in add_constructor_variables()

Reproduction case (from issue)

contract A {
    struct S {
        uint32 user;
    }
}

contract B {
    A.S a;
    A.S b;
    uint32 result = (true ? a.user : b.user);
}

Before this fix, analyzing contract B would crash with:

SlithIRError: Ternary operator are not convertible to SlithIR if true then a.user else b.user

Test plan

  • New unit test test_ternary_in_state_variable_initializer verifies parsing succeeds
  • All existing CI checks pass

🤖 Generated with Claude Code

@ep0chzer0 ep0chzer0 requested a review from smonicas as a code owner February 24, 2026 00:24
ep0chzer0 and others added 2 commits February 26, 2026 10:26
State variable initializers are processed outside the normal function
parsing pipeline via synthetic CONSTRUCTOR_VARIABLES functions. These
synthetic functions were not going through _rewrite_ternary_as_if_else,
causing ConditionalExpressions to reach SlithIR conversion unsplit,
resulting in SlithIRError.

Add _rewrite_ternary_in_constructor_variables to Contract that performs
the equivalent IF/EXPRESSION/ENDIF node splitting for constructor
variable nodes, called after node creation in add_constructor_variables.

Fixes crytic#2836

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ep0chzer0 ep0chzer0 force-pushed the fix/ternary-in-state-var-init branch from e7f8721 to 82950eb Compare February 26, 2026 15:27
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.

[Bug-Candidate]: The ternary operator does not support structs.

1 participant