Skip to content

Commit b52ade2

Browse files
authored
Fix to not coerce nested arrays to single level when type is any (#7787)
1 parent 2f360a1 commit b52ade2

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

rust/kcl-lib/src/execution/mod.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,12 @@ mod tests {
14521452
use pretty_assertions::assert_eq;
14531453

14541454
use super::*;
1455-
use crate::{ModuleId, errors::KclErrorDetails, execution::memory::Stack};
1455+
use crate::{
1456+
ModuleId,
1457+
errors::KclErrorDetails,
1458+
exec::NumericType,
1459+
execution::{memory::Stack, types::RuntimeType},
1460+
};
14561461

14571462
/// Convenience function to get a JSON value from memory and unwrap.
14581463
#[track_caller]
@@ -1943,6 +1948,40 @@ clone001 = map(extrudes, f = clone)
19431948
parse_execute(ast).await.unwrap();
19441949
}
19451950

1951+
#[tokio::test(flavor = "multi_thread")]
1952+
async fn test_array_reduce_nested_array() {
1953+
let code = r#"
1954+
fn id(@el, accum) { return accum }
1955+
1956+
answer = reduce([], initial=[[[0,0]]], f=id)
1957+
"#;
1958+
let result = parse_execute(code).await.unwrap();
1959+
assert_eq!(
1960+
mem_get_json(result.exec_state.stack(), result.mem_env, "answer"),
1961+
KclValue::HomArray {
1962+
value: vec![KclValue::HomArray {
1963+
value: vec![KclValue::HomArray {
1964+
value: vec![
1965+
KclValue::Number {
1966+
value: 0.0,
1967+
ty: NumericType::default(),
1968+
meta: vec![SourceRange::new(69, 70, Default::default()).into()],
1969+
},
1970+
KclValue::Number {
1971+
value: 0.0,
1972+
ty: NumericType::default(),
1973+
meta: vec![SourceRange::new(71, 72, Default::default()).into()],
1974+
}
1975+
],
1976+
ty: RuntimeType::any(),
1977+
}],
1978+
ty: RuntimeType::any(),
1979+
}],
1980+
ty: RuntimeType::any(),
1981+
}
1982+
);
1983+
}
1984+
19461985
#[tokio::test(flavor = "multi_thread")]
19471986
async fn test_zero_param_fn() {
19481987
let ast = r#"sigmaAllow = 35000 // psi

rust/kcl-lib/src/execution/types.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,12 +1090,18 @@ impl KclValue {
10901090
exec_state: &mut ExecState,
10911091
) -> Result<KclValue, CoercionError> {
10921092
match self {
1093-
KclValue::Tuple { value, .. } if value.len() == 1 && !matches!(ty, RuntimeType::Tuple(..)) => {
1093+
KclValue::Tuple { value, .. }
1094+
if value.len() == 1
1095+
&& !matches!(ty, RuntimeType::Primitive(PrimitiveType::Any) | RuntimeType::Tuple(..)) =>
1096+
{
10941097
if let Ok(coerced) = value[0].coerce(ty, convert_units, exec_state) {
10951098
return Ok(coerced);
10961099
}
10971100
}
1098-
KclValue::HomArray { value, .. } if value.len() == 1 && !matches!(ty, RuntimeType::Array(..)) => {
1101+
KclValue::HomArray { value, .. }
1102+
if value.len() == 1
1103+
&& !matches!(ty, RuntimeType::Primitive(PrimitiveType::Any) | RuntimeType::Array(..)) =>
1104+
{
10991105
if let Ok(coerced) = value[0].coerce(ty, convert_units, exec_state) {
11001106
return Ok(coerced);
11011107
}

0 commit comments

Comments
 (0)