Repository: MystenLabs/sui
File: external-crates/move/crates/move-vm-runtime/src/jit/execution/ast.rs (Rust)

Code before:
    /// fields as a pair of index, first to the type, second to the field position in that type
    /// [ALLOC] This vector (and sub-definitions) are allocated in the package arena
    pub field_handles: ArenaVec<FieldHandle>,
    /// materialized instantiations, whether partial or not
    /// [ALLOC] This vector (and sub-definitions) are allocated in the package arena
    pub field_instantiations: ArenaVec<FieldInstantiation>,

    /// a map from signatures in instantiations to the `ArenaVec<ArenaType>` that represents it.
    /// [ALLOC] This vector (and sub-definitions) are allocated in the package arena
    #[allow(dead_code)]
    pub(crate) instantiation_signatures: ArenaVec<ArenaVec<ArenaType>>,

    /// constant references carry an index into a global vector of values.
    /// [ALLOC] This vector (and sub-definitions) are allocated in the package arena
    pub constants: ArenaVec<Constant>,
}

impl Drop for Module {
    #![allow(unsafe_code)]
    fn drop(&mut self) {
        // Manually drop all NativeFunction Arc references to prevent memory leaks.
        //
        // ArenaVec uses ManuallyDrop and does NOT call drop on its elements.
        // This means Function.native (which contains Arc<UnboxedNativeFunction>)
        // would leak if we don't explicitly handle it.
        self.functions.drain_all();
    }
}

// A runtime constant
#[derive(Debug)]
pub struct Constant {
    pub(crate) value: ConstantValue,
    pub(crate) type_: ArenaType,
    // Size of constant -- used for gas charging.
    pub size: u64,
}

// A runtime function
// #[derive(Debug)]
// https://github.com/rust-lang/rust/issues/70263
pub struct Function {
    #[allow(unused)]
    pub file_format_version: u32,
    pub is_entry: bool,
    pub visibility: Visibility,
    pub index: FunctionDefinitionIndex,
    pub(crate) code: ArenaVec<Bytecode>,
    pub(crate) parameters: ArenaVec<ArenaType>,
    pub(crate) locals: ArenaVec<ArenaType>,
    pub(crate) return_: ArenaVec<ArenaType>,

Review comment (suggestion):
```suggestion
        // We need to manually drop the arena-allocated functions to ensure their native
        // Arc fields are correctly dropped.
        // Note the provided drain iterator calls the destructor on its elements when it is 
        // dropped, so this is sufficient.
        self.functions.drain();
```

Code after:
    /// fields as a pair of index, first to the type, second to the field position in that type
    /// [ALLOC] This vector (and sub-definitions) are allocated in the package arena
    pub field_handles: ArenaVec<FieldHandle>,
    /// materialized instantiations, whether partial or not
    /// [ALLOC] This vector (and sub-definitions) are allocated in the package arena
    pub field_instantiations: ArenaVec<FieldInstantiation>,

    /// a map from signatures in instantiations to the `ArenaVec<ArenaType>` that represents it.
    /// [ALLOC] This vector (and sub-definitions) are allocated in the package arena
    #[allow(dead_code)]
    pub(crate) instantiation_signatures: ArenaVec<ArenaVec<ArenaType>>,

    /// constant references carry an index into a global vector of values.
    /// [ALLOC] This vector (and sub-definitions) are allocated in the package arena
    pub constants: ArenaVec<Constant>,
}

impl Drop for Module {
    #![allow(unsafe_code)]
    fn drop(&mut self) {
        // We need to manually drop the arena-allocated functions to ensure their native
        // Arc fields are correctly dropped.
        // Note the provided drain iterator calls the destructor on its elements when it is
        // dropped, so this is sufficient.
        self.functions.drain();
    }
}

// A runtime constant
#[derive(Debug)]
pub struct Constant {
    pub(crate) value: ConstantValue,
    pub(crate) type_: ArenaType,
    // Size of constant -- used for gas charging.
    pub size: u64,
}

// A runtime function
// #[derive(Debug)]
// https://github.com/rust-lang/rust/issues/70263
pub struct Function {
    #[allow(unused)]
    pub file_format_version: u32,
    pub is_entry: bool,
    pub visibility: Visibility,
    pub index: FunctionDefinitionIndex,
    pub(crate) code: ArenaVec<Bytecode>,
    pub(crate) parameters: ArenaVec<ArenaType>,
    pub(crate) locals: ArenaVec<ArenaType>,
    pub(crate) return_: ArenaVec<ArenaType>,
    pub(crate) type_parameters: ArenaVec<AbilitySet>,