Skip to content

Commit

Permalink
codegen: Fix nullable array in function params
Browse files Browse the repository at this point in the history
Co-authored-by: Andy Russell <arussell123@gmail.com>
Fixes #1133
  • Loading branch information
bilelmoussaoui committed Feb 27, 2024
1 parent 9ebc443 commit cb94c81
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/analysis/function_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub enum TransformationType {
array_name: String,
array_length_name: String,
array_length_type: String,
nullable: library::Nullable,
},
IntoRaw(String),
ToSome(String),
Expand Down Expand Up @@ -176,7 +177,13 @@ impl Parameters {
let transformation = Transformation {
ind_c,
ind_rust: None,
transformation_type: get_length_type(env, "", &c_par.name, c_par.typ),
transformation_type: get_length_type(
env,
"",
&c_par.name,
c_par.typ,
library::Nullable(false),
),
};
self.transformations.push(transformation);
}
Expand Down Expand Up @@ -301,7 +308,13 @@ pub fn analyze(
let transformation = Transformation {
ind_c,
ind_rust: None,
transformation_type: get_length_type(env, &array_name, &par.name, typ),
transformation_type: get_length_type(
env,
&array_name,
&par.name,
typ,
array_par.nullable,
),
};
parameters.transformations.push(transformation);
}
Expand Down Expand Up @@ -448,12 +461,14 @@ fn get_length_type(
array_name: &str,
length_name: &str,
length_typ: TypeId,
nullable: library::Nullable,
) -> TransformationType {
let array_length_type = RustType::try_new(env, length_typ).into_string();
TransformationType::Length {
array_name: array_name.to_string(),
array_length_name: length_name.to_string(),
array_length_type,
nullable,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/analysis/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl<'env> RustTypeBuilder<'env> {
};

if let Some(s) = array_type {
skip_option = true;
skip_option = self.direction == ParameterDirection::Return;
if self.ref_mode.is_ref() {
Ok(format!("[{s}]").into())
} else {
Expand Down
10 changes: 9 additions & 1 deletion src/codegen/function_body_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,11 +944,19 @@ impl Builder {
if let TransformationType::Length {
ref array_name,
ref array_length_name,
nullable,
..
} = trans.transformation_type
{
if let In = self.parameters[trans.ind_c] {
let value = Chunk::Custom(format!("{array_name}.len() as _"));
let value = if !*nullable {
Chunk::Custom(format!("{}.len() as _", array_name))
} else {
Chunk::Custom(format!(
"{}.map(|arr| arr.len()).unwrap_or(0) as _",
array_name
))
};
chunks.push(Chunk::Let {
name: array_length_name.clone(),
is_mut: false,
Expand Down

0 comments on commit cb94c81

Please sign in to comment.