1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! # `Felt` dictionary type
//!
//! A key value storage for values whose type implement Copy. The key is always a felt.
//!
//! This type is represented as a pointer to a tuple of a heap allocated Rust hashmap along with a u64
//! used to count accesses to the dictionary. The type is interacted through the runtime functions to
//! insert, get elements and increment the access counter.
use super::WithSelf;
use crate::{error::Result, metadata::MetadataStorage};
use cairo_lang_sierra::{
extensions::{
core::{CoreLibfunc, CoreType},
types::InfoAndTypeConcreteType,
},
program_registry::ProgramRegistry,
};
use melior::{
dialect::llvm,
ir::{Module, Type},
Context,
};
/// Build the MLIR type.
///
/// Dict snapshots are semantically useless in Cairo, so the default dup
/// (returns the same pointer twice) is correct. The arena owns dict memory and
/// HashMaps are reclaimed via DICT_REGISTRY during arena reset, so the default
/// drop (noop) is also correct. No dup/drop overrides are registered.
///
/// Check out [the module](self) for more info.
pub fn build<'ctx>(
context: &'ctx Context,
_module: &Module<'ctx>,
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_metadata: &mut MetadataStorage,
_info: WithSelf<InfoAndTypeConcreteType>,
) -> Result<Type<'ctx>> {
Ok(llvm::r#type::pointer(context, 0))
}
#[cfg(test)]
mod test {
use crate::{
jit_dict,
utils::testing::{get_compiled_program, run_program},
values::Value,
};
use pretty_assertions_sorted::assert_eq;
use starknet_types_core::felt::Felt;
use std::collections::HashMap;
#[test]
fn dict_snapshot_take() {
let program = get_compiled_program("test_data_artifacts/programs/types/dict_snapshot_take");
let result = run_program(&program, "run_test", &[]).return_value;
assert_eq!(
result,
jit_dict!(
2 => 1u32
),
);
}
#[test]
fn dict_snapshot_take_complex() {
let program =
get_compiled_program("test_data_artifacts/programs/types/dict_snapshot_complex");
let result = run_program(&program, "run_test", &[]).return_value;
assert_eq!(
result,
jit_dict!(
2 => Value::Array(vec![3u32.into(), 4u32.into()])
),
);
}
#[test]
fn dict_snapshot_take_compare() {
let program = get_compiled_program(
"test_data_artifacts/programs/types/dict_snapshot_compare_snapshot",
);
let program2 =
get_compiled_program("test_data_artifacts/programs/types/dict_snapshot_compare_owned");
let result1 = run_program(&program, "run_test", &[]).return_value;
let result2 = run_program(&program2, "run_test", &[]).return_value;
assert_eq!(result1, result2);
}
/// Ensure that a dictionary of booleans compiles.
#[test]
fn dict_type_bool() {
let program = get_compiled_program("test_data_artifacts/programs/types/dict_bool");
let result = run_program(&program, "run_program", &[]);
assert_eq!(
result.return_value,
Value::Felt252Dict {
value: HashMap::from([
(
Felt::ZERO,
Value::Enum {
tag: 0,
value: Box::new(Value::Struct {
fields: Vec::new(),
debug_name: None
}),
debug_name: None,
},
),
(
Felt::ONE,
Value::Enum {
tag: 1,
value: Box::new(Value::Struct {
fields: Vec::new(),
debug_name: None
}),
debug_name: None,
},
),
]),
debug_name: None,
},
);
}
/// Ensure that a dictionary of felts compiles.
#[test]
fn dict_type_felt252() {
let program = get_compiled_program("test_data_artifacts/programs/types/dict_felt252");
let result = run_program(&program, "run_program", &[]);
assert_eq!(
result.return_value,
Value::Felt252Dict {
value: HashMap::from([
(Felt::ZERO, Value::Felt252(Felt::ZERO)),
(Felt::ONE, Value::Felt252(Felt::ONE)),
(Felt::TWO, Value::Felt252(Felt::TWO)),
(Felt::THREE, Value::Felt252(Felt::THREE)),
]),
debug_name: None,
},
);
}
/// Ensure that a dictionary of nullables compiles.
#[test]
fn dict_type_nullable() {
let program = get_compiled_program("test_data_artifacts/programs/types/dict_nullable");
let result = run_program(&program, "run_program", &[]);
pretty_assertions_sorted::assert_eq_sorted!(
result.return_value,
Value::Felt252Dict {
value: HashMap::from([
(Felt::ZERO, Value::Null),
(
Felt::ONE,
Value::Struct {
fields: Vec::from([
Value::Uint8(0),
Value::Sint16(1),
Value::Felt252(2.into()),
]),
debug_name: None,
},
),
(
Felt::TWO,
Value::Struct {
fields: Vec::from([
Value::Uint8(1),
Value::Sint16(-2),
Value::Felt252(3.into()),
]),
debug_name: None,
},
),
(
Felt::THREE,
Value::Struct {
fields: Vec::from([
Value::Uint8(2),
Value::Sint16(3),
Value::Felt252(4.into()),
]),
debug_name: None,
},
),
]),
debug_name: None,
},
);
}
/// Ensure that a dictionary of unsigned integers compiles.
#[test]
fn dict_type_unsigned() {
let program = get_compiled_program("test_data_artifacts/programs/types/dict_u128");
let result = run_program(&program, "run_program", &[]);
assert_eq!(
result.return_value,
Value::Felt252Dict {
value: HashMap::from([
(Felt::ZERO, Value::Uint128(0)),
(Felt::ONE, Value::Uint128(1)),
(Felt::TWO, Value::Uint128(2)),
(Felt::THREE, Value::Uint128(3)),
]),
debug_name: None,
},
);
}
}