Skip to main content

kcl_lib/execution/
fn_call.rs

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