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