Skip to content

Commit

Permalink
Added check before adding constants to constant folding map.
Browse files Browse the repository at this point in the history
Closes #5967.
  • Loading branch information
orizi committed Jul 5, 2024
1 parent a767ed0 commit cb70525
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crates/cairo-lang-lowering/src/optimizations/const_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ pub fn const_folding(db: &dyn LoweringGroup, lowered: &mut FlatLowered) {
maybe_replace_inputs(&var_info, stmt.inputs_mut());
match stmt {
Statement::Const(StatementConst { value, output }) => {
var_info.insert(*output, VarInfo::Const(value.clone()));
// Preventing the insertion of non-member consts values (such as a `Box` of a
// const).
if matches!(
value,
ConstValue::Int(..)
| ConstValue::Struct(..)
| ConstValue::Enum(..)
| ConstValue::NonZero(..)
) {
var_info.insert(*output, VarInfo::Const(value.clone()));
}
}
Statement::Snapshot(stmt) => {
if let Some(VarInfo::Const(val)) = var_info.get(&stmt.input.var_id) {
Expand Down
23 changes: 23 additions & 0 deletions tests/bug_samples/issue5967.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[derive(Drop)]
pub enum Pattern {
Leaf,
Boxed: Box<Pattern>
}

pub fn boxed(pat: Pattern) -> Pattern {
return Pattern::Boxed(BoxTrait::new(pat));
}

pub fn boxed_leaf() -> Pattern {
boxed(Pattern::Leaf)
}

#[inline(never)]
pub fn boxed_boxed_leaf() -> Pattern {
boxed(boxed_leaf())
}

#[test]
fn test() {
let _ = boxed_boxed_leaf();
}
1 change: 1 addition & 0 deletions tests/bug_samples/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod issue5438;
mod issue5629;
mod issue5680;
mod issue5764;
mod issue5967;
mod loop_break_in_match;
mod loop_only_change;
mod partial_param_local;
Expand Down

0 comments on commit cb70525

Please sign in to comment.