Skip to content

Commit

Permalink
Handlng zero sized locals. (#4414)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Nov 19, 2023
1 parent 9f1ae13 commit c8444c2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/cairo-lang-sierra-generator/src/local_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
use itertools::{zip_eq, Itertools};
use itertools::{chain, zip_eq, Itertools};
use lowering::borrow_check::analysis::{Analyzer, BackAnalysis, StatementLocation};
use lowering::borrow_check::demand::DemandReporter;
use lowering::borrow_check::Demand;
Expand Down Expand Up @@ -49,7 +49,15 @@ pub fn analyze_ap_changes(
lowered_function,
used_after_revoke: Default::default(),
block_callers: Default::default(),
non_ap_based: UnorderedHashSet::from_iter(lowered_function.parameters.iter().cloned()),
non_ap_based: UnorderedHashSet::from_iter(chain!(
// Parameters are not ap based.
lowered_function.parameters.iter().cloned(),
// All empty variables are not ap based.
lowered_function.variables.iter().filter_map(|(id, var)| {
let info = db.get_type_info(db.get_concrete_type_id(var.ty).ok()?).ok()?;
if info.zero_sized { Some(id) } else { None }
})
)),
aliases: Default::default(),
partial_param_parents: Default::default(),
};
Expand Down
1 change: 1 addition & 0 deletions tests/bug_samples/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ mod inconsistent_gas;
mod partial_param_local;
mod loop_break_in_match;
mod generic_cycles;
mod zero_sized_locals;
22 changes: 22 additions & 0 deletions tests/bug_samples/zero_sized_locals.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[derive(Drop)]
struct ZeroSized {}

#[inline(never)]
fn use_zero_sized(ref value: ZeroSized) -> felt252 {
0
}

fn foo(ref value: ZeroSized, array_with_data: Array<felt252>) {
if 1 == 0 {
use_zero_sized(ref value);
}
// Unkown ap change tracking, while not using `value`.
let mut data_span = array_with_data.span();
let a: Option<Array<Array<felt252>>> = Serde::deserialize(ref data_span);
}

#[test]
fn call_foo() {
let mut value = ZeroSized {};
foo(ref value, array![0]);
}

0 comments on commit c8444c2

Please sign in to comment.