Skip to content

Commit 108ff27

Browse files
committed
fix(compiler): handle nested container ref pointer options correctly
1 parent 810943c commit 108ff27

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

compiler/fory_compiler/generators/rust.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,12 +1039,18 @@ def generate_type(
10391039
elif isinstance(field_type, ListType):
10401040
effective_element_optional = element_optional or field_type.element_optional
10411041
effective_element_ref = element_ref or field_type.element_ref
1042+
element_pointer_type = pointer_type
1043+
if field_type.element_ref:
1044+
element_pointer_type = self.get_pointer_type(
1045+
field_type.element_ref_options,
1046+
field_type.element_ref_options.get("weak_ref") is True,
1047+
)
10421048
element_type = self.generate_type(
10431049
field_type.element_type,
10441050
nullable=effective_element_optional,
10451051
ref=effective_element_ref,
10461052
parent_stack=parent_stack,
1047-
pointer_type=pointer_type,
1053+
pointer_type=element_pointer_type,
10481054
)
10491055
list_type = f"::std::vec::Vec<{element_type}>"
10501056
if ref:
@@ -1076,12 +1082,18 @@ def generate_type(
10761082
parent_stack=parent_stack,
10771083
pointer_type=pointer_type,
10781084
)
1085+
value_pointer_type = pointer_type
1086+
if field_type.value_ref:
1087+
value_pointer_type = self.get_pointer_type(
1088+
field_type.value_ref_options,
1089+
field_type.value_ref_options.get("weak_ref") is True,
1090+
)
10791091
value_type = self.generate_type(
10801092
field_type.value_type,
10811093
nullable=False,
10821094
ref=field_type.value_ref,
10831095
parent_stack=parent_stack,
1084-
pointer_type=pointer_type,
1096+
pointer_type=value_pointer_type,
10851097
)
10861098
map_type = f"::std::collections::HashMap<{key_type}, {value_type}>"
10871099
if ref:

compiler/fory_compiler/tests/test_generated_code.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,43 @@ def test_rust_generated_code_can_use_chrono_temporal_types():
205205
assert "::fory::Duration" not in rust_output
206206

207207

208+
def test_rust_nested_container_ref_uses_correct_pointer_type():
209+
schema = parse_fdl(
210+
dedent(
211+
"""
212+
package gen;
213+
214+
message Node {
215+
string value = 1;
216+
}
217+
218+
message Request {
219+
list<list<ref(thread_safe=true) Node>> groups = 1;
220+
map<string, map<string, ref(thread_safe=true) Node>> nodes = 2;
221+
}
222+
"""
223+
)
224+
)
225+
226+
rust_output = render_files(generate_files(schema, RustGenerator))
227+
228+
assert (
229+
"pub groups: ::std::vec::Vec<::std::vec::Vec<::std::sync::Arc<Node>>>,"
230+
in rust_output
231+
)
232+
assert "::std::vec::Vec<::std::vec::Vec<::std::rc::Rc<Node>>>" not in rust_output
233+
assert (
234+
"pub nodes: ::std::collections::HashMap<::std::string::String, "
235+
"::std::collections::HashMap<::std::string::String, ::std::sync::Arc<Node>>>,"
236+
in rust_output
237+
)
238+
assert (
239+
"::std::collections::HashMap<::std::string::String, "
240+
"::std::collections::HashMap<::std::string::String, ::std::rc::Rc<Node>>>"
241+
not in rust_output
242+
)
243+
244+
208245
def test_generated_code_integer_encoding_variants_equivalent():
209246
fdl = dedent(
210247
"""

0 commit comments

Comments
 (0)