diff --git a/corelib/src/test/language_features/const_test.cairo b/corelib/src/test/language_features/const_test.cairo index 702ef3e1ca4..872373a7ee6 100644 --- a/corelib/src/test/language_features/const_test.cairo +++ b/corelib/src/test/language_features/const_test.cairo @@ -158,3 +158,25 @@ fn test_complex_consts() { }; assert_eq!(IF_CONST_FALSE, 7); } + +mod const_starknet_consts { + pub extern fn const_as_box() -> 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, + value::Const, + >, + 0, + >() + .unbox() == (1000.try_into().unwrap(), 1001.try_into().unwrap()), + ); +} + diff --git a/crates/cairo-lang-sierra-to-casm/src/test_data/errors b/crates/cairo-lang-sierra-to-casm/src/test_data/errors index 52ad030dc12..81467fd49f8 100644 --- a/crates/cairo-lang-sierra-to-casm/src/test_data/errors +++ b/crates/cairo-lang-sierra-to-casm/src/test_data/errors @@ -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; +type OutOfRange = Const; + +//! > 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; +type OutOfRange = Const; + +//! > error +Error from program registry: Error during type specialization of `OutOfRange`: Could not specialize type diff --git a/crates/cairo-lang-sierra/src/extensions/modules/const_type.rs b/crates/cairo-lang-sierra/src/extensions/modules/const_type.rs index e7d439273c6..886712b3a4e 100644 --- a/crates/cairo-lang-sierra/src/extensions/modules/const_type.rs +++ b/crates/cairo-lang-sierra/src/extensions/modules/const_type.rs @@ -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; @@ -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(()) }