Commit graph

405 commits

Author SHA1 Message Date
Igor Rončević
bd3813606a
Optimize vec and bytes modules for bytecode size and gas usage (#7162)
## Description

This PR mostly optimizes implementations of several traits, like
`AbiEncode`, `Iterator`, `PartialEq`, and methods for `Vec` and `Bytes`.

One typical common optimization was replacing the internal calls to the
public `get` method with a direct element read. Each call to the public
`get` in internal implementations of those traits had an overhead of
double-checking the boundary and unnecessary wrapping and unwrapping the
fetched value.

Also, whenever possible `mcp` instruction was used for converting
between `Bytes` and `Vec<u8>`, instead of costly iteration and copying
of each element individually.

The PR also removes calls to getters in internal implementations to
mitigate the issue of inlining of getters explained in #7152.

The bytecode size of the sample script that uses optimized code was
**reduced for 37.58%, from 2384 bytes to 1488**:

```
script;

use std::bytes::Bytes;

fn main() {
    let vec: Vec<u8> = Vec::new();
    let bytes: Bytes = vec.into();
    let vec: Vec<u8> = bytes.into();

    let _ = vec == vec;
    for _ in vec.iter() { }

    let _ = bytes == bytes;
    for _ in bytes.iter() { }
}

```

Out of 822 `in_language_tests`, **303 tests got reduced gas costs, up to
57.75% and in average 18.97%**.

<details>
<summary>Click to see improvements in gas costs</summary>

| Test | Before | After | Percentage |
|------|--------|-------|------------|
| address_inline_tests::address_hash | 1648 | 1468 | 10.92% |
| address_inline_tests::address_try_from_bytes | 1968 | 1714 | 12.91% |
| address_inline_tests::address_try_into_bytes | 11555 | 9566 | 17.21% |
| asset_id_inline_tests::asset_id_hasher | 1240 | 1105 | 10.89% |
| asset_id_inline_tests::asset_id_try_from_bytes | 1968 | 1714 | 12.91%
|
| asset_id_inline_tests::asset_id_try_into_bytes | 11555 | 9566 | 17.21%
|
| b512_inline_tests::b512_into_bytes | 22732 | 18823 | 17.20% |
| b512_inline_tests::b512_try_from_bytes | 2372 | 2127 | 10.33% |
| bytes_conversions_b256_inline_tests::b256_from_be_bytes | 4401 | 3069
| 30.27% |
| bytes_conversions_b256_inline_tests::b256_from_le_bytes | 5681 | 3810
| 32.93% |
| bytes_conversions_b256_inline_tests::b256_to_be_bytes | 5337 | 4697 |
11.99% |
| bytes_conversions_b256_inline_tests::b256_to_le_bytes | 6951 | 5879 |
15.42% |
| bytes_conversions_u16_inline_tests::u16_from_be_bytes | 285 | 218 |
23.51% |
| bytes_conversions_u16_inline_tests::u16_from_le_bytes | 285 | 218 |
23.51% |
| bytes_conversions_u16_inline_tests::u16_to_be_bytes | 365 | 325 |
10.96% |
| bytes_conversions_u16_inline_tests::u16_to_le_bytes | 366 | 326 |
10.93% |
| bytes_conversions_u256_inline_tests::u256_from_be_bytes | 4413 | 3081
| 30.18% |
| bytes_conversions_u256_inline_tests::u256_from_le_bytes | 5681 | 3810
| 32.93% |
| bytes_conversions_u256_inline_tests::u256_to_be_bytes | 5376 | 4736 |
11.90% |
| bytes_conversions_u256_inline_tests::u256_to_le_bytes | 6951 | 5879 |
15.42% |
| bytes_conversions_u32_inline_tests::u32_from_be_bytes | 435 | 325 |
25.29% |
| bytes_conversions_u32_inline_tests::u32_from_le_bytes | 435 | 325 |
25.29% |
| bytes_conversions_u32_inline_tests::u32_to_be_bytes | 594 | 514 |
13.47% |
| bytes_conversions_u32_inline_tests::u32_to_le_bytes | 594 | 514 |
13.47% |
| bytes_conversions_u64_inline_tests::u64_from_be_bytes | 713 | 518 |
27.35% |
| bytes_conversions_u64_inline_tests::u64_from_le_bytes | 713 | 518 |
27.35% |
| bytes_conversions_u64_inline_tests::u64_to_be_bytes | 1052 | 892 |
15.21% |
| bytes_conversions_u64_inline_tests::u64_to_le_bytes | 1052 | 892 |
15.21% |
| bytes_inline_tests::bytes_append | 3619 | 2900 | 19.87% |
| bytes_inline_tests::bytes_append_empty | 1243 | 990 | 20.35% |
| bytes_inline_tests::bytes_append_empty_self | 151 | 130 | 13.91% |
| bytes_inline_tests::bytes_append_self | 2117 | 1667 | 21.26% |
| bytes_inline_tests::bytes_append_to_empty | 1662 | 1349 | 18.83% |
| bytes_inline_tests::bytes_as_raw_slice | 716 | 572 | 20.11% |
| bytes_inline_tests::bytes_b256_into | 2880 | 1536 | 46.67% |
| bytes_inline_tests::bytes_b256_try_from | 6684 | 3812 | 42.97% |
| bytes_inline_tests::bytes_bigger_than_3064 | 249483 | 120753 | 51.60%
|
| bytes_inline_tests::bytes_buffer_ownership | 1335 | 1321 | 1.05% |
| bytes_inline_tests::bytes_capacity | 1010 | 662 | 34.46% |
| bytes_inline_tests::bytes_clear | 691 | 542 | 21.56% |
| bytes_inline_tests::bytes_clear_empty_bytes | 273 | 227 | 16.85% |
| bytes_inline_tests::bytes_clear_twice | 926 | 712 | 23.11% |
| bytes_inline_tests::bytes_clone | 1654 | 1237 | 25.21% |
| bytes_inline_tests::bytes_eq | 1261 | 925 | 26.65% |
| bytes_inline_tests::bytes_for_loop | 2110 | 1393 | 33.98% |
| bytes_inline_tests::bytes_from_b256 | 2875 | 1531 | 46.75% |
| bytes_inline_tests::bytes_from_raw_slice | 332 | 309 | 6.93% |
| bytes_inline_tests::bytes_from_vec_u8 | 2135 | 1092 | 48.85% |
| bytes_inline_tests::bytes_get | 1476 | 1221 | 17.28% |
| bytes_inline_tests::bytes_insert | 1332 | 1037 | 22.15% |
| bytes_inline_tests::bytes_insert_back | 1341 | 1046 | 22.00% |
| bytes_inline_tests::bytes_insert_before_back | 1348 | 1053 | 21.88% |
| bytes_inline_tests::bytes_insert_front | 1339 | 1044 | 22.03% |
| bytes_inline_tests::bytes_insert_twice | 2242 | 1781 | 20.56% |
| bytes_inline_tests::bytes_into_raw_slice | 716 | 572 | 20.11% |
| bytes_inline_tests::bytes_into_vec_u8 | 1687 | 1020 | 39.54% |
| bytes_inline_tests::bytes_is_empty | 722 | 596 | 17.45% |
| bytes_inline_tests::bytes_iter | 2194 | 1461 | 33.41% |
| bytes_inline_tests::bytes_len | 1047 | 753 | 28.08% |
| bytes_inline_tests::bytes_ne | 1233 | 914 | 25.87% |
| bytes_inline_tests::bytes_new | 145 | 121 | 16.55% |
| bytes_inline_tests::bytes_pop | 2856 | 2029 | 28.96% |
| bytes_inline_tests::bytes_ptr | 815 | 620 | 23.93% |
| bytes_inline_tests::bytes_push | 1841 | 1233 | 33.03% |
| bytes_inline_tests::bytes_raw_slice_from | 716 | 572 | 20.11% |
| bytes_inline_tests::bytes_raw_slice_into | 332 | 309 | 6.93% |
| bytes_inline_tests::bytes_remove | 1335 | 1038 | 22.25% |
| bytes_inline_tests::bytes_remove_all | 932 | 714 | 23.39% |
| bytes_inline_tests::bytes_remove_end | 1057 | 845 | 20.06% |
| bytes_inline_tests::bytes_remove_front | 1052 | 840 | 20.15% |
| bytes_inline_tests::bytes_resize | 4687 | 4081 | 12.93% |
| bytes_inline_tests::bytes_set | 1133 | 877 | 22.59% |
| bytes_inline_tests::bytes_set_back | 1156 | 900 | 22.15% |
| bytes_inline_tests::bytes_set_front | 1133 | 877 | 22.59% |
| bytes_inline_tests::bytes_set_twice | 1647 | 1284 | 22.04% |
| bytes_inline_tests::bytes_splice | 1883 | 1431 | 24.00% |
| bytes_inline_tests::bytes_splice_empty_range | 1365 | 1077 | 21.10% |
| bytes_inline_tests::bytes_splice_end | 1385 | 1097 | 20.79% |
| bytes_inline_tests::bytes_splice_entire_range | 1368 | 1121 | 18.06% |
| bytes_inline_tests::bytes_splice_front | 1363 | 1076 | 21.06% |
| bytes_inline_tests::bytes_splice_replace_empty_bytes | 1363 | 1076 |
21.06% |
| bytes_inline_tests::bytes_splice_replace_larger | 2264 | 1709 | 24.51%
|
| bytes_inline_tests::bytes_splice_replace_overlap | 1950 | 1497 |
23.23% |
| bytes_inline_tests::bytes_splice_replace_same_length | 1948 | 1495 |
23.25% |
| bytes_inline_tests::bytes_splice_replace_smaller | 2198 | 1643 |
25.25% |
| bytes_inline_tests::bytes_split_at | 1620 | 1174 | 27.53% |
| bytes_inline_tests::bytes_split_at_end | 1423 | 1110 | 22.00% |
| bytes_inline_tests::bytes_split_at_front | 1404 | 1088 | 22.51% |
| bytes_inline_tests::bytes_split_at_twice | 1619 | 1087 | 32.86% |
| bytes_inline_tests::bytes_swap | 1352 | 1034 | 23.52% |
| bytes_inline_tests::bytes_swap_end | 1069 | 836 | 21.80% |
| bytes_inline_tests::bytes_swap_front | 1068 | 835 | 21.82% |
| bytes_inline_tests::bytes_swap_front_with_end | 1069 | 836 | 21.80% |
| bytes_inline_tests::bytes_swap_twice | 2003 | 1558 | 22.22% |
| bytes_inline_tests::bytes_test_packing | 1325 | 840 | 36.60% |
| bytes_inline_tests::bytes_test_u8_limits | 1639 | 1244 | 24.10% |
| bytes_inline_tests::bytes_try_into_b256 | 6595 | 3803 | 42.34% |
| bytes_inline_tests::bytes_vec_u8_from | 1661 | 995 | 40.10% |
| bytes_inline_tests::bytes_vec_u8_into | 2135 | 1092 | 48.85% |
| bytes_inline_tests::bytes_with_capacity | 856 | 741 | 13.43% |
| bytes_inline_tests::revert_bytes_insert_out_of_bounds | 612 | 484 |
20.92% |
| bytes_inline_tests::revert_bytes_remove_out_of_bounds | 585 | 456 |
22.05% |
| bytes_inline_tests::revert_bytes_set_out_of_bounds | 587 | 457 |
22.15% |
| bytes_inline_tests::revert_bytes_splice_end_out_of_bounds | 696 | 570
| 18.10% |
| bytes_inline_tests::revert_bytes_splice_start_greater_than_end | 649 |
523 | 19.41% |
| bytes_inline_tests::revert_bytes_split_at_out_of_bounds | 606 | 483 |
20.30% |
| bytes_inline_tests::revert_bytes_swap_element_1_out_of_bounds | 587 |
457 | 22.15% |
| bytes_inline_tests::revert_bytes_swap_element_2_out_of_bounds | 591 |
461 | 22.00% |
| bytes_inline_tests::test_encode_decode | 588 | 619 | -5.27% |
| codec_implemented_tests::test_logging | 27189 | 27185 | 0.01% |
| contract_id_inline_tests::contract_id_hash | 1648 | 1468 | 10.92% |
| contract_id_inline_tests::contract_id_try_from_bytes | 1968 | 1714 |
12.91% |
| contract_id_inline_tests::contract_id_try_into_bytes | 11555 | 9566 |
17.21% |
| crypto_ed25519_inline_tests::ed25519_hash | 1926 | 1882 | 2.28% |
| crypto_ed25519_inline_tests::ed25519_into_bytes | 26703 | 22863 |
14.38% |
| crypto_ed25519_inline_tests::ed25519_try_from_bytes | 87153 | 86958 |
0.22% |
| crypto_ed25519_inline_tests::ed25519__verify | 13047 | 12823 | 1.72% |
| crypto_message_inline_tests::message_bytes | 996 | 787 | 20.98% |
| crypto_message_inline_tests::message_eq | 38026 | 32008 | 15.83% |
| crypto_message_inline_tests::message_from_b256 | 19321 | 17401 | 9.94%
|
| crypto_message_inline_tests::message_from_bytes | 3134 | 2516 | 19.72%
|
| crypto_message_inline_tests::message_hash | 606 | 518 | 14.52% |
| crypto_message_inline_tests::message_new | 209 | 186 | 11.00% |
| crypto_message_inline_tests::message_try_into_b256 | 1053 | 942 |
10.54% |
| crypto_point2d_inline_tests::point2d_b256_array_try_from | 1498 | 1360
| 9.21% |
| crypto_point2d_inline_tests::point2d_b256_tuple_try_from | 1457 | 1319
| 9.47% |
| crypto_point2d_inline_tests::point2d_from_b256_array | 2591 | 2213 |
14.59% |
| crypto_point2d_inline_tests::point2d_from_b256_tuple | 2561 | 2183 |
14.76% |
| crypto_point2d_inline_tests::point2d_from_u256_array | 2642 | 2264 |
14.31% |
| crypto_point2d_inline_tests::point2d_from_u256_tuple | 2612 | 2234 |
14.47% |
| crypto_point2d_inline_tests::point2d_from_u8_array | 19636 | 11386 |
42.01% |
| crypto_point2d_inline_tests::point2d_is_zero | 44315 | 36635 | 17.33%
|
| crypto_point2d_inline_tests::point2d_min | 683 | 603 | 11.71% |
| crypto_point2d_inline_tests::point2d_u256_array_try_from | 1549 | 1411
| 8.91% |
| crypto_point2d_inline_tests::point2d_u256_tuple_try_from | 1508 | 1370
| 9.15% |
| crypto_point2d_inline_tests::point2d_x | 887 | 818 | 7.78% |
| crypto_point2d_inline_tests::point2d_y | 890 | 821 | 7.75% |
| crypto_point2d_inline_tests::point2d_zero | 683 | 603 | 11.71% |
| crypto_public_key_inline_tests::public_key_bytes | 638 | 555 | 13.01%
|
| crypto_public_key_inline_tests::public_key_eq | 96007 | 80487 | 16.17%
|
| crypto_public_key_inline_tests::public_key_from_b256 | 19281 | 17361 |
9.96% |
| crypto_public_key_inline_tests::public_key_from_b256_tuple | 38468 |
34628 | 9.98% |
| crypto_public_key_inline_tests::public_key_from_b512 | 38704 | 34864 |
9.92% |
| crypto_public_key_inline_tests::public_key_hash | 606 | 518 | 14.52% |
| crypto_public_key_inline_tests::public_key_is_zero | 13201 | 11281 |
14.54% |
| crypto_public_key_inline_tests::public_key_new | 209 | 186 | 11.00% |
| crypto_public_key_inline_tests::public_key_try_from_bytes | 59073 |
53187 | 9.96% |
| crypto_public_key_inline_tests::public_key_try_into_b256 | 1139 | 1070
| 6.06% |
| crypto_public_key_inline_tests::public_key_try_into_b256_tuple | 1606
| 1468 | 8.59% |
| crypto_public_key_inline_tests::public_key_try_into_b512 | 2043 | 1905
| 6.75% |
| crypto_scalar_inline_tests::scalar_b256_try_from | 878 | 809 | 7.86% |
| crypto_scalar_inline_tests::scalar_bytes | 633 | 564 | 10.90% |
| crypto_scalar_inline_tests::scalar_from_b256 | 1282 | 1093 | 14.74% |
| crypto_scalar_inline_tests::scalar_from_u256 | 1306 | 1117 | 14.47% |
| crypto_scalar_inline_tests::scalar_from_u8_array | 6978 | 2949 |
57.74% |
| crypto_scalar_inline_tests::scalar_is_zero | 22466 | 18626 | 17.09% |
| crypto_scalar_inline_tests::scalar_min | 355 | 315 | 11.27% |
| crypto_scalar_inline_tests::scalar_new | 181 | 158 | 12.71% |
| crypto_scalar_inline_tests::scalar_u256_try_from | 878 | 809 | 7.86% |
| crypto_scalar_inline_tests::scalar_zero | 355 | 315 | 11.27% |
| crypto_secp256k1_inline_tests::secp256k1_address | 6123 | 6033 | 1.47%
|
| crypto_secp256k1_inline_tests::secp256k1_evm_address | 6111 | 6021 |
1.47% |
| crypto_secp256k1_inline_tests::secp256k1_hash | 1926 | 1882 | 2.28% |
| crypto_secp256k1_inline_tests::secp256k1_into_bytes | 26703 | 22863 |
14.38% |
| crypto_secp256k1_inline_tests::secp256k1_recover | 21047 | 18441 |
12.38% |
| crypto_secp256k1_inline_tests::secp256k1_try_from_bytes | 87153 |
86958 | 0.22% |
| crypto_secp256k1_inline_tests::secp256k1_verify | 23267 | 20661 |
11.20% |
| crypto_secp256k1_inline_tests::secp256k1_verify_address | 6663 | 6529
| 2.01% |
| crypto_secp256k1_inline_tests::secp256k1_verify_evm_address | 6632 |
6498 | 2.02% |
| crypto_secp256r1_inline_tests::secp256r1_address | 10221 | 10131 |
0.88% |
| crypto_secp256r1_inline_tests::secp256r1_evm_address | 10209 | 10119 |
0.88% |
| crypto_secp256r1_inline_tests::secp256r1_hash | 1926 | 1882 | 2.28% |
| crypto_secp256r1_inline_tests::secp256r1_into_bytes | 26703 | 22863 |
14.38% |
| crypto_secp256r1_inline_tests::secp256r1_recover | 25145 | 22539 |
10.36% |
| crypto_secp256r1_inline_tests::secp256r1_try_from_bytes | 87153 |
86958 | 0.22% |
| crypto_secp256r1_inline_tests::secp256r1_verify | 27941 | 25295 |
9.47% |
| crypto_secp256r1_inline_tests::secp256r1_verify_address | 10761 |
10627 | 1.25% |
| crypto_secp256r1_inline_tests::secp256r1_verify_evm_address | 11051 |
10917 | 1.21% |
| crypto_signature_inline_tests::signature_address | 11056 | 10878 |
1.61% |
| crypto_signature_inline_tests::signature_evm_address | 11032 | 10854 |
1.61% |
| crypto_signature_inline_tests::signature_recover | 41145 | 35935 |
12.66% |
| crypto_signature_inline_tests::signature_verify | 46227 | 40971 |
11.37% |
| crypto_signature_inline_tests::signature_verify_address | 11106 |
10928 | 1.60% |
| crypto_signature_inline_tests::signature_verify_evm_address | 11105 |
10927 | 1.60% |
| crypto_zk_inline_tests::revert_zk_alt_bn128_add_fail | 4499 | 4407 |
2.04% |
| crypto_zk_inline_tests::revert_zk_alt_bn128_mul_fail | 4264 | 4195 |
1.62% |
| crypto_zk_inline_tests::revert_zk_alt_bn128_pairing_check | 123487 |
123269 | 0.18% |
| crypto_zk_inline_tests::zk_alt_bn128_add | 105641 | 92381 | 12.55% |
| crypto_zk_inline_tests::zk_alt_bn128_mul | 104468 | 91323 | 12.58% |
| crypto_zk_inline_tests::zk_alt_bn128_pairing_check | 1146355 | 1142872
| 0.30% |
| ecr_inline_tests::ecr_ec_recover_address | 3721 | 3533 | 5.05% |
| ecr_inline_tests::ecr_ec_recover_address_r1 | 7819 | 7631 | 2.40% |
| ecr_inline_tests::ecr_ed_verify | 20067 | 15455 | 22.98% |
| hash_inline_tests::hash_hasher_keccak256_b256 | 970 | 882 | 9.07% |
| hash_inline_tests::hash_hasher_keccak256_bool | 797 | 627 | 21.33% |
| hash_inline_tests::hash_hasher_keccak256_bytes | 749 | 579 | 22.70% |
| hash_inline_tests::hash_hasher_keccak256_str | 973 | 885 | 9.04% |
| hash_inline_tests::hash_hasher_keccak256_str_array | 936 | 848 | 9.40%
|
| hash_inline_tests::hash_hasher_keccak256_u16 | 938 | 850 | 9.38% |
| hash_inline_tests::hash_hasher_keccak256_u256 | 970 | 882 | 9.07% |
| hash_inline_tests::hash_hasher_keccak256_u32 | 938 | 850 | 9.38% |
| hash_inline_tests::hash_hasher_keccak256_u64 | 936 | 848 | 9.40% |
| hash_inline_tests::hash_hasher_keccak256_u8 | 794 | 624 | 21.41% |
| hash_inline_tests::hash_hasher_sha256_10_array | 11064 | 8384 | 24.22%
|
| hash_inline_tests::hash_hasher_sha256_1_array | 972 | 884 | 9.05% |
| hash_inline_tests::hash_hasher_sha256_2_array | 2092 | 1716 | 17.97% |
| hash_inline_tests::hash_hasher_sha256_3_tuple | 3172 | 2508 | 20.93% |
| hash_inline_tests::hash_hasher_sha256_4_array | 4300 | 3348 | 22.14% |
| hash_inline_tests::hash_hasher_sha256_4_tuple | 4276 | 3324 | 22.26% |
| hash_inline_tests::hash_hasher_sha256_5_array | 5404 | 4164 | 22.95% |
| hash_inline_tests::hash_hasher_sha256_5_tuple | 5380 | 4140 | 23.05% |
| hash_inline_tests::hash_hasher_sha256_6_array | 6591 | 5063 | 23.18% |
| hash_inline_tests::hash_hasher_sha256_8_array | 8827 | 6723 | 23.84% |
| hash_inline_tests::hash_hasher_sha256_9_array | 9945 | 7553 | 24.05% |
| hash_inline_tests::hash_hasher_sha256_b256 | 952 | 864 | 9.24% |
| hash_inline_tests::hash_hasher_sha256_bool | 779 | 609 | 21.82% |
| hash_inline_tests::hash_hasher_sha256_bytes | 809 | 637 | 21.26% |
| hash_inline_tests::hash_hasher_sha256_u16 | 920 | 832 | 9.57% |
| hash_inline_tests::hash_hasher_sha256_u256 | 952 | 864 | 9.24% |
| hash_inline_tests::hash_hasher_sha256_u32 | 920 | 832 | 9.57% |
| hash_inline_tests::hash_hasher_sha256_u64 | 918 | 830 | 9.59% |
| hash_inline_tests::hash_hasher_sha256_u8 | 776 | 606 | 21.91% |
| hash_inline_tests::hash_hasher_write_str | 955 | 867 | 9.21% |
| hash_inline_tests::hash_hasher_write_str_array | 918 | 830 | 9.59% |
| hash_inline_tests::hash_keccak256 | 997 | 909 | 8.83% |
| hash_inline_tests::hash_sha256 | 979 | 891 | 8.99% |
| hash_inline_tests::hash_sha256_str_array | 950 | 862 | 9.26% |
| identity_inline_tests::identity_hash | 5227 | 3613 | 30.88% |
| primitive_conversions_b256_inline_tests::b256_from_u128 | 677 | 572 |
15.51% |
| primitive_conversions_b256_inline_tests::b256_into_u128 | 677 | 572 |
15.51% |
| primitive_conversions_b256_inline_tests::b256_try_from_bytes | 8510 |
4472 | 47.45% |
| string_inline_tests::string_as_bytes | 1050 | 745 | 29.05% |
| string_inline_tests::string_as_raw_slice | 1020 | 733 | 28.14% |
| string_inline_tests::string_bytes_from | 1484 | 1013 | 31.74% |
| string_inline_tests::string_bytes_into | 1484 | 1013 | 31.74% |
| string_inline_tests::string_capacity | 602 | 436 | 27.57% |
| string_inline_tests::string_clear | 485 | 365 | 24.74% |
| string_inline_tests::string_clone | 2747 | 1879 | 31.60% |
| string_inline_tests::string_from_ascii | 1733 | 1245 | 28.16% |
| string_inline_tests::string_from_ascii_str | 1246 | 1025 | 17.74% |
| string_inline_tests::string_from_bytes | 1484 | 1013 | 31.74% |
| string_inline_tests::string_from_raw_slice | 1391 | 1044 | 24.95% |
| string_inline_tests::string_into_bytes | 2080 | 1449 | 30.34% |
| string_inline_tests::string_into_raw_slice | 1509 | 1227 | 18.69% |
| string_inline_tests::string_is_empty | 1100 | 818 | 25.64% |
| string_inline_tests::string_new | 196 | 173 | 11.73% |
| string_inline_tests::string_ptr | 1169 | 790 | 32.42% |
| string_inline_tests::string_raw_slice_from | 1020 | 733 | 28.14% |
| string_inline_tests::string_raw_slice_into | 1020 | 733 | 28.14% |
| string_inline_tests::string_test_abi_encoding | 939 | 861 | 8.31% |
| string_inline_tests::string_test_equal | 1227 | 1071 | 12.71% |
| string_inline_tests::string_test_hash | 983 | 775 | 21.16% |
| string_inline_tests::string_with_capacity | 2484 | 2047 | 17.59% |
| u128_inline_tests::u128_as_u256 | 10083 | 8974 | 11.00% |
| vec_inline_tests::revert_vec_insert_out_of_bounds | 655 | 527 | 19.54%
|
| vec_inline_tests::revert_vec_remove_out_of_bounds | 629 | 501 | 20.35%
|
| vec_inline_tests::revert_vec_set_out_of_bounds | 631 | 501 | 20.60% |
| vec_inline_tests::revert_vec_swap_element_1_out_of_bounds | 632 | 501
| 20.73% |
| vec_inline_tests::revert_vec_swap_element_2_out_of_bounds | 636 | 505
| 20.60% |
| vec_inline_tests::vec_as_raw_slice | 758 | 587 | 22.56% |
| vec_inline_tests::vec_buffer_ownership | 2325 | 1607 | 30.88% |
| vec_inline_tests::vec_capacity | 1103 | 755 | 31.55% |
| vec_inline_tests::vec_clear | 631 | 505 | 19.97% |
| vec_inline_tests::vec_clear_empty_vec | 189 | 143 | 24.34% |
| vec_inline_tests::vec_clear_twice | 735 | 567 | 22.86% |
| vec_inline_tests::vec_clone | 1798 | 1376 | 23.47% |
| vec_inline_tests::vec_encode_and_decode | 2527 | 1893 | 25.09% |
| vec_inline_tests::vec_from_raw_slice | 310 | 287 | 7.42% |
| vec_inline_tests::vec_get | 1613 | 1367 | 15.25% |
| vec_inline_tests::vec_insert | 1466 | 1171 | 20.12% |
| vec_inline_tests::vec_insert_back | 1467 | 1172 | 20.11% |
| vec_inline_tests::vec_insert_before_back | 1478 | 1183 | 19.96% |
| vec_inline_tests::vec_insert_front | 1477 | 1182 | 19.97% |
| vec_inline_tests::vec_insert_twice | 2493 | 2032 | 18.49% |
| vec_inline_tests::vec_is_empty | 742 | 616 | 16.98% |
| vec_inline_tests::vec_iter | 2303 | 1656 | 28.09% |
| vec_inline_tests::vec_last | 1623 | 1313 | 19.10% |
| vec_inline_tests::vec_len | 1179 | 885 | 24.94% |
| vec_inline_tests::vec_new | 119 | 96 | 19.33% |
| vec_inline_tests::vec_pop | 3292 | 2456 | 25.39% |
| vec_inline_tests::vec_ptr | 834 | 639 | 23.38% |
| vec_inline_tests::vec_push | 2022 | 1414 | 30.07% |
| vec_inline_tests::vec_raw_slice_into | 310 | 287 | 7.42% |
| vec_inline_tests::vec_remove | 1475 | 1178 | 20.14% |
| vec_inline_tests::vec_remove_all | 1048 | 830 | 20.80% |
| vec_inline_tests::vec_remove_end | 1152 | 940 | 18.40% |
| vec_inline_tests::vec_remove_front | 1155 | 943 | 18.35% |
| vec_inline_tests::vec_resize | 5107 | 4437 | 13.12% |
| vec_inline_tests::vec_set | 1244 | 988 | 20.58% |
| vec_inline_tests::vec_set_back | 1267 | 1011 | 20.21% |
| vec_inline_tests::vec_set_front | 1244 | 988 | 20.58% |
| vec_inline_tests::vec_set_twice | 1827 | 1464 | 19.87% |
| vec_inline_tests::vec_swap | 1541 | 1202 | 22.00% |
| vec_inline_tests::vec_swap_end | 1221 | 967 | 20.80% |
| vec_inline_tests::vec_swap_front | 1220 | 966 | 20.82% |
| vec_inline_tests::vec_swap_front_with_end | 1221 | 967 | 20.80% |
| vec_inline_tests::vec_swap_twice | 2319 | 1832 | 21.00% |
| vec_inline_tests::vec_with_capacity | 3078 | 2963 | 3.74% |
| vm_evm_ecr_inline_tests::ecr_ec_recover | 3416 | 3185 | 6.76% |
| vm_evm_evm_address_inline_tests::evm_address_hash | 1904 | 1708 |
10.29% |
| vm_evm_evm_address_inline_tests::evm_address_into_bytes | 7827 | 6558
| 16.21% |
| vm_evm_evm_address_inline_tests::evm_address_try_from_bytes | 3111 |
2340 | 24.78% |

</details>

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-05-08 07:25:02 +00:00
Sophie Dankel
a37e3cdc99
ci: Ensure that std-lib version matches forc version (#7154)
## Description

Sway `std` library will be released when `forc` is released. To keep it
simple, we'll ensure that the version of sway `std` matches the version
of `forc`. This PR adds a CI step to enforce this.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
2025-05-08 08:12:35 +04:00
Igor Rončević
8fb7a55f21
Optimize crypto module for bytecode size and gas usage (#7151)
## Description

This PR optimizes the `std::crypto` module for bytecode size and gas
cost by using optimization techniques similar to those used in #7087,
#7092, and #7096.

The optimized public functions are listed in the below code snippet.
**The bytecode size of that code got reduced from 46104 bytes to 44288
bytes.**

We actually expect a bigger gain here. At one point in the optimization,
the bytecode size went down to 33896 bytes. Bouncing back to ~44600 is
related to the issue described in #7150. Once that issue is solved, we
expect even smaller bytcode sizes.

```sway
script;

use std::crypto::ed25519::*;
use std::crypto::point2d::*;
use std::crypto::public_key::*;
use std::crypto::scalar::*;
use std::crypto::secp256k1::*;
use std::crypto::secp256r1::*;
use std::crypto::signature::*;

use std::vm::evm::evm_address::EvmAddress;

fn main() {
    let ed25519 = std::crypto::ed25519::Ed25519::new();
    let _ = ed25519 == ed25519;

    let point2d = std::crypto::point2d::Point2D::new();
    let _ = point2d == point2d;

    let public_key = std::crypto::public_key::PublicKey::new();
    let _ = public_key == public_key;

    let _ = public_key.is_zero();

    let scalar = std::crypto::scalar::Scalar::new();
    let _ = scalar == scalar;

    let message = std::crypto::message::Message::new();

    let secp256k1 = std::crypto::secp256k1::Secp256k1::new();
    let _ = secp256k1 == secp256k1;
    let _ = secp256k1.address(message);
    let _ = secp256k1.evm_address(message);
    let _ = secp256k1.verify(public_key, message);
    let _ = secp256k1.verify_address(Address::zero(), message);
    let _ = secp256k1.verify_evm_address(EvmAddress::zero(), message);

    let secp256r1 = std::crypto::secp256r1::Secp256r1::new();
    let _ = secp256r1 == secp256r1;
    let _ = secp256r1.address(message);
    let _ = secp256r1.evm_address(message);
    let _ = secp256r1.verify(public_key, message);
    let _ = secp256r1.verify_address(Address::zero(), message);
    let _ = secp256r1.verify_evm_address(EvmAddress::zero(), message);
}
```

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-05-07 11:19:41 +00:00
Cameron Carstens
f9fcae93c1
Support new expiry policy in std-lib (#7155)
## Description

Adds support for the new expiry policy gtf in the std-lib.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-05-07 14:53:18 +04:00
João Matos
f635cc22ea
Remove AbiEncode impl for raw_ptr (#7160)
This was added originally in https://github.com/FuelLabs/sway/pull/6844
but should not have been introduced in the first place.

I added this while in development for the trait coherence feature but
its not necessary and not something we wish to support in the public ABI
surface.

Fixes https://github.com/FuelLabs/sway/issues/7153.

Thanks to @ironcev for catching this.

Co-authored-by: IGI-111 <igi-111@protonmail.com>
2025-05-06 13:12:26 +00:00
Daniel Frederico Lins Leite
324f04fbab
Better Debug tests and impls for primitives and std lib types (#7119)
## Description

This PR is a continuation of https://github.com/FuelLabs/sway/pull/7015.
It is fixing the number printing function, which was not working
correctly for zero. It also fixes a problem when printing `enum`
variants.

To improve `__dbg`, we are also now printing the argument span.

An improved test now tests if all relevant `strucs` and `enums` from the
`std` library are debug and show how they are printed.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: kaya <kaya.gokalp@fuel.sh>
2025-05-02 06:40:34 -03:00
Daniel Frederico Lins Leite
42321ef0e0
improve dbg intrinsic with fflush (#7126)
## Description

This PR brings a new syscall "fflush" to help `EcalHandler` know when
the `__dbg` is finished and they flush their buffers.

In theory, Linux `fflush` is not a syscall because it just flushes
"user-space" buffers. As we do not have this difference, we are still
going to call `fflush` as a syscall.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

Co-authored-by: João Matos <joao@tritao.eu>
2025-04-30 11:31:32 -03:00
Igor Rončević
5f64b96f9c
Implement panic expression (#7073)
## Description

This PR introduces the `panic` expression to the language, as defined in
the [ABI Errors
RFC](https://github.com/FuelLabs/sway-rfcs/blob/master/rfcs/0014-abi-errors.md#panic).

The `panic` expression can be used without arguments, or accept an
argument that implements the `std::marker::Error` trait. The `Error`
trait is implemented by the compiler for the unit `()`, string slices,
and `#[error_type]` enums.

Using the `panic` expression without arguments gives the symetry with
the `return` expression and acts in the same way as having unit as an
argument.

```
panic;
panic ();
panic "This is some error.";
panic Errors::SomeError(42);
```

Panicking without an argument or with unit as argument is discouraged to
use. In the upcoming PR that finalizes the ABI errors feature, we will
emit a warning if the `panic` is used without arguments or with unit as
argument.

`panic` expression is available in all program kinds. In predicates it
currently compiles only to revert. Once `__dbg` intrinsic is
implemented, we can consider compiling to it in predicates. In the
upcoming PR, the `error_codes` entry in the ABI JSON will be available
for all program kinds.

The dead code analysis for the `panic` expression is implemented in the
straightforward way, following the current approach of connecting
reverts to the exit node. This will be revisted in a separate PR,
together with the open TODOs in the DCA implementation of `Return`.
Essentially, we want reverting/panicking to connect to program exit and
implicit returns to the exit node.

Additionally, the PR:
- extends `forc test` CLI attributes with `--revert-codes` that prints
revert codes even if tests are successful but revert.
- updates outdated "Logs Inside Tests" chapter in the documentation on
unit testing.
- extends the snapshot testing infrastructure to support `forc test` in
snapshot tests.
- fixes #7072.

Partially addresses #6765.

## Breaking Change

`panic` is a new reserved keyword. Once the `error_type` feature becomes
integrated, compiling any existing code containing a "panic" as an
identifier will result in an "Identifiers cannot be a reserved keyword."
error.

In this PR, `panic` keyword is hidden behind the `error_type` feature
flag. This prevents existing code from breaking, unless opted-in.

Note that, although being behind a feature flag, `panic` cannot be used
in conditional compilation, means in combination with the
`#[cfg(experimental_error_type = true/false)]`. The reason is that `cfg`
evaluation happens after parsing, and we can either parse `panic` as a
keyword or identifier during the parsing, because we cannot distinguish
ambiguous cases like `panic;` or just `panic`.

This limitation means, that introducing `panic` to `std` can be done
only once the `error_type` feature is fully integrated. In practice,
this is not a serious limitation, because introducing `panic` in `std`
would anyhow, due to effort and design decisions, happen after the
feature is integrated.

We will provide a migration for this breaking change that will migrate
the existing "panic" identifiers to "r#panic".

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-30 20:07:05 +10:00
camden-smallwood
00ab8bcaf1
Add str conversion methods to std::string::String in sway-lib-std (#7121)
## Description
This adds string slice conversion support to the `std::string::String`
struct in the Sway standard library through the `String::as_str` and
equivalent `impl From<str> for String` and `impl From<String> for str`
methods.

## Checklist

- [ ] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
2025-04-29 16:27:51 +10:00
João Matos
c39fe5d220
Fix Eq constraint in vec.sw impl<T> PartialEq for Vec<T>. (#7106)
Fixes a mistake where an `Eq` constraint was used where a `PartialEq`
suffixes.
2025-04-23 16:24:53 +00:00
Igor Rončević
fe03e4ea6a
assert_eq requires PartialEq instead of Eq (#7105)
## Description

This PR changes the trait constraints on `assert_eq` and `assert_ne`
from `Eq` to `PartialEq`. The `Eq` requirement was unnecessary strong,
and is actually a "leftover" from the original implementation, before we
introduced `PartialEq`.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-23 17:56:54 +02:00
Daniel Frederico Lins Leite
f736fce7ac
Debug trait and its auto implementation (#7015)
## Description

This PR implements the `__dbg(...)` intrinsic, which is very similar to
Rust `dbg!(...)` macro.

Up until now, it has being the norm to use `__log` to debug values.
Given that this is NOT the first use case for log, we have always found
some issues with it: log does not work on predicates, log does not show
"private" fields like `Vec::capacity` and others.

To solve these problems `__dbg` is being introduced:
1 - it will work on all program types, including predicates;
2 - it also prints the file name, line and column;
3 - All types will have an automatic implementation of Debug if
possible, which can still be customized.
4 - Even `raw_ptr` and other non "loggable" types, have `Debug` impls.
5 - `__dbg` will be completely stripped in the release build by default.
It can be turned on again if needed.

So this:

```
// Aggregates
let _ = __dbg((1u64, 2u64));
let _ = __dbg([1u64, 2u64]);

// Structs and Enums
let _ = __dbg(S { });
let _ = __dbg(E::None);
let _ = __dbg(E::Some(S { }));
```

will generate this:

```
[src/main.sw:19:13] = (1, 2)
[src/main.sw:20:13] = [1, 2]
[src/main.sw:23:13] = S { }
[src/main.sw:24:13] = None
[src/main.sw:25:13] = E(S { })
```

How does this work?

`__dbg(value)` intrinsic is desugared into `{ let f = Formatter{};
f.print_str(...); let value = value; value.fmt(f); value }`.

`Formatter` is similar to Rust's one. The difference is that we still do
not support string formatting, so the `Formatter` has a lot of `print_*`
functions.

And each `print` function calls a "syscall". This `syscall` uses `ecal`
under the hood and it follows unix write syscall schema.

```sway
// ssize_t write(int fd, const void buf[.count], size_t count);
fn syscall_write(fd: u64, buf: raw_ptr, count: u64) {
    asm(id: 1000, fd: fd, buf: buf, count: count) {
        ecal id fd buf count;
    }
}
```

For that to work, the VM interpreter must have its `EcalState` setup and
interpret syscall number 1000 as `write`. This PR does this for `forc
test` and our `e2e test suite`.

Each test in `forc test` will capture these calls and only print to the
terminal when requested with the `--log` flag.

## Garbage Collector and auto generated

Before, we were associating all auto-generated code with a pseudo file
called "&lt;autogenerated&gt;.sw" that was never garbage collected.

This generated a problem inside the LSP when the `auto_impl.rs` ran a
second time because of a collision in the "shareable type" map. When we
try to solve this collision, choosing to keep the old value or to insert
the new, the type inside the map points to already collected types and
the compiler panics. This is a known problem.

The workaround for this is to break the auto-generated code into
multiple files. Now they are named "main.autogenerated.sw", for example.
We create one pseudo-file for each real file that needs one.

When we garbage collect one file, `main.sw`, for example, we also
collect its associated auto-generated file.

## Checklist

- [ ] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-22 07:49:31 -03:00
Igor Rončević
7cb78097d0
Optimize auth module for bytecode size and gas usage (#7096)
## Description

This PR optimizes the `std::auth` module for bytecode size and gas cost
by using optimization techniques similar to those used in #7087 and
#7092.

The optimized public functions are listed in the below code snippet.
**The bytecode size of that code got reduced from 2480 bytes to 1200
bytes.**

```sway
fn main() {
    let _ = std::auth::predicate_address();
    let _ = std::auth::caller_addresses();
    let _ = std::auth::caller_address();
}
```

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-19 07:42:05 -03:00
João Matos
3afc5c18c8
Improved trait coherence checking (#6844)
## Description

This PR refines trait coherence checking to guarantee that there is only
one valid implementation for each trait. It introduces improved orphan
rules and more rigorous overlap checks to prevent conflicting trait
implementations. With these changes, the system now more reliably
enforces coherence, ensuring that traits are implemented uniquely, and
reducing potential ambiguities.

This approach mirrors Rust’s design by enforcing two distinct safety and
coherence checks on trait implementations:

**1. Orphan Rules Check**
The orphan rules require that for any trait implementation, either the
trait or the type must be defined within the current package. This
restriction prevents external packages from implementing foreign traits
for foreign types, which could otherwise lead to conflicting
implementations and ambiguities across different parts of a codebase.
Essentially, it helps maintain clear ownership and boundaries of trait
implementations.

**2. Overlap Impl Check**
The overlap impl check ensures that no two trait implementations can
apply to the same type in an ambiguous manner. If two implementations
could potentially match the same type, it would be unclear which one
should be used, leading to coherence issues. By enforcing that
implementations do not overlap, the system guarantees that trait
resolution is unambiguous and predictable.

Together, these checks promote a robust and maintainable system by
ensuring that trait implementations are both locally controlled (orphan
rules) and non-conflicting (overlap check).

The test suite has been updated to comply with the new rules. However,
there is one current limitation regarding arrays. For arrays, the
coherence checks have been relaxed to avoid the need for numerous
concrete implementations in the standard library for traits like `Eq`
and `PartialEq`. This decision was made because support for const
generics is expected soon, which will allow these traits to be
implemented more cleanly.

Closes issue https://github.com/FuelLabs/sway/issues/5892.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-17 22:57:45 +01:00
Igor Rončević
ddec15af09
Optimize inputs and outputs modules for bytecode size and gas usage (#7092)
## Description

This PR optimizes the `std::inputs` and `std::outputs` modules for
bytcode size and gas cost using the same optimization techinques like in
#7087.

The optimized public functions are listed in the below code snippet.
**The bytcode size of that code got reduced from 5688 bytes to 2544
bytes.**

```sway
fn main() {
    let _ = std::tx::tx_witnesses_count();

    let _ = std::inputs::input_count();
    let _ = std::inputs::input_amount(0);
    let _ = std::inputs::input_coin_owner(0);
    let _ = std::inputs::input_predicate_data::<u64>(0);
    let _ = std::inputs::input_asset_id(0);
    let _ = std::inputs::input_witness_index(0);
    let _ = std::inputs::input_predicate_length(0);
    let _ = std::inputs::input_predicate(0);
    let _ = std::inputs::input_predicate_data_length(0);
    let _ = std::inputs::input_message_sender(0);
    let _ = std::inputs::input_message_recipient(0);
    let _ = std::inputs::input_message_nonce(0);
    let _ = std::inputs::input_message_data_length(0);
    let _ = std::inputs::input_message_data(0, 0);

    let _ = std::outputs::output_count();
    let _ = std::outputs::output_amount(0);
    let _ = std::outputs::output_asset_id(0);
    let _ = std::outputs::output_asset_to(0);
}
```

Additionally, the PR adds a new function
`std::outputs::output_asset_id_and_to` to efficiently retrieve the id
and the receiver of the output asset in a single call. Calling this
function is more performant that calling `output_asset_id` and
`output_asset_to` separately.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-15 17:35:07 +01:00
Igor Rončević
8c132f1b56
Optimize tx module for bytecode size and gas usage (#7087)
## Description

This PR optimizes functions in the `tx` module for bytecode size and gas
usage. E.g., the bytecode size of the following script that calls all of
the optimized functions got **reduced from 1792 bytes to 648 bytes**:

```sway
fn main() {
    let _ = std::tx::tx_script_length();
    let _ = std::tx::tx_script_data_length();
    let _ = std::tx::tx_script_data::<u64>();
    let _ = std::tx::tx_script_bytecode::<u64>();
    let _ = std::tx::tx_script_bytecode_hash();
    let _ = std::tx::tx_witnesses_count();
    let _ = std::tx::tx_witness_data::<u64>(0);
}
```

The optimization:
- mostly removes the duplicated checks for `tx_type()` and where
necessary, replaces calls to `tx_type()` to a single `if`.
- removes the private helper function `tx_script_start_pointer`. The
function was re-checking the `tx_type()` to be a script, although always
being called only if the `tx_type` was already checked for being a
script. Additionally, it was wrapping the result in `Option` requiring
an additional unnecessary indirection.
- removes the private helper function `tx_script_data_start_pointer` for
similar reasons.
- rewrites `tx_witness_data` by removing unnecessary length calculation
and duplicated code.
- simplifies `match` expressions by replacing duplicated match arms with
a single arm.

The PR introduces a slight change in the semantics of functions like,
e.g., `tx_script_length` that return `Option`. Before, if the `tx_type`
was `Mint` they would panic instead of returning `None`. Now they return
`None`. The new behavior actually looks like the proper one to me. A
function returning `Option` shouldn't panic.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-14 18:54:51 +04:00
Cameron Carstens
036e0e9e5c
Implement Eq for Signature and add missing codec tests (#7084)
## Description

This PR implement `Eq` and `PartialEq` for `Signature` and adds missing
`AbiEncode` and `AbiDecode` tests for the crypto module.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-14 16:44:20 +04:00
Igor Rončević
c25c5af2da
Add #[error_type] and #[error] attributes (#7058)
## Description

This PR adds `#[error_type]` and `#[error]` attributes to the collection
of known attributes, as defined in the [ABI Errors
RFC](https://github.com/FuelLabs/sway-rfcs/blob/master/rfcs/0014-abi-errors.md).

Additionally, the PR updates the outdated documentation for _Attributes_
and _Keywords_.

Partially addresses #6765.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-04-07 09:37:04 +00:00
Daniel Frederico Lins Leite
b28af85532
impl PartialEq, Eq, Hash, Clone for array (#7037)
## Description

This PR is part of https://github.com/FuelLabs/sway/issues/6860.

It continues the implementation of more `std` library "traits" for
arrays.
It also fixes a small error when formatting "const generics
declarations".

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-03-25 06:38:56 -03:00
Igor Rončević
74282e2c7b
Defining, parsing, and checking #[attribute]s (#6986)
## Description

This PR reimplements how `#[attribute]`s are parsed, defined, and
checked. It fixes the following issues we had with attributes:
- attributes were in general not checked for their expected and allowed
usage.
- when checked, checks were fixing particular reported issues and were
scattered through various compilation phases: lexing, parsing, and tree
conversion.
- when checked, reported errors in some cases had bugs and were
overalapping with other errors and warnings.
- attribute handling was not centralized, but rahter scattered throught
the whole code base, and mostly done at use sites.

For concrete examples of these issues, see the list of closed issues
below.

The PR:
- encapsulates the attributes handling within a single abstraction:
`Attributes`.
- assigns the following properties to every attribute:
  - _target_: what kind of elements an attribute can annotate.
- _multiplicity_: if more then one attribute of the same kind can be
applied to an element.
- _arguments multiplicity_: a range defining how many arguments an
attribute can or must have.
- _arguments value expectation_: if attribute arguments are expected to
have values.
- validates those properties in a single place, during the
`convert_parse_tree`. Note that this means that modules with attribute
issues will now consistently not reach the type checking phase. This was
previously the case for some of the issues with attributes where custom
checks where done during the type checking phase. #6987 proposes to move
those checks after the tree conversion phase, allowing the continuation
of compilation.
- adds the `AttributeKind::Unknow` to preserve unknown attributes in the
typed tree.
- removes redundant code related to attributes: specific warnings and
errors, specialized parsing, repetitive and error-prone at use site
checks, etc.
- removes the unused `Doc` attribute.

The PR also introduces a new pattern in emitting errors. The
`attr_decls_to_attributes` function will always return `Attributes` even
if they have errors, because:
- we expect the compilation to continue even in the case of errors.
- we expect those errors to be ignored if a valid `#[cfg]` attribute
among those evaluates to false.
- we expect them to be emitted with eventual other errors, or alone, at
the end of the tree conversion of the annotated element.

For more details, see the comment on `attr_decls_to_attributes` and
`cfg_eval`.

Closes #6880; closes #6913; closes #6914; closes #6915; closes #6916;
closes #6917; closes #6918; closes #6931; closes #6981; closes #6983;
closes #6984; closes #6985.

## Breaking changes

Strictly seen, this PR can cause breaking changes, but only in the case
of invalid existing attribute usage. We treat those breaking changes as
bug fixes in the compiler and, thus, do not have them behind a feature
flag.

E.g., this kind of code was possible before, but will now emit and
error:
```sway
#[storage(read, write, read, write)]
struct Struct {}
```

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-03-24 09:34:31 +11:00
SwayStar123
516c999c15
Import codec to all files that define new types in std lib (#7028)
## Description
Codec needs to be imported to all libraries in the std lib which define
new types, or else they cannot be used in Abi functions or logged.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
2025-03-21 20:54:06 +05:30
Daniel Frederico Lins Leite
fa960886b4
AbiDecode for arrays using const_generics (#7008)
## Description

This is part of https://github.com/FuelLabs/sway/issues/6860.

This completes abi encoding for arrays. Allowing arrays of any size as
inputs and outputs in predicates/scripts/contracts.
This PR also allows arrays to be initialized with "const generics" such
as:

```sway
#[cfg(experimental_const_generics = true)]
impl<T, const N: u64> AbiDecode for [T; N]
where
    T: AbiDecode,
{
    fn abi_decode(ref mut buffer: BufferReader) -> [T; N] {
        let first: T = buffer.decode::<T>();
        let mut array = [first; N];  // <-------------------- This can used now
        ...
        array
    }
}
```` 


## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-03-20 14:46:57 +00:00
Sophie Dankel
f46a2dc453
chore: add more information to lib-std project manifest (#7003)
## Description

These fields will be used when the package is published to forc.pub
registry.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
2025-03-17 17:17:18 -07:00
Cameron Carstens
d821dcb0c7
Merge commit from fork
* Ensure U128 Pow reverts on overflow

* Add comments

* Add upper and lower edge case

---------

Co-authored-by: IGI-111 <igi-111@protonmail.com>
2025-03-13 08:25:49 -07:00
Igor Rončević
58114d7e68
Promote experimental features (#7016)
## Description

This PR promotes the following experimental features to standard ones:
- #6701
- #6883
- #6994
- #7006

Additionally, the PR stenghtens the migration infrastructure by checking
some additional cases in selecting corresponding typed elements in
desugared code which were not checked before.

Closes #6701.
Closes #6883.
Closes #6994.
Closes #7006.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-03-13 20:47:01 +11:00
SwayStar123
a5d9d2835f
Merge std and core libraries (#6729)
## Description
Merges the two libraries. They were initially separate to separate the
core logic and fuel vm specific functionality, but that separation is no
longer maintained so having a merged library is better.

Closes #6708

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Sophie <47993817+sdankel@users.noreply.github.com>
Co-authored-by: Igor Rončević <ironcev@hotmail.com>
2025-03-12 23:52:38 +01:00
Igor Rončević
8b3d822112
Implement trees visitors and migrations for TryFrom<Bytes> for b256 (#7005)
## Description

This PR:
- extends infrastructure for writing migrations that modify individual
expressions and do not need access to a broader scope. It does that by
providing lexed and typed tree visitors. Visiting lexed and typed tree
is implemented to the level needed to write the migration for `impl
TryFrom<Bytes> for b256`.
- implements the migration for `impl TryFrom<Bytes> for b256` as
explained in #6994.

Adding `#[cfg(experimental_try_from_bytes_for_b256 = true)]` to the
`impl TryFrom<Bytes> for b256` revealed that there existed already an
`impl TryFrom<Bytes> for b256` in `std::primitive_conversions::b256`.
Due the lack of trait coherence there was no any errors when it was
re-implemented in `std::bytes`.

This PR marks the existing impl in `std::primitive_conversions::b256`
with `#[cfg(experimental_try_from_bytes_for_b256 = false)]` so that it
will be removed in the next breaking release, in favor of the new impl
in `std::bytes`.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-03-11 11:20:13 +11:00
Cameron Carstens
27be22cfe0
Add inline tests for all arithmetic cases in u8, u16, u32, u64, U128, and u256 (#6995)
## Description

This PR adds arithmetic test cases to ensure `u8`, `u16`, `u32`, `u64`,
`U128`, and `u256` behave as expected in normal conditions, when
overflow is disabled, when unsafe math is enabled, and edge cases.

Previously, the modulo and square root functions of `U128` would not
revert, even if unsafe math is enabled.

The following test cases are new for inline tests:

### `u8`

- Add 
- Revert Add on overflow
- Revert on unsafe math on overflow
- Binary log of zero fails on overflow disabled
- Binary log of zero returns zero on unsafe math
- Divide
- Divide behavior on unsafe math
- Divide by zero reverts
- Log still reverts when overflow enabled
- Log behavior on unsafe math enabled
- Log parity with Rust tests
- Modulo zero still reverts with overflow enabled
- Multiply
- Multiply overflow still reverts on unsafe math enabled
- Pow still reverts on unsafe math enabled
- Root behavior the same on overflow enabled
- Root behavior the same on unsafe math enabled
- 0th root will fail
- Subtract
- Subtract underflow still reverts with unsafe math enabled

### `u16`

- Add 
- Revert Add on overflow
- Revert on unsafe math on overflow
- Binary log of zero fails on overflow disabled
- Binary log of zero returns zero on unsafe math
- Divide
- Divide behavior on unsafe math
- Divide by zero reverts
- Log still reverts when overflow enabled
- Log behavior on unsafe math enabled
- Log parity with Rust tests
- Modulo zero still reverts with overflow enabled
- Multiply
- Multiply overflow still reverts on unsafe math enabled
- Pow still reverts on unsafe math enabled
- Root behavior the same on overflow enabled
- Root behavior the same on unsafe math enabled
- 0th root will fail
- Subtract
- Subtract underflow still reverts with unsafe math enabled

### `u32`

- Add 
- Revert Add on overflow
- Revert on unsafe math on overflow
- Binary log of zero fails on overflow disabled
- Binary log of zero returns zero on unsafe math
- Divide
- Divide behavior on unsafe math
- Divide by zero reverts
- Log still reverts when overflow enabled
- Log behavior on unsafe math enabled
- Log parity with Rust tests
- Modulo zero still reverts with overflow enabled
- Multiply
- Multiply overflow still reverts on unsafe math enabled
- Pow still reverts on unsafe math enabled
- Root behavior the same on overflow enabled
- Root behavior the same on unsafe math enabled
- 0th root will fail
- Subtract
- Subtract underflow still reverts with unsafe math enabled

### `u64`

- Add 
- Revert Add on overflow
- Revert on unsafe math on overflow
- Binary log of zero fails on overflow disabled
- Binary log of zero returns zero on unsafe math
- Divide
- Divide behavior on unsafe math
- Divide by zero reverts
- Log still reverts when overflow enabled
- Log behavior on unsafe math enabled
- Log parity with Rust tests
- Modulo zero still reverts with overflow enabled
- Multiply
- Multiply overflow still reverts on unsafe math enabled
- Pow still reverts on unsafe math enabled
- Root behavior the same on overflow enabled
- Root behavior the same on unsafe math enabled
- 0th root will fail
- Subtract
- Subtract underflow still reverts with unsafe math enabled

### `U128`

- Revert Add on overflow
- Revert on unsafe math on overflow
- Binary log of zero fails on overflow disabled
- Binary log of zero returns zero on unsafe math
- Divide behavior on unsafe math
- Log still reverts when overflow enabled
- Log behavior on unsafe math enabled
- Log parity with Rust tests
- Modulo zero still reverts with overflow enabled
- Multiply overflow still reverts on unsafe math enabled
- Pow still reverts on unsafe math enabled
- Root behavior the same on overflow enabled
- Root behavior the same on unsafe math enabled
- Subtract underflow still reverts with unsafe math enabled

### `u256`

- Add 
- Revert Add on overflow
- Revert on unsafe math on overflow
- Binary log of zero fails on overflow disabled
- Binary log of zero returns zero on unsafe math
- Divide
- Divide behavior on unsafe math
- Divide by zero reverts
- Log still reverts when overflow enabled
- Log behavior on unsafe math enabled
- Log parity with Rust tests
- Modulo zero still reverts with overflow enabled
- Multiply
- Multiply overflow still reverts on unsafe math enabled
- Pow still reverts on unsafe math enabled
- Root behavior the same on overflow enabled
- Root behavior the same on unsafe math enabled
- Subtract
- Subtract underflow still reverts with unsafe math enabled

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
2025-03-07 11:52:44 +00:00
Cameron Carstens
be5ecbd692
Implement TryFrom<Bytes> for b256 (#6958)
## Description

`impl From<Bytes> for b256` has been removed and replaced with `impl
TryFrom<Bytes> for b256` increasing saftey when converting between the
two types. `impl Into<Bytes> for b256` and `impl TryInto<b256> for
Bytes` have also been added.

This PR breaks the following implementation:

```sway
let mut my_bytes = Bytes::new();
my_bytes.push(1u8);

let result: b256 = b256::from(my_bytes);
```

The implementation should now be:

```sway
let mut my_bytes = Bytes::new();
my_bytes.push(1u8);

// Option 1
let result_1: Option<b256> = b256::try_from(my_bytes);
// Option 2
let result_2 = my_bytes.try_into();
```

Closes #6994.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Igor Rončević <ironcev@hotmail.com>
2025-03-05 17:20:13 +04:00
SwayStar123
1c3183a42f
Implement Iteration for Bytes (#6953)
## Description
Adds an iterator to allow for loops over bytes

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Cameron Carstens <bitzoic.eth@gmail.com>
2025-02-27 15:25:50 +01:00
SwayStar123
a090937bef
Add as_u256 for U128 (#6951)
## Description
Adds the conversion helper for U128

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-02-25 17:56:11 +04:00
IGI-111
f871a48807
Extend #[deprecated] support (#6944)
## Description

Add support for the `#[deprecated]` attribute to functions, enums,
struct items, enum variants and propagate deprecation check exhaustively
throughout expressions variants.

Partially addresses #6942.

Support for deprecating traits, abis and the methods thereof in their
definitions is still missing, as well as scrutinees and storage fields.


## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-02-25 09:48:27 +01:00
Igor Rončević
bb7c99b24a
Implement partial equivalence and extend forc migrate tool (#6900)
## Description

This PR:
- implements partial equivalence in the `core::ops`, as explained in
detail in #6883. The implementation is opt-in and behind the
`partial_eq` experimental feature flag.
- implements `forc migrate` migration steps for automatic migration to
`partial_eq` feature.
- extends the infrastructure for writing `forc migrate` migrations.
- preserves `Annotation`s on `LexedModule`s in the `LexedProgram`.
(Those were before stripped off and not available in the compiled
`Programs`. This resulted in loss off `//!` doc-comments that are
represented as `doc-comment` annotations.)

Extensions in the `forc migrate` infrastructure include:
- possibility to postpone migration steps. This allows writing
multi-step migrations where the first step is usually early adopting an
experimental feature by using `cfg` attributes in code. This possibility
is crucial for migrating our own codebase where we want to early-adopt
and test and stabilize experimental features.
- possibility to match elements on both mutable and immutable parsed
trees. The initial assumption that immutable matching on typed trees
will always be sufficient does not hold. Experimental `cfg` attributes
can remove parts of typed trees that might be needed in analysis.
- `LexedLocate(As)Annotated` for easier location and retrieval of
`Annotated` elements.
- additional implementations of existing traits.
- additional `Modifier`s for modifying existing elements in the lexed
tree.
- `New` struct for convenient creation of often needed lexed tree
elements. Note that we do not expect migrations to generate large new
parts of lexed trees, but mostly to modify or copy and modify existing
ones.

Almost all of the changes in the Sway files are done automatically by
the migration steps. The only manual change was in the `core` library
(`std` library is also automatically migrated) and in slight extension
of two of the tests.

Note that some of the tests have `Eq` traits in trait constraints. As
explained in the #6883, it is not necessary to change those to
`PartialEq`. We might consider changing some of them, for testing
purposes, and such a change is done on one of the tests that was testing
the behavior of the `Eq` trait. But for the majority of the tests, there
is no strict need for switching from `Eq` to `PartialEq`.

Rolling `PartialEq` out in the tests provoked three existing issues that
will be fixed in separate PRs: #6897, #6898, #6899.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-02-11 10:02:45 -03:00
Cameron Carstens
c6daba53cd
Std-lib support for ZK opcodes (#6832)
## Description

This PR adds support for the `ecop` and `epar` opcodes in the std-lib.
It also introduces the `Point2D` and `Scalar` type support cryptographic
operations.

The new crypto module follows the same format as developed in
https://github.com/FuelLabs/sway/pull/5747 which is still yet to come,
ensuring compatibility. The `Point2D` and `Scalar` types also use
`Bytes` under the hood to ensure future curves with points larger than
32 bytes are still supported.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: green <xgreenx9999@gmail.com>
Co-authored-by: Vaivaswatha Nagaraj <vaivaswatha.nagaraj@fuel.sh>
2025-02-05 16:15:46 +00:00
Cameron Carstens
e511ddaac1
Add last() to Vec (#6870)
## Description

`last()` is not implemented for `Vec`.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
2025-01-30 09:34:26 +01:00
Cameron Carstens
1194375781
Add resize() to Vec and Bytes (#6864)
## Description

There exists no `resize()` functionality for `Vec` and `Bytes`. This has
been implemented in this PR and mimics Rust's behavior.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-01-29 10:35:12 +01:00
Cameron Carstens
d48a793e54
Add Clone implementation for String and Vec (#6865)
## Description

This PR implements the `Clone` for `String` and `Vec`.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-01-29 06:34:11 +00:00
Cameron Carstens
b03c76e482
Introduce crypto module and expand cryptographic functions (#6837)
## Description

This PR replaces https://github.com/FuelLabs/sway/pull/5747 and intends
to introduce the crypto module.

> Note: https://github.com/FuelLabs/sway/pull/6832 will also use the
crypto module.

The std-lib currently contains the `ecr.sw` and `vm/evm/ecr.sw` files
which have the following functions:

- ec_recover()
- ec_recover_r1()
- ed_verify()
- ec_recover_address()
- ec_recover_address_r1()
- ec_recover_evm_address()

There are a number of issues with this including no type safety for
signatures from different elliptic curves, functions split across
multiple files, poor naming, and generic arguments. All of these are
resolved by this PR which deprecates both `ecr.sw` files and replaces
them with a crypto module which syntactically matches Rust.

The following new types are introduced:

* `PublicKey` - An Asymmetric public key, supporting both 64 and 32-byte
public keys
* `Message` - Hashed message authenticated by a signature type that
handles variable lengths
* `Secp256k1` - A secp256k1 signature
* `Secp256r1` - A secp256r1 signature
* `Ed25519` -  An ed25519 signature
* `Signature` - An ECDSA signature

All original functionality is retained with the new module:

* `Secp256k1::recover()` - Recovers a public key.
* `Secp256r1::recover()` - Recovers a public key.
* `Secp256k1::address()` - Recovers an address.
* `Secp256r1::address()` - Recovers an address.
* `Secp256k1::evm_address()` - Recovers an EVM address.
* `Ed25519::verify()` - Verify that a signature matches the given public
key.

The following new functionality has been added:

* `Secp256k1::verify()` - Verify that a signature matches the given
public key.
* `Secp256r1::verify()` - Verify that a signature matches the given
public key.
* `Secp256k1::verify_address()` - Verify that a signature matches the
given address.
* `Secp256r1::verify_address()` - Verify that a signature matches the
given address.
* `Secp256k1::verify_evm_address()` - Verify that a signature matches
the given EVM address.
* `Secp256r1::verify_evm_address()` - Verify that a signature matches
the given EVM address.
* `Secp256r1::evm_address()` - Recovers an EVM address.

The following functions have been deprecated: 

- `std::ecr::ec_recover()`
- `std::ecr::ec_recover_r1()`
- `std::ecr::ed_verify()`
- `std::ecr::ec_recover_address()`
- `std::ecr::ec_recover_address_r1()`
- `std::vm::evm::ecr::ec_recover_evm_address()`

Example of changes for recovering a public key:
```sway
// Before
fn foo(signature: B512, message: b256) {
     let recovered_public_key: B512 = ec_recover(signature, message).unwrap();
}

// After
fn bar(signature: Signature, message: Message) {
     let recovered_public_key: PublicKey = signature.recover(message).unwrap();
}
```

Example of changes for recovering an Address:
```sway
// Before
fn foo(signature: B512, message: b256) {
     let recovered_address: Address = ec_recover_address(signature, message).unwrap();
}

// After
fn bar(signature: Signature, message: Message) {
     let recovered_address: Address = signature.address(message).unwrap();
}
```

Complete recovery example using the `Signature` type:

```sway
use std::crypto::{Message, PublicKey, Secp256r1, Signature};

fn foo() {
    let secp256r1_signature = Signature::Secp256r1(Secp256r1::from((
        0xbd0c9b8792876712afadbff382e1bf31c44437823ed761cc3600d0016de511ac,
        0x44ac566bd156b4fc71a4a4cb2655d3da360c695edb27dc3b64d621e122fea23d,
    )));
    let signed_message = Message::from(0x1e45523606c96c98ba970ff7cf9511fab8b25e1bcd52ced30b81df1e4a9c4323);
    
    // A recovered public key pair.
    let secp256r1_public_key = secp256r1_signature.recover(signed_message);
    assert(secp256r1_public_key.is_ok());
    assert(
        secp256r1_public_key
            .unwrap() == PublicKey::from((
            0xd6ea577a54ae42411fbc78d686d4abba2150ca83540528e4b868002e346004b2,
            0x62660ecce5979493fe5684526e8e00875b948e507a89a47096bc84064a175452,
        )),
    );
}
```

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-01-25 23:23:40 +01:00
Cameron Carstens
e9dccba58e
Add Bytes conversions for Address, ContractId, EvmAddress, B512 and AssetId (#6857)
## Description

Several stack types did not convert from or into `Bytes`. These have
been added in this PR for better interop and devex.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
2025-01-24 12:02:51 +01:00
SwayStar123
986aee2c1e
Add splice method to Bytes (#6823)
## Description
Adds a splice method to bytes that lets you select start and end
indices, and returns the range of bytes, removing them from the original
bytes.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
2025-01-23 16:06:14 +05:30
Igor Rončević
4fe687094e
Implement Iterator for StorageVec (#6821)
## Description

This PR implements `Iterator` for `StorageVec`.

Iterating over a `StorageVec` via `for` loop should now be the preferred
option. Compared to the traversal via `while` loop and incrementing
index, the `for` loop is:
- more performant. `for` loop eliminates the redundant boundary checks
done in every `get()` call in the `while` loop.
- more idiomatic and convenient.

Closes #6796.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: bitzoic <bitzoic.eth@gmail.com>
2025-01-14 16:18:16 +01:00
SwayStar123
561007df93
Remove instances of multiple impl blocks (#6822)
## Description
Previously due to issue #1548 it was not possible to use a method
defined in an impl block in another method defined in the same impl
block, that issue is now fixed.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
2025-01-09 16:41:25 +05:30
Igor Rončević
23576769d1
Refactor swayfmt to support arbitrary lexed trees (#6806)
## Description

This PR refactors `swayfmt` to be able generate code from arbitrary
lexed trees. Arbitrary means a lexed tree that can be fully or partially
created in-memory by manipulating the tree structure in code. It does
not need to necessarily be backed by source code. This is needed to be
able to reuse `swayfmt` for generating source code from tools that
analyze and modify Sway code, as explained in #6779.

This PR makes the `swayfmt` independent of spans backed by the source
code, by doing the following changes:
- Keywords are rendered by using theirs `AS_STR` associated constants.
- The `Token` trait is extended with the `AS_STR` associated constant,
and tokens are rendered by using that constant.
- `Ident`s are rendered by using theirs `as_str()` methods. This method
takes the textual implementation from `Ident::name_override_opt` if it
is provided, and ignores the span in that case.
- `Literal`s are rendered based on the literal value and not their
spans. The exception are numeric literals that do not have empty spans.
Those are considered to be backed by source code and are rendered as
written in code, e.g., `1_000_000u64`.

The PR also fixes some existing bugs in `swayfmt`:
- `use path::{single_import,};` will be formatted as `use
path::single_import;`
- `use path::{a,b,};` will be formatted as `use path::{a, b};`
- partial addresses #6802 by rendering annotations for final values.
- partial addresses #6805 by properly rendering final values, but still
not using the expected field alignment.

The PR also removes the `path` parameter from the `parse_file`. It was
used only to provide an unnecessary dummy `source_id` which was always
pointing to the package root file.

Closes #6779.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-01-07 12:55:03 +01:00
Cameron Carstens
192f163828
Update output_asset_to() and output_asset_id() to handle Output::Variable (#6781)
## Description

Allows for fetching of the `AssetId` and the to `Address` from an
`Output::Variable`.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
Co-authored-by: IGI-111 <igi-111@protonmail.com>
2024-12-12 08:26:49 +01:00
Cameron Carstens
ff8291a2b1
Add mod to U128 (#6771)
## Description

The `mod` trait implementation was missing for `U128`. This has been
added.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
Co-authored-by: IGI-111 <igi-111@protonmail.com>
2024-12-07 02:01:54 +01:00
Marcos Henrich
a02e8ead10
Error on use of self intead of Self in return type (#6763)
## Description

With this PR we no longer support using `self` instead of `Self` when
referring to the Self Type.

Fixes #6396

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2024-11-28 01:52:09 +00:00
Igor Rončević
3b07f499c2
Storage domains (#6466)
## Description

This PR fixes #6317 by implementing `storage_domains` experimental
feature.

This feature introduces two _storage domains_, one for the storage
fields defined inside of the `storage` declaration, and a different one
for storage slots generated inside of the `StorageMap`.

The PR strictly fixes the issue described in #6317 by applying the
recommendation proposed in the issue.
A general approach to storage key domains will be discussed as a part of
the [Configurable and composable storage
RFC](https://github.com/FuelLabs/sway-rfcs/pull/40).

Additionally, the PR:
- adds expressive diagnostics for the duplicated storage keys warning.
- removes the misleading internal compiler error that always followed
the storage key type mismatch error (see demo below).

Closes #6317, #6701.

## Breaking Changes

The PR changes the way how storage keys are generated for `storage`
fields. Instead of `sha256("storage::ns1::ns2.field_name")` we now use
`sha256((0u8, "storage::ns1::ns2.field_name"))`.

This is a breaking change for those who relied on the storage key
calculation.

## Demo

Before, every type-mismatch was always followed by an ICE, because the
compilation continued despite the type-mismatch.

![Mismatched types and ICE -
Before](https://github.com/user-attachments/assets/ac7915f7-3458-409e-a2bb-118dd4925234)

This is solved now, and the type-mismatch has a dedicated help message.

![Mismatched types and ICE -
After](https://github.com/user-attachments/assets/570aedd3-4c9c-4945-bfd0-5f12d68dbead)

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
Co-authored-by: IGI-111 <igi-111@protonmail.com>
2024-11-15 04:46:56 +01:00
SwayStar123
f7fa086f5a
Add revert with log function to std lib (#6717)
## Description
Adds a `revert_with_log` function that reverts unconditionally with a
message

Closes #6629

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
2024-11-14 18:31:39 +05:30
Daniel Frederico Lins Leite
861ca10a06
Test encoding raw slices does not allows buffer overflows (#6699)
## Description

Test for https://github.com/FuelLabs/sway/pull/6686 buffer overflow on
`Bytes` encoding. This test fails without the fix and pass with it.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
Co-authored-by: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
2024-11-13 16:35:46 -03:00
Daniel Frederico Lins Leite
ae9f172005
Send method arguments with numerics to the second pass (#6700)
## Description

This PR brings back encode/decode buffer ownership tests and closes
https://github.com/FuelLabs/sway/issues/6567.

The original issue was that an error in the CI file was hiding that
these tests were not compiling anymore.

IR generation now also tests the expression and the variant type match,
giving a better error than before.

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2024-11-12 07:48:14 -03:00