From b3b6fa01b68e29ffb8eb8261e2eb7eb3a66a3e0b Mon Sep 17 00:00:00 2001 From: Phoenix Himself Date: Mon, 11 Nov 2024 12:07:47 +0100 Subject: [PATCH 1/4] fix(function): fix optional argument type matching --- src/modules/function/declaration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/function/declaration.rs b/src/modules/function/declaration.rs index e25f83fd..d62bfebb 100644 --- a/src/modules/function/declaration.rs +++ b/src/modules/function/declaration.rs @@ -191,7 +191,7 @@ impl SyntaxModule for FunctionDeclaration { optional = true; let mut expr = Expr::new(); syntax(meta, &mut expr)?; - if arg_type != Type::Generic && arg_type != expr.get_type() { + if !expr.get_type().is_subset_of(&arg_type) { return error!(meta, name_token, "Optional argument does not match annotated type"); } self.arg_optionals.push(expr); From f5fa027b1f3e1d6458b9c0532b5f790dcc63d32b Mon Sep 17 00:00:00 2001 From: Phoenix Himself Date: Mon, 11 Nov 2024 12:19:34 +0100 Subject: [PATCH 2/4] fix(types): improve type checking functions --- src/modules/function/declaration.rs | 2 +- src/modules/types.rs | 2 +- .../function_optional_argument_generic_array.ab | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/tests/validity/function_optional_argument_generic_array.ab diff --git a/src/modules/function/declaration.rs b/src/modules/function/declaration.rs index d62bfebb..9e9036ae 100644 --- a/src/modules/function/declaration.rs +++ b/src/modules/function/declaration.rs @@ -191,7 +191,7 @@ impl SyntaxModule for FunctionDeclaration { optional = true; let mut expr = Expr::new(); syntax(meta, &mut expr)?; - if !expr.get_type().is_subset_of(&arg_type) { + if !expr.get_type().is_allowed_in(&arg_type) { return error!(meta, name_token, "Optional argument does not match annotated type"); } self.arg_optionals.push(expr); diff --git a/src/modules/types.rs b/src/modules/types.rs index 41b2bebb..b2175009 100644 --- a/src/modules/types.rs +++ b/src/modules/types.rs @@ -28,7 +28,7 @@ impl Type { } pub fn is_allowed_in(&self, other: &Type) -> bool { - self == other || self.is_subset_of(other) + self == other || self.is_subset_of(other) || other == &Type::Generic } pub fn is_array(&self) -> bool { diff --git a/src/tests/validity/function_optional_argument_generic_array.ab b/src/tests/validity/function_optional_argument_generic_array.ab new file mode 100644 index 00000000..45f7fa83 --- /dev/null +++ b/src/tests/validity/function_optional_argument_generic_array.ab @@ -0,0 +1,10 @@ +// Output +// Hello World +// 1 2 3 + +fun echo_var(arg: [] = [1, 2, 3]){ + echo arg +} + +echo_var(["Hello", "World"]) +echo_var() From 4f76c03f7e5360cdf0367820fe7fabc30d7ef32e Mon Sep 17 00:00:00 2001 From: Phoenix Himself Date: Thu, 14 Nov 2024 18:39:58 +0100 Subject: [PATCH 3/4] fix: update is_subset_of --- src/modules/types.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/types.rs b/src/modules/types.rs index b2175009..825b8a1f 100644 --- a/src/modules/types.rs +++ b/src/modules/types.rs @@ -17,6 +17,7 @@ pub enum Type { impl Type { pub fn is_subset_of(&self, other: &Type) -> bool { match (self, other) { + (_, Type::Generic) => true, (Type::Array(current), Type::Array(other)) => { **current != Type::Generic && **other == Type::Generic } @@ -28,7 +29,7 @@ impl Type { } pub fn is_allowed_in(&self, other: &Type) -> bool { - self == other || self.is_subset_of(other) || other == &Type::Generic + self == other || self.is_subset_of(other) } pub fn is_array(&self) -> bool { From b7329a25fdeaaca55245955eab4315679006f8eb Mon Sep 17 00:00:00 2001 From: Phoenix Himself Date: Thu, 14 Nov 2024 18:41:07 +0100 Subject: [PATCH 4/4] fix: linting in test file --- src/tests/validity/function_optional_argument_generic_array.ab | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/validity/function_optional_argument_generic_array.ab b/src/tests/validity/function_optional_argument_generic_array.ab index 45f7fa83..329af889 100644 --- a/src/tests/validity/function_optional_argument_generic_array.ab +++ b/src/tests/validity/function_optional_argument_generic_array.ab @@ -2,7 +2,7 @@ // Hello World // 1 2 3 -fun echo_var(arg: [] = [1, 2, 3]){ +fun echo_var(arg: [] = [1, 2, 3]): Null { echo arg }