Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen: Fix nullable array in function params #1551

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading