Skip to main content

kcl_lib/execution/
fn_call.rs

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