Skip to content

fix(sema): validate ternary conditions in return statements#1887

Open
aryanbaranwal001 wants to merge 1 commit intohyperledger-solang:mainfrom
aryanbaranwal001:fix/return-void-ternary
Open

fix(sema): validate ternary conditions in return statements#1887
aryanbaranwal001 wants to merge 1 commit intohyperledger-solang:mainfrom
aryanbaranwal001:fix/return-void-ternary

Conversation

@aryanbaranwal001
Copy link
Copy Markdown

This PR fixes two compiler panics caused by invalid ternary conditions used in return statements:

The root cause is that Solang has two sema paths for ternary expressions:

  • The normal expression resolver handles generic ternaries at /sema/expression/resolve_expression.rs:180. It resolves the condition and validates it with cond.cast(...)? at /sema/expression/resolve_expression.rs:187.

  • The return specific resolver handles ternaries inside return values at /sema/statements.rs:2034. It resolves the condition at line:2035, but did not apply the equivalent .cast(...) boolean check before storing the condition in Expression::ConditionalOperator.

Because of that missing check, invalid conditions such as type(bool) or a void returning function call slip through sema and reach codegen.

Fix

Add the missing boolean check .cast(...) after the condition is resolved at /sema/statements.rs:2042 and marked used at /sema/statements.rs:2043

used_variable(ns, &cond, symtable); // for reference

let cond = cond.cast(&cond.loc(), &Type::Bool, true, ns, diagnostics)?;

This makes sema reject invalid conditions like following before codegen:

  • return type(bool) ? 1 : 2;
  • return g() ? 1 : 2; where g() returns no value

This mirrors the existing generic ternary behavior at /sema/expression/resolve_expression.rs:187.

Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
@aryanbaranwal001 aryanbaranwal001 force-pushed the fix/return-void-ternary branch from e4a6b24 to 42f4659 Compare April 24, 2026 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant