Skip to content

Commit ca96726

Browse files
authored
Remove workarounds based on asm blocks from std::hash (#7446)
## Description This PR removes workarounds based on `asm` blocks used in the `std::hash` module to obtain an address of a copy-type. Those workarounds are replaced with `__addr_of` which since #7255 supports taking addresses of copy-types. ## 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) - [ ] 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.
1 parent 74bab18 commit ca96726

File tree

4 files changed

+21
-41
lines changed

4 files changed

+21
-41
lines changed

sway-lib-std/src/hash.sw

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,42 +107,21 @@ impl Hash for u8 {
107107

108108
impl Hash for u16 {
109109
fn hash(self, ref mut state: Hasher) {
110-
// TODO: Remove this workaround once `__addr_of(self)` is supported
111-
// for `u16`.
112-
let temp = self;
113-
let ptr = asm(ptr: &temp) {
114-
ptr: raw_ptr
115-
};
116-
let ptr = ptr.add::<u8>(6);
117-
110+
let ptr = __addr_of(self).add::<u8>(6);
118111
state.write_raw_slice(raw_slice::from_parts::<u8>(ptr, 2));
119112
}
120113
}
121114

122115
impl Hash for u32 {
123116
fn hash(self, ref mut state: Hasher) {
124-
// TODO: Remove this workaround once `__addr_of(self)` is supported
125-
// for `u32`.
126-
let temp = self;
127-
let ptr = asm(ptr: &temp) {
128-
ptr: raw_ptr
129-
};
130-
let ptr = ptr.add::<u8>(4);
131-
117+
let ptr = __addr_of(self).add::<u8>(4);
132118
state.write_raw_slice(raw_slice::from_parts::<u8>(ptr, 4));
133119
}
134120
}
135121

136122
impl Hash for u64 {
137123
fn hash(self, ref mut state: Hasher) {
138-
// TODO: Remove this workaround once `__addr_of(self)` is supported
139-
// for `u64`.
140-
let temp = self;
141-
let ptr = asm(ptr: &temp) {
142-
ptr: raw_ptr
143-
};
144-
145-
state.write_raw_slice(raw_slice::from_parts::<u8>(ptr, 8));
124+
state.write_raw_slice(raw_slice::from_parts::<u8>(__addr_of(self), 8));
146125
}
147126
}
148127

test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,97 +63,97 @@
6363
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
6464
"indirect": false,
6565
"name": "BOOL",
66-
"offset": 4936
66+
"offset": 4912
6767
},
6868
{
6969
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
7070
"indirect": false,
7171
"name": "U8",
72-
"offset": 5128
72+
"offset": 5104
7373
},
7474
{
7575
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
7676
"indirect": false,
7777
"name": "ANOTHER_U8",
78-
"offset": 4864
78+
"offset": 4840
7979
},
8080
{
8181
"concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef",
8282
"indirect": false,
8383
"name": "U16",
84-
"offset": 5072
84+
"offset": 5048
8585
},
8686
{
8787
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
8888
"indirect": false,
8989
"name": "U32",
90-
"offset": 5112
90+
"offset": 5088
9191
},
9292
{
9393
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
9494
"indirect": false,
9595
"name": "U64",
96-
"offset": 5120
96+
"offset": 5096
9797
},
9898
{
9999
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
100100
"indirect": false,
101101
"name": "U256",
102-
"offset": 5080
102+
"offset": 5056
103103
},
104104
{
105105
"concreteTypeId": "7c5ee1cecf5f8eacd1284feb5f0bf2bdea533a51e2f0c9aabe9236d335989f3b",
106106
"indirect": false,
107107
"name": "B256",
108-
"offset": 4904
108+
"offset": 4880
109109
},
110110
{
111111
"concreteTypeId": "81fc10c4681a3271cf2d66b2ec6fbc8ed007a442652930844fcf11818c295bff",
112112
"indirect": false,
113113
"name": "CONFIGURABLE_STRUCT",
114-
"offset": 5024
114+
"offset": 5000
115115
},
116116
{
117117
"concreteTypeId": "a2922861f03be8a650595dd76455b95383a61b46dd418f02607fa2e00dc39d5c",
118118
"indirect": false,
119119
"name": "CONFIGURABLE_ENUM_A",
120-
"offset": 4944
120+
"offset": 4920
121121
},
122122
{
123123
"concreteTypeId": "a2922861f03be8a650595dd76455b95383a61b46dd418f02607fa2e00dc39d5c",
124124
"indirect": false,
125125
"name": "CONFIGURABLE_ENUM_B",
126-
"offset": 4984
126+
"offset": 4960
127127
},
128128
{
129129
"concreteTypeId": "4926d35d1a5157936b0a29bc126b8aace6d911209a5c130e9b716b0c73643ea6",
130130
"indirect": false,
131131
"name": "ARRAY_BOOL",
132-
"offset": 4872
132+
"offset": 4848
133133
},
134134
{
135135
"concreteTypeId": "776fb5a3824169d6736138565fdc20aad684d9111266a5ff6d5c675280b7e199",
136136
"indirect": false,
137137
"name": "ARRAY_U64",
138-
"offset": 4880
138+
"offset": 4856
139139
},
140140
{
141141
"concreteTypeId": "c998ca9a5f221fe7b5c66ae70c8a9562b86d964408b00d17f883c906bc1fe4be",
142142
"indirect": false,
143143
"name": "TUPLE_BOOL_U64",
144-
"offset": 5056
144+
"offset": 5032
145145
},
146146
{
147147
"concreteTypeId": "94f0fa95c830be5e4f711963e83259fe7e8bc723278ab6ec34449e791a99b53a",
148148
"indirect": false,
149149
"name": "STR_4",
150-
"offset": 5048
150+
"offset": 5024
151151
},
152152
{
153153
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
154154
"indirect": false,
155155
"name": "NOT_USED",
156-
"offset": 5040
156+
"offset": 5016
157157
}
158158
],
159159
"encodingVersion": "1",

test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use basic_storage_abi::{BasicStorage, Quad};
44
#[cfg(experimental_new_encoding = false)]
55
const CONTRACT_ID = 0x94db39f409a31b9f2ebcadeea44378e419208c20de90f5d8e1e33dc1523754cb;
66
#[cfg(experimental_new_encoding = true)]
7-
const CONTRACT_ID = 0x7bea5fdc8235952cd918b15ae4553e449bdd2b6297c1ce70a4188a176e8b145d; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release
7+
const CONTRACT_ID = 0x55a636b2843307e245948ea70b113b81d1f30307d4fa6fa0f4bb27c0149afd5c; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release
88

99
fn main() -> u64 {
1010
let addr = abi(BasicStorage, CONTRACT_ID);

test/src/in_language_tests/test_programs/hash_inline_tests/src/main.sw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ fn hash_fn_sha256_str_array() {
113113
// hex = "0.4"
114114
// bincode = "1.3"
115115
// serde = { version = "1", features = ["derive"] }
116+
116117
#[test()]
117118
fn hash_u8() {
118119
let mut hasher = Hasher::new();

0 commit comments

Comments
 (0)