From b3dd610949a80ebe76a155462e7e3da7c2919a77 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 19 Jun 2024 00:22:37 -0400 Subject: [PATCH] Continue implementing type inference --- dust-lang/src/abstract_tree/function_call.rs | 40 ++++++++++++++------ examples/type_inference.ds | 1 + 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/dust-lang/src/abstract_tree/function_call.rs b/dust-lang/src/abstract_tree/function_call.rs index c5e9a41..18ce91b 100644 --- a/dust-lang/src/abstract_tree/function_call.rs +++ b/dust-lang/src/abstract_tree/function_call.rs @@ -142,20 +142,36 @@ impl ExpectedType for FunctionCall { .. } = *return_type.clone() { - for (constructor, identifier) in self - .type_arguments - .as_ref() - .unwrap() - .into_iter() - .zip(type_parameters.unwrap().into_iter()) + if let (Some(type_arguments), Some(type_parameters)) = + (&self.type_arguments, &type_parameters) { - if return_identifier == identifier { - let concrete_type = constructor.clone().construct(&context)?; + for (constructor, identifier) in + type_arguments.into_iter().zip(type_parameters.into_iter()) + { + if &return_identifier == identifier { + let concrete_type = constructor.clone().construct(&context)?; - return Ok(Type::Generic { - identifier, - concrete_type: Some(Box::new(concrete_type)), - }); + return Ok(Type::Generic { + identifier: identifier.clone(), + concrete_type: Some(Box::new(concrete_type)), + }); + } + } + } + + if let (None, Some(type_parameters)) = (&self.type_arguments, type_parameters) { + for (expression, identifier) in (&self.value_arguments) + .into_iter() + .zip(type_parameters.into_iter()) + { + if identifier == return_identifier { + let concrete_type = expression.expected_type(context)?; + + return Ok(Type::Generic { + identifier, + concrete_type: Some(Box::new(concrete_type)), + }); + } } } } diff --git a/examples/type_inference.ds b/examples/type_inference.ds index 681f7e2..60d7a6b 100644 --- a/examples/type_inference.ds +++ b/examples/type_inference.ds @@ -1,2 +1,3 @@ foo = fn |T| (x: T) -> T { x } bar: str = foo::(str)::("hi") +baz: str = foo("hi")