kcl_lib/execution/
fn_call.rs

1use async_recursion::async_recursion;
2use indexmap::IndexMap;
3
4use crate::{
5    CompilationError, NodePath, SourceRange,
6    errors::{KclError, KclErrorDetails},
7    execution::{
8        BodyType, ExecState, ExecutorContext, KclValue, Metadata, StatementKind, TagEngineInfo, TagIdentifier,
9        annotations,
10        cad_op::{Group, OpArg, OpKclValue, Operation},
11        kcl_value::{FunctionBody, FunctionSource},
12        memory,
13        types::RuntimeType,
14    },
15    parsing::ast::types::{CallExpressionKw, Node, Type},
16};
17
18#[derive(Debug, Clone)]
19pub struct Args<Status: ArgsStatus = Desugared> {
20    /// Unlabeled keyword args. Currently only the first formal arg can be unlabeled.
21    /// If the argument was a local variable, then the first element of the tuple is its name
22    /// which may be used to treat this arg as a labelled arg.
23    pub unlabeled: Vec<(Option<String>, Arg)>,
24    /// Labeled args.
25    pub labeled: IndexMap<String, Arg>,
26    pub source_range: SourceRange,
27    pub ctx: ExecutorContext,
28    /// If this call happens inside a pipe (|>) expression, this holds the LHS of that |>.
29    /// Otherwise it's None.
30    pub pipe_value: Option<Arg>,
31    _status: std::marker::PhantomData<Status>,
32}
33
34pub trait ArgsStatus: std::fmt::Debug + Clone {}
35
36#[derive(Debug, Clone)]
37pub struct Sugary;
38impl ArgsStatus for Sugary {}
39
40// Invariants guaranteed by the `Desugared` status:
41// - There is either 0 or 1 unlabeled arguments
42// - Any lableled args are in the labeled map, and not the unlabeled Vec.
43// - The arguments match the type signature of the function exactly
44// - pipe_value.is_none()
45#[derive(Debug, Clone)]
46pub struct Desugared;
47impl ArgsStatus for Desugared {}
48
49impl Args<Sugary> {
50    /// Collect the given keyword arguments.
51    pub fn new(
52        labeled: IndexMap<String, Arg>,
53        unlabeled: Vec<(Option<String>, Arg)>,
54        source_range: SourceRange,
55        exec_state: &mut ExecState,
56        ctx: ExecutorContext,
57    ) -> Args<Sugary> {
58        Args {
59            labeled,
60            unlabeled,
61            source_range,
62            ctx,
63            pipe_value: exec_state.pipe_value().map(|v| Arg::new(v.clone(), source_range)),
64            _status: std::marker::PhantomData,
65        }
66    }
67}
68
69impl<Status: ArgsStatus> Args<Status> {
70    /// How many arguments are there?
71    pub fn len(&self) -> usize {
72        self.labeled.len() + self.unlabeled.len()
73    }
74
75    /// Are there no arguments?
76    pub fn is_empty(&self) -> bool {
77        self.labeled.is_empty() && self.unlabeled.is_empty()
78    }
79}
80
81impl Args<Desugared> {
82    pub fn new_no_args(source_range: SourceRange, ctx: ExecutorContext) -> Args {
83        Args {
84            unlabeled: Default::default(),
85            labeled: Default::default(),
86            source_range,
87            ctx,
88            pipe_value: None,
89            _status: std::marker::PhantomData,
90        }
91    }
92
93    /// Get the unlabeled keyword argument. If not set, returns None.
94    pub(crate) fn unlabeled_kw_arg_unconverted(&self) -> Option<&Arg> {
95        self.unlabeled.first().map(|(_, a)| a)
96    }
97}
98
99#[derive(Debug, Clone)]
100pub struct Arg {
101    /// The evaluated argument.
102    pub value: KclValue,
103    /// The source range of the unevaluated argument.
104    pub source_range: SourceRange,
105}
106
107impl Arg {
108    pub fn new(value: KclValue, source_range: SourceRange) -> Self {
109        Self { value, source_range }
110    }
111
112    pub fn synthetic(value: KclValue) -> Self {
113        Self {
114            value,
115            source_range: SourceRange::synthetic(),
116        }
117    }
118
119    pub fn source_ranges(&self) -> Vec<SourceRange> {
120        vec![self.source_range]
121    }
122}
123
124impl Node<CallExpressionKw> {
125    #[async_recursion]
126    pub async fn execute(&self, exec_state: &mut ExecState, ctx: &ExecutorContext) -> Result<KclValue, KclError> {
127        let fn_name = &self.callee;
128        let callsite: SourceRange = self.into();
129
130        // Clone the function so that we can use a mutable reference to
131        // exec_state.
132        let func: KclValue = fn_name.get_result(exec_state, ctx).await?.clone();
133
134        let Some(fn_src) = func.as_function() else {
135            return Err(KclError::new_semantic(KclErrorDetails::new(
136                "cannot call this because it isn't a function".to_string(),
137                vec![callsite],
138            )));
139        };
140
141        // Build a hashmap from argument labels to the final evaluated values.
142        let mut fn_args = IndexMap::with_capacity(self.arguments.len());
143        let mut unlabeled = Vec::new();
144
145        // Evaluate the unlabeled first param, if any exists.
146        if let Some(ref arg_expr) = self.unlabeled {
147            let source_range = SourceRange::from(arg_expr.clone());
148            let metadata = Metadata { source_range };
149            let value = ctx
150                .execute_expr(arg_expr, exec_state, &metadata, &[], StatementKind::Expression)
151                .await?;
152
153            let label = arg_expr.ident_name().map(str::to_owned);
154
155            unlabeled.push((label, Arg::new(value, source_range)))
156        }
157
158        for arg_expr in &self.arguments {
159            let source_range = SourceRange::from(arg_expr.arg.clone());
160            let metadata = Metadata { source_range };
161            let value = ctx
162                .execute_expr(&arg_expr.arg, exec_state, &metadata, &[], StatementKind::Expression)
163                .await?;
164            let arg = Arg::new(value, source_range);
165            match &arg_expr.label {
166                Some(l) => {
167                    fn_args.insert(l.name.clone(), arg);
168                }
169                None => {
170                    unlabeled.push((arg_expr.arg.ident_name().map(str::to_owned), arg));
171                }
172            }
173        }
174
175        let args = Args::new(fn_args, unlabeled, callsite, exec_state, ctx.clone());
176
177        let return_value = fn_src
178            .call_kw(Some(fn_name.to_string()), exec_state, ctx, args, callsite)
179            .await
180            .map_err(|e| {
181                // Add the call expression to the source ranges.
182                //
183                // TODO: Use the name that the function was defined
184                // with, not the identifier it was used with.
185                e.add_unwind_location(Some(fn_name.name.name.clone()), callsite)
186            })?;
187
188        let result = return_value.ok_or_else(move || {
189            let mut source_ranges: Vec<SourceRange> = vec![callsite];
190            // We want to send the source range of the original function.
191            if let KclValue::Function { meta, .. } = func {
192                source_ranges = meta.iter().map(|m| m.source_range).collect();
193            };
194            KclError::new_undefined_value(
195                KclErrorDetails::new(
196                    format!("Result of user-defined function {fn_name} is undefined"),
197                    source_ranges,
198                ),
199                None,
200            )
201        })?;
202
203        Ok(result)
204    }
205}
206
207impl FunctionSource {
208    pub async fn call_kw(
209        &self,
210        fn_name: Option<String>,
211        exec_state: &mut ExecState,
212        ctx: &ExecutorContext,
213        args: Args<Sugary>,
214        callsite: SourceRange,
215    ) -> Result<Option<KclValue>, KclError> {
216        if self.deprecated {
217            exec_state.warn(
218                CompilationError::err(
219                    callsite,
220                    format!(
221                        "{} is deprecated, see the docs for a recommended replacement",
222                        match &fn_name {
223                            Some(n) => format!("`{n}`"),
224                            None => "This function".to_owned(),
225                        }
226                    ),
227                ),
228                annotations::WARN_DEPRECATED,
229            );
230        }
231        if self.experimental {
232            exec_state.warn_experimental(
233                &match &fn_name {
234                    Some(n) => format!("`{n}`"),
235                    None => "This function".to_owned(),
236                },
237                callsite,
238            );
239        }
240
241        let args = type_check_params_kw(fn_name.as_deref(), self, args, exec_state)?;
242
243        // Don't early return until the stack frame is popped!
244        self.body.prep_mem(exec_state);
245
246        // Some function calls might get added to the feature tree.
247        // We do this by adding an "operation".
248
249        // Don't add operations if the KCL code being executed is
250        // just the KCL stdlib calling other KCL stdlib,
251        // because the stdlib internals aren't relevant to users,
252        // that would just be pointless noise.
253        //
254        // Do add operations if the KCL being executed is
255        // user-defined, or the calling code is user-defined,
256        // because that's relevant to the user.
257        let would_trace_stdlib_internals = exec_state.mod_local.inside_stdlib && self.is_std;
258        // self.include_in_feature_tree is set by the KCL annotation `@(feature_tree = true)`.
259        let should_track_operation = !would_trace_stdlib_internals && self.include_in_feature_tree;
260        let op = if should_track_operation {
261            let op_labeled_args = args
262                .labeled
263                .iter()
264                .map(|(k, arg)| (k.clone(), OpArg::new(OpKclValue::from(&arg.value), arg.source_range)))
265                .collect();
266
267            // If you're calling a stdlib function, track that call as an operation.
268            if self.is_std {
269                Some(Operation::StdLibCall {
270                    name: fn_name.clone().unwrap_or_else(|| "unknown function".to_owned()),
271                    unlabeled_arg: args
272                        .unlabeled_kw_arg_unconverted()
273                        .map(|arg| OpArg::new(OpKclValue::from(&arg.value), arg.source_range)),
274                    labeled_args: op_labeled_args,
275                    node_path: NodePath::placeholder(),
276                    source_range: callsite,
277                    is_error: false,
278                })
279            } else {
280                // Otherwise, you're calling a user-defined function, track that call as an operation.
281                exec_state.push_op(Operation::GroupBegin {
282                    group: Group::FunctionCall {
283                        name: fn_name.clone(),
284                        function_source_range: self.ast.as_source_range(),
285                        unlabeled_arg: args
286                            .unlabeled_kw_arg_unconverted()
287                            .map(|arg| OpArg::new(OpKclValue::from(&arg.value), arg.source_range)),
288                        labeled_args: op_labeled_args,
289                    },
290                    node_path: NodePath::placeholder(),
291                    source_range: callsite,
292                });
293
294                None
295            }
296        } else {
297            None
298        };
299
300        let is_calling_into_stdlib = match &self.body {
301            FunctionBody::Rust(_) => true,
302            FunctionBody::Kcl(_) => self.is_std,
303        };
304
305        let prev_inside_stdlib = std::mem::replace(&mut exec_state.mod_local.inside_stdlib, is_calling_into_stdlib);
306        // Do not early return via ? or something until we've
307        // - put this `prev_inside_stdlib` value back.
308        // - called the pop_env.
309        let mut result = match &self.body {
310            FunctionBody::Rust(f) => f(exec_state, args).await.map(Some),
311            FunctionBody::Kcl(_) => {
312                if let Err(e) = assign_args_to_params_kw(self, args, exec_state) {
313                    exec_state.mod_local.inside_stdlib = prev_inside_stdlib;
314                    exec_state.mut_stack().pop_env();
315                    return Err(e);
316                }
317
318                ctx.exec_block(&self.ast.body, exec_state, BodyType::Block)
319                    .await
320                    .map(|_| {
321                        exec_state
322                            .stack()
323                            .get(memory::RETURN_NAME, self.ast.as_source_range())
324                            .ok()
325                            .cloned()
326                    })
327            }
328        };
329        exec_state.mod_local.inside_stdlib = prev_inside_stdlib;
330        exec_state.mut_stack().pop_env();
331
332        if should_track_operation {
333            if let Some(mut op) = op {
334                op.set_std_lib_call_is_error(result.is_err());
335                // Track call operation.  We do this after the call
336                // since things like patternTransform may call user code
337                // before running, and we will likely want to use the
338                // return value. The call takes ownership of the args,
339                // so we need to build the op before the call.
340                exec_state.push_op(op);
341            } else if !is_calling_into_stdlib {
342                exec_state.push_op(Operation::GroupEnd);
343            }
344        }
345
346        if self.is_std
347            && let Ok(Some(result)) = &mut result
348        {
349            update_memory_for_tags_of_geometry(result, exec_state)?;
350        }
351
352        coerce_result_type(result, self, exec_state)
353    }
354}
355
356impl FunctionBody {
357    fn prep_mem(&self, exec_state: &mut ExecState) {
358        match self {
359            FunctionBody::Rust(_) => exec_state.mut_stack().push_new_root_env(true),
360            FunctionBody::Kcl(memory) => exec_state.mut_stack().push_new_env_for_call(*memory),
361        }
362    }
363}
364
365fn update_memory_for_tags_of_geometry(result: &mut KclValue, exec_state: &mut ExecState) -> Result<(), KclError> {
366    // If the return result is a sketch or solid, we want to update the
367    // memory for the tags of the group.
368    // TODO: This could probably be done in a better way, but as of now this was my only idea
369    // and it works.
370    match result {
371        KclValue::Sketch { value } => {
372            for (name, tag) in value.tags.iter() {
373                if exec_state.stack().cur_frame_contains(name) {
374                    exec_state.mut_stack().update(name, |v, _| {
375                        v.as_mut_tag().unwrap().merge_info(tag);
376                    });
377                } else {
378                    exec_state
379                        .mut_stack()
380                        .add(
381                            name.to_owned(),
382                            KclValue::TagIdentifier(Box::new(tag.clone())),
383                            SourceRange::default(),
384                        )
385                        .unwrap();
386                }
387            }
388        }
389        KclValue::Solid { value } => {
390            for v in &value.value {
391                if let Some(tag) = v.get_tag() {
392                    // Get the past tag and update it.
393                    let tag_id = if let Some(t) = value.sketch.tags.get(&tag.name) {
394                        let mut t = t.clone();
395                        let Some(info) = t.get_cur_info() else {
396                            return Err(KclError::new_internal(KclErrorDetails::new(
397                                format!("Tag {} does not have path info", tag.name),
398                                vec![tag.into()],
399                            )));
400                        };
401
402                        let mut info = info.clone();
403                        info.surface = Some(v.clone());
404                        info.sketch = value.id;
405                        t.info.push((exec_state.stack().current_epoch(), info));
406                        t
407                    } else {
408                        // It's probably a fillet or a chamfer.
409                        // Initialize it.
410                        TagIdentifier {
411                            value: tag.name.clone(),
412                            info: vec![(
413                                exec_state.stack().current_epoch(),
414                                TagEngineInfo {
415                                    id: v.get_id(),
416                                    surface: Some(v.clone()),
417                                    path: None,
418                                    sketch: value.id,
419                                },
420                            )],
421                            meta: vec![Metadata {
422                                source_range: tag.clone().into(),
423                            }],
424                        }
425                    };
426
427                    // update the sketch tags.
428                    value.sketch.merge_tags(Some(&tag_id).into_iter());
429
430                    if exec_state.stack().cur_frame_contains(&tag.name) {
431                        exec_state.mut_stack().update(&tag.name, |v, _| {
432                            v.as_mut_tag().unwrap().merge_info(&tag_id);
433                        });
434                    } else {
435                        exec_state
436                            .mut_stack()
437                            .add(
438                                tag.name.clone(),
439                                KclValue::TagIdentifier(Box::new(tag_id)),
440                                SourceRange::default(),
441                            )
442                            .unwrap();
443                    }
444                }
445            }
446
447            // Find the stale sketch in memory and update it.
448            if !value.sketch.tags.is_empty() {
449                let sketches_to_update: Vec<_> = exec_state
450                    .stack()
451                    .find_keys_in_current_env(|v| match v {
452                        KclValue::Sketch { value: sk } => sk.original_id == value.sketch.original_id,
453                        _ => false,
454                    })
455                    .cloned()
456                    .collect();
457
458                for k in sketches_to_update {
459                    exec_state.mut_stack().update(&k, |v, _| {
460                        let sketch = v.as_mut_sketch().unwrap();
461                        sketch.merge_tags(value.sketch.tags.values());
462                    });
463                }
464            }
465        }
466        KclValue::Tuple { value, .. } | KclValue::HomArray { value, .. } => {
467            for v in value {
468                update_memory_for_tags_of_geometry(v, exec_state)?;
469            }
470        }
471        _ => {}
472    }
473    Ok(())
474}
475
476fn type_err_str(expected: &Type, found: &KclValue, source_range: &SourceRange, exec_state: &mut ExecState) -> String {
477    fn strip_backticks(s: &str) -> &str {
478        let mut result = s;
479        if s.starts_with('`') {
480            result = &result[1..]
481        }
482        if s.ends_with('`') {
483            result = &result[..result.len() - 1]
484        }
485        result
486    }
487
488    let expected_human = expected.human_friendly_type();
489    let expected_ty = expected.to_string();
490    let expected_str =
491        if expected_human == expected_ty || expected_human == format!("a value with type `{expected_ty}`") {
492            format!("a value with type `{expected_ty}`")
493        } else {
494            format!("{expected_human} (`{expected_ty}`)")
495        };
496    let found_human = found.human_friendly_type();
497    let found_ty = found.principal_type_string();
498    let found_str = if found_human == found_ty || found_human == format!("a {}", strip_backticks(&found_ty)) {
499        format!("a value with type {found_ty}")
500    } else {
501        format!("{found_human} (with type {found_ty})")
502    };
503
504    let mut result = format!("{expected_str}, but found {found_str}.");
505
506    if found.is_unknown_number() {
507        exec_state.clear_units_warnings(source_range);
508        result.push_str("\nThe found value is a number but has incomplete units information. You can probably fix this error by specifying the units using type ascription, e.g., `len: mm` or `(a * b): deg`.");
509    }
510
511    result
512}
513
514fn type_check_params_kw(
515    fn_name: Option<&str>,
516    fn_def: &FunctionSource,
517    mut args: Args<Sugary>,
518    exec_state: &mut ExecState,
519) -> Result<Args<Desugared>, KclError> {
520    let mut result = Args::new_no_args(args.source_range, args.ctx);
521
522    // If it's possible the input arg was meant to be labelled and we probably don't want to use
523    // it as the input arg, then treat it as labelled.
524    if let Some((Some(label), _)) = args.unlabeled.first()
525        && args.unlabeled.len() == 1
526        && (fn_def.input_arg.is_none() || args.pipe_value.is_some())
527        && fn_def.named_args.iter().any(|p| p.0 == label)
528        && !args.labeled.contains_key(label)
529    {
530        let (label, arg) = args.unlabeled.pop().unwrap();
531        args.labeled.insert(label.unwrap(), arg);
532    }
533
534    // Apply the `a == a: a` shorthand by desugaring unlabeled args into labeled ones.
535    let (labeled_unlabeled, unlabeled_unlabeled) = args.unlabeled.into_iter().partition(|(l, _)| {
536        if let Some(l) = l
537            && fn_def.named_args.contains_key(l)
538            && !args.labeled.contains_key(l)
539        {
540            true
541        } else {
542            false
543        }
544    });
545    args.unlabeled = unlabeled_unlabeled;
546    for (l, arg) in labeled_unlabeled {
547        let previous = args.labeled.insert(l.unwrap(), arg);
548        debug_assert!(previous.is_none());
549    }
550
551    if let Some((name, ty)) = &fn_def.input_arg {
552        // Expecting an input arg
553
554        if args.unlabeled.is_empty() {
555            // No args provided
556
557            if let Some(pipe) = args.pipe_value {
558                // But there is a pipeline
559                result.unlabeled = vec![(None, pipe)];
560            } else if let Some(arg) = args.labeled.swap_remove(name) {
561                // Mistakenly labelled
562                exec_state.err(CompilationError::err(
563                    arg.source_range,
564                    format!(
565                        "{} expects an unlabeled first argument (`@{name}`), but it is labelled in the call. You might try removing the `{name} = `",
566                        fn_name
567                            .map(|n| format!("The function `{n}`"))
568                            .unwrap_or_else(|| "This function".to_owned()),
569                    ),
570                ));
571                result.unlabeled = vec![(Some(name.clone()), arg)];
572            } else {
573                // Just missing
574                return Err(KclError::new_argument(KclErrorDetails::new(
575                    "This function expects an unlabeled first parameter, but you haven't passed it one.".to_owned(),
576                    fn_def.ast.as_source_ranges(),
577                )));
578            }
579        } else if args.unlabeled.len() == 1 {
580            let mut arg = args.unlabeled.pop().unwrap().1;
581            if let Some(ty) = ty {
582                let rty = RuntimeType::from_parsed(ty.clone(), exec_state, arg.source_range, false)
583                    .map_err(|e| KclError::new_semantic(e.into()))?;
584                arg.value = arg.value.coerce(&rty, true, exec_state).map_err(|_| {
585                    KclError::new_argument(KclErrorDetails::new(
586                        format!(
587                            "The input argument of {} requires {}",
588                            fn_name
589                                .map(|n| format!("`{n}`"))
590                                .unwrap_or_else(|| "this function".to_owned()),
591                            type_err_str(ty, &arg.value, &arg.source_range, exec_state),
592                        ),
593                        vec![arg.source_range],
594                    ))
595                })?;
596            }
597            result.unlabeled = vec![(None, arg)]
598        } else {
599            // Multiple unlabelled args
600
601            // Try to un-spread args into an array
602            if let Some(Type::Array { len, .. }) = ty {
603                if len.satisfied(args.unlabeled.len(), false).is_none() {
604                    exec_state.err(CompilationError::err(
605                        args.source_range,
606                        format!(
607                            "{} expects an array input argument with {} elements",
608                            fn_name
609                                .map(|n| format!("The function `{n}`"))
610                                .unwrap_or_else(|| "This function".to_owned()),
611                            len.human_friendly_type(),
612                        ),
613                    ));
614                }
615
616                let source_range = SourceRange::merge(args.unlabeled.iter().map(|(_, a)| a.source_range));
617                exec_state.warn_experimental("array input arguments", source_range);
618                result.unlabeled = vec![(
619                    None,
620                    Arg {
621                        source_range,
622                        value: KclValue::HomArray {
623                            value: args.unlabeled.drain(..).map(|(_, a)| a.value).collect(),
624                            ty: RuntimeType::any(),
625                        },
626                    },
627                )]
628            }
629        }
630    }
631
632    // Either we didn't move the arg above, or we're not expecting one.
633    if !args.unlabeled.is_empty() {
634        // Not expecting an input arg, but found one or more
635        let actuals = args.labeled.keys();
636        let formals: Vec<_> = fn_def
637            .named_args
638            .keys()
639            .filter_map(|name| {
640                if actuals.clone().any(|a| a == name) {
641                    return None;
642                }
643
644                Some(format!("`{name}`"))
645            })
646            .collect();
647
648        let suggestion = if formals.is_empty() {
649            String::new()
650        } else {
651            format!("; suggested labels: {}", formals.join(", "))
652        };
653
654        let mut errors = args.unlabeled.iter().map(|(_, arg)| {
655            CompilationError::err(
656                arg.source_range,
657                format!("This argument needs a label, but it doesn't have one{suggestion}"),
658            )
659        });
660
661        let first = errors.next().unwrap();
662        errors.for_each(|e| exec_state.err(e));
663
664        return Err(KclError::new_argument(first.into()));
665    }
666
667    for (label, mut arg) in args.labeled {
668        match fn_def.named_args.get(&label) {
669            Some((def, ty)) => {
670                // For optional args, passing None should be the same as not passing an arg.
671                if !(def.is_some() && matches!(arg.value, KclValue::KclNone { .. })) {
672                    if let Some(ty) = ty {
673                        let rty = RuntimeType::from_parsed(ty.clone(), exec_state, arg.source_range, false)
674                            .map_err(|e| KclError::new_semantic(e.into()))?;
675                        arg.value = arg
676                                .value
677                                .coerce(
678                                    &rty,
679                                    true,
680                                    exec_state,
681                                )
682                                .map_err(|e| {
683                                    let mut message = format!(
684                                        "{label} requires {}",
685                                        type_err_str(ty, &arg.value, &arg.source_range, exec_state),
686                                    );
687                                    if let Some(ty) = e.explicit_coercion {
688                                        // TODO if we have access to the AST for the argument we could choose which example to suggest.
689                                        message = format!("{message}\n\nYou may need to add information about the type of the argument, for example:\n  using a numeric suffix: `42{ty}`\n  or using type ascription: `foo(): {ty}`");
690                                    }
691                                    KclError::new_argument(KclErrorDetails::new(
692                                        message,
693                                        vec![arg.source_range],
694                                    ))
695                                })?;
696                    }
697                    result.labeled.insert(label, arg);
698                }
699            }
700            None => {
701                exec_state.err(CompilationError::err(
702                    arg.source_range,
703                    format!(
704                        "`{label}` is not an argument of {}",
705                        fn_name
706                            .map(|n| format!("`{n}`"))
707                            .unwrap_or_else(|| "this function".to_owned()),
708                    ),
709                ));
710            }
711        }
712    }
713
714    Ok(result)
715}
716
717fn assign_args_to_params_kw(
718    fn_def: &FunctionSource,
719    args: Args<Desugared>,
720    exec_state: &mut ExecState,
721) -> Result<(), KclError> {
722    // Add the arguments to the memory.  A new call frame should have already
723    // been created.
724    let source_ranges = fn_def.ast.as_source_ranges();
725
726    for (name, (default, _)) in fn_def.named_args.iter() {
727        let arg = args.labeled.get(name);
728        match arg {
729            Some(arg) => {
730                exec_state.mut_stack().add(
731                    name.clone(),
732                    arg.value.clone(),
733                    arg.source_ranges().pop().unwrap_or(SourceRange::synthetic()),
734                )?;
735            }
736            None => match default {
737                Some(default_val) => {
738                    let value = KclValue::from_default_param(default_val.clone(), exec_state);
739                    exec_state
740                        .mut_stack()
741                        .add(name.clone(), value, default_val.source_range())?;
742                }
743                None => {
744                    return Err(KclError::new_argument(KclErrorDetails::new(
745                        format!("This function requires a parameter {name}, but you haven't passed it one."),
746                        source_ranges,
747                    )));
748                }
749            },
750        }
751    }
752
753    if let Some((param_name, _)) = &fn_def.input_arg {
754        let Some(unlabeled) = args.unlabeled_kw_arg_unconverted() else {
755            debug_assert!(false, "Bad args");
756            return Err(KclError::new_internal(KclErrorDetails::new(
757                "Desugared arguments are inconsistent".to_owned(),
758                source_ranges,
759            )));
760        };
761        exec_state.mut_stack().add(
762            param_name.clone(),
763            unlabeled.value.clone(),
764            unlabeled.source_ranges().pop().unwrap_or(SourceRange::synthetic()),
765        )?;
766    }
767
768    Ok(())
769}
770
771fn coerce_result_type(
772    result: Result<Option<KclValue>, KclError>,
773    fn_def: &FunctionSource,
774    exec_state: &mut ExecState,
775) -> Result<Option<KclValue>, KclError> {
776    if let Ok(Some(val)) = result {
777        if let Some(ret_ty) = &fn_def.return_type {
778            let ty = RuntimeType::from_parsed(ret_ty.inner.clone(), exec_state, ret_ty.as_source_range(), false)
779                .map_err(|e| KclError::new_semantic(e.into()))?;
780            let val = val.coerce(&ty, true, exec_state).map_err(|_| {
781                KclError::new_type(KclErrorDetails::new(
782                    format!(
783                        "This function requires its result to be {}",
784                        type_err_str(ret_ty, &val, &(&val).into(), exec_state)
785                    ),
786                    ret_ty.as_source_ranges(),
787                ))
788            })?;
789            Ok(Some(val))
790        } else {
791            Ok(Some(val))
792        }
793    } else {
794        result
795    }
796}
797
798#[cfg(test)]
799mod test {
800    use std::sync::Arc;
801
802    use super::*;
803    use crate::{
804        execution::{ContextType, EnvironmentRef, memory::Stack, parse_execute, types::NumericType},
805        parsing::ast::types::{DefaultParamVal, FunctionExpression, Identifier, Parameter, Program},
806    };
807
808    #[tokio::test(flavor = "multi_thread")]
809    async fn test_assign_args_to_params() {
810        // Set up a little framework for this test.
811        fn mem(number: usize) -> KclValue {
812            KclValue::Number {
813                value: number as f64,
814                ty: NumericType::count(),
815                meta: Default::default(),
816            }
817        }
818        fn ident(s: &'static str) -> Node<Identifier> {
819            Node::no_src(Identifier {
820                name: s.to_owned(),
821                digest: None,
822            })
823        }
824        fn opt_param(s: &'static str) -> Parameter {
825            Parameter {
826                identifier: ident(s),
827                param_type: None,
828                default_value: Some(DefaultParamVal::none()),
829                labeled: true,
830                digest: None,
831            }
832        }
833        fn req_param(s: &'static str) -> Parameter {
834            Parameter {
835                identifier: ident(s),
836                param_type: None,
837                default_value: None,
838                labeled: true,
839                digest: None,
840            }
841        }
842        fn additional_program_memory(items: &[(String, KclValue)]) -> Stack {
843            let mut program_memory = Stack::new_for_tests();
844            for (name, item) in items {
845                program_memory
846                    .add(name.clone(), item.clone(), SourceRange::default())
847                    .unwrap();
848            }
849            program_memory
850        }
851        // Declare the test cases.
852        for (test_name, params, args, expected) in [
853            ("empty", Vec::new(), Vec::new(), Ok(additional_program_memory(&[]))),
854            (
855                "all params required, and all given, should be OK",
856                vec![req_param("x")],
857                vec![("x", mem(1))],
858                Ok(additional_program_memory(&[("x".to_owned(), mem(1))])),
859            ),
860            (
861                "all params required, none given, should error",
862                vec![req_param("x")],
863                vec![],
864                Err(KclError::new_argument(KclErrorDetails::new(
865                    "This function requires a parameter x, but you haven't passed it one.".to_owned(),
866                    vec![SourceRange::default()],
867                ))),
868            ),
869            (
870                "all params optional, none given, should be OK",
871                vec![opt_param("x")],
872                vec![],
873                Ok(additional_program_memory(&[("x".to_owned(), KclValue::none())])),
874            ),
875            (
876                "mixed params, too few given",
877                vec![req_param("x"), opt_param("y")],
878                vec![],
879                Err(KclError::new_argument(KclErrorDetails::new(
880                    "This function requires a parameter x, but you haven't passed it one.".to_owned(),
881                    vec![SourceRange::default()],
882                ))),
883            ),
884            (
885                "mixed params, minimum given, should be OK",
886                vec![req_param("x"), opt_param("y")],
887                vec![("x", mem(1))],
888                Ok(additional_program_memory(&[
889                    ("x".to_owned(), mem(1)),
890                    ("y".to_owned(), KclValue::none()),
891                ])),
892            ),
893            (
894                "mixed params, maximum given, should be OK",
895                vec![req_param("x"), opt_param("y")],
896                vec![("x", mem(1)), ("y", mem(2))],
897                Ok(additional_program_memory(&[
898                    ("x".to_owned(), mem(1)),
899                    ("y".to_owned(), mem(2)),
900                ])),
901            ),
902        ] {
903            // Run each test.
904            let func_expr = Node::no_src(FunctionExpression {
905                params,
906                body: Program::empty(),
907                return_type: None,
908                digest: None,
909            });
910            let func_src = FunctionSource::kcl(
911                Box::new(func_expr),
912                EnvironmentRef::dummy(),
913                crate::execution::kcl_value::KclFunctionSourceParams {
914                    is_std: false,
915                    experimental: false,
916                    include_in_feature_tree: false,
917                },
918            );
919            let labeled = args
920                .iter()
921                .map(|(name, value)| {
922                    let arg = Arg::new(value.clone(), SourceRange::default());
923                    ((*name).to_owned(), arg)
924                })
925                .collect::<IndexMap<_, _>>();
926            let exec_ctxt = ExecutorContext {
927                engine: Arc::new(Box::new(crate::engine::conn_mock::EngineConnection::new().unwrap())),
928                fs: Arc::new(crate::fs::FileManager::new()),
929                settings: Default::default(),
930                context_type: ContextType::Mock,
931            };
932            let mut exec_state = ExecState::new(&exec_ctxt);
933            exec_state.mod_local.stack = Stack::new_for_tests();
934
935            let args = Args {
936                labeled,
937                unlabeled: Vec::new(),
938                source_range: SourceRange::default(),
939                ctx: exec_ctxt,
940                pipe_value: None,
941                _status: std::marker::PhantomData,
942            };
943
944            let actual = assign_args_to_params_kw(&func_src, args, &mut exec_state).map(|_| exec_state.mod_local.stack);
945            assert_eq!(
946                actual, expected,
947                "failed test '{test_name}':\ngot {actual:?}\nbut expected\n{expected:?}"
948            );
949        }
950    }
951
952    #[tokio::test(flavor = "multi_thread")]
953    async fn type_check_user_args() {
954        let program = r#"fn makeMessage(prefix: string, suffix: string) {
955  return prefix + suffix
956}
957
958msg1 = makeMessage(prefix = "world", suffix = " hello")
959msg2 = makeMessage(prefix = 1, suffix = 3)"#;
960        let err = parse_execute(program).await.unwrap_err();
961        assert_eq!(
962            err.message(),
963            "prefix requires a value with type `string`, but found a value with type `number`.\nThe found value is a number but has incomplete units information. You can probably fix this error by specifying the units using type ascription, e.g., `len: mm` or `(a * b): deg`."
964        )
965    }
966
967    #[tokio::test(flavor = "multi_thread")]
968    async fn array_input_arg() {
969        let ast = r#"fn f(@input: [mm]) { return 1 }
970f([1, 2, 3])
971f(1, 2, 3)
972"#;
973        parse_execute(ast).await.unwrap();
974    }
975}