Skip to content

Commit 13e4df8

Browse files
authored
Enable storage_vec_iter_tests in-language test (#7356)
## Description This PR brings the `storage_vec_iter_tests` in-language test to the newest compiler version and enables it. The test was originally disabled because of #6899 which has been fixed. To bring the test to the newest compiler version, this PR: - fixes trait coherence violations. - adds support for `const_generics` experimental feature. ## 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.
1 parent 8fa01b8 commit 13e4df8

File tree

5 files changed

+57
-83
lines changed

5 files changed

+57
-83
lines changed

sway-core/src/semantic_analysis/cei_pattern_analysis.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,14 @@ fn effects_of_expression(engines: &Engines, expr: &ty::TyExpression) -> HashSet<
515515
let type_engine = engines.te();
516516
let decl_engine = engines.de();
517517
match &expr.expression {
518-
ConstGenericExpression { .. } => {
519-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
520-
}
521518
Literal(_)
522519
| ConstantExpression { .. }
523520
| ConfigurableExpression { .. }
524521
| VariableExpression { .. }
525522
| FunctionParameter
526523
| Break
527524
| Continue
525+
| ConstGenericExpression { .. }
528526
| AbiName(_) => HashSet::new(),
529527
// this type of assignment only mutates local variables and not storage
530528
Reassignment(reassgn) => effects_of_expression(engines, &reassgn.rhs),
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# TODO Enable this test once https://github.com/FuelLabs/sway/issues/6899 is fixed.
2-
# Compilation fails with error: No method "contains4(Data2<u64, u64>, Data2<numeric, numeric>) -> bool" found for type "Data2<T, K>".
3-
category = "disabled"
1+
category = "run"
42
expected_result = { action = "return", value = 10 }
53
expected_result_new_encoding = { action = "return_data", value = "000000000000000A" }
64
validate_abi = true

test/src/in_language_tests/Forc.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ members = [
3030
"test_programs/result_inline_tests",
3131
"test_programs/revert_inline_tests",
3232
"test_programs/storage_key_inline_tests",
33-
# TODO: Enable this test once https://github.com/FuelLabs/sway/issues/6899 is fixed.
34-
# Compilation fails with error: No method "eq(Self, Self) -> ()" found for type "Self".
35-
# "test_programs/storage_vec_iter_tests",
33+
"test_programs/storage_vec_iter_tests",
3634
"test_programs/string_inline_tests",
3735
"test_programs/time_inline_tests",
3836
"test_programs/tx_inline_tests",

test/src/in_language_tests/test_programs/storage_vec_iter_tests/src/impls.sw

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl TestInstance for str {
117117
}
118118
}
119119

120+
#[cfg(experimental_const_generics = false)]
120121
impl PartialEq for str[6] {
121122
fn eq(self, other: Self) -> bool {
122123
let mut i = 0;
@@ -134,6 +135,7 @@ impl PartialEq for str[6] {
134135
true
135136
}
136137
}
138+
#[cfg(experimental_const_generics = false)]
137139
impl Eq for str[6] {}
138140

139141
impl TestInstance for str[6] {
@@ -154,11 +156,13 @@ impl TestInstance for str[6] {
154156
}
155157
}
156158

159+
#[cfg(experimental_const_generics = false)]
157160
impl PartialEq for [u64; 2] {
158161
fn eq(self, other: Self) -> bool {
159162
self[0] == other[0] && self[1] == other[1]
160163
}
161164
}
165+
#[cfg(experimental_const_generics = false)]
162166
impl Eq for [u64; 2] {}
163167

164168
impl TestInstance for [u64; 2] {
@@ -251,13 +255,6 @@ impl TestInstance for Enum {
251255
}
252256
}
253257

254-
impl PartialEq for (u8, u32) {
255-
fn eq(self, other: Self) -> bool {
256-
self.0 == other.0 && self.1 == other.1
257-
}
258-
}
259-
impl Eq for (u8, u32) {}
260-
261258
impl TestInstance for (u8, u32) {
262259
fn elements(len: u64) -> Vec<Self> {
263260
let u8_values = u8::elements(len);
@@ -288,16 +285,11 @@ impl TestInstance for b256 {
288285
}
289286
}
290287

291-
impl AbiEncode for raw_ptr {
292-
fn abi_encode(self, buffer: Buffer) -> Buffer {
293-
let ptr_as_u64 = asm(p: self) {
294-
p: u64
295-
};
296-
ptr_as_u64.abi_encode(buffer)
297-
}
288+
pub struct RawPtrNewtype {
289+
ptr: raw_ptr,
298290
}
299291

300-
impl TestInstance for raw_ptr {
292+
impl TestInstance for RawPtrNewtype {
301293
fn elements(len: u64) -> Vec<Self> {
302294
let values = u8::elements(len);
303295
let mut res = Vec::new();
@@ -306,16 +298,39 @@ impl TestInstance for raw_ptr {
306298
let null_ptr = asm() {
307299
zero: raw_ptr
308300
};
309-
res.push(null_ptr.add::<u64>(values.get(i).unwrap().as_u64()));
301+
res.push(RawPtrNewtype {
302+
ptr: null_ptr.add::<u64>(values.get(i).unwrap().as_u64())
303+
});
310304
i += 1;
311305
}
312306
res
313307
}
314308
}
315309

310+
impl AbiEncode for RawPtrNewtype {
311+
fn abi_encode(self, buffer: Buffer) -> Buffer {
312+
let ptr_as_u64 = asm(p: self.ptr) {
313+
p: u64
314+
};
315+
ptr_as_u64.abi_encode(buffer)
316+
}
317+
}
318+
319+
impl PartialEq for RawPtrNewtype {
320+
fn eq(self, other: Self) -> bool {
321+
self.ptr == other.ptr
322+
}
323+
}
324+
impl Eq for RawPtrNewtype {}
325+
316326
impl TestInstance for raw_slice {
317327
fn elements(len: u64) -> Vec<Self> {
318-
let ptr_values = raw_ptr::elements(len);
328+
let raw_ptr_values = RawPtrNewtype::elements(len);
329+
let mut ptr_values = Vec::<raw_ptr>::new();
330+
for raw_ptr in raw_ptr_values.iter() {
331+
ptr_values.push(raw_ptr.ptr);
332+
}
333+
319334
let len_values = u8::elements(len);
320335
let mut res = Vec::new();
321336
let mut i = 0;
@@ -335,13 +350,6 @@ impl TestInstance for raw_slice {
335350
}
336351
}
337352

338-
impl PartialEq for raw_slice {
339-
fn eq(self, other: Self) -> bool {
340-
self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes()
341-
}
342-
}
343-
impl Eq for raw_slice {}
344-
345353
impl TestInstance for () {
346354
fn elements(len: u64) -> Vec<Self> {
347355
let mut res = Vec::new();
@@ -354,13 +362,6 @@ impl TestInstance for () {
354362
}
355363
}
356364

357-
impl PartialEq for () {
358-
fn eq(self, other: Self) -> bool {
359-
true
360-
}
361-
}
362-
impl Eq for () {}
363-
364365
impl TestInstance for [u64; 0] {
365366
fn elements(len: u64) -> Vec<Self> {
366367
let mut res = Vec::new();
@@ -373,9 +374,11 @@ impl TestInstance for [u64; 0] {
373374
}
374375
}
375376

377+
#[cfg(experimental_const_generics = false)]
376378
impl PartialEq for [u64; 0] {
377379
fn eq(self, other: Self) -> bool {
378380
true
379381
}
380382
}
383+
#[cfg(experimental_const_generics = false)]
381384
impl Eq for [u64; 0] {}

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

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@ use impls::Enum;
88
use std::hash::{Hash, sha256};
99
use std::storage::storage_vec::*;
1010

11-
abi StorageVecIterTest {
12-
#[storage(read)]
13-
fn assert_empty_vec_next_returns_none();
14-
15-
#[storage(read, write)]
16-
fn assert_vec_with_elements_next_returns_element();
17-
18-
#[storage(read, write)]
19-
fn assert_vec_with_elements_for_loop_iteration();
20-
21-
#[storage(read, write)]
22-
fn storage_vec_field_for_loop_iteration();
23-
24-
#[storage(read, write)]
25-
fn storage_vec_field_nested_for_loop_iteration();
26-
}
27-
2811
storage {
2912
vec: StorageVec<u64> = StorageVec {},
3013
vec_of_vec: StorageVec<StorageVec<u64>> = StorageVec {},
@@ -33,8 +16,7 @@ storage {
3316
#[storage(read)]
3417
fn assert_empty_vec_next_returns_none_impl<T>(slot_id_preimage: u64) {
3518
let vec: StorageKey<StorageVec<T>> = StorageKey::new(sha256(slot_id_preimage), 0, sha256(slot_id_preimage + 100));
36-
// TODO: Replace with `.is_none()` method call once https://github.com/FuelLabs/sway/issues/6825 is fixed.
37-
assert(Option::is_none(vec.iter().next()));
19+
assert(vec.iter().next().is_none());
3820
}
3921

4022
#[storage(read, write)]
@@ -58,20 +40,18 @@ where
5840
let mut iter = vec.iter();
5941
while i < NUM_OF_ELEMENTS {
6042
let element = iter.next();
61-
// TODO: Replace with `.is_some()` method call once https://github.com/FuelLabs/sway/issues/6825 is fixed.
62-
assert(Option::is_some(element));
43+
assert(element.is_some());
6344

6445
let value = element.unwrap().read();
6546
assert_eq(value, vec.get(i).unwrap().read());
6647

6748
i += 1;
6849
}
6950

70-
// TODO: Replace with `.is_none()` method call once https://github.com/FuelLabs/sway/issues/6825 is fixed.
7151
let element_after_last = iter.next();
72-
assert(Option::is_none(element_after_last));
52+
assert(element_after_last.is_none());
7353
let element_after_last = iter.next();
74-
assert(Option::is_none(element_after_last));
54+
assert(element_after_last.is_none());
7555
}
7656

7757
#[storage(read, write)]
@@ -91,21 +71,18 @@ where
9171
i += 1;
9272
}
9373

94-
// TODO: This code fails to compile with the error given below.
95-
// Uncomment this test once https://github.com/FuelLabs/sway/issues/6825 is fixed.
96-
// let mut i = 0;
97-
// for element in vec.iter() {
98-
// // ERROR: ^^^^^^^^^^ No method "unwrap(Option<StorageKey<T>>)" found for type "Option<StorageKey<T>>".
99-
// let value = element.read();
100-
// assert_eq(value, vec.get(i).unwrap().read());
74+
let mut i = 0;
75+
for element in vec.iter() {
76+
let value = element.read();
77+
assert_eq(value, vec.get(i).unwrap().read());
10178

102-
// i += 1;
103-
// }
79+
i += 1;
80+
}
10481

105-
// assert_eq(vec.len(), i);
82+
assert_eq(vec.len(), i);
10683
}
10784

108-
impl StorageVecIterTest for Contract {
85+
impl Contract {
10986
#[storage(read)]
11087
fn assert_empty_vec_next_returns_none() {
11188
assert_empty_vec_next_returns_none_impl::<()>(1);
@@ -124,7 +101,7 @@ impl StorageVecIterTest for Contract {
124101
assert_empty_vec_next_returns_none_impl::<Enum>(14);
125102
assert_empty_vec_next_returns_none_impl::<(u8, u32)>(15);
126103
assert_empty_vec_next_returns_none_impl::<b256>(16);
127-
assert_empty_vec_next_returns_none_impl::<raw_ptr>(17);
104+
assert_empty_vec_next_returns_none_impl::<RawPtrNewtype>(17);
128105
assert_empty_vec_next_returns_none_impl::<raw_slice>(18);
129106
}
130107

@@ -147,7 +124,7 @@ impl StorageVecIterTest for Contract {
147124
assert_vec_with_elements_next_returns_element_impl::<Enum>(14);
148125
assert_vec_with_elements_next_returns_element_impl::<(u8, u32)>(15);
149126
assert_vec_with_elements_next_returns_element_impl::<b256>(16);
150-
assert_vec_with_elements_next_returns_element_impl::<raw_ptr>(17);
127+
assert_vec_with_elements_next_returns_element_impl::<RawPtrNewtype>(17);
151128
assert_vec_with_elements_next_returns_element_impl::<raw_slice>(18);
152129
}
153130

@@ -170,7 +147,7 @@ impl StorageVecIterTest for Contract {
170147
assert_vec_with_elements_for_loop_iteration_impl::<Enum>(14);
171148
assert_vec_with_elements_for_loop_iteration_impl::<(u8, u32)>(15);
172149
assert_vec_with_elements_for_loop_iteration_impl::<b256>(16);
173-
assert_vec_with_elements_for_loop_iteration_impl::<raw_ptr>(17);
150+
assert_vec_with_elements_for_loop_iteration_impl::<RawPtrNewtype>(17);
174151
assert_vec_with_elements_for_loop_iteration_impl::<raw_slice>(18);
175152
}
176153

@@ -239,30 +216,30 @@ impl StorageVecIterTest for Contract {
239216

240217
#[test]
241218
fn empty_vec_next_returns_none() {
242-
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
219+
let contract_abi = abi(StorageVecIterTestsAbi, CONTRACT_ID);
243220
contract_abi.assert_empty_vec_next_returns_none();
244221
}
245222

246223
#[test]
247224
fn vec_with_elements_next_returns_element() {
248-
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
225+
let contract_abi = abi(StorageVecIterTestsAbi, CONTRACT_ID);
249226
contract_abi.assert_vec_with_elements_next_returns_element();
250227
}
251228

252229
#[test]
253230
fn vec_with_elements_for_loop_iteration() {
254-
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
231+
let contract_abi = abi(StorageVecIterTestsAbi, CONTRACT_ID);
255232
contract_abi.assert_vec_with_elements_for_loop_iteration();
256233
}
257234

258235
#[test]
259236
fn storage_vec_field_for_loop_iteration() {
260-
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
237+
let contract_abi = abi(StorageVecIterTestsAbi, CONTRACT_ID);
261238
contract_abi.storage_vec_field_for_loop_iteration();
262239
}
263240

264241
#[test]
265242
fn storage_vec_field_nested_for_loop_iteration() {
266-
let contract_abi = abi(StorageVecIterTest, CONTRACT_ID);
243+
let contract_abi = abi(StorageVecIterTestsAbi, CONTRACT_ID);
267244
contract_abi.storage_vec_field_nested_for_loop_iteration();
268245
}

0 commit comments

Comments
 (0)