Skip to main content

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