Skip to content

Commit

Permalink
Removed support for derive on externs. (#7319)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Feb 20, 2025
1 parent 744f020 commit dfb5d3f
Show file tree
Hide file tree
Showing 27 changed files with 111 additions and 160 deletions.
3 changes: 2 additions & 1 deletion corelib/src/array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ use crate::option::OptionTrait;
#[feature("deprecated-index-traits")]
use crate::traits::IndexView;
/// A collection of elements of the same type contiguous in memory.
#[derive(Drop)]
pub extern type Array<T>;

impl ArrayDrop<T, +Drop<T>> of Drop<Array<T>>;

extern fn array_new<T>() -> Array<T> nopanic;
extern fn array_append<T>(ref arr: Array<T>, value: T) nopanic;
extern fn array_pop_front<T>(ref arr: Array<T>) -> Option<Box<T>> nopanic;
Expand Down
5 changes: 4 additions & 1 deletion corelib/src/box.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@

/// A `Box` is a type that points to a wrapped value.
/// It allows for cheap moving around of the value, as its size is small, and may wrap a large size.
#[derive(Copy, Drop)]
pub extern type Box<T>;

impl BoxCopy<T, +Copy<T>> of Copy<Box<T>>;
impl BoxDrop<T, +Drop<T>> of Drop<Box<T>>;


// These functions are only exposed in the corelib through the trait below since calling them
// directly with tuples panics due to auto unpacking of the tuple.
// TODO(Gil): Expose in the core lib when the described behaviour is fixed.
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/bytes_31.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ pub(crate) const POW_2_128: felt252 = 0x100000000000000000000000000000000;
pub(crate) const POW_2_8: u128 = 0x100;

/// Represents a 31-byte fixed-size byte type.
#[derive(Copy, Drop)]
pub extern type bytes31;

impl bytes31Copy of Copy<bytes31>;
impl bytes31Drop of Drop<bytes31>;

pub(crate) extern fn bytes31_const<const value: felt252>() -> bytes31 nopanic;
extern fn bytes31_try_from_felt252(value: felt252) -> Option<bytes31> implicits(RangeCheck) nopanic;
extern fn bytes31_to_felt252(value: bytes31) -> felt252 nopanic;
Expand Down
7 changes: 5 additions & 2 deletions corelib/src/ec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ pub extern type EcOp;
///
/// Points can be created using [`EcPointTrait::new`] or [`EcPointTrait::new_from_x`].
/// The zero point represents the point at infinity.
#[derive(Copy, Drop)]
pub extern type EcPoint;

impl EcPointCopy of Copy<EcPoint>;
impl EcPointDrop of Drop<EcPoint>;

/// A non-zero point on the STARK curve (cannot be the point at infinity).
pub type NonZeroEcPoint = NonZero<EcPoint>;

Expand Down Expand Up @@ -119,9 +121,10 @@ impl EcPointTryIntoNonZero of TryInto<EcPoint, NonZeroEcPoint> {
/// Use this to perform multiple point operations efficiently.
/// Initialize with [`EcStateTrait::init`], add points with [`EcStateTrait::add`]
/// or [`EcStateTrait::add_mul`], and finalize with [`EcStateTrait::finalize`].
#[derive(Drop)]
pub extern type EcState;

impl EcStateDrop of Drop<EcState>;

mod internal {
impl EcStateCopy of Copy<super::EcState>;
pub impl EcStateClone of Clone<super::EcState> {
Expand Down
5 changes: 3 additions & 2 deletions corelib/src/gas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use crate::RangeCheck;

/// Type representing the table of the costs of the different builtin usages.
#[cfg(not(gas: "disabled"))]
#[derive(Copy, Drop)]
pub extern type BuiltinCosts;

/// Placeholder when gas mechanism is disabled.
#[cfg(gas: "disabled")]
#[derive(Copy, Drop)]
pub struct BuiltinCosts {}

impl BuiltinCostsCopy of Copy<BuiltinCosts>;
impl BuiltinCostsDrop of Drop<BuiltinCosts>;

/// The gas builtin.
/// This type is used to handle gas in the Cairo code.
/// Contains the amount of gas available for the current run.
Expand Down
40 changes: 30 additions & 10 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ impl NumericLiteralfelt252 of NumericLiteral<felt252>;
impl NumericLiteralNonZero<T, +NumericLiteral<T>> of NumericLiteral<NonZero<T>>;

/// The 128-bit unsigned integer type.
#[derive(Copy, Drop)]
pub extern type u128;

impl u128Copy of Copy<u128>;
impl u128Drop of Drop<u128>;

impl NumericLiteralu128 of NumericLiteral<u128>;

impl U128Serde = crate::serde::into_felt252_based::SerdeImpl<u128>;
Expand Down Expand Up @@ -301,9 +303,11 @@ pub(crate) extern fn u128_is_zero(a: u128) -> IsZeroResult<u128> implicits() nop
pub extern fn u128_byte_reverse(input: u128) -> u128 implicits(Bitwise) nopanic;

/// The 8-bit unsigned integer type.
#[derive(Copy, Drop)]
pub extern type u8;

impl u8Copy of Copy<u8>;
impl u8Drop of Drop<u8>;

impl NumericLiteralu8 of NumericLiteral<u8>;

extern const fn u8_to_felt252(a: u8) -> felt252 nopanic;
Expand Down Expand Up @@ -455,9 +459,11 @@ impl U8BitSize of crate::num::traits::BitSize<u8> {
}

/// The 16-bit unsigned integer type.
#[derive(Copy, Drop)]
pub extern type u16;

impl u16Copy of Copy<u16>;
impl u16Drop of Drop<u16>;

impl NumericLiteralu16 of NumericLiteral<u16>;

extern const fn u16_to_felt252(a: u16) -> felt252 nopanic;
Expand Down Expand Up @@ -615,9 +621,11 @@ impl U16BitSize of crate::num::traits::BitSize<u16> {
}

/// The 32-bit unsigned integer type.
#[derive(Copy, Drop)]
pub extern type u32;

impl u32Copy of Copy<u32>;
impl u32Drop of Drop<u32>;

impl NumericLiteralu32 of NumericLiteral<u32>;

extern const fn u32_to_felt252(a: u32) -> felt252 nopanic;
Expand Down Expand Up @@ -775,9 +783,11 @@ impl U32BitSize of crate::num::traits::BitSize<u32> {
}

/// The 64-bit unsigned integer type.
#[derive(Copy, Drop)]
pub extern type u64;

impl u64Copy of Copy<u64>;
impl u64Drop of Drop<u64>;

impl NumericLiteralu64 of NumericLiteral<u64>;

extern const fn u64_to_felt252(a: u64) -> felt252 nopanic;
Expand Down Expand Up @@ -1924,9 +1934,11 @@ impl I128WrappingAdd = signed_int_impls::WrappingAddImpl<i128>;
impl I128WrappingSub = signed_int_impls::WrappingSubImpl<i128>;

/// The 8-bit signed integer type.
#[derive(Copy, Drop)]
pub extern type i8;

impl i8Copy of Copy<i8>;
impl i8Drop of Drop<i8>;

impl NumericLiterali8 of NumericLiteral<i8>;

extern const fn i8_try_from_felt252(a: felt252) -> Option<i8> implicits(RangeCheck) nopanic;
Expand Down Expand Up @@ -2009,9 +2021,11 @@ impl I8BitSize of crate::num::traits::BitSize<i8> {
}

/// The 16-bit signed integer type.
#[derive(Copy, Drop)]
pub extern type i16;

impl i16Copy of Copy<i16>;
impl i16Drop of Drop<i16>;

impl NumericLiterali16 of NumericLiteral<i16>;

extern const fn i16_try_from_felt252(a: felt252) -> Option<i16> implicits(RangeCheck) nopanic;
Expand Down Expand Up @@ -2095,9 +2109,11 @@ impl I16BitSize of crate::num::traits::BitSize<i16> {
}

/// The 32-bit signed integer type.
#[derive(Copy, Drop)]
pub extern type i32;

impl i32Copy of Copy<i32>;
impl i32Drop of Drop<i32>;

impl NumericLiterali32 of NumericLiteral<i32>;

extern const fn i32_try_from_felt252(a: felt252) -> Option<i32> implicits(RangeCheck) nopanic;
Expand Down Expand Up @@ -2181,9 +2197,11 @@ impl I32BitSize of crate::num::traits::BitSize<i32> {
}

/// The 64-bit signed integer type.
#[derive(Copy, Drop)]
pub extern type i64;

impl i64Copy of Copy<i64>;
impl i64Drop of Drop<i64>;

impl NumericLiterali64 of NumericLiteral<i64>;

extern const fn i64_try_from_felt252(a: felt252) -> Option<i64> implicits(RangeCheck) nopanic;
Expand Down Expand Up @@ -2267,9 +2285,11 @@ impl I64BitSize of crate::num::traits::BitSize<i64> {
}

/// The 128-bit signed integer type.
#[derive(Copy, Drop)]
pub extern type i128;

impl i128Copy of Copy<i128>;
impl i128Drop of Drop<i128>;

impl NumericLiterali128 of NumericLiteral<i128>;

extern const fn i128_try_from_felt252(a: felt252) -> Option<i128> implicits(RangeCheck) nopanic;
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/internal/bounded_int.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::integer::{downcast, upcast};
use crate::RangeCheck;

#[derive(Copy, Drop)]
pub(crate) extern type BoundedInt<const MIN: felt252, const MAX: felt252>;

impl BoundedIntCopy<const MIN: felt252, const MAX: felt252> of Copy<BoundedInt<MIN, MAX>>;
impl BoundedIntDrop<const MIN: felt252, const MAX: felt252> of Drop<BoundedInt<MIN, MAX>>;

impl NumericLiteralBoundedInt<
const MIN: felt252, const MAX: felt252,
> of crate::integer::NumericLiteral<BoundedInt<MIN, MAX>>;
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ use felt_252::{Felt252One, Felt252Zero};
/// a very large prime number currently equal to 2^251 + 17⋅2^192 + 1.
///
/// Any operation that uses `felt252` will be computed modulo P.
#[derive(Copy, Drop)]
pub extern type felt252;

impl felt252Copy of Copy<felt252>;
impl felt252Drop of Drop<felt252>;

extern fn felt252_const<const value: felt252>() -> felt252 nopanic;

impl Felt252Serde of Serde<felt252> {
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/nullable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ use crate::box::BoxTrait;
use crate::traits::{Default, Felt252DictValue};

/// A type that can either be null or contain a boxed value.
#[derive(Copy, Drop)]
pub extern type Nullable<T>;

impl NullableCopy<T, +Copy<T>> of Copy<Nullable<T>>;
impl NullableDrop<T, +Drop<T>> of Drop<Nullable<T>>;

/// Represents the result of matching a `Nullable` value.
///
/// Used to safely handle both null and non-null cases when using `match_nullable` on a
Expand Down
5 changes: 4 additions & 1 deletion corelib/src/ops/range.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,11 @@ mod internal {
use core::internal::OptionRev;
use core::iter::Iterator;

#[derive(Copy, Drop)]
pub extern type IntRange<T>;

impl IntRangeCopy<T> of Copy<IntRange<T>>;
impl IntRangeDrop<T> of Drop<IntRange<T>>;

pub extern fn int_range_try_new<T>(
x: T, y: T,
) -> Result<IntRange<T>, IntRange<T>> implicits(core::RangeCheck) nopanic;
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/sha256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use starknet::SyscallResultTrait;

/// A handle to the state of a SHA-256 hash.
#[derive(Copy, Drop)]
pub(crate) extern type Sha256StateHandle;

impl Sha256StateHandleCopy of Copy<Sha256StateHandle>;
impl Sha256StateHandleDrop of Drop<Sha256StateHandle>;

/// Initializes a new SHA-256 state handle with the given initial state.
extern fn sha256_state_handle_init(state: Box<[u32; 8]>) -> Sha256StateHandle nopanic;

Expand Down
4 changes: 3 additions & 1 deletion corelib/src/starknet/class_hash.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ use core::serde::Serde;

/// Represents a Starknet contract class hash.
/// The value range of this type is `[0, 2**251)`.
#[derive(Copy, Drop)]
pub extern type ClassHash;

impl ClassHashCopy of Copy<ClassHash>;
impl ClassHashDrop of Drop<ClassHash>;

/// Returns a `ClassHash` given a `felt252` value.
///
/// # Examples
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/starknet/contract_address.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ use core::zeroable::Zeroable;

/// Represents a Starknet contract address.
/// The value range of this type is `[0, 2**251)`.
#[derive(Copy, Drop)]
pub extern type ContractAddress;

impl ContractAddressCopy of Copy<ContractAddress>;
impl ContractAddressDrop of Drop<ContractAddress>;

/// Returns a `ContractAddress` given a `felt252` value.
///
/// # Examples
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/starknet/secp256k1.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ use starknet::secp256_trait::{
use starknet::{SyscallResult, SyscallResultTrait};

/// A point on the secp256k1 curve.
#[derive(Copy, Drop)]
pub extern type Secp256k1Point;

impl Secp256k1PointCopy of Copy<Secp256k1Point>;
impl Secp256k1PointDrop of Drop<Secp256k1Point>;

pub(crate) impl Secp256k1Impl of Secp256Trait<Secp256k1Point> {
// TODO(yuval): change to constant once u256 constants are supported.
fn get_curve_size() -> u256 {
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/starknet/secp256r1.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ use starknet::secp256_trait::{Secp256PointTrait, Secp256Trait};
use starknet::{EthAddress, SyscallResult, SyscallResultTrait};

/// Represents a point on the secp256r1 elliptic curve.
#[derive(Copy, Drop)]
pub extern type Secp256r1Point;

impl Secp256r1PointCopy of Copy<Secp256r1Point>;
impl Secp256r1PointDrop of Drop<Secp256r1Point>;

pub(crate) impl Secp256r1Impl of Secp256Trait<Secp256r1Point> {
// TODO(yuval): change to constant once u256 constants are supported.
fn get_curve_size() -> u256 {
Expand Down
8 changes: 6 additions & 2 deletions corelib/src/starknet/storage_access.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ use starknet::syscalls::{storage_read_syscall, storage_write_syscall};

/// Represents the address of a storage value in a Starknet contract.
/// The value range of this type is `[0, 2**251)`.
#[derive(Copy, Drop)]
pub extern type StorageAddress;

impl StorageAddressCopy of Copy<StorageAddress>;
impl StorageAddressDrop of Drop<StorageAddress>;

/// Represents a base storage address that can be combined with offsets.
/// The value range of this type is `[0, 2**251 - 256)`.
#[derive(Copy, Drop)]
pub extern type StorageBaseAddress;

impl StorageBaseAddressCopy of Copy<StorageBaseAddress>;
impl StorageBaseAddressDrop of Drop<StorageBaseAddress>;

/// Returns a `StorageBaseAddress` given a constant `felt252` value.
///
/// The value is validated to be in the range `[0, 2**251 - 256)` at compile time.
Expand Down
4 changes: 3 additions & 1 deletion corelib/src/zeroable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ pub(crate) impl Felt252Zeroable = zero_based::ZeroableImpl<felt252>;
/// A wrapper type for non-zero values of type T.
///
/// This type guarantees that the wrapped value is never zero.
#[derive(Copy, Drop)]
pub extern type NonZero<T>;

impl NonZeroCopy<T> of Copy<NonZero<T>>;
impl NonZeroDrop<T> of Drop<NonZero<T>>;

impl NonZeroNeg<T, +Neg<T>, +TryInto<T, NonZero<T>>> of Neg<NonZero<T>> {
fn neg(a: NonZero<T>) -> NonZero<T> {
// TODO(orizi): Optimize using bounded integers.
Expand Down
14 changes: 2 additions & 12 deletions crates/cairo-lang-plugins/src/plugins/derive/clone.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_syntax::node::ast;
use indent::indent_by;
use indoc::formatdoc;
use itertools::Itertools;

use super::{DeriveInfo, unsupported_for_extern_diagnostic};
use super::DeriveInfo;
use crate::plugins::derive::TypeVariantInfo;

/// Adds derive result for the `Clone` trait.
pub fn handle_clone(
info: &DeriveInfo,
derived: &ast::ExprPath,
diagnostics: &mut Vec<PluginDiagnostic>,
) -> Option<String> {
pub fn handle_clone(info: &DeriveInfo) -> Option<String> {
let header =
info.format_impl_header("core::clone", "Clone", &["core::clone::Clone", "Destruct"]);
let full_typename = info.full_typename();
Expand Down Expand Up @@ -45,10 +39,6 @@ pub fn handle_clone(
}).join("\n"))
}
}
TypeVariantInfo::Extern => {
diagnostics.push(unsupported_for_extern_diagnostic(derived));
return None;
}
},
);
Some(formatdoc! {"
Expand Down
Loading

0 comments on commit dfb5d3f

Please sign in to comment.