Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added const for starknet types. #6961

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions corelib/src/test/language_features/const_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,24 @@ fn test_two_complex_enums() {
.unbox() == (ThreeOptions2::A(1337), ThreeOptions2::C),
);
}

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
Loading