Skip to content

Commit

Permalink
Merge pull request #1363 from froydnj/froydnj-optional-location-fix
Browse files Browse the repository at this point in the history
[rust] fix accessing optional locations
  • Loading branch information
kddnewton authored Aug 30, 2023
2 parents 4ebd956 + a1ed3d8 commit 57c5a6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rust/yarp/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ fn write_node(file: &mut File, node: &Node) -> Result<(), Box<dyn std::error::Er
NodeFieldType::OptionalLocation => {
writeln!(file, " pub fn {}(&self) -> Option<Location<'pr>> {{", field.name)?;
writeln!(file, " let pointer: *mut yp_location_t = unsafe {{ &mut (*self.pointer).{} }};", field.name)?;
writeln!(file, " if pointer.is_null() {{")?;
writeln!(file, " let start = unsafe {{ (*pointer).start }};")?;
writeln!(file, " if start.is_null() {{")?;
writeln!(file, " None")?;
writeln!(file, " }} else {{")?;
writeln!(file, " Some(Location {{ pointer: unsafe {{ NonNull::new_unchecked(pointer) }}, marker: PhantomData }})")?;
Expand Down
36 changes: 36 additions & 0 deletions rust/yarp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,40 @@ mod tests {

assert_eq!(locals[0].as_slice(), b"x");
}

#[test]
fn optional_loc_test() {
let source = r#"
module Example
x = call_func(3, 4)
y = x.call_func 5, 6
end
"#;
let result = parse(source.as_ref());

let node = result.node();
let module = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
let module = module.as_module_node().unwrap();
let body = module.body();
let writes = body.iter().next().unwrap().as_statements_node().unwrap().body().iter().collect::<Vec<_>>();
assert_eq!(writes.len(), 2);

let asgn = &writes[0];
let call = asgn.as_local_variable_write_node().unwrap().value();
let call = call.as_call_node().unwrap();

let operator_loc = call.operator_loc();
assert!(operator_loc.is_none());
let closing_loc = call.closing_loc();
assert!(closing_loc.is_some());

let asgn = &writes[1];
let call = asgn.as_local_variable_write_node().unwrap().value();
let call = call.as_call_node().unwrap();

let operator_loc = call.operator_loc();
assert!(operator_loc.is_some());
let closing_loc = call.closing_loc();
assert!(closing_loc.is_none());
}
}

0 comments on commit 57c5a6e

Please sign in to comment.