From 81b79da90f94c184680036026ccbee06d0ba4dd7 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Thu, 1 Aug 2024 19:40:22 -0700 Subject: [PATCH] fix cryptic error (#3234) * fix cryptic error Signed-off-by: Jess Frazelle * Update types.rs --------- Signed-off-by: Jess Frazelle --- src/wasm-lib/kcl/src/ast/types.rs | 7 ++++++- src/wasm-lib/kcl/src/executor.rs | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/wasm-lib/kcl/src/ast/types.rs b/src/wasm-lib/kcl/src/ast/types.rs index 7dac0b8af..99373a235 100644 --- a/src/wasm-lib/kcl/src/ast/types.rs +++ b/src/wasm-lib/kcl/src/ast/types.rs @@ -1494,9 +1494,14 @@ impl CallExpression { })?; let result = result.ok_or_else(|| { + let mut source_ranges: Vec = vec![self.into()]; + // We want to send the source range of the original function. + if let MemoryItem::Function { meta, .. } = func { + source_ranges = meta.iter().map(|m| m.source_range).collect(); + }; KclError::UndefinedValue(KclErrorDetails { message: format!("Result of user-defined function {} is undefined", fn_name), - source_ranges: vec![self.into()], + source_ranges, }) })?; let result = result.get_value()?; diff --git a/src/wasm-lib/kcl/src/executor.rs b/src/wasm-lib/kcl/src/executor.rs index 9aea47f36..0354c3916 100644 --- a/src/wasm-lib/kcl/src/executor.rs +++ b/src/wasm-lib/kcl/src/executor.rs @@ -2735,6 +2735,22 @@ const bracket = startSketchOn('XY') parse_execute(ast).await.unwrap(); } + #[tokio::test(flavor = "multi_thread")] + async fn test_execute_function_no_return() { + let ast = r#"fn test = (origin) => { + origin +} + +test([0, 0]) +"#; + let result = parse_execute(ast).await; + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().to_string(), + r#"undefined value: KclErrorDetails { source_ranges: [SourceRange([10, 34])], message: "Result of user-defined function test is undefined" }"#.to_owned() + ); + } + #[tokio::test(flavor = "multi_thread")] async fn test_math_doubly_nested_parens() { let ast = r#"const sigmaAllow = 35000 // psi