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

Code before:
        match self.datatype_info.inner_ref() {
            Datatype::Enum(ptr) => ptr.to_ref().def_vtable_key.clone(),
            Datatype::Struct(ptr) => ptr.to_ref().def_vtable_key.clone(),
        }
    }

    pub(crate) fn intra_package_key(&self) -> IntraPackageKey {
        match self.datatype_info.inner_ref() {
            Datatype::Enum(ptr) => *ptr.def_vtable_key.intra_package_key(),
            Datatype::Struct(ptr) => *ptr.def_vtable_key.intra_package_key(),
        }
    }
}

impl Type {
    const LEGACY_BASE_MEMORY_SIZE: AbstractMemorySize = AbstractMemorySize::new(1);

    /// Returns the abstract memory size the data structure occupies.
    ///
    /// This kept only for legacy reasons.
    /// New applications should not use this.
    ///
    /// SAFETY: Addition over `AbstractMemorySize` is saturating and so this is safe against
    /// overflow.
    #[allow(clippy::arithmetic_side_effects)]
    pub fn size(&self) -> AbstractMemorySize {
        use Type::*;

        match self {
            TyParam(_) | Bool | U8 | U16 | U32 | U64 | U128 | U256 | Address | Signer => {
                Self::LEGACY_BASE_MEMORY_SIZE
            }
            Vector(ty) | Reference(ty) | MutableReference(ty) => {
                Self::LEGACY_BASE_MEMORY_SIZE + ty.size()
            }
            Datatype(_) => Self::LEGACY_BASE_MEMORY_SIZE,
            DatatypeInstantiation(inst) => {
                let (_, tys) = &**inst;
                tys.iter()
                    .fold(Self::LEGACY_BASE_MEMORY_SIZE, |acc, ty| acc + ty.size())
            }
        }
    }

    pub fn from_const_signature(constant_signature: &SignatureToken) -> PartialVMResult<Self> {
        use SignatureToken as S;
        use Type as L;

        Ok(match constant_signature {
            S::Bool => L::Bool,
            S::U8 => L::U8,

Review comment (bug):
This is _not_ safe: if the type is max-sized, then adding `Self::LEGACY_BASE_MEMORY_SIZE` will overflow. 

Code after:
        match self.datatype_info.inner_ref() {
            Datatype::Enum(ptr) => ptr.to_ref().def_vtable_key.clone(),
            Datatype::Struct(ptr) => ptr.to_ref().def_vtable_key.clone(),
        }
    }

    pub(crate) fn intra_package_key(&self) -> IntraPackageKey {
        match self.datatype_info.inner_ref() {
            Datatype::Enum(ptr) => *ptr.def_vtable_key.intra_package_key(),
            Datatype::Struct(ptr) => *ptr.def_vtable_key.intra_package_key(),
        }
    }
}

impl Type {
    const LEGACY_BASE_MEMORY_SIZE: AbstractMemorySize = AbstractMemorySize::new(1);

    /// Returns the abstract memory size the data structure occupies.
    ///
    /// This kept only for legacy reasons.
    /// New applications should not use this.
    ///
    /// SAFETY: Addition over `AbstractMemorySize` is saturating and so this is safe against
    /// overflow. See the implementation of [`Add`] for [`AbstractMemorySize`] in the
    /// [`move_core_types::gas_algebra`] module for more details on this and why this is safe.
    #[allow(clippy::arithmetic_side_effects)]
    pub fn size(&self) -> AbstractMemorySize {
        use Type::*;

        match self {
            TyParam(_) | Bool | U8 | U16 | U32 | U64 | U128 | U256 | Address | Signer => {
                Self::LEGACY_BASE_MEMORY_SIZE
            }
            Vector(ty) | Reference(ty) | MutableReference(ty) => {
                Self::LEGACY_BASE_MEMORY_SIZE + ty.size()
            }
            Datatype(_) => Self::LEGACY_BASE_MEMORY_SIZE,
            DatatypeInstantiation(inst) => {
                let (_, tys) = &**inst;
                tys.iter()
                    .fold(Self::LEGACY_BASE_MEMORY_SIZE, |acc, ty| acc + ty.size())
            }
        }
    }

    pub fn from_const_signature(constant_signature: &SignatureToken) -> PartialVMResult<Self> {
        use SignatureToken as S;
        use Type as L;

        Ok(match constant_signature {
            S::Bool => L::Bool,