Sitelet https://github.com/starkware-libs/cairo/commit/f84e76aea2b0a60a63adcaf5c3f27d9f931db918
Skip to content

Commit f84e76a

Browse files
committed
fix(semantic): evaluate const !, &, |, ^ on bool operands
Const bitwise ops on bool operands (`ConstValue::Enum`) fell through to the numeric path, where `NumericArg::try_new` rejects them, dropping the value to `Missing` and surfacing later as a sierra-generation ICE. Handle bool operands directly alongside the existing `eq`/`ne` cases: `not`, `bitand`, `bitor`, `bitxor`. Integer operands still take the numeric path.
1 parent daff9bc commit f84e76a

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎corelib/src/test/language_features/const_test.cairo‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ fn test_complex_consts() {
159159
assert_eq!(IF_CONST_FALSE, 7);
160160
}
161161

162+
#[test]
163+
fn test_const_bool_bitwise() {
164+
const AND: bool = true & false;
165+
const OR: bool = true | false;
166+
const XOR: bool = true ^ false;
167+
assert!(!AND);
168+
assert!(OR);
169+
assert!(XOR);
170+
}
171+
162172
#[derive(Copy, Drop, PartialEq, Debug)]
163173
struct Pair<T> {
164174
a: T,

‎crates/cairo-lang-semantic/src/items/constant.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,17 @@ impl<'a, 'r, 'mt> ConstantEvaluateContext<'a, 'r, 'mt> {
922922
return bool_value(self.const_values_eq(args[0], args[1]));
923923
} else if imp.function == self.ne_fn {
924924
return bool_value(!self.const_values_eq(args[0], args[1]));
925-
} else if imp.function == self.not_fn {
926-
return bool_value(args[0] == self.false_const);
925+
} else if args.iter().all(|arg| [self.false_const, self.true_const].contains(arg)) {
926+
let as_bool = |v| v == self.true_const;
927+
if imp.function == self.not_fn {
928+
return bool_value(!as_bool(args[0]));
929+
} else if imp.function == self.bitand_fn {
930+
return bool_value(as_bool(args[0]) & as_bool(args[1]));
931+
} else if imp.function == self.bitor_fn {
932+
return bool_value(as_bool(args[0]) | as_bool(args[1]));
933+
} else if imp.function == self.bitxor_fn {
934+
return bool_value(as_bool(args[0]) ^ as_bool(args[1]));
935+
}
927936
}
928937

929938
let args = match args

0 commit comments

Comments
 (0)