Skip to content

Commit

Permalink
Fix array_get's drop implementation. (#832)
Browse files Browse the repository at this point in the history
* Fix `array_get`'s drop implementation.

* Add a test.
  • Loading branch information
azteca1998 authored Oct 4, 2024
1 parent cfd0f06 commit 0b73f1c
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/libfuncs/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,17 +548,17 @@ pub fn build_get<'ctx, 'this>(
let region = Region::new();
let block = region.append_block(Block::new(&[]));

let value = block.load(context, location, value_ptr, elem_ty)?;
drop_overrides_meta
.invoke_override(context, &block, location, &info.ty, value)?;

block.append_operation(scf::r#yield(&[], location));
region
},
{
let region = Region::new();
let block = region.append_block(Block::new(&[]));

let value = block.load(context, location, value_ptr, elem_ty)?;
drop_overrides_meta
.invoke_override(context, &block, location, &info.ty, value)?;

block.append_operation(scf::r#yield(&[], location));
region
},
Expand Down Expand Up @@ -2437,4 +2437,22 @@ mod test {
)
);
}

/// Test to ensure that the returned element in `array_get` does NOT get dropped.
#[test]
fn array_get_avoid_dropping_element() {
let program = load_cairo! {
use core::{array::{array_append, array_at, array_new}, box::{into_box, unbox}};

fn run_test() -> @Box<felt252> {
let mut x: Array<Box<felt252>> = array_new();
array_append(ref x, into_box(42));

unbox(array_at(@x, 0))
}
};
let result = run_program(&program, "run_test", &[]).return_value;

assert_eq!(result, jit_enum!(0, jit_struct!(Value::Felt252(42.into()))));
}
}

0 comments on commit 0b73f1c

Please sign in to comment.