leo_passes/type_checking/
program.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use super::TypeCheckingVisitor;
18use crate::{VariableSymbol, VariableType};
19
20use leo_ast::{DiGraphError, Type, *};
21use leo_errors::{Label, TypeCheckerError};
22use leo_span::{Symbol, sym};
23
24use itertools::Itertools;
25use snarkvm::prelude::{CanaryV0, MainnetV0, TestnetV0};
26use std::collections::{BTreeMap, HashMap};
27
28impl ProgramVisitor for TypeCheckingVisitor<'_> {
29    fn visit_program(&mut self, input: &Program) {
30        // Typecheck the program's stubs.
31        input.stubs.iter().for_each(|(symbol, stub)| {
32            // Check that naming and ordering is consistent.
33            if symbol != &stub.stub_id.name.name {
34                self.emit_err(TypeCheckerError::stub_name_mismatch(
35                    symbol,
36                    stub.stub_id.name,
37                    stub.stub_id.network.span,
38                ));
39            }
40            self.visit_stub(stub)
41        });
42        self.scope_state.is_stub = false;
43
44        // Typecheck the modules.
45        input.modules.values().for_each(|module| self.visit_module(module));
46
47        // Typecheck the program scopes.
48        input.program_scopes.values().for_each(|scope| self.visit_program_scope(scope));
49    }
50
51    fn visit_program_scope(&mut self, input: &ProgramScope) {
52        let program_name = input.program_id.name;
53
54        // Set the current program name.
55        self.scope_state.program_name = Some(program_name.name);
56
57        // Collect a map from record names to their spans
58        let record_info: BTreeMap<String, leo_span::Span> = input
59            .structs
60            .iter()
61            .filter(|(_, c)| c.is_record)
62            .map(|(_, r)| (r.name().to_string(), r.identifier.span))
63            .collect();
64
65        // Check if any record name is a prefix for another record name. We don't really collect all possible prefixes
66        // here but only adjacent ones. That is, if we have records `Foo`, `FooBar`, and `FooBarBaz`, we only emit
67        // errors for `Foo/FooBar` and for `FooBar/FooBarBaz` but not for `Foo/FooBarBaz`.
68        for ((prev_name, _), (curr_name, curr_span)) in record_info.iter().tuple_windows() {
69            if curr_name.starts_with(prev_name) {
70                self.state
71                    .handler
72                    .emit_err(TypeCheckerError::record_prefixed_by_other_record(curr_name, prev_name, *curr_span));
73            }
74        }
75
76        // Typecheck each const definition, and append to symbol table.
77        input.consts.iter().for_each(|(_, c)| self.visit_const(c));
78
79        // Typecheck each struct definition.
80        input.structs.iter().for_each(|(_, function)| self.visit_struct(function));
81
82        // Check that the struct dependency graph does not have any cycles.
83        if let Err(DiGraphError::CycleDetected(path)) = self.state.struct_graph.post_order() {
84            self.emit_err(TypeCheckerError::cyclic_struct_dependency(
85                path.iter().map(|p| p.iter().format("::")).collect(),
86            ));
87        }
88
89        // Typecheck each mapping definition.
90        let mut mapping_count = 0;
91        for (_, mapping) in input.mappings.iter() {
92            self.visit_mapping(mapping);
93            mapping_count += 1;
94        }
95
96        // Typecheck each storage variable definition.
97        for (_, storage_variable) in input.storage_variables.iter() {
98            self.visit_storage_variable(storage_variable);
99        }
100
101        // Check that the number of mappings does not exceed the maximum.
102        if mapping_count > self.limits.max_mappings {
103            self.emit_err(TypeCheckerError::too_many_mappings(
104                self.limits.max_mappings,
105                input.program_id.name.span + input.program_id.network.span,
106            ));
107        }
108
109        // Typecheck each function definitions.
110        let mut transition_count = 0;
111        for (_, function) in input.functions.iter() {
112            self.visit_function(function);
113            if function.variant.is_transition() {
114                transition_count += 1;
115            }
116        }
117
118        // Typecheck the constructor.
119        // Note: Constructors are required for all **new** programs once they are supported in the AVM.
120        //  However, we do not require them to exist to ensure backwards compatibility with existing programs.
121        if let Some(constructor) = &input.constructor {
122            self.visit_constructor(constructor);
123        }
124
125        // Check that the call graph does not have any cycles.
126        if let Err(DiGraphError::CycleDetected(path)) = self.state.call_graph.post_order() {
127            self.emit_err(TypeCheckerError::cyclic_function_dependency(path));
128        }
129
130        // TODO: Need similar checks for structs (all in separate PR)
131        // Check that the number of transitions does not exceed the maximum.
132        if transition_count > self.limits.max_functions {
133            self.emit_err(TypeCheckerError::too_many_transitions(
134                self.limits.max_functions,
135                input.program_id.name.span + input.program_id.network.span,
136            ));
137        }
138        // Check that each program has at least one transition function.
139        // This is a snarkvm requirement.
140        else if transition_count == 0 {
141            self.emit_err(TypeCheckerError::no_transitions(input.program_id.name.span + input.program_id.network.span));
142        }
143    }
144
145    fn visit_module(&mut self, input: &Module) {
146        let parent_module = self.scope_state.module_name.clone();
147        // Set the current program name.
148        self.scope_state.program_name = Some(input.program_name);
149        self.scope_state.module_name = input.path.clone();
150
151        // Typecheck each const definition, and append to symbol table.
152        input.consts.iter().for_each(|(_, c)| self.visit_const(c));
153
154        // Typecheck each struct definition.
155        input.structs.iter().for_each(|(_, function)| self.visit_struct(function));
156
157        for (_, function) in input.functions.iter() {
158            self.visit_function(function);
159        }
160
161        self.scope_state.module_name = parent_module;
162    }
163
164    fn visit_stub(&mut self, input: &Stub) {
165        // Set the scope state.
166        self.scope_state.program_name = Some(input.stub_id.name.name);
167        self.scope_state.is_stub = true;
168
169        // Cannot have constant declarations in stubs.
170        if !input.consts.is_empty() {
171            self.emit_err(TypeCheckerError::stubs_cannot_have_const_declarations(input.consts.first().unwrap().1.span));
172        }
173
174        // Typecheck the program's structs.
175        input.structs.iter().for_each(|(_, function)| self.visit_struct_stub(function));
176
177        // Typecheck the program's functions.
178        input.functions.iter().for_each(|(_, function)| self.visit_function_stub(function));
179    }
180
181    fn visit_struct(&mut self, input: &Composite) {
182        self.in_conditional_scope(|slf| {
183            slf.in_scope(input.id, |slf| {
184                if input.is_record && !input.const_parameters.is_empty() {
185                    slf.emit_err(TypeCheckerError::unexpected_record_const_parameters(input.span));
186                } else {
187                    input
188                        .const_parameters
189                        .iter()
190                        .for_each(|const_param| slf.insert_symbol_conditional_scope(const_param.identifier.name));
191
192                    for const_param in &input.const_parameters {
193                        slf.visit_type(const_param.type_());
194
195                        // Restrictions for const parameters
196                        if !matches!(
197                            const_param.type_(),
198                            Type::Boolean | Type::Integer(_) | Type::Address | Type::Scalar | Type::Group | Type::Field
199                        ) {
200                            slf.emit_err(TypeCheckerError::bad_const_generic_type(
201                                const_param.type_(),
202                                const_param.span(),
203                            ));
204                        }
205
206                        // Add the input to the symbol table.
207                        if let Err(err) = slf.state.symbol_table.insert_variable(
208                            slf.scope_state.program_name.unwrap(),
209                            &[const_param.identifier().name],
210                            VariableSymbol {
211                                type_: const_param.type_().clone(),
212                                span: const_param.identifier.span(),
213                                declaration: VariableType::ConstParameter,
214                            },
215                        ) {
216                            slf.state.handler.emit_err(err);
217                        }
218
219                        // Add the input to the type table.
220                        slf.state.type_table.insert(const_param.identifier().id(), const_param.type_().clone());
221                    }
222                }
223
224                input.members.iter().for_each(|member| slf.visit_type(&member.type_));
225            })
226        });
227
228        // Check for conflicting struct/record member names.
229        let mut used = HashMap::new();
230        for Member { identifier, type_, span, .. } in &input.members {
231            // Check that the member types are defined.
232            self.assert_type_is_valid(type_, *span);
233
234            if let Some(first_span) = used.get(&identifier.name) {
235                self.emit_err(if input.is_record {
236                    TypeCheckerError::duplicate_record_variable(identifier.name, *span).with_labels(vec![
237                        Label::new(format!("`{}` first declared here", identifier.name), *first_span)
238                            .with_color(leo_errors::Color::Blue),
239                        Label::new("record variable already declared", *span),
240                    ])
241                } else {
242                    TypeCheckerError::duplicate_struct_member(identifier.name, *span).with_labels(vec![
243                        Label::new(format!("`{}` first declared here", identifier.name), *first_span)
244                            .with_color(leo_errors::Color::Blue),
245                        Label::new("struct field already declared", *span),
246                    ])
247                });
248            } else {
249                used.insert(identifier.name, *span);
250            }
251        }
252
253        // For records, enforce presence of the `owner: Address` member.
254        if input.is_record {
255            let check_has_field =
256                |need, expected_ty: Type| match input.members.iter().find_map(|Member { identifier, type_, .. }| {
257                    (identifier.name == need).then_some((identifier, type_))
258                }) {
259                    Some((_, actual_ty)) if expected_ty.eq_flat_relaxed(actual_ty) => {} // All good, found + right type!
260                    Some((field, _)) => {
261                        self.emit_err(TypeCheckerError::record_var_wrong_type(field, expected_ty, input.span()));
262                    }
263                    None => {
264                        self.emit_err(TypeCheckerError::required_record_variable(need, expected_ty, input.span()));
265                    }
266                };
267            check_has_field(sym::owner, Type::Address);
268
269            for Member { identifier, type_, span, .. } in input.members.iter() {
270                if self.contains_optional_type(type_) {
271                    self.emit_err(TypeCheckerError::record_field_cannot_be_optional(identifier, type_, *span));
272                }
273            }
274        }
275        // For structs, check that there is at least one member.
276        else if input.members.is_empty() {
277            self.emit_err(TypeCheckerError::empty_struct(input.span()));
278        }
279
280        if !(input.is_record && self.scope_state.is_stub) {
281            for Member { mode, identifier, type_, span, .. } in input.members.iter() {
282                // Check that the member type is not a tuple.
283                if matches!(type_, Type::Tuple(_)) {
284                    self.emit_err(TypeCheckerError::composite_data_type_cannot_contain_tuple(
285                        if input.is_record { "record" } else { "struct" },
286                        identifier.span,
287                    ));
288                } else if matches!(type_, Type::Future(..)) {
289                    self.emit_err(TypeCheckerError::composite_data_type_cannot_contain_future(
290                        if input.is_record { "record" } else { "struct" },
291                        identifier.span,
292                    ));
293                }
294
295                // Ensure that there are no record members.
296                self.assert_member_is_not_record(identifier.span, input.identifier.name, type_);
297                // If the member is a struct, add it to the struct dependency graph.
298                // Note that we have already checked that each member is defined and valid.
299                let composite_path = self
300                    .scope_state
301                    .module_name
302                    .iter()
303                    .cloned()
304                    .chain(std::iter::once(input.identifier.name))
305                    .collect::<Vec<Symbol>>();
306                if let Type::Composite(struct_member_type) = type_ {
307                    // Note that since there are no cycles in the program dependency graph, there are no cycles in the struct dependency graph caused by external structs.
308                    self.state.struct_graph.add_edge(composite_path, struct_member_type.path.absolute_path().to_vec());
309                } else if let Type::Array(array_type) = type_ {
310                    // Get the base element type.
311                    let base_element_type = array_type.base_element_type();
312                    // If the base element type is a struct, then add it to the struct dependency graph.
313                    if let Type::Composite(member_type) = base_element_type {
314                        self.state.struct_graph.add_edge(composite_path, member_type.path.absolute_path().to_vec());
315                    }
316                }
317
318                // If the input is a struct, then check that the member does not have a mode.
319                if !input.is_record && !matches!(mode, Mode::None) {
320                    self.emit_err(TypeCheckerError::struct_cannot_have_member_mode(*span));
321                }
322            }
323        }
324    }
325
326    fn visit_mapping(&mut self, input: &Mapping) {
327        self.visit_type(&input.key_type);
328        self.visit_type(&input.value_type);
329
330        // Check that a mapping's key type is valid.
331        self.assert_type_is_valid(&input.key_type, input.span);
332        // Check that a mapping's key type is not a future, tuple, record, or mapping.
333        match input.key_type.clone() {
334            Type::Future(_) => self.emit_err(TypeCheckerError::invalid_mapping_type("key", "future", input.span)),
335            Type::Tuple(_) => self.emit_err(TypeCheckerError::invalid_mapping_type("key", "tuple", input.span)),
336            Type::Composite(struct_type) => {
337                if let Some(comp) = self.lookup_struct(
338                    struct_type.program.or(self.scope_state.program_name),
339                    &struct_type.path.absolute_path(),
340                ) {
341                    if comp.is_record {
342                        self.emit_err(TypeCheckerError::invalid_mapping_type("key", "record", input.span));
343                    }
344                } else {
345                    self.emit_err(TypeCheckerError::undefined_type(&input.key_type, input.span));
346                }
347            }
348            // Note that this is not possible since the parser does not currently accept mapping types.
349            Type::Mapping(_) => self.emit_err(TypeCheckerError::invalid_mapping_type("key", "mapping", input.span)),
350            _ => {}
351        }
352
353        if self.contains_optional_type(&input.key_type) {
354            self.emit_err(TypeCheckerError::optional_type_not_allowed_in_mapping(
355                input.key_type.clone(),
356                "key",
357                input.span,
358            ))
359        }
360
361        // Check that a mapping's value type is valid.
362        self.assert_type_is_valid(&input.value_type, input.span);
363        // Check that a mapping's value type is not a future, tuple, record or mapping.
364        match input.value_type.clone() {
365            Type::Future(_) => self.emit_err(TypeCheckerError::invalid_mapping_type("value", "future", input.span)),
366            Type::Tuple(_) => self.emit_err(TypeCheckerError::invalid_mapping_type("value", "tuple", input.span)),
367            Type::Composite(struct_type) => {
368                if let Some(comp) = self.lookup_struct(
369                    struct_type.program.or(self.scope_state.program_name),
370                    &struct_type.path.absolute_path(),
371                ) {
372                    if comp.is_record {
373                        self.emit_err(TypeCheckerError::invalid_mapping_type("value", "record", input.span));
374                    }
375                } else {
376                    self.emit_err(TypeCheckerError::undefined_type(&input.value_type, input.span));
377                }
378            }
379            // Note that this is not possible since the parser does not currently accept mapping types.
380            Type::Mapping(_) => self.emit_err(TypeCheckerError::invalid_mapping_type("value", "mapping", input.span)),
381            _ => {}
382        }
383
384        if self.contains_optional_type(&input.value_type) {
385            self.emit_err(TypeCheckerError::optional_type_not_allowed_in_mapping(
386                input.value_type.clone(),
387                "value",
388                input.span,
389            ))
390        }
391    }
392
393    fn visit_storage_variable(&mut self, input: &StorageVariable) {
394        self.visit_type(&input.type_);
395
396        let storage_type = if let Type::Vector(VectorType { element_type }) = &input.type_ {
397            *element_type.clone()
398        } else {
399            input.type_.clone()
400        };
401
402        self.assert_storage_type_is_valid(&storage_type, input.span);
403    }
404
405    fn visit_function(&mut self, function: &Function) {
406        // Reset the scope state.
407        self.scope_state.reset();
408
409        // Set the scope state before traversing the function.
410        self.scope_state.variant = Some(function.variant);
411
412        // Check that the function's annotations are valid.
413        for annotation in function.annotations.iter() {
414            if !matches!(annotation.identifier.name, sym::test | sym::should_fail) {
415                self.emit_err(TypeCheckerError::unknown_annotation(annotation, annotation.span))
416            }
417        }
418
419        let get = |symbol: Symbol| -> &Annotation {
420            function.annotations.iter().find(|ann| ann.identifier.name == symbol).unwrap()
421        };
422
423        let check_annotation = |symbol: Symbol, allowed_keys: &[Symbol]| -> bool {
424            let count = function.annotations.iter().filter(|ann| ann.identifier.name == symbol).count();
425            if count > 0 {
426                let annotation = get(symbol);
427                for key in annotation.map.keys() {
428                    if !allowed_keys.contains(key) {
429                        self.emit_err(TypeCheckerError::annotation_error(
430                            format_args!("Invalid key `{key}` for annotation @{symbol}"),
431                            annotation.span,
432                        ));
433                    }
434                }
435                if count > 1 {
436                    self.emit_err(TypeCheckerError::annotation_error(
437                        format_args!("Duplicate annotation @{symbol}"),
438                        annotation.span,
439                    ));
440                }
441            }
442            count > 0
443        };
444
445        let has_test = check_annotation(sym::test, &[sym::private_key]);
446        let has_should_fail = check_annotation(sym::should_fail, &[]);
447
448        if has_test && !self.state.is_test {
449            self.emit_err(TypeCheckerError::annotation_error(
450                format_args!("Test annotation @test appears outside of tests"),
451                get(sym::test).span,
452            ));
453        }
454
455        if has_should_fail && !self.state.is_test {
456            self.emit_err(TypeCheckerError::annotation_error(
457                format_args!("Test annotation @should_fail appears outside of tests"),
458                get(sym::should_fail).span,
459            ));
460        }
461
462        if has_should_fail && !has_test {
463            self.emit_err(TypeCheckerError::annotation_error(
464                format_args!("Annotation @should_fail appears without @test"),
465                get(sym::should_fail).span,
466            ));
467        }
468
469        if has_test
470            && !self.scope_state.variant.unwrap().is_script()
471            && !self.scope_state.variant.unwrap().is_transition()
472        {
473            self.emit_err(TypeCheckerError::annotation_error(
474                format_args!("Annotation @test may appear only on scripts and transitions"),
475                get(sym::test).span,
476            ));
477        }
478
479        if (has_test) && !function.input.is_empty() {
480            self.emit_err(TypeCheckerError::annotation_error(
481                "A test procedure cannot have inputs",
482                function.input[0].span,
483            ));
484        }
485
486        self.in_conditional_scope(|slf| {
487            slf.in_scope(function.id, |slf| {
488                function
489                    .const_parameters
490                    .iter()
491                    .for_each(|const_param| slf.insert_symbol_conditional_scope(const_param.identifier.name));
492
493                function.input.iter().for_each(|input| slf.insert_symbol_conditional_scope(input.identifier.name));
494
495                // Store the name of the function.
496                slf.scope_state.function = Some(function.name());
497
498                // Query helper function to type check function parameters and outputs.
499                slf.check_function_signature(function, false);
500
501                if function.variant == Variant::Function && function.input.is_empty() {
502                    slf.emit_err(TypeCheckerError::empty_function_arglist(function.span));
503                }
504
505                slf.visit_block(&function.block);
506
507                // If the function has a return type, then check that it has a return.
508                if function.output_type != Type::Unit && !slf.scope_state.has_return {
509                    slf.emit_err(TypeCheckerError::missing_return(function.span));
510                }
511            })
512        });
513
514        // Make sure that async transitions call finalize.
515        if self.scope_state.variant == Some(Variant::AsyncTransition)
516            && !self.scope_state.has_called_finalize
517            && !self.scope_state.already_contains_an_async_block
518        {
519            self.emit_err(TypeCheckerError::missing_async_operation_in_async_transition(function.span));
520        }
521
522        self.scope_state.reset();
523    }
524
525    fn visit_constructor(&mut self, constructor: &Constructor) {
526        // Reset the scope state.
527        self.scope_state.reset();
528        // Set the scope state before traversing the constructor.
529        self.scope_state.function = Some(sym::constructor);
530        // Note: We set the variant to `AsyncFunction` since constructors have similar semantics.
531        self.scope_state.variant = Some(Variant::AsyncFunction);
532        self.scope_state.is_constructor = true;
533
534        // Get the upgrade variant.
535        // Note, `get_upgrade_variant` will return an error if the constructor is not well-formed.
536        let result = match self.state.network {
537            NetworkName::CanaryV0 => constructor.get_upgrade_variant::<CanaryV0>(),
538            NetworkName::TestnetV0 => constructor.get_upgrade_variant::<TestnetV0>(),
539            NetworkName::MainnetV0 => constructor.get_upgrade_variant::<MainnetV0>(),
540        };
541        let upgrade_variant = match result {
542            Ok(upgrade_variant) => upgrade_variant,
543            Err(e) => {
544                self.emit_err(TypeCheckerError::custom(e, constructor.span));
545                return;
546            }
547        };
548
549        // Validate the number of statements.
550        match (&upgrade_variant, constructor.block.statements.is_empty()) {
551            (UpgradeVariant::Custom, true) => {
552                self.emit_err(TypeCheckerError::custom("A 'custom' constructor cannot be empty", constructor.span));
553            }
554            (UpgradeVariant::NoUpgrade | UpgradeVariant::Admin { .. } | UpgradeVariant::Checksum { .. }, false) => {
555                self.emit_err(TypeCheckerError::custom("A 'noupgrade', 'admin', or 'checksum' constructor must be empty. The Leo compiler will insert the appropriate code.", constructor.span));
556            }
557            _ => {}
558        }
559
560        // For the checksum variant, check that the mapping exists and that the type matches.
561        if let UpgradeVariant::Checksum { mapping, key, key_type } = &upgrade_variant {
562            // Look up the mapping type.
563            let Some(VariableSymbol { type_: Type::Mapping(mapping_type), .. }) =
564                self.state.symbol_table.lookup_global(mapping)
565            else {
566                self.emit_err(TypeCheckerError::custom(
567                    format!("The mapping '{mapping}' does not exist. Please ensure that it is imported or defined in your program."),
568                    constructor.annotations[0].span,
569                ));
570                return;
571            };
572            // Check that the mapping key type matches the expected key type.
573            if *mapping_type.key != *key_type {
574                self.emit_err(TypeCheckerError::custom(
575                    format!(
576                        "The mapping '{}' key type '{}' does not match the key '{}' in the `@checksum` annotation",
577                        mapping, mapping_type.key, key
578                    ),
579                    constructor.annotations[0].span,
580                ));
581            }
582            // Check that the value type is a `[u8; 32]`.
583            let check_value_type = |type_: &Type| -> bool {
584                if let Type::Array(array_type) = type_ {
585                    if !matches!(array_type.element_type.as_ref(), &Type::Integer(_)) {
586                        return false;
587                    }
588                    if let Some(length) = array_type.length.as_u32() {
589                        return length == 32;
590                    }
591                    return false;
592                }
593                false
594            };
595            if !check_value_type(&mapping_type.value) {
596                self.emit_err(TypeCheckerError::custom(
597                    format!("The mapping '{}' value type '{}' must be a '[u8; 32]'", mapping, mapping_type.value),
598                    constructor.annotations[0].span,
599                ));
600            }
601        }
602
603        // Traverse the constructor.
604        self.in_conditional_scope(|slf| {
605            slf.in_scope(constructor.id, |slf| {
606                slf.visit_block(&constructor.block);
607            })
608        });
609
610        // Check that the constructor does not call `finalize`.
611        if self.scope_state.has_called_finalize {
612            self.emit_err(TypeCheckerError::custom("The constructor cannot call `finalize`.", constructor.span));
613        }
614
615        // Check that the constructor does not have an `async` block.
616        if self.scope_state.already_contains_an_async_block {
617            self.emit_err(TypeCheckerError::custom("The constructor cannot have an `async` block.", constructor.span));
618        }
619
620        self.scope_state.reset();
621    }
622
623    fn visit_function_stub(&mut self, input: &FunctionStub) {
624        // Must not be an inline function
625        if input.variant == Variant::Inline {
626            self.emit_err(TypeCheckerError::stub_functions_must_not_be_inlines(input.span));
627        }
628
629        // Create future stubs.
630        if input.variant == Variant::AsyncFunction {
631            let finalize_input_map = &mut self.async_function_input_types;
632            let resolved_inputs: Vec<Type> = input
633                .input
634                .iter()
635                .map(|input| {
636                    match &input.type_ {
637                        Type::Future(f) => {
638                            // Since we traverse stubs in post-order, we can assume that the corresponding finalize stub has already been traversed.
639                            Type::Future(FutureType::new(
640                                finalize_input_map.get(f.location.as_ref().unwrap()).unwrap().clone(),
641                                f.location.clone(),
642                                true,
643                            ))
644                        }
645                        _ => input.clone().type_,
646                    }
647                })
648                .collect();
649
650            finalize_input_map.insert(
651                Location::new(self.scope_state.program_name.unwrap(), vec![input.identifier.name]),
652                resolved_inputs,
653            );
654        }
655
656        // Query helper function to type check function parameters and outputs.
657        self.check_function_signature(&Function::from(input.clone()), /* is_stub */ true);
658    }
659
660    fn visit_struct_stub(&mut self, input: &Composite) {
661        self.visit_struct(input);
662    }
663}