Skip to content

Commit

Permalink
Added const for starknet types. (#6961)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi committed Jan 15, 2025
1 parent 31f41d8 commit 4996361
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
22 changes: 22 additions & 0 deletions corelib/src/test/language_features/const_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,25 @@ fn test_complex_consts() {
};
assert_eq!(IF_CONST_FALSE, 7);
}

mod const_starknet_consts {
pub extern fn const_as_box<T, const SEGMENT_INDEX: felt252>() -> Box<
(starknet::ContractAddress, starknet::ClassHash),
> nopanic;
}

#[test]
fn test_starknet_consts() {
assert!(
const_starknet_consts::const_as_box::<
struct2::Const<
(starknet::ContractAddress, starknet::ClassHash),
value::Const<starknet::ContractAddress, 1000>,
value::Const<starknet::ClassHash, 1001>,
>,
0,
>()
.unbox() == (1000.try_into().unwrap(), 1001.try_into().unwrap()),
);
}

30 changes: 30 additions & 0 deletions crates/cairo-lang-sierra-to-casm/src/test_data/errors
Original file line number Diff line number Diff line change
Expand Up @@ -1079,3 +1079,33 @@ foo@0() -> ();

//! > error
Code size limit exceeded.

//! > ==========================================================================

//! > ContractAddress out of range.

//! > test_runner_name
compiler_errors

//! > sierra_code
type ContractAddress = ContractAddress;
type InRange = Const<ContractAddress, 3618502788666131106986593281521497120414687020801267626233049500247285301247>;
type OutOfRange = Const<ContractAddress, 3618502788666131106986593281521497120414687020801267626233049500247285301248>;

//! > error
Error from program registry: Error during type specialization of `OutOfRange`: Could not specialize type

//! > ==========================================================================

//! > ClassHash out of range.

//! > test_runner_name
compiler_errors

//! > sierra_code
type ClassHash = ClassHash;
type InRange = Const<ClassHash, 3618502788666131106986593281521497120414687020801267626233049500247285301247>;
type OutOfRange = Const<ClassHash, 3618502788666131106986593281521497120414687020801267626233049500247285301248>;

//! > error
Error from program registry: Error during type specialization of `OutOfRange`: Could not specialize type
37 changes: 24 additions & 13 deletions crates/cairo-lang-sierra/src/extensions/modules/const_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ use itertools::Itertools;
use num_traits::{ToPrimitive, Zero};

use super::boxing::box_ty;
use super::consts::ConstGenLibfunc;
use super::enm::EnumType;
use super::int::unsigned128::Uint128Type;
use super::non_zero::NonZeroType;
use super::starknet::interoperability::{
ClassHashConstLibfuncWrapped, ClassHashType, ContractAddressConstLibfuncWrapped,
ContractAddressType,
};
use super::structure::StructType;
use super::utils::Range;
use crate::define_libfunc_hierarchy;
Expand Down Expand Up @@ -67,20 +72,26 @@ fn validate_const_data(
inner_data: &[GenericArg],
) -> Result<(), SpecializationError> {
let inner_type_info = context.get_type_info(inner_ty.clone())?;
if inner_type_info.long_id.generic_id == StructType::ID {
validate_const_struct_data(context, &inner_type_info, inner_data)?;
} else if inner_type_info.long_id.generic_id == EnumType::ID {
validate_const_enum_data(context, &inner_type_info, inner_data)?;
} else if inner_type_info.long_id.generic_id == NonZeroType::ID {
validate_const_nz_data(context, &inner_type_info, inner_data)?;
let inner_generic_id = &inner_type_info.long_id.generic_id;
if *inner_generic_id == StructType::ID {
return validate_const_struct_data(context, &inner_type_info, inner_data);
} else if *inner_generic_id == EnumType::ID {
return validate_const_enum_data(context, &inner_type_info, inner_data);
} else if *inner_generic_id == NonZeroType::ID {
return validate_const_nz_data(context, &inner_type_info, inner_data);
}
let type_range = if *inner_generic_id == ContractAddressType::id() {
Range::half_open(0, ContractAddressConstLibfuncWrapped::bound())
} else if *inner_generic_id == ClassHashType::id() {
Range::half_open(0, ClassHashConstLibfuncWrapped::bound())
} else {
let type_range = Range::from_type_info(&inner_type_info)?;
let [GenericArg::Value(value)] = inner_data else {
return Err(SpecializationError::WrongNumberOfGenericArgs);
};
if !(&type_range.lower <= value && value < &type_range.upper) {
return Err(SpecializationError::UnsupportedGenericArg);
}
Range::from_type_info(&inner_type_info)?
};
let [GenericArg::Value(value)] = inner_data else {
return Err(SpecializationError::WrongNumberOfGenericArgs);
};
if !(&type_range.lower <= value && value < &type_range.upper) {
return Err(SpecializationError::UnsupportedGenericArg);
}
Ok(())
}
Expand Down

0 comments on commit 4996361

Please sign in to comment.