fix(sema): validate ternary conditions in return statements#1887
Open
aryanbaranwal001 wants to merge 1 commit intohyperledger-solang:mainfrom
Open
fix(sema): validate ternary conditions in return statements#1887aryanbaranwal001 wants to merge 1 commit intohyperledger-solang:mainfrom
aryanbaranwal001 wants to merge 1 commit intohyperledger-solang:mainfrom
Conversation
8a68009 to
e4a6b24
Compare
Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
e4a6b24 to
42f4659
Compare
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.
This PR fixes two compiler panics caused by invalid ternary conditions used in return statements:
expr should not be in cfg: Poisonwhen a void-returning call is used as a ternary condition #1865List and Type Operator shall not appear in the CFGwhentype(T)is used as a ternary condition #1873The 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 withcond.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 inExpression::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:2042and marked used at/sema/statements.rs:2043This makes sema reject invalid conditions like following before codegen:
return type(bool) ? 1 : 2;return g() ? 1 : 2;whereg()returns no valueThis mirrors the existing generic ternary behavior at
/sema/expression/resolve_expression.rs:187.