diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index bfc7f8ce88d..1ca47895c79 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -1792,12 +1792,14 @@ impl<'eng> FnCompiler<'eng> { ) .or(self.compile_const_decl(context, md_mgr, const_decl, span_md_idx, true))?; - // parse constant string data into a string slice + // String slices are not allowed in constants if let Some(TypeContent::StringSlice) = result.get_type(context).map(|t| t.get_content(context)) { - todo!(); - // return self.compile_string_slice(context, span_md_idx, result, *len); + return Err(CompileError::TypeNotAllowed { + reason: sway_error::error::TypeNotAllowedReason::StringSliceInConst, + span: const_decl.span.clone(), + }); } Ok(result) diff --git a/sway-lib-std/src/hash.sw b/sway-lib-std/src/hash.sw index 8763748b41c..130c98f0f4c 100644 --- a/sway-lib-std/src/hash.sw +++ b/sway-lib-std/src/hash.sw @@ -325,11 +325,24 @@ pub fn sha256(s: T) -> b256 where T: Hash { hasher.sha256() } +/// Returns the `SHA-2-256` hash of `param`. +/// This function is specific for string arrays +/// +/// # Examples +/// +/// ```sway +/// use std::hash::*; +/// +/// fn foo() { +/// let result = sha256_str_array(__to_str_array("Fuel")); +/// assert(result = 0xa80f942f4112036dfc2da86daf6d2ef6ede3164dd56d1000eb82fa87c992450f); +/// } +/// ``` #![inline(never)] -pub fn sha256_str(s: S) -> b256 { +pub fn sha256_str_array(param: S) -> b256 { __check_str_type::(); let str_size = __size_of_str::(); - let str_ptr = __addr_of(s); + let str_ptr = __addr_of(param); let mut bytes = Bytes::with_capacity(str_size); bytes.len = str_size; @@ -369,10 +382,9 @@ pub fn keccak256(s: T) -> b256 where T: Hash { } // Tests -// #[test()] -fn test_hasher_sha256_str() { +fn test_hasher_sha256_str_array() { use ::assert::assert; let mut hasher = Hasher::new(); hasher.write_str("test"); diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/hash_str/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/hash_str/src/main.sw index 0d07eaae741..5169a0c05f7 100644 --- a/test/src/e2e_vm_tests/test_programs/should_fail/hash_str/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_fail/hash_str/src/main.sw @@ -3,6 +3,6 @@ script; use std::hash::*; fn main() -> u64 { - let _hash = sha256_str(0_u8); + let _hash = sha256_str_array(0_u8); 5 } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/src/main.sw index bca9ba5d618..33b505b1863 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/src/main.sw @@ -44,7 +44,7 @@ fn test_first_use() { assert(C3.y == true); assert(C4 == MyEnum::A(42)); assert(C5 == MyEnum::B(true)); - assert(sha256_str(C6) == sha256("fuel")); + assert(sha256_str_array(C6) == sha256("fuel")); assert(C7[0] == 1); assert(C7[1] == 2); assert(C7[2] == 3); @@ -61,7 +61,7 @@ fn test_second_use() { assert(C3.y == true); assert(C4 == MyEnum::A(42)); assert(C5 == MyEnum::B(true)); - assert(sha256_str(C6) == sha256("fuel")); + assert(sha256_str_array(C6) == sha256("fuel")); assert(C7[0] == 1); assert(C7[1] == 2); assert(C7[2] == 3); @@ -78,7 +78,7 @@ fn test_inline_use() { assert(C3.y == true); assert(C4 == MyEnum::A(42)); assert(C5 == MyEnum::B(true)); - assert(sha256_str(C6) == sha256("fuel")); + assert(sha256_str_array(C6) == sha256("fuel")); assert(C7[0] == 1); assert(C7[1] == 2); assert(C7[2] == 3); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw index 16c23e31153..6956142bc0f 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw @@ -30,7 +30,7 @@ fn main() -> u64 { __to_str_array("222"), __to_str_array("333") ]); - assert(sha256("111") == sha256_str(result)); + assert(sha256("111") == sha256_str_array(result)); 1 } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw index 71ec2b653d5..33f19772df8 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw @@ -49,7 +49,7 @@ fn main() -> bool { revert(0) } } - assert(sha256_str(caller.get_string()) == sha256("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + assert(sha256_str_array(caller.get_string()) == sha256("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); // Test 1 caller.set_x(1); @@ -192,7 +192,7 @@ fn main() -> bool { caller.set_string(__to_str_array("fuelfuelfuelfuelfuelfuelfuelfuelfuelfuel")); // Can't compare strings right now so compare hashes instead - assert(sha256_str(caller.get_string()) == sha256("fuelfuelfuelfuelfuelfuelfuelfuelfuelfuel")); + assert(sha256_str_array(caller.get_string()) == sha256("fuelfuelfuelfuelfuelfuelfuelfuelfuelfuel")); true } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/raw_ptr/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/raw_ptr/src/main.sw index c03193e8f9a..b4d55156a74 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/raw_ptr/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/raw_ptr/src/main.sw @@ -79,8 +79,8 @@ fn main() -> bool { let read_small_string_1 = buf_ptr.read::(); buf_ptr.write(small_string_2); let read_small_string_2 = buf_ptr.read::(); - assert(sha256_str(small_string_1) == sha256_str(read_small_string_1)); - assert(sha256_str(small_string_2) == sha256_str(read_small_string_2)); + assert(sha256_str_array(small_string_1) == sha256_str_array(read_small_string_1)); + assert(sha256_str_array(small_string_2) == sha256_str_array(read_small_string_2)); let buf_ptr = alloc::(2); let large_string_1 = __to_str_array("fuelfuelfuel"); @@ -89,8 +89,8 @@ fn main() -> bool { let read_large_string_1 = buf_ptr.read::(); buf_ptr.write(large_string_2); let read_large_string_2 = buf_ptr.read::(); - assert(sha256_str(large_string_1) == sha256_str(read_large_string_1)); - assert(sha256_str(large_string_2) == sha256_str(read_large_string_2)); + assert(sha256_str_array(large_string_1) == sha256_str_array(read_large_string_1)); + assert(sha256_str_array(large_string_2) == sha256_str_array(read_large_string_2)); true } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/data_structures.sw b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/data_structures.sw index da016fba689..7a9229829da 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/data_structures.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/data_structures.sw @@ -46,6 +46,6 @@ impl Eq for [u64; 3] { impl Eq for str[4] { fn eq(self, other: Self) -> bool { - sha256_str(self) == sha256_str(other) + sha256_str_array(self) == sha256_str_array(other) } } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/test_abis/contract_with_type_aliases_abi/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/test_abis/contract_with_type_aliases_abi/src/main.sw index 1b6a9b7d162..8dc4aa0cc74 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/test_abis/contract_with_type_aliases_abi/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/test_abis/contract_with_type_aliases_abi/src/main.sw @@ -25,7 +25,7 @@ impl core::ops::Eq for Tuple { pub type StringTy = str[9]; impl core::ops::Eq for StringTy { fn eq(self, other: Self) -> bool { - sha256_str(self) == sha256_str(other) + sha256_str_array(self) == sha256_str_array(other) } } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/basic_storage/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/basic_storage/src/main.sw index b19137dbf27..fb0012c45a8 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/basic_storage/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/basic_storage/src/main.sw @@ -209,5 +209,5 @@ fn test_storage() { // all the ASM blocks. #[inline(never)] fn assert_streq(lhs: S1, rhs: str) { - assert(sha256_str(lhs) == sha256(rhs)); + assert(sha256_str_array(lhs) == sha256(rhs)); } diff --git a/test/src/sdk-harness/test_projects/hashing/mod.rs b/test/src/sdk-harness/test_projects/hashing/mod.rs index 4a91d7fdf1a..1e94945ac0b 100644 --- a/test/src/sdk-harness/test_projects/hashing/mod.rs +++ b/test/src/sdk-harness/test_projects/hashing/mod.rs @@ -306,19 +306,19 @@ mod sha256 { let call_1 = instance .methods() - .sha256_str(SizedAsciiString::try_from("John").unwrap()) + .sha256_str_array(SizedAsciiString::try_from("John").unwrap()) .call() .await .unwrap(); let call_2 = instance .methods() - .sha256_str(SizedAsciiString::try_from("John").unwrap()) + .sha256_str_array(SizedAsciiString::try_from("John").unwrap()) .call() .await .unwrap(); let call_3 = instance .methods() - .sha256_str(SizedAsciiString::try_from("Nick").unwrap()) + .sha256_str_array(SizedAsciiString::try_from("Nick").unwrap()) .call() .await .unwrap(); @@ -468,11 +468,21 @@ mod sha256 { Hash::Sha256, )); - let call_1 = instance.methods().sha256_struct(true).call().await.unwrap(); - let call_2 = instance.methods().sha256_struct(true).call().await.unwrap(); + let call_1 = instance + .methods() + .sha256_str_arrayuct(true) + .call() + .await + .unwrap(); + let call_2 = instance + .methods() + .sha256_str_arrayuct(true) + .call() + .await + .unwrap(); let call_3 = instance .methods() - .sha256_struct(false) + .sha256_str_arrayuct(false) .call() .await .unwrap(); diff --git a/test/src/sdk-harness/test_projects/hashing/src/main.sw b/test/src/sdk-harness/test_projects/hashing/src/main.sw index 7ee310072ec..e10d711f9f1 100644 --- a/test/src/sdk-harness/test_projects/hashing/src/main.sw +++ b/test/src/sdk-harness/test_projects/hashing/src/main.sw @@ -46,12 +46,12 @@ abi MyContract { fn sha256_u32(value: u32) -> b256; fn sha256_u64(value: u64) -> b256; fn sha256_bool(value: bool) -> b256; - fn sha256_str(value: str[4]) -> b256; + fn sha256_str_array(value: str[4]) -> b256; fn sha256_b256(value: b256) -> b256; fn sha256_tuple(value: (bool, u64)) -> b256; fn sha256_array(value1: u64, value2: u64) -> b256; fn sha256_enum(location: bool) -> b256; - fn sha256_struct(location: bool) -> b256; + fn sha256_str_arrayuct(location: bool) -> b256; fn keccak256_u8(value: u8) -> b256; fn keccak256_u16(value: u16) -> b256; @@ -106,7 +106,7 @@ impl MyContract for Contract { sha256(value) } - fn sha256_str(value: str[4]) -> b256 { + fn sha256_str_array(value: str[4]) -> b256 { sha256(value) } @@ -130,7 +130,7 @@ impl MyContract for Contract { }) } - fn sha256_struct(location: bool) -> b256 { + fn sha256_str_arrayuct(location: bool) -> b256 { sha256(Person { name: "John", age: 18, diff --git a/test/src/sdk-harness/test_projects/storage_init/src/main.sw b/test/src/sdk-harness/test_projects/storage_init/src/main.sw index 70cc4d78692..5f3886378b2 100644 --- a/test/src/sdk-harness/test_projects/storage_init/src/main.sw +++ b/test/src/sdk-harness/test_projects/storage_init/src/main.sw @@ -140,7 +140,7 @@ impl ExperimentalStorageInitTest for Contract { assert(storage.s.t.int32.read() == s.t.int32); assert(storage.e.read() == e); assert(storage.e2.read() == e2); - assert(sha256_str(storage.string.read()) == sha256_str(string)); + assert(sha256_str_array(storage.string.read()) == sha256_str_array(string)); true } }